LCOV - code coverage report
Current view: top level - tests/src - test0030.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 93.3 % 313 292
Test Date: 2026-03-01 04:31:48 Functions: 100.0 % 13 13
Branches: 48.4 % 570 276

             Branch data     Line data    Source code
       1                 :             : #include "sute.h"
       2                 :             : 
       3                 :             : #define TARGET_REL_PATH "path1/AAA/BCB/CCC/a.txt"
       4                 :             : #define TARGET_FILE_REL "tests/fixtures/diffs/diff1/" TARGET_REL_PATH
       5                 :             : #define TARGET_FILE "${TMPDIR}/tests/fixtures/diffs/diff1/" TARGET_REL_PATH
       6                 :             : #define LOCKED_TAMPER_PATH "path1/AAA/ZAW/A/b/c/a_file.txt"
       7                 :             : #define LOCKED_TAMPER_FILE "tests/fixtures/diffs/diff1/" LOCKED_TAMPER_PATH
       8                 :             : 
       9                 :           2 : static Return tamper_locked_checksum(
      10                 :             :         const char *db_filename,
      11                 :             :         const char *relative_path)
      12                 :             : {
      13                 :             :         /* Status returned by this function through provide()
      14                 :             :            Default value assumes successful completion */
      15                 :           2 :         Return status = SUCCESS;
      16                 :           2 :         sqlite3 *db = NULL;
      17                 :           2 :         sqlite3_stmt *stmt = NULL;
      18                 :           2 :         const char *sql = "UPDATE files SET sha512 = (substr(sha512,1,2) || X'BEEF' || substr(sha512,5)) " "WHERE relative_path = ?1;";
      19                 :             : 
      20                 :           2 :         create(char,db_path);
      21                 :             : 
      22   [ +  -  +  -  :           2 :         if(SUCCESS == status && (db_filename == NULL || relative_path == NULL))
                   -  + ]
      23                 :             :         {
      24                 :           0 :                 status = FAILURE;
      25                 :             :         }
      26                 :             : 
      27         [ +  - ]:           2 :         if(SUCCESS == status)
      28                 :             :         {
      29                 :           2 :                 status = construct_path(db_filename,db_path);
      30                 :             :         }
      31                 :             : 
      32   [ +  -  -  + ]:           2 :         if(SUCCESS == status && SQLITE_OK != sqlite3_open_v2(getcstring(db_path),&db,SQLITE_OPEN_READWRITE,NULL))
      33                 :             :         {
      34                 :           0 :                 status = FAILURE;
      35                 :             :         }
      36                 :             : 
      37   [ +  -  -  + ]:           2 :         if(SUCCESS == status && SQLITE_OK != sqlite3_prepare_v2(db,sql,-1,&stmt,NULL))
      38                 :             :         {
      39                 :           0 :                 status = FAILURE;
      40                 :             :         }
      41                 :             : 
      42   [ +  -  -  + ]:           2 :         if(SUCCESS == status && SQLITE_OK != sqlite3_bind_text(stmt,1,relative_path,(int)strlen(relative_path),SQLITE_TRANSIENT))
      43                 :             :         {
      44                 :           0 :                 status = FAILURE;
      45                 :             :         }
      46                 :             : 
      47   [ +  -  -  + ]:           2 :         if(SUCCESS == status && SQLITE_DONE != sqlite3_step(stmt))
      48                 :             :         {
      49                 :           0 :                 status = FAILURE;
      50                 :             :         }
      51                 :             : 
      52   [ +  -  -  + ]:           2 :         if(SUCCESS == status && sqlite3_changes(db) < 1)
      53                 :             :         {
      54                 :           0 :                 status = FAILURE;
      55                 :             :         }
      56                 :             : 
      57         [ +  - ]:           2 :         if(stmt != NULL)
      58                 :             :         {
      59                 :           2 :                 (void)sqlite3_finalize(stmt);
      60                 :             :         }
      61                 :             : 
      62         [ +  - ]:           2 :         if(db != NULL)
      63                 :             :         {
      64                 :           2 :                 (void)sqlite3_close(db);
      65                 :             :         }
      66                 :             : 
      67                 :           2 :         del(db_path);
      68                 :             : 
      69                 :           2 :         return(status);
      70                 :             : }
      71                 :             : 
      72                 :           4 : static Return read_cmpctstat_from_db(
      73                 :             :         const char *db_filename,
      74                 :             :         const char *relative_path,
      75                 :             :         CmpctStat *stat_out)
      76                 :             : {
      77                 :             :         /* Status returned by this function through provide()
      78                 :             :            Default value assumes successful completion */
      79                 :           4 :         Return status = SUCCESS;
      80                 :           4 :         sqlite3 *db = NULL;
      81                 :           4 :         sqlite3_stmt *stmt = NULL;
      82                 :           4 :         const char *sql = "SELECT stat FROM files WHERE relative_path = ?1;";
      83                 :           4 :         create(char,db_path);
      84                 :             : 
      85   [ +  -  +  -  :           4 :         if(SUCCESS == status && (db_filename == NULL || relative_path == NULL || stat_out == NULL))
             +  -  -  + ]
      86                 :             :         {
      87                 :           0 :                 status = FAILURE;
      88                 :             :         }
      89                 :             : 
      90         [ +  - ]:           4 :         if(SUCCESS == status)
      91                 :             :         {
      92                 :           4 :                 status = construct_path(db_filename,db_path);
      93                 :             :         }
      94                 :             : 
      95   [ +  -  -  + ]:           4 :         if(SUCCESS == status && SQLITE_OK != sqlite3_open_v2(getcstring(db_path),&db,SQLITE_OPEN_READONLY,NULL))
      96                 :             :         {
      97                 :           0 :                 status = FAILURE;
      98                 :             :         }
      99                 :             : 
     100   [ +  -  -  + ]:           4 :         if(SUCCESS == status && SQLITE_OK != sqlite3_prepare_v2(db,sql,-1,&stmt,NULL))
     101                 :             :         {
     102                 :           0 :                 status = FAILURE;
     103                 :             :         }
     104                 :             : 
     105   [ +  -  -  + ]:           4 :         if(SUCCESS == status && SQLITE_OK != sqlite3_bind_text(stmt,1,relative_path,(int)strlen(relative_path),SQLITE_TRANSIENT))
     106                 :             :         {
     107                 :           0 :                 status = FAILURE;
     108                 :             :         }
     109                 :             : 
     110         [ +  - ]:           4 :         if(SUCCESS == status)
     111                 :             :         {
     112                 :           4 :                 int rc = sqlite3_step(stmt);
     113                 :             : 
     114         [ +  - ]:           4 :                 if(rc == SQLITE_ROW)
     115                 :             :                 {
     116                 :           4 :                         const void *blob = sqlite3_column_blob(stmt,0);
     117                 :           4 :                         int bytes = sqlite3_column_bytes(stmt,0);
     118                 :             : 
     119   [ +  -  -  + ]:           4 :                         if(blob == NULL || bytes != (int)sizeof(CmpctStat))
     120                 :             :                         {
     121                 :           0 :                                 status = FAILURE;
     122                 :             :                         } else {
     123                 :           4 :                                 memcpy(stat_out,blob,sizeof(CmpctStat));
     124                 :             :                         }
     125                 :             : 
     126                 :           4 :                         rc = sqlite3_step(stmt);
     127                 :             : 
     128   [ +  -  -  + ]:           4 :                         if(SUCCESS == status && rc != SQLITE_DONE)
     129                 :             :                         {
     130                 :           0 :                                 status = FAILURE;
     131                 :             :                         }
     132                 :             :                 } else {
     133                 :           0 :                         status = FAILURE;
     134                 :             :                 }
     135                 :             :         }
     136                 :             : 
     137         [ +  - ]:           4 :         if(stmt != NULL)
     138                 :             :         {
     139                 :           4 :                 (void)sqlite3_finalize(stmt);
     140                 :             :         }
     141                 :             : 
     142         [ +  - ]:           4 :         if(db != NULL)
     143                 :             :         {
     144                 :           4 :                 (void)sqlite3_close(db);
     145                 :             :         }
     146                 :             : 
     147                 :           4 :         del(db_path);
     148                 :             : 
     149                 :           4 :         return(status);
     150                 :             : }
     151                 :             : 
     152                 :           4 : static bool cmpctstat_matches_stat_timestamps(
     153                 :             :         const CmpctStat *db_stat,
     154                 :             :         const struct stat *file_stat)
     155                 :             : {
     156   [ +  -  -  + ]:           4 :         if(db_stat == NULL || file_stat == NULL)
     157                 :             :         {
     158                 :           0 :                 return(false);
     159                 :             :         }
     160                 :             : 
     161                 :           8 :         return(db_stat->mtim_tv_sec == file_stat->st_mtim.tv_sec &&
     162         [ +  - ]:           4 :                 db_stat->mtim_tv_nsec == file_stat->st_mtim.tv_nsec &&
     163   [ +  -  +  - ]:          12 :                 db_stat->ctim_tv_sec == file_stat->st_ctim.tv_sec &&
     164         [ +  - ]:           4 :                 db_stat->ctim_tv_nsec == file_stat->st_ctim.tv_nsec);
     165                 :             : }
     166                 :             : 
     167                 :           4 : static Return tamper_locked_file_bytes(
     168                 :             :         const char *relative_path)
     169                 :             : {
     170                 :             :         /* Status returned by this function through provide()
     171                 :             :            Default value assumes successful completion */
     172                 :           4 :         Return status = SUCCESS;
     173                 :           4 :         int fd = -1;
     174                 :           4 :         struct stat before = {0};
     175                 :           4 :         unsigned char buffer[2] = {0};
     176                 :           4 :         struct timespec times[2] = {{0}};
     177                 :           4 :         create(char,file_path);
     178                 :             : 
     179   [ +  -  -  + ]:           4 :         if(SUCCESS == status && relative_path == NULL)
     180                 :             :         {
     181                 :           0 :                 status = FAILURE;
     182                 :             :         }
     183                 :             : 
     184         [ +  - ]:           4 :         if(SUCCESS == status)
     185                 :             :         {
     186                 :           4 :                 status = construct_path(relative_path,file_path);
     187                 :             :         }
     188                 :             : 
     189   [ +  -  -  + ]:           4 :         if(SUCCESS == status && (fd = open(getcstring(file_path),O_RDWR)) < 0)
     190                 :             :         {
     191                 :           0 :                 status = FAILURE;
     192                 :             :         }
     193                 :             : 
     194   [ +  -  -  + ]:           4 :         if(SUCCESS == status && fstat(fd,&before) != 0)
     195                 :             :         {
     196                 :           0 :                 status = FAILURE;
     197                 :             :         }
     198                 :             : 
     199   [ +  -  -  + ]:           4 :         if(SUCCESS == status && before.st_size < (off_t)sizeof(buffer))
     200                 :             :         {
     201                 :           0 :                 status = FAILURE;
     202                 :             :         }
     203                 :             : 
     204   [ +  -  -  + ]:           4 :         if(SUCCESS == status && pread(fd,buffer,sizeof(buffer),0) != (ssize_t)sizeof(buffer))
     205                 :             :         {
     206                 :           0 :                 status = FAILURE;
     207                 :             :         }
     208                 :             : 
     209         [ +  - ]:           4 :         if(SUCCESS == status)
     210                 :             :         {
     211                 :           4 :                 buffer[0] = (unsigned char)~buffer[0];
     212                 :           4 :                 buffer[1] = (unsigned char)~buffer[1];
     213                 :             :         }
     214                 :             : 
     215   [ +  -  -  + ]:           4 :         if(SUCCESS == status && pwrite(fd,buffer,sizeof(buffer),0) != (ssize_t)sizeof(buffer))
     216                 :             :         {
     217                 :           0 :                 status = FAILURE;
     218                 :             :         }
     219                 :             : 
     220         [ +  - ]:           4 :         if(SUCCESS == status)
     221                 :             :         {
     222                 :             :                 // Best effort: restore atime/mtime; ctime will still update on POSIX.
     223                 :           4 :                 times[0] = before.st_atim;
     224                 :           4 :                 times[1] = before.st_mtim;
     225                 :             : 
     226         [ -  + ]:           4 :                 if(futimens(fd,times) != 0)
     227                 :             :                 {
     228                 :           0 :                         status = FAILURE;
     229                 :             :                 }
     230                 :             :         }
     231                 :             : 
     232         [ +  - ]:           4 :         if(fd >= 0)
     233                 :             :         {
     234                 :           4 :                 (void)close(fd);
     235                 :             :         }
     236                 :             : 
     237                 :           4 :         del(file_path);
     238                 :             : 
     239                 :           4 :         return(status);
     240                 :             : }
     241                 :             : 
     242                 :             : /**
     243                 :             :  * README --lock-checksum example: size differs -> WARNING, independent of
     244                 :             :  * --watch-timestamps or --rehash-locked.
     245                 :             :  */
     246                 :           2 : static Return test0030_1(void)
     247                 :             : {
     248                 :           2 :         INITTEST;
     249                 :             : 
     250                 :           2 :         create(char,result);
     251                 :           2 :         create(char,pattern);
     252                 :             : 
     253   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     254                 :             : 
     255                 :           2 :         const char *command = "cd ${TMPDIR};"
     256                 :             :                 "mv tests/fixtures/diffs/diff1 tests/fixtures/diff1_backup;"
     257                 :             :                 "cp -a tests/fixtures/diff1_backup tests/fixtures/diffs/diff1;";
     258                 :             : 
     259   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     260                 :             : 
     261                 :           2 :         const char *arguments = "--database=lock_s1.db --progress "
     262                 :             :                 "--lock-checksum=\"^path1/.*\" tests/fixtures/diffs/diff1";
     263                 :             : 
     264   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     265                 :             : 
     266                 :           2 :         const char *filename = "templates/0030_001_1.txt";
     267                 :             : 
     268   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     269                 :             : 
     270   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     271                 :             : 
     272                 :           2 :         command = "printf 'pad' >> " TARGET_FILE;
     273                 :             : 
     274   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     275                 :             : 
     276                 :           2 :         arguments = "--update --rehash-locked --lock-checksum=\"^path1/.*\" "
     277                 :             :                 "--database=lock_s1.db tests/fixtures/diffs/diff1";
     278                 :             : 
     279   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,WARNING,ALLOW_BOTH));
     280                 :             : 
     281                 :           2 :         filename = "templates/0030_001_2.txt";
     282                 :             : 
     283   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     284                 :             : 
     285   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     286                 :             : 
     287                 :           2 :         command = "cd ${TMPDIR} && "
     288                 :             :                 "rm -f lock_s1.db && "
     289                 :             :                 "rm -rf tests/fixtures/diffs/diff1 && "
     290                 :             :                 "mv tests/fixtures/diff1_backup tests/fixtures/diffs/diff1";
     291   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     292                 :             : 
     293                 :           2 :         del(pattern);
     294                 :           2 :         del(result);
     295                 :             : 
     296   [ +  -  -  +  :           2 :         RETURN_STATUS;
          -  -  -  +  +  
                      - ]
     297                 :             : }
     298                 :             : 
     299                 :             : /**
     300                 :             :  * README --lock-checksum example: timestamps differ, --watch-timestamps enabled,
     301                 :             :  * --rehash-locked disabled -> WARNING.
     302                 :             :  */
     303                 :           2 : static Return test0030_2(void)
     304                 :             : {
     305                 :           2 :         INITTEST;
     306                 :             : 
     307                 :           2 :         create(char,result);
     308                 :           2 :         create(char,pattern);
     309                 :             : 
     310   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     311                 :             : 
     312                 :           2 :         const char *command = "cd ${TMPDIR};"
     313                 :             :                 "mv tests/fixtures/diffs/diff1 tests/fixtures/diff1_backup;"
     314                 :             :                 "cp -a tests/fixtures/diff1_backup tests/fixtures/diffs/diff1;"
     315                 :             :                 "rm -f lock_s2.db;";
     316                 :             : 
     317   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     318                 :             : 
     319                 :           2 :         const char *arguments = "--database=lock_s2.db --progress "
     320                 :             :                 "--lock-checksum=\"^path1/.*\" tests/fixtures/diffs/diff1";
     321                 :             : 
     322   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     323                 :             : 
     324                 :           2 :         const char *filename = "templates/0030_002_1.txt";
     325                 :             : 
     326   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     327                 :             : 
     328   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     329                 :             : 
     330                 :           2 :         command = "touch -m " TARGET_FILE;
     331   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     332                 :             : 
     333                 :           2 :         arguments = "--update --watch-timestamps --lock-checksum=\"^path1/.*\" "
     334                 :             :                 "--database=lock_s2.db tests/fixtures/diffs/diff1";
     335                 :             : 
     336   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,WARNING,ALLOW_BOTH));
     337                 :             : 
     338                 :           2 :         filename = "templates/0030_002_2.txt";
     339                 :             : 
     340   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     341                 :             : 
     342   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     343                 :             : 
     344                 :           2 :         command = "cd ${TMPDIR} && "
     345                 :             :                 "rm -f lock_s2.db && "
     346                 :             :                 "rm -rf tests/fixtures/diffs/diff1 && "
     347                 :             :                 "mv tests/fixtures/diff1_backup tests/fixtures/diffs/diff1";
     348   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     349                 :             : 
     350                 :           2 :         del(pattern);
     351                 :           2 :         del(result);
     352                 :             : 
     353   [ +  -  -  +  :           2 :         RETURN_STATUS;
          -  -  -  +  +  
                      - ]
     354                 :             : }
     355                 :             : 
     356                 :             : /**
     357                 :             :  * README --lock-checksum example: size matches, --watch-timestamps disabled,
     358                 :             :  * --rehash-locked disabled -> SUCCESS (timestamps ignored).
     359                 :             :  */
     360                 :           2 : static Return test0030_3(void)
     361                 :             : {
     362                 :           2 :         INITTEST;
     363                 :             : 
     364                 :           2 :         create(char,result);
     365                 :           2 :         create(char,pattern);
     366                 :             : 
     367   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     368                 :             : 
     369                 :           2 :         const char *command = "cd ${TMPDIR};"
     370                 :             :                 "mv tests/fixtures/diffs/diff1 tests/fixtures/diff1_backup;"
     371                 :             :                 "cp -a tests/fixtures/diff1_backup tests/fixtures/diffs/diff1;"
     372                 :             :                 "rm -f lock_s3.db;";
     373                 :             : 
     374   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     375                 :             : 
     376                 :           2 :         const char *arguments = "--database=lock_s3.db --progress "
     377                 :             :                 "--lock-checksum=\"^path1/.*\" tests/fixtures/diffs/diff1";
     378                 :             : 
     379   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     380                 :             : 
     381                 :           2 :         const char *filename = "templates/0030_003_1.txt";
     382                 :             : 
     383   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     384                 :             : 
     385   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     386                 :             : 
     387                 :           2 :         command = "touch -m " TARGET_FILE;
     388   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     389                 :             : 
     390                 :           2 :         arguments = "--update --lock-checksum=\"^path1/.*\" "
     391                 :             :                 "--database=lock_s3.db tests/fixtures/diffs/diff1";
     392                 :             : 
     393   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     394                 :             : 
     395                 :           2 :         filename = "templates/0030_003_2.txt";
     396                 :             : 
     397   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     398                 :             : 
     399   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     400                 :             : 
     401                 :           2 :         command = "cd ${TMPDIR} && "
     402                 :             :                 "rm -f lock_s3.db && "
     403                 :             :                 "rm -rf tests/fixtures/diffs/diff1 && "
     404                 :             :                 "mv tests/fixtures/diff1_backup tests/fixtures/diffs/diff1";
     405   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     406                 :             : 
     407                 :           2 :         del(pattern);
     408                 :           2 :         del(result);
     409                 :             : 
     410   [ +  -  -  +  :           2 :         RETURN_STATUS;
          -  -  -  +  +  
                      - ]
     411                 :             : }
     412                 :             : 
     413                 :             : /**
     414                 :             :  * README --lock-checksum example: --rehash-locked decides consistency; when the
     415                 :             :  * checksum matches, result is SUCCESS regardless of --watch-timestamps.
     416                 :             :  */
     417                 :           2 : static Return test0030_4(void)
     418                 :             : {
     419                 :           2 :         INITTEST;
     420                 :             : 
     421                 :           2 :         create(char,result);
     422                 :           2 :         create(char,pattern);
     423                 :           2 :         create(char,target_path);
     424                 :           2 :         struct stat file_stat_before = {0};
     425                 :           2 :         struct stat file_stat_after = {0};
     426                 :           2 :         CmpctStat db_stat_before = {0};
     427                 :           2 :         CmpctStat db_stat_after = {0};
     428                 :             : 
     429   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     430                 :             : 
     431                 :           2 :         const char *command = "cd ${TMPDIR};"
     432                 :             :                 "mv tests/fixtures/diffs/diff1 tests/fixtures/diff1_backup;"
     433                 :             :                 "cp -a tests/fixtures/diff1_backup tests/fixtures/diffs/diff1;"
     434                 :             :                 "rm -f lock_s4.db;";
     435                 :             : 
     436   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     437                 :             : 
     438                 :           2 :         const char *arguments = "--database=lock_s4.db --progress "
     439                 :             :                 "--lock-checksum=\"^path1/.*\" tests/fixtures/diffs/diff1";
     440                 :             : 
     441   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     442                 :             : 
     443                 :           2 :         const char *filename = "templates/0030_004_1.txt";
     444                 :             : 
     445   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     446                 :             : 
     447   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     448                 :             : 
     449   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == construct_path(TARGET_FILE_REL,target_path));
     450                 :             : 
     451   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == read_cmpctstat_from_db("lock_s4.db",TARGET_REL_PATH,&db_stat_before));
     452                 :             : 
     453   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_stat(getcstring(target_path),&file_stat_before));
     454                 :             : 
     455   [ +  -  +  - ]:           2 :         ASSERT(cmpctstat_matches_stat_timestamps(&db_stat_before,&file_stat_before));
     456                 :             : 
     457                 :           2 :         command = "touch -m " TARGET_FILE;
     458   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     459                 :             : 
     460                 :           2 :         arguments = "--update --watch-timestamps --rehash-locked "
     461                 :             :                 "--lock-checksum=\"^path1/.*\" --database=lock_s4.db "
     462                 :             :                 "tests/fixtures/diffs/diff1";
     463                 :             : 
     464   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     465                 :             : 
     466                 :           2 :         filename = "templates/0030_004_2.txt";
     467                 :             : 
     468   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     469                 :             : 
     470   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     471                 :             : 
     472   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == read_cmpctstat_from_db("lock_s4.db",TARGET_REL_PATH,&db_stat_after));
     473                 :             : 
     474   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_stat(getcstring(target_path),&file_stat_after));
     475                 :             : 
     476   [ +  -  +  - ]:           2 :         ASSERT(cmpctstat_matches_stat_timestamps(&db_stat_after,&file_stat_after));
     477                 :             : 
     478                 :           2 :         command = "cd ${TMPDIR} && "
     479                 :             :                 "rm -f lock_s4.db && "
     480                 :             :                 "rm -rf tests/fixtures/diffs/diff1 && "
     481                 :             :                 "mv tests/fixtures/diff1_backup tests/fixtures/diffs/diff1";
     482                 :             : 
     483   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     484                 :             : 
     485                 :           2 :         del(target_path);
     486                 :           2 :         del(pattern);
     487                 :           2 :         del(result);
     488                 :             : 
     489   [ +  -  -  +  :           2 :         RETURN_STATUS;
          -  -  -  +  +  
                      - ]
     490                 :             : }
     491                 :             : 
     492                 :             : /**
     493                 :             :  * README --lock-checksum example extension: rehash compares computed checksum with
     494                 :             :  * the stored one; a mismatch should trigger WARNING. Simulate by modifying the DB.
     495                 :             :  */
     496                 :           2 : static Return test0030_5(void)
     497                 :             : {
     498                 :           2 :         INITTEST;
     499                 :             : 
     500                 :           2 :         create(char,result);
     501                 :           2 :         create(char,pattern);
     502                 :             : 
     503   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     504                 :             : 
     505                 :           2 :         const char *command = "cd ${TMPDIR};"
     506                 :             :                 "mv tests/fixtures/diffs/diff1 tests/fixtures/diff1_backup;"
     507                 :             :                 "cp -a tests/fixtures/diff1_backup tests/fixtures/diffs/diff1;"
     508                 :             :                 "rm -f lock_s5.db;";
     509                 :             : 
     510   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     511                 :             : 
     512                 :           2 :         const char *arguments = "--database=lock_s5.db --progress "
     513                 :             :                 "--lock-checksum=\"^path1/.*\" tests/fixtures/diffs/diff1";
     514                 :             : 
     515   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     516                 :             : 
     517                 :           2 :         const char *filename = "templates/0030_005_1.txt";
     518                 :             : 
     519   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     520                 :             : 
     521   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     522                 :             : 
     523                 :           2 :         command = "touch -m " TARGET_FILE;
     524                 :             : 
     525   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     526                 :             : 
     527                 :             :         // Corrupt the stored checksum for a locked file without touching it on disk.
     528   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == tamper_locked_checksum("lock_s5.db",LOCKED_TAMPER_PATH));
     529                 :             : 
     530                 :           2 :         arguments = "--update --watch-timestamps --rehash-locked "
     531                 :             :                 "--lock-checksum=\"^path1/.*\" --database=lock_s5.db "
     532                 :             :                 "tests/fixtures/diffs/diff1";
     533                 :             : 
     534   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,WARNING,ALLOW_BOTH));
     535                 :             : 
     536                 :           2 :         filename = "templates/0030_005_2.txt";
     537                 :             : 
     538   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     539                 :             : 
     540   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     541                 :             : 
     542                 :           2 :         command = "cd ${TMPDIR} && "
     543                 :             :                 "rm -f lock_s5.db && "
     544                 :             :                 "rm -rf tests/fixtures/diffs/diff1 && "
     545                 :             :                 "mv tests/fixtures/diff1_backup tests/fixtures/diffs/diff1";
     546                 :             : 
     547   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     548                 :             : 
     549                 :           2 :         del(pattern);
     550                 :           2 :         del(result);
     551                 :             : 
     552   [ +  -  -  +  :           2 :         RETURN_STATUS;
          -  -  -  +  +  
                      - ]
     553                 :             : }
     554                 :             : 
     555                 :             : /**
     556                 :             :  * README --lock-checksum example extension: on-disk content change with
     557                 :             :  * --rehash-locked should trigger WARNING.
     558                 :             :  */
     559                 :           2 : static Return test0030_6(void)
     560                 :             : {
     561                 :           2 :         INITTEST;
     562                 :             : 
     563                 :           2 :         create(char,result);
     564                 :           2 :         create(char,pattern);
     565                 :             : 
     566   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     567                 :             : 
     568                 :           2 :         const char *command = "cd ${TMPDIR};"
     569                 :             :                 "mv tests/fixtures/diffs/diff1 tests/fixtures/diff1_backup;"
     570                 :             :                 "cp -a tests/fixtures/diff1_backup tests/fixtures/diffs/diff1;"
     571                 :             :                 "rm -f lock_s6.db;";
     572                 :             : 
     573   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     574                 :             : 
     575                 :           2 :         const char *arguments = "--database=lock_s6.db --progress "
     576                 :             :                 "--lock-checksum=\"^path1/.*\" tests/fixtures/diffs/diff1";
     577                 :             : 
     578   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     579                 :             : 
     580                 :           2 :         const char *filename = "templates/0030_006_1.txt";
     581                 :             : 
     582   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     583                 :             : 
     584   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     585                 :             : 
     586   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == tamper_locked_file_bytes(LOCKED_TAMPER_FILE));
     587                 :             : 
     588                 :           2 :         arguments = "--update --rehash-locked --lock-checksum=\"^path1/.*\" "
     589                 :             :                 "--database=lock_s6.db tests/fixtures/diffs/diff1";
     590                 :             : 
     591   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,WARNING,ALLOW_BOTH));
     592                 :             : 
     593                 :           2 :         filename = "templates/0030_006_2.txt";
     594                 :             : 
     595   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     596                 :             : 
     597   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     598                 :             : 
     599                 :           2 :         command = "cd ${TMPDIR} && "
     600                 :             :                 "rm -f lock_s6.db && "
     601                 :             :                 "rm -rf tests/fixtures/diffs/diff1 && "
     602                 :             :                 "mv tests/fixtures/diff1_backup tests/fixtures/diffs/diff1";
     603                 :             : 
     604   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     605                 :             : 
     606                 :           2 :         del(pattern);
     607                 :           2 :         del(result);
     608                 :             : 
     609   [ +  -  -  +  :           2 :         RETURN_STATUS;
          -  -  -  +  +  
                      - ]
     610                 :             : }
     611                 :             : 
     612                 :             : /**
     613                 :             :  * Same as test0030_6 but with --watch-timestamps; per README example, this option
     614                 :             :  * does not change the --rehash-locked outcome.
     615                 :             :  */
     616                 :           2 : static Return test0030_7(void)
     617                 :             : {
     618                 :           2 :         INITTEST;
     619                 :             : 
     620                 :           2 :         create(char,result);
     621                 :           2 :         create(char,pattern);
     622                 :             : 
     623   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     624                 :             : 
     625                 :           2 :         const char *command = "cd ${TMPDIR};"
     626                 :             :                 "mv tests/fixtures/diffs/diff1 tests/fixtures/diff1_backup;"
     627                 :             :                 "cp -a tests/fixtures/diff1_backup tests/fixtures/diffs/diff1;"
     628                 :             :                 "rm -f lock_s7.db;";
     629                 :             : 
     630   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     631                 :             : 
     632                 :           2 :         const char *arguments = "--database=lock_s7.db --progress "
     633                 :             :                 "--lock-checksum=\"^path1/.*\" tests/fixtures/diffs/diff1";
     634                 :             : 
     635   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     636                 :             : 
     637                 :           2 :         const char *filename = "templates/0030_007_1.txt";
     638                 :             : 
     639   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     640                 :             : 
     641   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     642                 :             : 
     643   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == tamper_locked_file_bytes(LOCKED_TAMPER_FILE));
     644                 :             : 
     645                 :           2 :         arguments = "--update --watch-timestamps --rehash-locked "
     646                 :             :                 "--lock-checksum=\"^path1/.*\" --database=lock_s7.db "
     647                 :             :                 "tests/fixtures/diffs/diff1";
     648                 :             : 
     649   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,WARNING,ALLOW_BOTH));
     650                 :             : 
     651                 :           2 :         filename = "templates/0030_007_2.txt";
     652                 :             : 
     653   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     654                 :             : 
     655   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     656                 :             : 
     657                 :           2 :         command = "cd ${TMPDIR} && "
     658                 :             :                 "rm -f lock_s7.db && "
     659                 :             :                 "rm -rf tests/fixtures/diffs/diff1 && "
     660                 :             :                 "mv tests/fixtures/diff1_backup tests/fixtures/diffs/diff1";
     661                 :             : 
     662   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     663                 :             : 
     664                 :           2 :         del(pattern);
     665                 :           2 :         del(result);
     666                 :             : 
     667   [ +  -  -  +  :           2 :         RETURN_STATUS;
          -  -  -  +  +  
                      - ]
     668                 :             : }
     669                 :             : 
     670                 :             : /**
     671                 :             :  * README --lock-checksum example: size and timestamps match,
     672                 :             :  * --watch-timestamps enabled, --rehash-locked disabled -> SUCCESS.
     673                 :             :  */
     674                 :           2 : static Return test0030_8(void)
     675                 :             : {
     676                 :           2 :         INITTEST;
     677                 :             : 
     678                 :           2 :         create(char,result);
     679                 :           2 :         create(char,pattern);
     680                 :             : 
     681   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     682                 :             : 
     683                 :           2 :         const char *command = "cd ${TMPDIR};"
     684                 :             :                 "mv tests/fixtures/diffs/diff1 tests/fixtures/diff1_backup;"
     685                 :             :                 "cp -a tests/fixtures/diff1_backup tests/fixtures/diffs/diff1;"
     686                 :             :                 "rm -f lock_s8.db;";
     687                 :             : 
     688   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     689                 :             : 
     690                 :           2 :         const char *arguments = "--database=lock_s8.db --progress "
     691                 :             :                 "--lock-checksum=\"^path1/.*\" tests/fixtures/diffs/diff1";
     692                 :             : 
     693   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     694                 :             : 
     695                 :           2 :         const char *filename = "templates/0030_008_1.txt";
     696                 :             : 
     697   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     698                 :             : 
     699   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     700                 :             : 
     701                 :           2 :         arguments = "--update --watch-timestamps --lock-checksum=\"^path1/.*\" "
     702                 :             :                 "--database=lock_s8.db tests/fixtures/diffs/diff1";
     703                 :             : 
     704   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     705                 :             : 
     706                 :           2 :         filename = "templates/0030_008_2.txt";
     707                 :             : 
     708   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     709                 :             : 
     710   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     711                 :             : 
     712                 :           2 :         command = "cd ${TMPDIR} && "
     713                 :             :                 "rm -f lock_s8.db && "
     714                 :             :                 "rm -rf tests/fixtures/diffs/diff1 && "
     715                 :             :                 "mv tests/fixtures/diff1_backup tests/fixtures/diffs/diff1";
     716                 :             : 
     717   [ +  -  +  - ]:           2 :         ASSERT(SUCCESS == external_call(command,NULL,NULL,COMPLETED,ALLOW_BOTH));
     718                 :             : 
     719                 :           2 :         del(pattern);
     720                 :           2 :         del(result);
     721                 :             : 
     722   [ +  -  -  +  :           2 :         RETURN_STATUS;
          -  -  -  +  +  
                      - ]
     723                 :             : }
     724                 :             : 
     725                 :             : /**
     726                 :             :  *
     727                 :             :  * README --lock-checksum examples.
     728                 :             :  * Scenarios with --lock-checksum, --rehash-locked, and --watch-timestamps
     729                 :             :  *
     730                 :             :  */
     731                 :           2 : Return test0030(void)
     732                 :             : {
     733                 :           2 :         INITTEST;
     734                 :             : 
     735         [ +  - ]:           2 :         TEST(test0030_1,"Size change with locked checksum triggers a warning…");
     736         [ +  - ]:           2 :         TEST(test0030_2,"Timestamp drift with --watch-timestamps triggers a warning…");
     737         [ +  - ]:           2 :         TEST(test0030_3,"Timestamp drift without --watch-timestamps completes successfully…");
     738         [ +  - ]:           2 :         TEST(test0030_4,"Timestamp drift with --watch-timestamps and --rehash-locked completes successfully…");
     739         [ +  - ]:           2 :         TEST(test0030_5,"Locked checksum mismatch in DB triggers a warning…");
     740         [ +  - ]:           2 :         TEST(test0030_6,"Locked file content change triggers a warning…");
     741         [ +  - ]:           2 :         TEST(test0030_7,"Locked file content change with --watch-timestamps triggers a warning…");
     742         [ +  - ]:           2 :         TEST(test0030_8,"No changes with --watch-timestamps completes successfully…");
     743                 :             : 
     744   [ +  -  -  +  :           2 :         RETURN_STATUS;
          -  -  -  +  +  
                      - ]
     745                 :             : }
        

Generated by: LCOV version 2.0-1