LCOV - code coverage report
Current view: top level - tests/src - test0033.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 90.5 % 263 238
Test Date: 2026-03-01 04:31:48 Functions: 100.0 % 9 9
Branches: 49.0 % 412 202

             Branch data     Line data    Source code
       1                 :             : #include "sute.h"
       2                 :             : 
       3                 :             : /**
       4                 :             :  * Compute SHA512 for a file using the project SHA512 library.
       5                 :             :  */
       6                 :           2 : static Return compute_file_sha512(
       7                 :             :         const char    *file_path,
       8                 :             :         unsigned char *sha512_out)
       9                 :             : {
      10                 :             :         /* Status returned by this function through provide()
      11                 :             :            Default value assumes successful completion */
      12                 :           2 :         Return status = SUCCESS;
      13                 :           2 :         FILE *file = NULL;
      14                 :             :         unsigned char buffer[65536];
      15                 :           2 :         SHA512_Context context = {0};
      16                 :             : 
      17   [ +  -  -  + ]:           2 :         if(file_path == NULL || sha512_out == NULL)
      18                 :             :         {
      19                 :           0 :                 status = FAILURE;
      20                 :             :         }
      21                 :             : 
      22         [ +  - ]:           2 :         if(SUCCESS == status)
      23                 :             :         {
      24                 :           2 :                 file = fopen(file_path,"rb");
      25         [ -  + ]:           2 :                 if(file == NULL)
      26                 :             :                 {
      27                 :           0 :                         status = FAILURE;
      28                 :             :                 }
      29                 :             :         }
      30                 :             : 
      31   [ +  -  -  + ]:           2 :         if(SUCCESS == status && sha512_init(&context) == 1)
      32                 :             :         {
      33                 :           0 :                 status = FAILURE;
      34                 :             :         }
      35                 :             : 
      36         [ +  - ]:         322 :         while(SUCCESS == status)
      37                 :             :         {
      38                 :         322 :                 const size_t bytes_read = fread(buffer,sizeof(unsigned char),sizeof(buffer),file);
      39                 :             : 
      40         [ +  + ]:         322 :                 if(bytes_read == 0U)
      41                 :             :                 {
      42         [ -  + ]:           2 :                         if(ferror(file) != 0)
      43                 :             :                         {
      44                 :           0 :                                 status = FAILURE;
      45                 :             :                         }
      46                 :           2 :                         break;
      47                 :             :                 }
      48                 :             : 
      49         [ -  + ]:         320 :                 if(sha512_update(&context,buffer,bytes_read) == 1)
      50                 :             :                 {
      51                 :           0 :                         status = FAILURE;
      52                 :             :                 }
      53                 :             :         }
      54                 :             : 
      55   [ +  -  -  + ]:           2 :         if(SUCCESS == status && sha512_final(&context,sha512_out) == 1)
      56                 :             :         {
      57                 :           0 :                 status = FAILURE;
      58                 :             :         }
      59                 :             : 
      60         [ +  - ]:           2 :         if(file != NULL)
      61                 :             :         {
      62                 :           2 :                 (void)fclose(file);
      63                 :             :         }
      64                 :             : 
      65                 :           2 :         return(status);
      66                 :             : }
      67                 :             : 
      68                 :             : /**
      69                 :             :  * Append one byte to a file using native C file I/O
      70                 :             :  */
      71                 :           2 : static Return append_byte_to_file(
      72                 :             :         const char   *file_path,
      73                 :             :         unsigned char byte)
      74                 :             : {
      75                 :             :         /* Status returned by this function through provide()
      76                 :             :            Default value assumes successful completion */
      77                 :           2 :         Return status = SUCCESS;
      78                 :             : 
      79         [ -  + ]:           2 :         if(file_path == NULL)
      80                 :             :         {
      81                 :           0 :                 status = FAILURE;
      82                 :             :         }
      83                 :             : 
      84                 :           2 :         FILE *file = NULL;
      85                 :             : 
      86         [ +  - ]:           2 :         if(SUCCESS == status)
      87                 :             :         {
      88                 :           2 :                 file = fopen(file_path,"ab");
      89         [ -  + ]:           2 :                 if(file == NULL)
      90                 :             :                 {
      91                 :           0 :                         status = FAILURE;
      92                 :             :                 }
      93                 :             :         }
      94                 :             : 
      95         [ +  - ]:           2 :         if(SUCCESS == status)
      96                 :             :         {
      97         [ -  + ]:           2 :                 if(fwrite(&byte,sizeof(unsigned char),1U,file) != 1U)
      98                 :             :                 {
      99                 :           0 :                         status = FAILURE;
     100                 :             :                 }
     101                 :             :         }
     102                 :             : 
     103         [ +  - ]:           2 :         if(file != NULL)
     104                 :             :         {
     105         [ -  + ]:           2 :                 if(fclose(file) != 0)
     106                 :             :                 {
     107                 :           0 :                         status = FAILURE;
     108                 :             :                 }
     109                 :             :         }
     110                 :             : 
     111                 :           2 :         return(status);
     112                 :             : }
     113                 :             : 
     114                 :             : /**
     115                 :             :  * Read intermediate offset and mdContext state for one file from DB.
     116                 :             :  */
     117                 :           4 : static Return read_resume_state_from_db(
     118                 :             :         const char     *db_filename,
     119                 :             :         const char     *relative_path,
     120                 :             :         sqlite3_int64  *offset_out,
     121                 :             :         int            *md_context_bytes_out)
     122                 :             : {
     123                 :             :         /* Status returned by this function through provide()
     124                 :             :            Default value assumes successful completion */
     125                 :           4 :         Return status = SUCCESS;
     126                 :           4 :         sqlite3 *db = NULL;
     127                 :           4 :         sqlite3_stmt *stmt = NULL;
     128                 :           4 :         const char *sql = "SELECT offset, mdContext FROM files WHERE relative_path = ?1;";
     129                 :           4 :         create(char,db_path);
     130                 :             : 
     131         [ +  - ]:           4 :         if(db_filename == NULL
     132         [ +  - ]:           4 :                 || relative_path == NULL
     133         [ +  - ]:           4 :                 || offset_out == NULL
     134         [ -  + ]:           4 :                 || md_context_bytes_out == NULL)
     135                 :             :         {
     136                 :           0 :                 status = FAILURE;
     137                 :             :         }
     138                 :             : 
     139         [ +  - ]:           4 :         if(TRIUMPH & status)
     140                 :             :         {
     141                 :           4 :                 status = construct_path(db_filename,db_path);
     142                 :             :         }
     143                 :             : 
     144   [ +  -  -  + ]:           4 :         if((TRIUMPH & status) && SQLITE_OK != sqlite3_open_v2(getcstring(db_path),&db,SQLITE_OPEN_READONLY,NULL))
     145                 :             :         {
     146                 :           0 :                 status = FAILURE;
     147                 :             :         }
     148                 :             : 
     149   [ +  -  -  + ]:           4 :         if((TRIUMPH & status) && SQLITE_OK != sqlite3_prepare_v2(db,sql,-1,&stmt,NULL))
     150                 :             :         {
     151                 :           0 :                 status = FAILURE;
     152                 :             :         }
     153                 :             : 
     154   [ +  -  -  + ]:           4 :         if((TRIUMPH & status) && SQLITE_OK != sqlite3_bind_text(stmt,1,relative_path,(int)strlen(relative_path),SQLITE_TRANSIENT))
     155                 :             :         {
     156                 :           0 :                 status = FAILURE;
     157                 :             :         }
     158                 :             : 
     159         [ +  - ]:           4 :         if(TRIUMPH & status)
     160                 :             :         {
     161                 :           4 :                 const int step_rc = sqlite3_step(stmt);
     162         [ -  + ]:           4 :                 if(step_rc != SQLITE_ROW)
     163                 :             :                 {
     164                 :           0 :                         status = FAILURE;
     165                 :             :                 }
     166                 :             :         }
     167                 :             : 
     168         [ +  - ]:           4 :         if(TRIUMPH & status)
     169                 :             :         {
     170         [ -  + ]:           4 :                 if(sqlite3_column_type(stmt,0) == SQLITE_NULL)
     171                 :             :                 {
     172                 :           0 :                         *offset_out = 0;
     173                 :             :                 } else {
     174                 :           4 :                         *offset_out = sqlite3_column_int64(stmt,0);
     175                 :             :                 }
     176                 :             : 
     177                 :           4 :                 *md_context_bytes_out = sqlite3_column_bytes(stmt,1);
     178                 :             : 
     179                 :           4 :                 const int done_rc = sqlite3_step(stmt);
     180         [ -  + ]:           4 :                 if(done_rc != SQLITE_DONE)
     181                 :             :                 {
     182                 :           0 :                         status = FAILURE;
     183                 :             :                 }
     184                 :             :         }
     185                 :             : 
     186         [ +  - ]:           4 :         if(stmt != NULL)
     187                 :             :         {
     188                 :           4 :                 (void)sqlite3_finalize(stmt);
     189                 :             :         }
     190                 :             : 
     191         [ +  - ]:           4 :         if(db != NULL)
     192                 :             :         {
     193                 :           4 :                 (void)sqlite3_close(db);
     194                 :             :         }
     195                 :             : 
     196                 :           4 :         del(db_path);
     197                 :             : 
     198                 :           4 :         return(status);
     199                 :             : }
     200                 :             : 
     201                 :             : /**
     202                 :             :  * Read final offset and SHA512 checksum for one file from DB.
     203                 :             :  */
     204                 :           2 : static Return read_final_sha512_from_db(
     205                 :             :         const char     *db_filename,
     206                 :             :         const char     *relative_path,
     207                 :             :         sqlite3_int64  *offset_out,
     208                 :             :         unsigned char  *sha512_out)
     209                 :             : {
     210                 :             :         /* Status returned by this function through provide()
     211                 :             :            Default value assumes successful completion */
     212                 :           2 :         Return status = SUCCESS;
     213                 :           2 :         sqlite3 *db = NULL;
     214                 :           2 :         sqlite3_stmt *stmt = NULL;
     215                 :           2 :         const char *sql = "SELECT offset, sha512 FROM files WHERE relative_path = ?1;";
     216                 :           2 :         create(char,db_path);
     217                 :             : 
     218         [ +  - ]:           2 :         if(db_filename == NULL
     219         [ +  - ]:           2 :                 || relative_path == NULL
     220         [ +  - ]:           2 :                 || offset_out == NULL
     221         [ -  + ]:           2 :                 || sha512_out == NULL)
     222                 :             :         {
     223                 :           0 :                 status = FAILURE;
     224                 :             :         }
     225                 :             : 
     226         [ +  - ]:           2 :         if(TRIUMPH & status)
     227                 :             :         {
     228                 :           2 :                 status = construct_path(db_filename,db_path);
     229                 :             :         }
     230                 :             : 
     231   [ +  -  -  + ]:           2 :         if((TRIUMPH & status) && SQLITE_OK != sqlite3_open_v2(getcstring(db_path),&db,SQLITE_OPEN_READONLY,NULL))
     232                 :             :         {
     233                 :           0 :                 status = FAILURE;
     234                 :             :         }
     235                 :             : 
     236   [ +  -  -  + ]:           2 :         if((TRIUMPH & status) && SQLITE_OK != sqlite3_prepare_v2(db,sql,-1,&stmt,NULL))
     237                 :             :         {
     238                 :           0 :                 status = FAILURE;
     239                 :             :         }
     240                 :             : 
     241   [ +  -  -  + ]:           2 :         if((TRIUMPH & status) && SQLITE_OK != sqlite3_bind_text(stmt,1,relative_path,(int)strlen(relative_path),SQLITE_TRANSIENT))
     242                 :             :         {
     243                 :           0 :                 status = FAILURE;
     244                 :             :         }
     245                 :             : 
     246         [ +  - ]:           2 :         if(TRIUMPH & status)
     247                 :             :         {
     248                 :           2 :                 const int step_rc = sqlite3_step(stmt);
     249         [ -  + ]:           2 :                 if(step_rc != SQLITE_ROW)
     250                 :             :                 {
     251                 :           0 :                         status = FAILURE;
     252                 :             :                 }
     253                 :             :         }
     254                 :             : 
     255         [ +  - ]:           2 :         if(TRIUMPH & status)
     256                 :             :         {
     257         [ +  - ]:           2 :                 if(sqlite3_column_type(stmt,0) == SQLITE_NULL)
     258                 :             :                 {
     259                 :           2 :                         *offset_out = 0;
     260                 :             :                 } else {
     261                 :           0 :                         *offset_out = sqlite3_column_int64(stmt,0);
     262                 :             :                 }
     263                 :             : 
     264                 :           2 :                 const void *sha512_blob = sqlite3_column_blob(stmt,1);
     265                 :           2 :                 const int sha512_bytes = sqlite3_column_bytes(stmt,1);
     266                 :             : 
     267   [ +  -  -  + ]:           2 :                 if(sha512_blob == NULL || sha512_bytes != SHA512_DIGEST_LENGTH)
     268                 :             :                 {
     269                 :           0 :                         status = FAILURE;
     270                 :             :                 } else {
     271                 :           2 :                         memcpy(sha512_out,sha512_blob,(size_t)SHA512_DIGEST_LENGTH);
     272                 :             :                 }
     273                 :             : 
     274                 :           2 :                 const int done_rc = sqlite3_step(stmt);
     275   [ +  -  -  + ]:           2 :                 if((TRIUMPH & status) && done_rc != SQLITE_DONE)
     276                 :             :                 {
     277                 :           0 :                         status = FAILURE;
     278                 :             :                 }
     279                 :             :         }
     280                 :             : 
     281         [ +  - ]:           2 :         if(stmt != NULL)
     282                 :             :         {
     283                 :           2 :                 (void)sqlite3_finalize(stmt);
     284                 :             :         }
     285                 :             : 
     286         [ +  - ]:           2 :         if(db != NULL)
     287                 :             :         {
     288                 :           2 :                 (void)sqlite3_close(db);
     289                 :             :         }
     290                 :             : 
     291                 :           2 :         del(db_path);
     292                 :             : 
     293                 :           2 :         return(status);
     294                 :             : }
     295                 :             : 
     296                 :             : /**
     297                 :             :  * Run background scenario with SIGTERM and verify output template.
     298                 :             :  */
     299                 :           2 : static Return test0033_1(void)
     300                 :             : {
     301                 :           2 :         INITTEST;
     302                 :             : 
     303                 :           2 :         const char *arguments = "--progress --database=0033_interrupt_resume.db tests/fixtures/diffs/";
     304                 :           2 :         const char *filename = "templates/0033_001.txt";
     305                 :           2 :         const char *cleanup_command = "rm -f \"${TMPDIR}/0033_interrupt_resume.db\"";
     306                 :             : 
     307                 :           2 :         create(char,stdout_result);
     308                 :           2 :         create(char,stderr_result);
     309                 :           2 :         create(char,stdout_pattern);
     310                 :           2 :         create(char,stderr_pattern);
     311                 :             : 
     312   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     313                 :             : 
     314   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit_background(
     315                 :             :                 arguments,
     316                 :             :                 stdout_result,
     317                 :             :                 stderr_result,
     318                 :             :                 SUCCESS|HALTED,
     319                 :             :                 ALLOW_BOTH,
     320                 :             :                 100U,
     321                 :             :                 1000U,
     322                 :             :                 SIGTERM,
     323                 :             :                 1U));
     324                 :             : 
     325   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,stdout_pattern));
     326   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(stdout_result,stdout_pattern,filename));
     327                 :             : 
     328   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == copy_literal(stderr_pattern,"\\A\\Z"));
     329   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(stderr_result,stderr_pattern));
     330                 :             : 
     331   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(cleanup_command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     332                 :             : 
     333                 :           2 :         del(stderr_pattern);
     334                 :           2 :         del(stdout_pattern);
     335                 :           2 :         del(stderr_result);
     336                 :           2 :         del(stdout_result);
     337                 :             : 
     338   [ +  -  -  +  :           2 :         RETURN_STATUS;
          -  -  -  +  +  
                      - ]
     339                 :             : }
     340                 :             : 
     341                 :             : /**
     342                 :             :  * Run background scenario with SIGINT and verify output template.
     343                 :             :  */
     344                 :           2 : static Return test0033_2(void)
     345                 :             : {
     346                 :           2 :         INITTEST;
     347                 :             : 
     348                 :           2 :         const char *arguments = "--progress --database=0033_interrupt_resume.db tests/fixtures/diffs/";
     349                 :           2 :         const char *filename = "templates/0033_002.txt";
     350                 :           2 :         const char *cleanup_command = "rm -f \"${TMPDIR}/0033_interrupt_resume.db\"";
     351                 :             : 
     352                 :           2 :         create(char,stdout_result);
     353                 :           2 :         create(char,stderr_result);
     354                 :           2 :         create(char,stdout_pattern);
     355                 :           2 :         create(char,stderr_pattern);
     356                 :             : 
     357   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     358                 :             : 
     359   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit_background(
     360                 :             :                 arguments,
     361                 :             :                 stdout_result,
     362                 :             :                 stderr_result,
     363                 :             :                 SUCCESS|HALTED,
     364                 :             :                 ALLOW_BOTH,
     365                 :             :                 100U,
     366                 :             :                 1000U,
     367                 :             :                 SIGINT,
     368                 :             :                 1U));
     369                 :             : 
     370   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,stdout_pattern));
     371   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(stdout_result,stdout_pattern,filename));
     372                 :             : 
     373   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == copy_literal(stderr_pattern,"\\A\\Z"));
     374   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(stderr_result,stderr_pattern));
     375                 :             : 
     376   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(cleanup_command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     377                 :             : 
     378                 :           2 :         del(stderr_pattern);
     379                 :           2 :         del(stdout_pattern);
     380                 :           2 :         del(stderr_result);
     381                 :           2 :         del(stdout_result);
     382                 :             : 
     383   [ +  -  -  +  :           2 :         RETURN_STATUS;
          -  -  -  +  +  
                      - ]
     384                 :             : }
     385                 :             : 
     386                 :             : /**
     387                 :             :  * Interrupt hashing of hugetestfile, resume with --update, and verify SHA512.
     388                 :             :  */
     389                 :           2 : static Return test0033_3(void)
     390                 :             : {
     391                 :           2 :         INITTEST;
     392                 :             : 
     393                 :           2 :         const char *relative_path = "hugetestfile";
     394                 :           2 :         const char *first_run_template = "templates/0033_003_1.txt";
     395                 :           2 :         const char *second_run_template = "templates/0033_003_2.txt";
     396                 :           2 :         const char *prepare_command = "cd ${TMPDIR};"
     397                 :             :                 "mkdir -p tests/fixtures/;"
     398                 :             :                 "rm -rf tests/fixtures/huge/;"
     399                 :             :                 "cp -a \"$ORIGIN_DIR/tests/fixtures/huge\" tests/fixtures/;";
     400                 :           2 :         const char *cleanup_command = "cd ${TMPDIR};"
     401                 :             :                 "rm -f 0033_interrupt_resume.db;"
     402                 :             :                 "rm -rf tests/fixtures/huge/;";
     403                 :             : 
     404                 :           2 :         create(char,stdout_result);
     405                 :           2 :         create(char,stderr_result);
     406                 :           2 :         create(char,stdout_pattern);
     407                 :           2 :         create(char,stderr_pattern);
     408                 :           2 :         create(char,huge_file_path);
     409                 :             : 
     410                 :             :         /*
     411                 :             :          * Step 1: Prepare isolated test data in TMPDIR and start from a clean DB
     412                 :             :          */
     413   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     414                 :             : 
     415   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(cleanup_command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     416   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(prepare_command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     417                 :             : 
     418   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == construct_path("tests/fixtures/huge/hugetestfile",huge_file_path));
     419                 :             : 
     420                 :             :         /*
     421                 :             :          * Read the real file size from the filesystem once and use it as
     422                 :             :          * an upper bound for the interrupted offset assertions below
     423                 :             :          */
     424                 :           2 :         struct stat huge_file_stat = {0};
     425   [ +  -  +  - ]:           2 :         ASSERT(0 == stat(getcstring(huge_file_path),&huge_file_stat));
     426   [ +  -  +  - ]:           2 :         ASSERT(huge_file_stat.st_size > 0);
     427                 :             : 
     428                 :           2 :         const char *arguments = "--progress --database=0033_interrupt_resume.db tests/fixtures/huge";
     429                 :             : 
     430                 :             :         /*
     431                 :             :          * Step 2: Run in background, wait until hashing reaches wait-point 2,
     432                 :             :          * then deliver SIGINT. The process must exit as SUCCESS|HALTED
     433                 :             :          */
     434   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit_background(
     435                 :             :                 arguments,
     436                 :             :                 stdout_result,
     437                 :             :                 stderr_result,
     438                 :             :                 SUCCESS|HALTED,
     439                 :             :                 ALLOW_BOTH,
     440                 :             :                 500U,
     441                 :             :                 5000U,
     442                 :             :                 SIGINT,
     443                 :             :                 2U));
     444                 :             : 
     445                 :             :         /*
     446                 :             :          * Step 3: Validate first-run output.
     447                 :             :          * It must contain the interruption scenario messages and no stderr output
     448                 :             :          */
     449   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(first_run_template,stdout_pattern));
     450   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(stdout_result,stdout_pattern,first_run_template));
     451                 :             : 
     452   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == copy_literal(stderr_pattern,"\\A\\Z"));
     453   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(stderr_result,stderr_pattern));
     454                 :             : 
     455                 :           2 :         sqlite3_int64 interrupted_offset = 0;
     456                 :           2 :         int interrupted_md_context_bytes = 0;
     457                 :             : 
     458                 :             :         /*
     459                 :             :          * Step 4: Read intermediate resume state from DB.
     460                 :             :          * The interrupted offset must be inside (0, real_file_size),
     461                 :             :          * and mdContext blob must be non-empty for resume.
     462                 :             :          */
     463   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == read_resume_state_from_db("0033_interrupt_resume.db",relative_path,&interrupted_offset,&interrupted_md_context_bytes));
     464                 :             : 
     465   [ +  -  +  - ]:           2 :         ASSERT(interrupted_offset > 0);
     466   [ +  -  +  - ]:           2 :         ASSERT(interrupted_offset < (sqlite3_int64)huge_file_stat.st_size);
     467                 :             : 
     468   [ +  -  +  - ]:           2 :         ASSERT(interrupted_md_context_bytes > 0);
     469                 :             : 
     470                 :             :         /*
     471                 :             :          * Step 5: Resume hashing with --update and verify second-run output
     472                 :             :          */
     473                 :           2 :         arguments = "--update --progress --database=0033_interrupt_resume.db tests/fixtures/huge";
     474   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,stdout_result,stderr_result,COMPLETED,ALLOW_BOTH));
     475                 :             : 
     476   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(second_run_template,stdout_pattern));
     477   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(stdout_result,stdout_pattern,second_run_template));
     478                 :             : 
     479   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == copy_literal(stderr_pattern,"\\A\\Z"));
     480   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(stderr_result,stderr_pattern));
     481                 :             : 
     482                 :           2 :         sqlite3_int64 final_offset = -1;
     483                 :           2 :         unsigned char db_sha512[SHA512_DIGEST_LENGTH] = {0};
     484                 :           2 :         unsigned char expected_sha512[SHA512_DIGEST_LENGTH] = {0};
     485                 :             : 
     486                 :             :         /*
     487                 :             :          * Step 6: Verify final DB state after resume.
     488                 :             :          * Offset must be reset to 0 and the stored SHA512 must match file content
     489                 :             :          */
     490   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == read_final_sha512_from_db("0033_interrupt_resume.db",relative_path,&final_offset,db_sha512));
     491                 :             : 
     492   [ +  -  +  - ]:           2 :         ASSERT(0 == final_offset);
     493                 :             : 
     494   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == compute_file_sha512(getcstring(huge_file_path),expected_sha512));
     495   [ +  -  +  - ]:           2 :         ASSERT(0 == memcmp(db_sha512,expected_sha512,(size_t)SHA512_DIGEST_LENGTH));
     496                 :             : 
     497                 :             :         /* Step 7: Cleanup temporary test artifacts */
     498   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(cleanup_command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     499                 :             : 
     500                 :           2 :         del(huge_file_path);
     501                 :           2 :         del(stderr_pattern);
     502                 :           2 :         del(stdout_pattern);
     503                 :           2 :         del(stderr_result);
     504                 :           2 :         del(stdout_result);
     505                 :             : 
     506   [ +  -  -  +  :           2 :         RETURN_STATUS;
          -  -  -  +  +  
                      - ]
     507                 :             : }
     508                 :             : 
     509                 :             : /**
     510                 :             :  * Interrupt hashing, modify file metadata, and verify restart from beginning
     511                 :             :  */
     512                 :           2 : static Return test0033_4(void)
     513                 :             : {
     514                 :           2 :         INITTEST;
     515                 :             : 
     516                 :           2 :         const char *db_filename = "0033_interrupt_rehash.db";
     517                 :           2 :         const char *relative_path = "hugetestfile";
     518                 :           2 :         const char *first_run_template = "templates/0033_004_1.txt";
     519                 :           2 :         const char *second_run_template = "templates/0033_004_2.txt";
     520                 :           2 :         const char *prepare_command = "cd ${TMPDIR};"
     521                 :             :                 "mkdir -p tests/fixtures/;"
     522                 :             :                 "rm -rf tests/fixtures/huge/;"
     523                 :             :                 "cp -a \"$ORIGIN_DIR/tests/fixtures/huge\" tests/fixtures/;";
     524                 :           2 :         const char *cleanup_command = "cd ${TMPDIR};"
     525                 :             :                 "rm -f 0033_interrupt_rehash.db;"
     526                 :             :                 "rm -rf tests/fixtures/huge/;";
     527                 :             : 
     528                 :           2 :         create(char,stdout_result);
     529                 :           2 :         create(char,stderr_result);
     530                 :           2 :         create(char,stdout_pattern);
     531                 :           2 :         create(char,stderr_pattern);
     532                 :           2 :         create(char,huge_file_path);
     533                 :             : 
     534   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     535   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(cleanup_command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     536   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(prepare_command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     537   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == construct_path("tests/fixtures/huge/hugetestfile",huge_file_path));
     538                 :             : 
     539                 :           2 :         const char *arguments = "--progress --database=0033_interrupt_rehash.db tests/fixtures/huge";
     540                 :             : 
     541   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit_background(
     542                 :             :                 arguments,
     543                 :             :                 stdout_result,
     544                 :             :                 stderr_result,
     545                 :             :                 SUCCESS|HALTED,
     546                 :             :                 ALLOW_BOTH,
     547                 :             :                 500U,
     548                 :             :                 5000U,
     549                 :             :                 SIGINT,
     550                 :             :                 2U));
     551                 :             : 
     552   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(first_run_template,stdout_pattern));
     553   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(stdout_result,stdout_pattern,first_run_template));
     554   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == copy_literal(stderr_pattern,"\\A\\Z"));
     555   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(stderr_result,stderr_pattern));
     556                 :             : 
     557                 :           2 :         sqlite3_int64 interrupted_offset = 0;
     558                 :           2 :         int interrupted_md_context_bytes = 0;
     559                 :             : 
     560   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == read_resume_state_from_db(db_filename,relative_path,&interrupted_offset,&interrupted_md_context_bytes));
     561   [ +  -  +  - ]:           2 :         ASSERT(interrupted_offset > 0);
     562   [ +  -  +  - ]:           2 :         ASSERT(interrupted_md_context_bytes > 0);
     563                 :             : 
     564   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == append_byte_to_file(getcstring(huge_file_path),(unsigned char)'X'));
     565                 :             : 
     566                 :           2 :         arguments = "--update --progress --database=0033_interrupt_rehash.db tests/fixtures/huge";
     567   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,stdout_result,stderr_result,COMPLETED,ALLOW_BOTH));
     568                 :             : 
     569   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(second_run_template,stdout_pattern));
     570   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(stdout_result,stdout_pattern,second_run_template));
     571   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == copy_literal(stderr_pattern,"\\A\\Z"));
     572   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(stderr_result,stderr_pattern));
     573                 :             : 
     574                 :           2 :         const char *expected_paths[] =
     575                 :             :         {
     576                 :             :                 "hugetestfile"
     577                 :             :         };
     578                 :             : 
     579   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == db_paths_match(db_filename,expected_paths,(int)(sizeof(expected_paths) / sizeof(expected_paths[0]))));
     580   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(cleanup_command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     581                 :             : 
     582                 :           2 :         del(huge_file_path);
     583                 :           2 :         del(stderr_pattern);
     584                 :           2 :         del(stdout_pattern);
     585                 :           2 :         del(stderr_result);
     586                 :           2 :         del(stdout_result);
     587                 :             : 
     588   [ +  -  -  +  :           2 :         RETURN_STATUS;
          -  -  -  +  +  
                      - ]
     589                 :             : }
     590                 :             : 
     591                 :             : /**
     592                 :             :  * Background interruption tests grouped as a separate suite.
     593                 :             :  */
     594                 :           2 : Return test0033(void)
     595                 :             : {
     596                 :           2 :         INITTEST;
     597                 :             : 
     598         [ +  - ]:           2 :         TEST(test0033_1,"Background run receives SIGTERM and exits with HALTED…");
     599         [ +  - ]:           2 :         TEST(test0033_2,"Background run receives SIGINT and exits with HALTED…");
     600         [ +  - ]:           2 :         TEST(test0033_3,"Random interruption on hugetestfile with resume and SHA512 verification…");
     601         [ +  - ]:           2 :         TEST(test0033_4,"Interrupted hash with file change restarts rehash from the beginning…");
     602                 :             : 
     603   [ +  -  -  +  :           2 :         RETURN_STATUS;
          -  -  -  +  +  
                      - ]
     604                 :             : }
        

Generated by: LCOV version 2.0-1