LCOV - code coverage report
Current view: top level - libs/mem/tests/src - test_libmem_0005.tpl (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 23 23
Test Date: 2026-07-12 01:01:34 Functions: - 0 0
Branches: 55.0 % 240 132

             Branch data     Line data    Source code
       1   [ +  +  +  +  :          44 : for(int i = 0; i < CYCLES; i++)
             +  +  +  + ]
       2                 :             : {
       3                 :             :         unsigned char hash_1[SHA512_DIGEST_LENGTH];
       4                 :             :         unsigned char hash_2[SHA512_DIGEST_LENGTH];
       5                 :             :         unsigned char source_bytes[1000U * sizeof(TYPE)];
       6                 :          40 :         uint64_t random = 0;
       7                 :             : 
       8                 :          40 :         m_create(TYPE,test);
       9                 :             : 
      10   [ +  +  +  +  :         440 :         for(int k = 0; k < CYCLES; k++)
             +  +  +  + ]
      11                 :             :         {
      12                 :             :                 /* Choose a random descriptor length within the fixed test range */
      13   [ +  -  +  -  :         400 :                 ASSERT(SUCCESS == random_number_generator_urandom(&random,1,1000));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
      14                 :             : 
      15                 :         400 :                 size_t array_length = (size_t)random;
      16                 :         400 :                 size_t array_size = array_length * sizeof(TYPE);
      17                 :             : 
      18                 :             :                 /* Fill a bounded stack buffer with random bytes. This keeps the test
      19                 :             :                    focused on libmem descriptors instead of raw heap allocation */
      20   [ +  +  +  +  :      697461 :                 for(size_t j = 0; j < array_size; j++)
             +  +  +  + ]
      21                 :             :                 {
      22   [ +  -  +  -  :      697061 :                         ASSERT(SUCCESS == random_number_generator_urandom(&random,0,255));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
      23                 :      697061 :                         source_bytes[j] = (unsigned char)random;
      24                 :             :                 }
      25                 :             : 
      26                 :             :                 /* Hash the original bytes before they enter the libmem descriptor */
      27                 :             :                 SHA512_Context source_context;
      28   [ +  -  +  -  :         400 :                 ASSERT(CRYPT_OK == sha512_init(&source_context));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
      29   [ +  -  +  -  :         400 :                 ASSERT(CRYPT_OK == sha512_update(&source_context,source_bytes,array_size));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
      30   [ +  -  +  -  :         400 :                 ASSERT(CRYPT_OK == sha512_final(&source_context,hash_1));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
      31                 :             : 
      32                 :             :                 #if SHOW_TEST
      33                 :             :                 /* Print the source buffer summary and digest when verbose diagnostics
      34                 :             :                    are enabled for this stress-style test */
      35                 :             :                 echo(STDERR,"Test %d:%d array size: %zu bytes, array_length=%zu, sizeof(TYPE)=%zu bytes\n",i,k,array_size,array_length,sizeof(TYPE));
      36                 :             :                 echo(STDERR,"Test %d:%d SHA-512 hash: ",i,k);
      37                 :             :                 print_hash(hash_1);
      38                 :             :                 #endif
      39                 :             : 
      40                 :             :                 /* Import the bounded byte range into libmem-managed typed storage */
      41   [ +  -  +  -  :         400 :                 ASSERT(SUCCESS == m_copy_buffer(test,array_size,source_bytes));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
      42   [ +  -  +  -  :         400 :                 ASSERT(test->length == array_length);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
      43                 :             : 
      44                 :             :                 /* Hash the descriptor view and compare it with the original bytes. The
      45                 :             :                    two digests must match exactly after the bounded-buffer import */
      46                 :             :                 SHA512_Context descriptor_context;
      47   [ +  -  +  -  :         400 :                 ASSERT(CRYPT_OK == sha512_init(&descriptor_context));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
      48                 :             : 
      49                 :         400 :                 const TYPE *test_view = m_data_ro(TYPE,test);
      50   [ +  -  +  -  :         400 :                 ASSERT(test_view != NULL);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
      51                 :             : 
      52   [ +  -  +  -  :         400 :                 IF(test_view != NULL)
             +  -  +  - ]
      53                 :             :                 {
      54   [ +  -  +  -  :         400 :                         ASSERT(CRYPT_OK == sha512_update(&descriptor_context,(const unsigned char *)test_view,test->length * sizeof(TYPE)));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
      55                 :             :                 }
      56                 :             : 
      57   [ +  -  +  -  :         400 :                 ASSERT(CRYPT_OK == sha512_final(&descriptor_context,hash_2));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
      58                 :             : 
      59                 :             :                 #if SHOW_TEST
      60                 :             :                 /* Print the descriptor summary and digest when verbose diagnostics are
      61                 :             :                    enabled for this stress-style test */
      62                 :             :                 echo(STDERR,"Test %d:%d array size: %zu bytes\n",i,k,test->length * sizeof(TYPE));
      63                 :             :                 echo(STDERR,"Test %d:%d SHA-512 hash: ",i,k);
      64                 :             :                 print_hash(hash_2);
      65                 :             :                 #endif
      66                 :             : 
      67   [ +  -  +  -  :         400 :                 ASSERT(0 == memcmp(hash_1,hash_2,(size_t)SHA512_DIGEST_LENGTH));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
      68                 :             :         }
      69                 :             : 
      70                 :             :         /* Release the descriptor even when an earlier iteration failed */
      71   [ -  +  -  +  :          40 :         call(m_del(test));
          -  +  -  +  -  
          +  -  +  -  +  
                   -  + ]
      72                 :             : }
        

Generated by: LCOV version 2.0-1