LCOV - code coverage report
Current view: top level - src/plugins/quickdump - testmod_quickdump.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 193 193 100.0 %
Date: 2019-09-12 12:28:41 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for quickdump plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include <stdlib.h>
      11             : #include <string.h>
      12             : 
      13             : #include <kdbconfig.h>
      14             : #include <kdbtypes.h>
      15             : 
      16             : #include <tests_plugin.h>
      17             : 
      18             : #include <stdio.h>
      19             : #include <unistd.h>
      20             : 
      21             : #include "quickdump/test.quickdump.h"
      22             : 
      23           6 : static int compare_binary_files (const char * filename1, const char * filename2)
      24             : {
      25           6 :         FILE * f1 = fopen (filename1, "rb");
      26           6 :         FILE * f2 = fopen (filename2, "rb");
      27             : 
      28           6 :         int result = 0;
      29             : 
      30             :         int c1, c2;
      31        2052 :         while (result == 0 && (c1 = fgetc (f1)) != EOF && (c2 = fgetc (f2)) != EOF)
      32             :         {
      33        2040 :                 result = c1 - c2;
      34             :         }
      35             : 
      36           6 :         if (result == 0)
      37             :         {
      38           6 :                 int end1 = fgetc (f1) == EOF && feof (f1) != 0;
      39           6 :                 int end2 = fgetc (f2) == EOF && feof (f2) != 0;
      40             : 
      41           6 :                 result = end1 - end2;
      42             :         }
      43             : 
      44           6 :         fclose (f1);
      45           6 :         fclose (f2);
      46             : 
      47           6 :         return result;
      48             : }
      49             : 
      50           4 : static int check_binary_file (const char * filename, const unsigned char * data, size_t dataSize)
      51             : {
      52           4 :         FILE * file = fopen (filename, "rb");
      53             : 
      54           4 :         int result = 0;
      55             : 
      56             :         int c;
      57           4 :         size_t pos = 0;
      58          98 :         while (result == 0 && (c = fgetc (file)) != EOF && pos < dataSize)
      59             :         {
      60          90 :                 result = c - (int) data[pos++];
      61             :         }
      62             : 
      63           4 :         if (result == 0)
      64             :         {
      65           4 :                 int end1 = fgetc (file) == EOF && feof (file) != 0;
      66           4 :                 int end2 = pos >= dataSize;
      67             : 
      68           4 :                 result = end1 - end2;
      69             :         }
      70             : 
      71           4 :         fclose (file);
      72             : 
      73           4 :         return result;
      74             : }
      75             : 
      76           2 : static void test_basics (void)
      77             : {
      78           2 :         printf ("test basics\n");
      79             : 
      80           2 :         KeySet * ks = ksNew (0, KS_END);
      81           2 :         char * infile = elektraStrDup (srcdir_file ("quickdump/test.quickdump"));
      82           2 :         char * outfile = elektraStrDup (srcdir_file ("quickdump/test.quickdump.out"));
      83             : 
      84             :         {
      85           2 :                 Key * getKey = keyNew ("dir/tests/bench", KEY_VALUE, infile, KEY_END);
      86             : 
      87           2 :                 KeySet * conf = ksNew (0, KS_END);
      88           2 :                 PLUGIN_OPEN ("quickdump");
      89             : 
      90           2 :                 KeySet * expected = test_quickdump_expected ();
      91             : 
      92           2 :                 succeed_if (plugin->kdbGet (plugin, ks, getKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbGet was not successful");
      93           2 :                 compare_keyset (expected, ks);
      94             : 
      95           2 :                 Key * k1 = ksLookupByName (ks, "dir/tests/bench/__112", 0);
      96           2 :                 Key * k8 = ksLookupByName (ks, "dir/tests/bench/__911", 0);
      97           2 :                 succeed_if (keyGetMeta (k1, "meta/_35") == keyGetMeta (k8, "meta/_35"), "copy meta failed");
      98             : 
      99           2 :                 ksDel (expected);
     100             : 
     101           2 :                 keyDel (getKey);
     102           2 :                 PLUGIN_CLOSE ();
     103             :         }
     104             : 
     105             :         {
     106           2 :                 Key * setKey = keyNew ("dir/tests/bench", KEY_VALUE, outfile, KEY_END);
     107             : 
     108           2 :                 KeySet * conf = ksNew (0, KS_END);
     109           2 :                 PLUGIN_OPEN ("quickdump");
     110             : 
     111           2 :                 succeed_if (plugin->kdbSet (plugin, ks, setKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbSet was not successful");
     112             : 
     113           2 :                 succeed_if (compare_binary_files (infile, outfile) == 0, "files differ");
     114           2 :                 remove (outfile);
     115             : 
     116           2 :                 keyDel (setKey);
     117           2 :                 PLUGIN_CLOSE ();
     118             :         }
     119             : 
     120           2 :         elektraFree (infile);
     121           2 :         elektraFree (outfile);
     122           2 :         ksDel (ks);
     123           2 : }
     124             : 
     125           2 : static void test_noParent (void)
     126             : {
     127           2 :         printf ("test noparent\n");
     128             : 
     129           2 :         KeySet * input = ksNew (2, keyNew ("", KEY_VALUE, "value", KEY_END), keyNew ("/a", KEY_VALUE, "value1", KEY_END), KS_END);
     130           2 :         KeySet * expected = ksNew (2, keyNew ("dir/tests/bench", KEY_VALUE, "value", KEY_END),
     131             :                                    keyNew ("dir/tests/bench/a", KEY_VALUE, "value1", KEY_END), KS_END);
     132           2 :         char * outfile = elektraStrDup (srcdir_file ("quickdump/test.quickdump.out"));
     133             : 
     134             :         {
     135           2 :                 Key * setKey = keyNew ("dir/tests/bench", KEY_VALUE, outfile, KEY_END);
     136             : 
     137           2 :                 KeySet * conf = ksNew (1, keyNew ("user/noparent", KEY_END), KS_END);
     138           2 :                 PLUGIN_OPEN ("quickdump");
     139             : 
     140           2 :                 succeed_if (plugin->kdbSet (plugin, input, setKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbSet was not successful");
     141             : 
     142           2 :                 succeed_if (check_binary_file (outfile, test_quickdump_noParent_data, test_quickdump_noParent_dataSize) == 0,
     143             :                             "files differ");
     144             : 
     145           2 :                 keyDel (setKey);
     146           2 :                 PLUGIN_CLOSE ();
     147             :         }
     148             : 
     149             :         {
     150           2 :                 Key * getKey = keyNew ("dir/tests/bench", KEY_VALUE, outfile, KEY_END);
     151             : 
     152           2 :                 KeySet * conf = ksNew (0, KS_END);
     153           2 :                 PLUGIN_OPEN ("quickdump");
     154             : 
     155             : 
     156           2 :                 KeySet * actual = ksNew (0, KS_END);
     157           2 :                 succeed_if (plugin->kdbGet (plugin, actual, getKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbGet was not successful");
     158           2 :                 compare_keyset (expected, actual);
     159             : 
     160           2 :                 ksDel (actual);
     161             : 
     162           2 :                 keyDel (getKey);
     163           2 :                 PLUGIN_CLOSE ();
     164             :         }
     165             : 
     166           2 :         remove (outfile);
     167             : 
     168           2 :         elektraFree (outfile);
     169           2 :         ksDel (input);
     170           2 :         ksDel (expected);
     171           2 : }
     172             : 
     173           2 : static void test_readV1 (void)
     174             : {
     175           2 :         printf ("test update v1 to current\n");
     176             : 
     177           2 :         KeySet * ks = ksNew (0, KS_END);
     178           2 :         char * infile = elektraStrDup (srcdir_file ("quickdump/test.v1.quickdump"));
     179           2 :         char * infileV2 = elektraStrDup (srcdir_file ("quickdump/test.quickdump"));
     180           2 :         char * outfile = elektraStrDup (srcdir_file ("quickdump/test.quickdump.out"));
     181             : 
     182             :         {
     183           2 :                 Key * getKey = keyNew ("dir/tests/bench", KEY_VALUE, infile, KEY_END);
     184             : 
     185           2 :                 KeySet * conf = ksNew (0, KS_END);
     186           2 :                 PLUGIN_OPEN ("quickdump");
     187             : 
     188           2 :                 KeySet * expected = test_quickdump_expected ();
     189             : 
     190           2 :                 succeed_if (plugin->kdbGet (plugin, ks, getKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbGet was not successful");
     191           2 :                 compare_keyset (expected, ks);
     192             : 
     193           2 :                 Key * k1 = ksLookupByName (ks, "dir/tests/bench/__112", 0);
     194           2 :                 Key * k8 = ksLookupByName (ks, "dir/tests/bench/__911", 0);
     195           2 :                 succeed_if (keyGetMeta (k1, "meta/_35") == keyGetMeta (k8, "meta/_35"), "copy meta failed");
     196             : 
     197           2 :                 ksDel (expected);
     198             : 
     199           2 :                 keyDel (getKey);
     200           2 :                 PLUGIN_CLOSE ();
     201             :         }
     202             : 
     203             :         {
     204           2 :                 Key * setKey = keyNew ("dir/tests/bench", KEY_VALUE, outfile, KEY_END);
     205             : 
     206           2 :                 KeySet * conf = ksNew (0, KS_END);
     207           2 :                 PLUGIN_OPEN ("quickdump");
     208             : 
     209           2 :                 succeed_if (plugin->kdbSet (plugin, ks, setKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbSet was not successful");
     210             : 
     211           2 :                 succeed_if (compare_binary_files (infileV2, outfile) == 0, "files differ");
     212           2 :                 remove (outfile);
     213             : 
     214           2 :                 keyDel (setKey);
     215           2 :                 PLUGIN_CLOSE ();
     216             :         }
     217             : 
     218           2 :         elektraFree (infile);
     219           2 :         elektraFree (infileV2);
     220           2 :         elektraFree (outfile);
     221           2 :         ksDel (ks);
     222           2 : }
     223             : 
     224           2 : static void test_readV2 (void)
     225             : {
     226           2 :         printf ("test update v2 to current\n");
     227             : 
     228           2 :         KeySet * ks = ksNew (0, KS_END);
     229           2 :         char * infile = elektraStrDup (srcdir_file ("quickdump/test.v2.quickdump"));
     230           2 :         char * infileV2 = elektraStrDup (srcdir_file ("quickdump/test.quickdump"));
     231           2 :         char * outfile = elektraStrDup (srcdir_file ("quickdump/test.quickdump.out"));
     232             : 
     233             :         {
     234           2 :                 Key * getKey = keyNew ("dir/tests/bench", KEY_VALUE, infile, KEY_END);
     235             : 
     236           2 :                 KeySet * conf = ksNew (0, KS_END);
     237           2 :                 PLUGIN_OPEN ("quickdump");
     238             : 
     239           2 :                 KeySet * expected = test_quickdump_expected ();
     240             : 
     241           2 :                 succeed_if (plugin->kdbGet (plugin, ks, getKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbGet was not successful");
     242           2 :                 compare_keyset (expected, ks);
     243             : 
     244           2 :                 Key * k1 = ksLookupByName (ks, "dir/tests/bench/__112", 0);
     245           2 :                 Key * k8 = ksLookupByName (ks, "dir/tests/bench/__911", 0);
     246           2 :                 succeed_if (keyGetMeta (k1, "meta/_35") == keyGetMeta (k8, "meta/_35"), "copy meta failed");
     247             : 
     248           2 :                 ksDel (expected);
     249             : 
     250           2 :                 keyDel (getKey);
     251           2 :                 PLUGIN_CLOSE ();
     252             :         }
     253             : 
     254             :         {
     255           2 :                 Key * setKey = keyNew ("dir/tests/bench", KEY_VALUE, outfile, KEY_END);
     256             : 
     257           2 :                 KeySet * conf = ksNew (0, KS_END);
     258           2 :                 PLUGIN_OPEN ("quickdump");
     259             : 
     260           2 :                 succeed_if (plugin->kdbSet (plugin, ks, setKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbSet was not successful");
     261             : 
     262           2 :                 succeed_if (compare_binary_files (infileV2, outfile) == 0, "files differ");
     263           2 :                 remove (outfile);
     264             : 
     265           2 :                 keyDel (setKey);
     266           2 :                 PLUGIN_CLOSE ();
     267             :         }
     268             : 
     269           2 :         elektraFree (infile);
     270           2 :         elektraFree (infileV2);
     271           2 :         elektraFree (outfile);
     272           2 :         ksDel (ks);
     273           2 : }
     274             : 
     275           2 : static void test_parentKeyValue (void)
     276             : {
     277           2 :         printf ("test parent key value\n");
     278             : 
     279           2 :         KeySet * expected = ksNew (1, keyNew ("dir/tests/bench", KEY_VALUE, "value", KEY_END), KS_END);
     280           2 :         char * outfile = elektraStrDup (srcdir_file ("quickdump/test.quickdump.out"));
     281             : 
     282             :         {
     283           2 :                 Key * setKey = keyNew ("dir/tests/bench", KEY_VALUE, outfile, KEY_END);
     284             : 
     285           2 :                 KeySet * conf = ksNew (0, KS_END);
     286           2 :                 PLUGIN_OPEN ("quickdump");
     287             : 
     288           2 :                 succeed_if (plugin->kdbSet (plugin, expected, setKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS,
     289             :                             "call to kdbSet was not successful");
     290             : 
     291           2 :                 succeed_if (check_binary_file (outfile, test_quickdump_parentKeyValue_data, test_quickdump_parentKeyValue_dataSize) == 0,
     292             :                             "files differ");
     293             : 
     294           2 :                 keyDel (setKey);
     295           2 :                 PLUGIN_CLOSE ();
     296             :         }
     297             : 
     298             :         {
     299           2 :                 Key * getKey = keyNew ("dir/tests/bench", KEY_VALUE, outfile, KEY_END);
     300             : 
     301           2 :                 KeySet * conf = ksNew (0, KS_END);
     302           2 :                 PLUGIN_OPEN ("quickdump");
     303             : 
     304             : 
     305           2 :                 KeySet * actual = ksNew (0, KS_END);
     306           2 :                 succeed_if (plugin->kdbGet (plugin, actual, getKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbGet was not successful");
     307           2 :                 compare_keyset (expected, actual);
     308             : 
     309           2 :                 ksDel (actual);
     310             : 
     311           2 :                 keyDel (getKey);
     312           2 :                 PLUGIN_CLOSE ();
     313             :         }
     314             : 
     315           2 :         remove (outfile);
     316             : 
     317           2 :         elektraFree (outfile);
     318           2 :         ksDel (expected);
     319           2 : }
     320             : 
     321             : #include "varint.c"
     322             : 
     323           2 : static void test_varint (void)
     324             : {
     325           2 :         printf ("test varint\n");
     326             : 
     327           2 :         kdb_unsigned_long_long_t testNumbers[] = {
     328             :                 0x0000000000000000, 0x0000000000000001, 0x0000000000000002, 0x0000000000000003, 0x0000000000000004, 0x0000000000000005,
     329             :                 0x0000000000000008, 0x0000000000000009, 0x0000000000000010, 0x0000000000000011, 0x0000000000000020, 0x0000000000000021,
     330             :                 0x000000000000007F, 0x0000000000000080, 0x0000000000000081, 0x0000000000000100, 0x0000000000000101, 0x0000000000000200,
     331             :                 0x0000000000000201, 0x0000000000000400, 0x0000000000000401, 0x0000000000000800, 0x0000000000000801, 0x0000000000001000,
     332             :                 0x0000000000001001, 0x0000000000002000, 0x0000000000002001, 0x0000000000003FFF, 0x0000000000004000, 0x0000000000004001,
     333             :                 0x0000000000008000, 0x0000000000008001, 0x0000000000010000, 0x0000000000010001, 0x0000000000020000, 0x0000000000020001,
     334             :                 0x0000000000040000, 0x0000000000040001, 0x0000000000080000, 0x0000000000080001, 0x0000000000100000, 0x0000000000100001,
     335             :                 0x00000000001FFFFF, 0x0000000000200000, 0x0000000000200001, 0x0000000000400000, 0x0000000000400001, 0x0000000000800000,
     336             :                 0x0000000000800001, 0x0000000001000000, 0x0000000001000001, 0x0000000002000000, 0x0000000002000001, 0x0000000004000000,
     337             :                 0x0000000004000001, 0x0000000008000000, 0x0000000008000001, 0x000000000FFFFFFF, 0x0000000010000000, 0x0000000010000001,
     338             :                 0x0000000020000000, 0x0000000020000001, 0x0000000040000000, 0x0000000040000001, 0x0000000080000000, 0x0000000080000001,
     339             :                 0x0000000100000000, 0x0000000100000001, 0x0000000200000000, 0x0000000200000001, 0x0000000400000000, 0x0000000400000001,
     340             :                 0x00000007FFFFFFFF, 0x0000000800000000, 0x0000000800000001, 0x0000001000000000, 0x0000001000000001, 0x0000002000000000,
     341             :                 0x0000002000000001, 0x0000004000000000, 0x0000004000000001, 0x0000008000000000, 0x0000008000000001, 0x0000010000000000,
     342             :                 0x0000010000000001, 0x0000020000000000, 0x0000020000000001, 0x000003FFFFFFFFFF, 0x0000040000000000, 0x0000040000000001,
     343             :                 0x0000080000000000, 0x0000080000000001, 0x0000100000000000, 0x0000100000000001, 0x0000200000000000, 0x0000200000000001,
     344             :                 0x0000400000000000, 0x0000400000000001, 0x0000800000000000, 0x0000800000000001, 0x0001000000000000, 0x0001000000000001,
     345             :                 0x0001FFFFFFFFFFFF, 0x0002000000000000, 0x0002000000000001, 0x0004000000000000, 0x0004000000000001, 0x0008000000000000,
     346             :                 0x0010000000000000, 0x0010000000000001, 0x0020000000000000, 0x0020000000000001, 0x0040000000000000, 0x0080000000000000,
     347             :                 0x0080000000000001, 0x00FFFFFFFFFFFFFF, 0x0100000000000000, 0x0100000000000001, 0x0200000000000000, 0x0200000000000001,
     348             :                 0x0400000000000000, 0x0400000000000001, 0x0800000000000000, 0x0800000000000001, 0x1000000000000000, 0x1000000000000001,
     349             :                 0x2000000000000000, 0x2000000000000001, 0x4000000000000000, 0x4000000000000001, 0x8000000000000000, 0x8000000000000001,
     350             :                 0xFFFFFFFFFFFFFFFF
     351             :         };
     352           2 :         size_t testNumbersCount = sizeof (testNumbers) / sizeof (kdb_unsigned_long_long_t);
     353             : 
     354             :         char errorBuf[128];
     355         268 :         for (size_t i = 0; i < testNumbersCount; ++i)
     356             :         {
     357         266 :                 FILE * f = fopen (elektraFilename (), "wb");
     358         266 :                 succeed_if (varintWrite (f, testNumbers[i]), "write error");
     359         266 :                 fclose (f);
     360             : 
     361         266 :                 f = fopen (elektraFilename (), "rb");
     362         266 :                 kdb_unsigned_long_long_t result = 0;
     363         266 :                 succeed_if (varintRead (f, &result), "read error");
     364         266 :                 fclose (f);
     365             : 
     366         266 :                 snprintf (errorBuf, sizeof (errorBuf), "conversion for %" PRIX64 " wrong, got %" PRIX64, testNumbers[i], result);
     367         266 :                 succeed_if (testNumbers[i] == result, errorBuf);
     368             :         }
     369           2 : }
     370             : 
     371           2 : int main (int argc, char ** argv)
     372             : {
     373           2 :         printf ("QUICKDUMP     TESTS\n");
     374           2 :         printf ("==================\n\n");
     375             : 
     376           2 :         init (argc, argv);
     377             : 
     378           2 :         test_varint ();
     379             : 
     380           2 :         test_basics ();
     381           2 :         test_noParent ();
     382           2 :         test_readV1 ();
     383           2 :         test_readV2 ();
     384           2 :         test_parentKeyValue ();
     385             : 
     386           2 :         print_result ("testmod_quickdump");
     387             : 
     388           2 :         return nbError;
     389             : }

Generated by: LCOV version 1.13