LCOV - code coverage report
Current view: top level - tests/src - test0013.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 270 270
Test Date: 2026-07-12 01:01:34 Functions: 100.0 % 9 9
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : #include "sute.h"
       2                 :             : 
       3                 :             : /**
       4                 :             :  * The db file should not be created in the Dry Run mode
       5                 :             :  */
       6                 :           2 : static Return test0013_1(void)
       7                 :             : {
       8                 :           2 :         INITTEST;
       9                 :             : 
      10                 :           2 :         m_create(char,result,MEMORY_STRING);
      11                 :             : 
      12                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
      13                 :             : 
      14                 :           2 :         const char *arguments = "--dry-run --database=database1.db tests/fixtures/diffs/diff1";
      15                 :             : 
      16                 :           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
      17                 :             : 
      18                 :             :         #if 0
      19                 :             :         echo(STDOUT,"%s\n",m_text(result));
      20                 :             :         #endif
      21                 :             : 
      22                 :           2 :         m_del(result);
      23                 :             : 
      24                 :             :         // Does file exists or not
      25                 :           2 :         const char *db_filename = "database1.db";
      26                 :           2 :         m_create(char,path,MEMORY_STRING);
      27                 :           2 :         bool file_exists = false;
      28                 :             : 
      29                 :           2 :         ASSERT(SUCCESS == construct_path(db_filename,path));
      30                 :             : 
      31                 :           2 :         ASSERT(SUCCESS == check_file_exists(&file_exists,m_text(path)));
      32                 :             : 
      33                 :           2 :         m_del(path);
      34                 :             : 
      35                 :             :         // Should not be exists
      36                 :           2 :         ASSERT(file_exists == false);
      37                 :             : 
      38                 :           2 :         RETURN_STATUS;
      39                 :             : }
      40                 :             : 
      41                 :             : /**
      42                 :             :  * In dry-run mode with checksums, files are hashed but DB is still not modified
      43                 :             :  */
      44                 :           2 : static Return test0013_2(void)
      45                 :             : {
      46                 :           2 :         INITTEST;
      47                 :             : 
      48                 :           2 :         m_create(char,result,MEMORY_STRING);
      49                 :           2 :         m_create(char,pattern,MEMORY_STRING);
      50                 :           2 :         m_create(char,path,MEMORY_STRING);
      51                 :           2 :         bool file_exists = false;
      52                 :             : 
      53                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
      54                 :             : 
      55                 :           2 :         const char *arguments = "--dry-run --database=dry_run_regular.db tests/fixtures/diffs/diff1";
      56                 :           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
      57                 :             : 
      58                 :           2 :         const char *filename = "templates/0013_001_1.txt";
      59                 :           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
      60                 :           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
      61                 :             : 
      62                 :           2 :         m_del(pattern);
      63                 :           2 :         m_del(result);
      64                 :             : 
      65                 :           2 :         arguments = "--dry-run=with-checksums --database=dry_run_with_checksums.db tests/fixtures/diffs/diff1";
      66                 :           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
      67                 :             : 
      68                 :           2 :         filename = "templates/0013_001_2.txt";
      69                 :           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
      70                 :           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
      71                 :             : 
      72                 :           2 :         m_del(pattern);
      73                 :           2 :         m_del(result);
      74                 :           2 :         m_del(path);
      75                 :             : 
      76                 :             :         /* Dry-run should not create either DB file. */
      77                 :           2 :         ASSERT(SUCCESS == construct_path("dry_run_regular.db",path));
      78                 :           2 :         ASSERT(SUCCESS == check_file_exists(&file_exists,m_text(path)));
      79                 :           2 :         ASSERT(file_exists == false);
      80                 :             : 
      81                 :           2 :         m_del(path);
      82                 :           2 :         ASSERT(SUCCESS == construct_path("dry_run_with_checksums.db",path));
      83                 :           2 :         ASSERT(SUCCESS == check_file_exists(&file_exists,m_text(path)));
      84                 :           2 :         ASSERT(file_exists == false);
      85                 :             : 
      86                 :           2 :         m_del(path);
      87                 :             : 
      88                 :           2 :         RETURN_STATUS;
      89                 :             : }
      90                 :             : 
      91                 :             : /**
      92                 :             :  * Invalid dry-run mode should fail with error in stderr
      93                 :             :  */
      94                 :           2 : static Return test0013_3(void)
      95                 :             : {
      96                 :           2 :         INITTEST;
      97                 :             : 
      98                 :           2 :         m_create(char,result,MEMORY_STRING);
      99                 :           2 :         m_create(char,pattern,MEMORY_STRING);
     100                 :             : 
     101                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     102                 :             : 
     103                 :           2 :         const char *arguments = "--dry-run=bad-mode --database=dry_run_invalid.db tests/fixtures/diffs/diff1";
     104                 :             : 
     105                 :           2 :         ASSERT(SUCCESS == runit(arguments,NULL,result,FAILURE,STDERR_ALLOW));
     106                 :             : 
     107                 :           2 :         const char *filename = "templates/0013_001_3.txt";
     108                 :             : 
     109                 :           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     110                 :           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     111                 :             : 
     112                 :           2 :         m_del(pattern);
     113                 :           2 :         m_del(result);
     114                 :             : 
     115                 :           2 :         RETURN_STATUS;
     116                 :             : }
     117                 :             : 
     118                 :             : /**
     119                 :             :  * The db file should not be updated in the Dry Run mode
     120                 :             :  */
     121                 :           2 : static Return test0013_4(void)
     122                 :             : {
     123                 :           2 :         INITTEST;
     124                 :             : 
     125                 :             :         // Create memory for the result
     126                 :           2 :         m_create(char,result,MEMORY_STRING);
     127                 :             :         struct stat stat1;
     128                 :             :         struct stat stat2;
     129                 :           2 :         m_create(char,pattern,MEMORY_STRING);
     130                 :           2 :         m_create(char,chunk,MEMORY_STRING);
     131                 :             : 
     132                 :             :         // Preparation for tests
     133                 :           2 :         ASSERT(SUCCESS == prepare_mutable_fixture("tests/fixtures/diffs/diff1"));
     134                 :             : 
     135                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     136                 :             : 
     137                 :           2 :         const char *arguments = "--database=database1.db tests/fixtures/diffs/diff1";
     138                 :             : 
     139                 :           2 :         ASSERT(SUCCESS == runit(arguments,NULL,NULL,COMPLETED,ALLOW_BOTH));
     140                 :             : 
     141                 :             :         #if 0
     142                 :             :         printf("Path: %s\n",path);
     143                 :             :         echo(STDOUT,"Path: %s\n",path);
     144                 :             :         #endif
     145                 :             : 
     146                 :           2 :         m_create(char,path,MEMORY_STRING);
     147                 :             : 
     148                 :           2 :         const char *db_filename = "database1.db";
     149                 :             : 
     150                 :           2 :         ASSERT(SUCCESS == construct_path(db_filename,path));
     151                 :             : 
     152                 :           2 :         ASSERT(SUCCESS == get_file_stat(m_text(path),&stat1));
     153                 :             : 
     154                 :           2 :         ASSERT(SUCCESS == delete_path("tests/fixtures/diffs/diff1/2/AAA/BBB/CZC/a.txt")); // Remove
     155                 :           2 :         ASSERT(SUCCESS == add_string_to("AFAKDSJ","tests/fixtures/diffs/diff1/1/AAA/ZAW/D/e/f/b_file.txt")); // Modify
     156                 :           2 :         ASSERT(SUCCESS == replase_to_string("WNEURHGO","tests/fixtures/diffs/diff1/2/AAA/BBB/CZC/b.txt")); // New file
     157                 :             : 
     158                 :           2 :         arguments = "--dry-run --update --database=database1.db"
     159                 :             :                 " tests/fixtures/diffs/diff1";
     160                 :             : 
     161                 :           2 :         ASSERT(SUCCESS == runit(arguments,chunk,NULL,COMPLETED,ALLOW_BOTH));
     162                 :           2 :         ASSERT(SUCCESS == m_copy(result,chunk));
     163                 :             : 
     164                 :             :         #if 0
     165                 :             :         printf("%s\n",m_text(result));
     166                 :             :         echo(STDOUT,"%s\n",m_text(result));
     167                 :             :         #endif
     168                 :             : 
     169                 :           2 :         m_del(result);
     170                 :           2 :         m_del(chunk);
     171                 :             : 
     172                 :           2 :         ASSERT(SUCCESS == get_file_stat(m_text(path),&stat2));
     173                 :             : 
     174                 :           2 :         ASSERT(SUCCESS == check_file_identity(&stat1,&stat2));
     175                 :             : 
     176                 :             :         // Compare against the sample. A message should be displayed indicating
     177                 :             :         // that the --db-drop-ignored option must be specified for permanent
     178                 :             :         // removal of ignored files from the database
     179                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     180                 :             : 
     181                 :           2 :         arguments = "--dry-run --ignore=\"^1/AAA/ZAW/.*\" --update "
     182                 :             :                 "--database=database1.db tests/fixtures/diffs/diff1";
     183                 :             : 
     184                 :           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     185                 :             : 
     186                 :             :         #if 0
     187                 :             :         echo(STDOUT,"%s\n",m_text(result));
     188                 :             :         #endif
     189                 :             : 
     190                 :           2 :         const char *filename = "templates/0013_002_1.txt";
     191                 :             : 
     192                 :           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     193                 :           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     194                 :             : 
     195                 :           2 :         m_del(pattern);
     196                 :             : 
     197                 :           2 :         m_del(result);
     198                 :             : 
     199                 :           2 :         ASSERT(SUCCESS == get_file_stat(m_text(path),&stat2));
     200                 :             : 
     201                 :           2 :         ASSERT(SUCCESS == check_file_identity(&stat1,&stat2));
     202                 :             : 
     203                 :             :         // Dry Run mode permanent deletion of all ignored file
     204                 :             :         // references from the database
     205                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     206                 :             : 
     207                 :           2 :         arguments = "--dry-run --db-drop-ignored --update"
     208                 :             :                 " --ignore=\"^1/AAA/ZAW/D/e/f/b_file\\..*\""
     209                 :             :                 " --database=database1.db tests/fixtures/diffs/diff1";
     210                 :             : 
     211                 :           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     212                 :             : 
     213                 :             :         #if 0
     214                 :             :         echo(STDOUT,"%s\n",m_text(result));
     215                 :             :         #endif
     216                 :             : 
     217                 :           2 :         filename = "templates/0013_002_2.txt";
     218                 :             : 
     219                 :           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     220                 :             : 
     221                 :           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     222                 :             : 
     223                 :           2 :         m_del(pattern);
     224                 :             : 
     225                 :           2 :         m_del(result);
     226                 :             : 
     227                 :           2 :         ASSERT(SUCCESS == get_file_stat(m_text(path),&stat2));
     228                 :             : 
     229                 :           2 :         ASSERT(SUCCESS == check_file_identity(&stat1,&stat2));
     230                 :             : 
     231                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     232                 :             : 
     233                 :           2 :         arguments = "--dry-run --db-drop-ignored --update --watch-timestamps"
     234                 :             :                 " --ignore=\"^path2/AAA/ZAW/.*\""
     235                 :             :                 " --database=database1.db tests/fixtures/diffs/diff1";
     236                 :             : 
     237                 :           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     238                 :             : 
     239                 :             :         #if 0
     240                 :             :         echo(STDOUT,"%s\n",m_text(result));
     241                 :             :         #endif
     242                 :             : 
     243                 :           2 :         filename = "templates/0013_002_3.txt";
     244                 :             : 
     245                 :           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     246                 :             : 
     247                 :           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     248                 :             : 
     249                 :           2 :         m_del(pattern);
     250                 :             : 
     251                 :           2 :         m_del(result);
     252                 :             : 
     253                 :           2 :         ASSERT(SUCCESS == get_file_stat(m_text(path),&stat2));
     254                 :             : 
     255                 :           2 :         ASSERT(SUCCESS == check_file_identity(&stat1,&stat2));
     256                 :             : 
     257                 :           2 :         m_del(path);
     258                 :             : 
     259                 :             :         // Clean up test results
     260                 :           2 :         ASSERT(SUCCESS == delete_path("database1.db"));
     261                 :           2 :         ASSERT(SUCCESS == restore_mutable_fixture("tests/fixtures/diffs/diff1"));
     262                 :             : 
     263                 :           2 :         RETURN_STATUS;
     264                 :             : }
     265                 :             : 
     266                 :             : /**
     267                 :             :  * Everything that was previously executed in Dry Run mode
     268                 :             :  * will now be tested in live mode without simulation, and
     269                 :             :  * the results will be compared against each other
     270                 :             :  */
     271                 :           2 : static Return test0013_5(void)
     272                 :             : {
     273                 :           2 :         INITTEST;
     274                 :             :         // Create memory for the result
     275                 :           2 :         m_create(char,result,MEMORY_STRING);
     276                 :           2 :         m_create(char,path,MEMORY_STRING);
     277                 :           2 :         m_create(char,pattern,MEMORY_STRING);
     278                 :           2 :         const char *db_file_name = "database1.db";
     279                 :           2 :         const char *arguments = NULL;
     280                 :             : 
     281                 :             :         // Preparation for tests
     282                 :           2 :         ASSERT(SUCCESS == prepare_mutable_fixture("tests/fixtures/diffs/diff1"));
     283                 :             : 
     284                 :           2 :         ASSERT(SUCCESS == construct_path(db_file_name,path));
     285                 :             : 
     286                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     287                 :             : 
     288                 :           2 :         arguments = "--database=database1.db tests/fixtures/diffs/diff1";
     289                 :             : 
     290                 :           2 :         ASSERT(SUCCESS == runit(arguments,NULL,NULL,COMPLETED,ALLOW_BOTH));
     291                 :             : 
     292                 :             :         #if 0
     293                 :             :         echo(STDOUT,"Path: %s\n",path);
     294                 :             :         #endif
     295                 :             : 
     296                 :           2 :         ASSERT(SUCCESS == delete_path("tests/fixtures/diffs/diff1/2/AAA/BBB/CZC/a.txt"));
     297                 :           2 :         ASSERT(SUCCESS == add_string_to("AFAKDSJ","tests/fixtures/diffs/diff1/1/AAA/ZAW/D/e/f/b_file.txt"));
     298                 :           2 :         ASSERT(SUCCESS == replase_to_string("WNEURHGO","tests/fixtures/diffs/diff1/2/AAA/BBB/CZC/b.txt")); // New file
     299                 :             : 
     300                 :             :         #if 0
     301                 :             :         echo(STDOUT,"%s\n",m_text(result));
     302                 :             :         #endif
     303                 :             : 
     304                 :           2 :         m_del(result);
     305                 :             : 
     306                 :             :         // Compare against the sample. A message should be displayed indicating
     307                 :             :         // that the --db-drop-ignored option must be specified for permanent
     308                 :             :         // removal of ignored files from the database
     309                 :           2 :         ASSERT(SUCCESS == copy_path("database1.db","database1.db.backup"));
     310                 :             : 
     311                 :           2 :         arguments = "--ignore=\"^1/AAA/ZAW/.*\" --update --database=database1.db "
     312                 :             :                 "tests/fixtures/diffs/diff1";
     313                 :             : 
     314                 :           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     315                 :             : 
     316                 :             :         #if 0
     317                 :             :         echo(STDOUT,"%s\n",m_text(result));
     318                 :             :         #endif
     319                 :             : 
     320                 :           2 :         const char *filename = "templates/0013_003_1.txt";
     321                 :             : 
     322                 :           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     323                 :           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     324                 :             : 
     325                 :           2 :         m_del(pattern);
     326                 :             : 
     327                 :           2 :         m_del(result);
     328                 :             : 
     329                 :             :         // Real live mode permanent deletion of all ignored file
     330                 :             :         // references from the database
     331                 :           2 :         ASSERT(SUCCESS == copy_path("database1.db.backup","database1.db"));
     332                 :             : 
     333                 :           2 :         arguments = "--db-drop-ignored --update"
     334                 :             :                 " --ignore=\"^1/AAA/ZAW/D/e/f/b_file\\..*\""
     335                 :             :                 " --database=database1.db tests/fixtures/diffs/diff1";
     336                 :             : 
     337                 :           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     338                 :             : 
     339                 :             :         #if 0
     340                 :             :         echo(STDOUT,"%s\n",m_text(result));
     341                 :             :         #endif
     342                 :             : 
     343                 :           2 :         filename = "templates/0013_003_2.txt";
     344                 :             : 
     345                 :           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     346                 :           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     347                 :             : 
     348                 :           2 :         m_del(pattern);
     349                 :             : 
     350                 :           2 :         m_del(result);
     351                 :             : 
     352                 :           2 :         ASSERT(SUCCESS == move_path("database1.db.backup","database1.db"));
     353                 :             : 
     354                 :           2 :         arguments = "--watch-timestamps --db-drop-ignored "
     355                 :             :                 "--ignore=\"^path2/AAA/ZAW/.*\" --update "
     356                 :             :                 "--database=database1.db tests/fixtures/diffs/diff1";
     357                 :             : 
     358                 :           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     359                 :             : 
     360                 :             :         #if 0
     361                 :             :         echo(STDOUT,"%s\n",m_text(result));
     362                 :             :         #endif
     363                 :             : 
     364                 :           2 :         filename = "templates/0013_003_3.txt";
     365                 :             : 
     366                 :           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     367                 :           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     368                 :             : 
     369                 :           2 :         m_del(pattern);
     370                 :             : 
     371                 :           2 :         m_del(result);
     372                 :             : 
     373                 :           2 :         m_del(path);
     374                 :             : 
     375                 :             :         // Clean up test results
     376                 :           2 :         ASSERT(SUCCESS == delete_path("database1.db"));
     377                 :           2 :         ASSERT(SUCCESS == restore_mutable_fixture("tests/fixtures/diffs/diff1"));
     378                 :             : 
     379                 :           2 :         RETURN_STATUS;
     380                 :             : }
     381                 :             : 
     382                 :             : /**
     383                 :             :  * @brief Verify diff generation for dry-run and writable database scenarios
     384                 :             :  *
     385                 :             :  * This test loads three pairs of saved outputs into string-mode memory
     386                 :             :  * descriptors and compares each pair through compare_memory_strings().
     387                 :             :  * The produced unified diff is matched against the corresponding template
     388                 :             :  *
     389                 :             :  * The test verifies that compare_memory_strings():
     390                 :             :  * - accepts file content loaded into string-mode memory descriptors
     391                 :             :  * - writes the produced unified diff directly into the destination descriptor
     392                 :             :  * - produces the exact diff text expected for the compared application runs
     393                 :             :  *
     394                 :             :  * @return Return status code
     395                 :             :  */
     396                 :           2 : Return test0013_6(void)
     397                 :             : {
     398                 :           2 :         INITTEST;
     399                 :             : 
     400                 :           2 :         m_create(char,text1,MEMORY_STRING);
     401                 :           2 :         m_create(char,text2,MEMORY_STRING);
     402                 :           2 :         m_create(char,pattern,MEMORY_STRING);
     403                 :           2 :         m_create(char,diff_buffer,MEMORY_STRING);
     404                 :           2 :         const char *filename = NULL;
     405                 :             : 
     406                 :             :         /* 0013 002 1 */
     407                 :           2 :         ASSERT(SUCCESS == get_file_content("templates/0013_002_1.txt",text1));
     408                 :             : 
     409                 :           2 :         ASSERT(SUCCESS == get_file_content("templates/0013_003_1.txt",text2));
     410                 :             : 
     411                 :           2 :         ASSERT(SUCCESS == compare_memory_strings(diff_buffer,text1,text2));
     412                 :             : 
     413                 :           2 :         filename = "templates/0013_004_1.txt";
     414                 :             : 
     415                 :           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     416                 :           2 :         ASSERT(SUCCESS == match_pattern(diff_buffer,pattern,filename));
     417                 :             : 
     418                 :           2 :         m_del(text1);
     419                 :           2 :         m_del(text2);
     420                 :           2 :         m_del(pattern);
     421                 :             : 
     422                 :             :         /* 0013 002 2 */
     423                 :           2 :         ASSERT(SUCCESS == get_file_content("templates/0013_002_2.txt",text1));
     424                 :             : 
     425                 :           2 :         ASSERT(SUCCESS == get_file_content("templates/0013_003_2.txt",text2));
     426                 :             : 
     427                 :           2 :         m_del(diff_buffer);
     428                 :           2 :         ASSERT(SUCCESS == compare_memory_strings(diff_buffer,text1,text2));
     429                 :             : 
     430                 :           2 :         filename = "templates/0013_004_2.txt";
     431                 :             : 
     432                 :           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     433                 :           2 :         ASSERT(SUCCESS == match_pattern(diff_buffer,pattern,filename));
     434                 :             : 
     435                 :           2 :         m_del(text1);
     436                 :           2 :         m_del(text2);
     437                 :           2 :         m_del(pattern);
     438                 :             : 
     439                 :             :         /* 0013 002 3 */
     440                 :           2 :         ASSERT(SUCCESS == get_file_content("templates/0013_002_3.txt",text1));
     441                 :             : 
     442                 :           2 :         ASSERT(SUCCESS == get_file_content("templates/0013_003_3.txt",text2));
     443                 :             : 
     444                 :           2 :         m_del(diff_buffer);
     445                 :           2 :         ASSERT(SUCCESS == compare_memory_strings(diff_buffer,text1,text2));
     446                 :             : 
     447                 :             :         // _2 is not a mistake. 2 and 3 are equals
     448                 :           2 :         filename = "templates/0013_004_2.txt";
     449                 :             : 
     450                 :           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     451                 :           2 :         ASSERT(SUCCESS == match_pattern(diff_buffer,pattern,filename));
     452                 :             : 
     453                 :           2 :         m_del(text1);
     454                 :           2 :         m_del(text2);
     455                 :           2 :         m_del(pattern);
     456                 :           2 :         m_del(diff_buffer);
     457                 :             : 
     458                 :           2 :         RETURN_STATUS;
     459                 :             : }
     460                 :             : 
     461                 :             : /**
     462                 :             :  * Verifies detection of unexpected database metadata drift in dry-run mode.
     463                 :             :  *
     464                 :             :  * The function performs two runs. First, it creates `database1.db` in normal
     465                 :             :  * mode. Then it enables the testing hook
     466                 :             :  * `TESTITALL_TEST_ENV_DB_FILE_TIMESTAMPS_WILL_BUMPED=true` and runs the application
     467                 :             :  * with `--dry-run --update`. In test-hook mode this forces a DB timestamp bump
     468                 :             :  * inside the process before `db_check_changes()` compares the saved and current
     469                 :             :  * file metadata.
     470                 :             :  *
     471                 :             :  * The expected outcome of the second run is `WARNING`
     472                 :             :  */
     473                 :           2 : static Return test0013_7(void)
     474                 :             : {
     475                 :           2 :         INITTEST;
     476                 :             : 
     477                 :           2 :         m_create(char,result,MEMORY_STRING);
     478                 :           2 :         m_create(char,pattern,MEMORY_STRING);
     479                 :           2 :         const char *arguments = NULL;
     480                 :             : 
     481                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     482                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTITALL_TEST_ENV_DB_FILE_TIMESTAMPS_WILL_BUMPED","false"));
     483                 :             : 
     484                 :             :         /* First run: create DB in normal mode. */
     485                 :           2 :         arguments = "--database=database1.db tests/fixtures/diffs/diff1";
     486                 :           2 :         ASSERT(SUCCESS == runit(arguments,NULL,NULL,COMPLETED,ALLOW_BOTH));
     487                 :             : 
     488                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTITALL_TEST_ENV_DB_FILE_TIMESTAMPS_WILL_BUMPED","true"));
     489                 :           2 :         arguments = "--dry-run --update --database=database1.db tests/fixtures/diffs/diff1";
     490                 :           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,WARNING,ALLOW_BOTH));
     491                 :             : 
     492                 :           2 :         const char *filename = "templates/0013_005.txt";
     493                 :           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     494                 :           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     495                 :             : 
     496                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTITALL_TEST_ENV_DB_FILE_TIMESTAMPS_WILL_BUMPED","false"));
     497                 :           2 :         ASSERT(SUCCESS == delete_path("database1.db"));
     498                 :             : 
     499                 :           2 :         m_del(pattern);
     500                 :           2 :         m_del(result);
     501                 :             : 
     502                 :           2 :         RETURN_STATUS;
     503                 :             : }
     504                 :             : 
     505                 :             : /**
     506                 :             :  * Verifies internal consistency check when DB should be modified but appears unchanged.
     507                 :             :  *
     508                 :             :  * The function first validates a real update path: it creates `database1.db`,
     509                 :             :  * modifies one sample file, runs `--update`, compares output with template,
     510                 :             :  * and verifies that DB file metadata has actually changed.
     511                 :             :  *
     512                 :             :  * After that, it restores the pre-update DB state, enables
     513                 :             :  * `TESTITALL_TEST_ENV_DB_FILE_STAT_WILL_BE_RESYNCED=true`, and runs `--update`
     514                 :             :  * again. The hook rewrites the saved baseline database stat to the current
     515                 :             :  * stat right before comparison in db_check_changes(). This simulates a faulty
     516                 :             :  * "no metadata drift" result after a real database update and must trigger
     517                 :             :  * WARNING.
     518                 :             :  */
     519                 :           2 : static Return test0013_8(void)
     520                 :             : {
     521                 :           2 :         INITTEST;
     522                 :             : 
     523                 :           2 :         m_create(char,result,MEMORY_STRING);
     524                 :           2 :         m_create(char,pattern,MEMORY_STRING);
     525                 :           2 :         m_create(char,path,MEMORY_STRING);
     526                 :             : 
     527                 :           2 :         struct stat stat_before_real_update = {0};
     528                 :           2 :         struct stat stat_after_real_update = {0};
     529                 :             : 
     530                 :           2 :         const char *arguments = NULL;
     531                 :             : 
     532                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     533                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTITALL_TEST_ENV_DB_FILE_TIMESTAMPS_WILL_BUMPED","false"));
     534                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTITALL_TEST_ENV_DB_FILE_STAT_WILL_BE_RESYNCED","false"));
     535                 :             : 
     536                 :           2 :         ASSERT(SUCCESS == prepare_mutable_fixture("tests/fixtures/diffs/diff1"));
     537                 :             : 
     538                 :             :         /* First run: create DB in normal mode. */
     539                 :           2 :         arguments = "--database=database1.db tests/fixtures/diffs/diff1";
     540                 :           2 :         ASSERT(SUCCESS == runit(arguments,NULL,NULL,COMPLETED,ALLOW_BOTH));
     541                 :             : 
     542                 :             :         /* Make at least one real filesystem change so --update modifies DB. */
     543                 :           2 :         ASSERT(SUCCESS == add_string_to("AFAKDSJ","tests/fixtures/diffs/diff1/1/AAA/ZAW/D/e/f/b_file.txt"));
     544                 :             : 
     545                 :             :         /* Control check: real --update must modify database metadata. */
     546                 :           2 :         ASSERT(SUCCESS == construct_path("database1.db",path));
     547                 :           2 :         ASSERT(SUCCESS == get_file_stat(m_text(path),&stat_before_real_update));
     548                 :             : 
     549                 :           2 :         ASSERT(SUCCESS == copy_path("database1.db","database1.db.backup"));
     550                 :             : 
     551                 :           2 :         arguments = "--update --database=database1.db tests/fixtures/diffs/diff1";
     552                 :           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
     553                 :             : 
     554                 :           2 :         const char *filename = "templates/0013_006_1.txt";
     555                 :           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     556                 :           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     557                 :             : 
     558                 :           2 :         ASSERT(SUCCESS == get_file_stat(m_text(path),&stat_after_real_update));
     559                 :           2 :         ASSERT(FAILURE == check_file_identity(&stat_before_real_update,&stat_after_real_update));
     560                 :             : 
     561                 :             :         /*
     562                 :             :          * Restore pre-update DB snapshot so the next run starts from the same state
     563                 :             :          * and tests only the simulation hook behavior.
     564                 :             :          */
     565                 :           2 :         ASSERT(SUCCESS == move_path("database1.db.backup","database1.db"));
     566                 :             : 
     567                 :           2 :         m_del(result);
     568                 :           2 :         m_del(pattern);
     569                 :             : 
     570                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTITALL_TEST_ENV_DB_FILE_STAT_WILL_BE_RESYNCED","true"));
     571                 :           2 :         arguments = "--update --database=database1.db tests/fixtures/diffs/diff1";
     572                 :           2 :         ASSERT(SUCCESS == runit(arguments,result,NULL,WARNING,ALLOW_BOTH));
     573                 :             : 
     574                 :           2 :         filename = "templates/0013_006_2.txt";
     575                 :           2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     576                 :           2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     577                 :             : 
     578                 :           2 :         ASSERT(SUCCESS == set_environment_variable("TESTITALL_TEST_ENV_DB_FILE_STAT_WILL_BE_RESYNCED","false"));
     579                 :           2 :         ASSERT(SUCCESS == delete_path("database1.db"));
     580                 :           2 :         ASSERT(SUCCESS == restore_mutable_fixture("tests/fixtures/diffs/diff1"));
     581                 :             : 
     582                 :           2 :         m_del(path);
     583                 :           2 :         m_del(pattern);
     584                 :           2 :         m_del(result);
     585                 :             : 
     586                 :           2 :         RETURN_STATUS;
     587                 :             : }
     588                 :             : 
     589                 :             : /**
     590                 :             :  *
     591                 :             :  * Dry Run mode testing
     592                 :             :  *
     593                 :             :  */
     594                 :           2 : Return test0013(void)
     595                 :             : {
     596                 :           2 :         INITTEST;
     597                 :             : 
     598                 :           2 :         TEST(test0013_1,"The DB file should not be created");
     599                 :           2 :         TEST(test0013_2,"Dry run with checksums hashes files but keeps DB untouched");
     600                 :           2 :         TEST(test0013_3,"Invalid dry-run mode should return failure and print stderr error");
     601                 :           2 :         TEST(test0013_4,"The DB file should not be updated");
     602                 :           2 :         TEST(test0013_5,"Now run the same without simulation");
     603                 :           2 :         TEST(test0013_6,"Compare dry and real mode templates");
     604                 :           2 :         TEST(test0013_7,"Dry-run DB metadata drift should trigger internal warning path");
     605                 :           2 :         TEST(test0013_8,"Live update: force missing DB metadata drift and trigger warning");
     606                 :             : 
     607                 :           2 :         RETURN_STATUS;
     608                 :             : }
        

Generated by: LCOV version 2.0-1