LCOV - code coverage report
Current view: top level - src/plugins/xmltool - testmod_xmltool.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 139 165 84.2 %
Date: 2022-05-21 16:19:22 Functions: 4 6 66.7 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include <tests_internal.h>
      10             : 
      11             : #include "kdbtools.h"
      12             : #include "xmltool.h"
      13             : 
      14             : #include <unistd.h>
      15             : 
      16           0 : void test_readwrite (void)
      17             : {
      18           0 :         KeySet * ks;
      19           0 :         FILE * fout;
      20             : 
      21           0 :         printf ("Testing Read and write xml\n");
      22             : 
      23           0 :         fout = fopen (srcdir_file ("xmltool/key-gen.xml"), "w");
      24           0 :         ks = ksNew (0, KS_END);
      25           0 :         exit_if_fail (ksFromXMLfile (ks, srcdir_file ("xmltool/key.xml")) == 0, "ksFromXMLfile(key.xml) failed.");
      26           0 :         ksToStream (ks, fout, KDB_O_HEADER);
      27           0 :         fclose (fout);
      28             : 
      29           0 :         compare_files (srcdir_file ("xmltool/key.xml"));
      30           0 :         unlink (srcdir_file ("xmltool/key-gen.xml"));
      31             : 
      32           0 :         ksDel (ks);
      33           0 : }
      34             : 
      35           0 : void test_readwrite_hier (void)
      36             : {
      37           0 :         KeySet * ks;
      38           0 :         FILE * fout;
      39             : 
      40           0 :         printf ("Testing Read and write xml\n");
      41             : 
      42           0 :         fout = fopen (srcdir_file ("xmltool/key-gen.xml"), "w");
      43           0 :         ks = ksNew (0, KS_END);
      44           0 :         exit_if_fail (ksFromXMLfile (ks, srcdir_file ("xmltool/key.xml")) == 0, "ksFromXMLfile(key.xml) failed.");
      45           0 :         ksToStream (ks, fout, KDB_O_HIER | KDB_O_HEADER);
      46           0 :         fclose (fout);
      47             : 
      48           0 :         compare_files (srcdir_file ("xmltool/key.xml"));
      49           0 :         unlink (srcdir_file ("xmltool/key-gen.xml"));
      50             : 
      51           0 :         ksDel (ks);
      52           0 : }
      53             : 
      54             : 
      55           2 : void test_key (void)
      56             : {
      57           2 :         KeySet * ks;
      58           2 :         Key * cur;
      59           2 :         int counter;
      60             : 
      61           2 :         printf ("Testing Key from xml\n");
      62             : 
      63           2 :         ks = ksNew (0, KS_END);
      64           2 :         exit_if_fail (ksFromXMLfile (ks, srcdir_file ("xmltool/key.xml")) == 0, "ksFromXMLfile(key.xml) failed.");
      65           2 :         counter = 0;
      66           2 :         ksRewind (ks);
      67          20 :         while ((cur = ksNext (ks)))
      68             :         {
      69          18 :                 counter++;
      70             :                 /* Prepend key root */
      71             :                 // snprintf(buf, sizeof(buf), "%s/%s", root, keyName(cur));
      72             :                 // keySetName(cur, buf);
      73             : 
      74             :                 /* Make tests ... */
      75             :                 // printf ("counter: %d - name: %s - value: %s\n", counter, (char*) keyName(cur), (char*)keyValue(cur));
      76          18 :                 switch (counter)
      77             :                 {
      78           2 :                 case 1:
      79           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/.HiddenBinaryKey") == 0, "wrong name");
      80           2 :                         succeed_if (strcmp (keyValue (cur), "BinaryValue") == 0, "value not correct");
      81           2 :                         succeed_if (strcmp (keyComment (cur), "Binary key with hidden name") == 0, "comment not correct");
      82           2 :                         succeed_if (keyIsBinary (cur) == 1, "key is not binary");
      83             :                         break;
      84           2 :                 case 2:
      85           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/.HiddenDirectoryKey") == 0, "wrong name");
      86           2 :                         succeed_if (strcmp (keyValue (cur), "DirectoryValue") == 0, "value not correct");
      87           2 :                         succeed_if (strcmp (keyComment (cur), "Directory key with hidden name") == 0, "comment not correct");
      88           2 :                         succeed_if (keyIsString (cur) == 1, "key is not string");
      89             :                         break;
      90           2 :                 case 3:
      91           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/.HiddenStringKey") == 0, "wrong name");
      92           2 :                         succeed_if (strcmp (keyValue (cur), "StringValue") == 0, "value not correct");
      93           2 :                         succeed_if (strcmp (keyComment (cur), "String key with hidden name") == 0, "comment not correct");
      94           2 :                         succeed_if (keyIsString (cur) == 1, "key is not string");
      95             :                         break;
      96           2 :                 case 4:
      97           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/PerfectBinaryKey") == 0, "wrong name");
      98           2 :                         succeed_if (strcmp (keyValue (cur), "BinaryValue") == 0, "value not correct");
      99           2 :                         succeed_if (strcmp (keyComment (cur), "Binary key with standard name") == 0, "comment not correct");
     100           2 :                         succeed_if (keyIsBinary (cur) == 1, "key is not binary");
     101             :                         break;
     102           2 :                 case 5:
     103           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/PerfectDirectoryKey") == 0, "wrong name");
     104           2 :                         succeed_if (strcmp (keyValue (cur), "DirectoryValue") == 0, "value not correct");
     105           2 :                         succeed_if (strcmp (keyComment (cur), "Directory key with standard name") == 0, "comment not correct");
     106           2 :                         succeed_if (keyIsString (cur) == 1, "key is not string");
     107             :                         break;
     108           2 :                 case 6:
     109           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/PerfectStringKey") == 0, "wrong name");
     110           2 :                         succeed_if (strcmp (keyValue (cur), "StringValue") == 0, "value not correct");
     111           2 :                         succeed_if (strcmp (keyComment (cur), "String key with\nstandard name") == 0, "comment not correct");
     112           2 :                         succeed_if (keyIsString (cur) == 1, "key is not string");
     113             :                         break;
     114           2 :                 case 7:
     115           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/Ug.ly:Bin@a€ry Key") == 0, "wrong name");
     116           2 :                         succeed_if (keyValue (cur) == 0, "value not correct");
     117           2 :                         succeed_if (strcmp (keyComment (cur), "Binary key with ugly name") == 0, "comment not correct");
     118           2 :                         succeed_if (keyIsBinary (cur) == 1, "key is not binary");
     119             :                         // <key type="binary" basename="Ug.ly:Bin@a€ry Key"><comment>Binary key with ugly name</comment></key>
     120             :                         break;
     121           2 :                 case 8:
     122           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/Ug.ly:Dir@ect€ory Key") == 0, "wrong name");
     123           2 :                         succeed_if (strcmp (keyValue (cur), "") == 0, "value not correct");
     124           2 :                         succeed_if (strcmp (keyComment (cur), "Directory with ugly name") == 0, "comment not correct");
     125           2 :                         succeed_if (keyIsString (cur) == 1, "key is not string");
     126             :                         // <key type="directory" basename="Ug.ly:Dir@ect€ory Key"><comment>Directory with ugly name</comment></key>
     127             :                         break;
     128           2 :                 case 9:
     129           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/Ug.ly:St@ri€n.g Key") == 0, "wrong name");
     130           2 :                         succeed_if (strcmp (keyValue (cur), "With a string value") == 0, "value not correct");
     131           2 :                         succeed_if (strcmp (keyComment (cur), "string key with ugly name") == 0, "comment not correct");
     132           2 :                         succeed_if (keyIsString (cur) == 1, "key is not string");
     133             :                         // <key type="string" basename="Ug.ly:St@ri€n.g Key" value="With a string value"><comment>string key with ugly
     134             :                         // name</comment></key>
     135             :                         break;
     136             :                 }
     137             :         }
     138             : 
     139           2 :         ksDel (ks);
     140           2 : }
     141             : 
     142           2 : void test_keyset (void)
     143             : {
     144           2 :         KeySet * ks;
     145           2 :         Key * cur;
     146           2 :         int counter;
     147             : 
     148           2 :         printf ("Testing KeySet from xml\n");
     149             : 
     150           2 :         ks = ksNew (0, KS_END);
     151           2 :         exit_if_fail (ksFromXMLfile (ks, srcdir_file ("xmltool/keyset.xml")) == 0, "ksFromXMLfile(key.xml) failed.");
     152           2 :         counter = 0;
     153           2 :         ksRewind (ks);
     154         186 :         while ((cur = ksNext (ks)))
     155             :         {
     156         184 :                 counter++;
     157             : 
     158             :                 /* Make tests ... */
     159             :                 // printf ("counter: %d - %s\n", counter, keyName(cur));
     160         184 :                 switch (counter)
     161             :                 {
     162             :                 // <key type="43" basename="0-27042916" value="0 216905227"><comment>2551516588474823843</comment></key>
     163           2 :                 case 1:
     164           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/0-27042916") == 0, "name of first key not correct");
     165           2 :                         succeed_if (strcmp (keyValue (cur), "0 216905227") == 0, "value of first key not correct");
     166           2 :                         succeed_if (strcmp (keyComment (cur), "2551516588474823843") == 0, "comment of first key not correct");
     167             :                         break;
     168             :                 // <key type="253" basename="1-2449524622" value="1 1679328197"><comment>3246436893195629244</comment></key>
     169           2 :                 case 2:
     170           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/1-2449524622") == 0, "name of 2. key not correct");
     171           2 :                         succeed_if (strcmp (keyValue (cur), "1 1679328197") == 0, "value of 2. key not correct");
     172           2 :                         succeed_if (strcmp (keyComment (cur), "3246436893195629244") == 0, "comment of 2. key not correct");
     173             :                         break;
     174             :                 // <key type="string" basename="dir-1-0">
     175           2 :                 case 3:
     176           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/dir-1-0") == 0, "name of 3. key not correct");
     177             :                         break;
     178             :                 // <key type="114" basename="0-294164813" value="0 216245011"><comment>18454108762891828026</comment></key>
     179           2 :                 case 4:
     180           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/dir-1-0/0-294164813") == 0, "name of 4. key not correct");
     181           2 :                         succeed_if (strcmp (keyValue (cur), "0 216245011") == 0, "value of 4. key not correct");
     182           2 :                         succeed_if (strcmp (keyComment (cur), "18454108762891828026") == 0, "comment of 4. key not correct");
     183             :                         break;
     184             :                 // <key type="135" basename="1-1479930365" value="1 2732423037"><comment>24597295372375238</comment></key>
     185           2 :                 case 5:
     186           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/dir-1-0/1-1479930365") == 0, "name of 4. key not correct");
     187           2 :                         succeed_if (strcmp (keyValue (cur), "1 2732423037") == 0, "value of 4. key not correct");
     188           2 :                         succeed_if (strcmp (keyComment (cur), "24597295372375238") == 0, "comment of 4. key not correct");
     189             :                         break;
     190             :                 // <key type="string" basename="dir-2-0">
     191           2 :                 case 6:
     192           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/dir-1-0/dir-2-0") == 0, "name of 3. key not correct");
     193             :                         break;
     194             :                 // <key type="144" basename="0-215571059" value="0 264857705"><comment>2188631490667217086</comment></key>
     195           2 :                 case 7:
     196           2 :                         succeed_if (strcmp (keyName (cur), "user:/tests/filesys/dir-1-0/dir-2-0/0-215571059") == 0,
     197           2 :                                     "name of 4. key not correct");
     198           2 :                         succeed_if (strcmp (keyValue (cur), "0 264857705") == 0, "value of 4. key not correct");
     199           2 :                         succeed_if (strcmp (keyComment (cur), "2188631490667217086") == 0, "comment of 4. key not correct");
     200             :                         break;
     201             :                 }
     202             :         }
     203             : 
     204           2 :         ksDel (ks);
     205           2 : }
     206             : 
     207             : #define MAX_SIZE 200
     208             : 
     209           2 : static void test_ksCommonParentName (void)
     210             : {
     211           2 :         char ret[MAX_SIZE + 1];
     212           2 :         KeySet * ks = ksNew (10, keyNew ("system:/sw/xorg/Monitors/Monitor1/vrefresh", KEY_END),
     213             :                              keyNew ("system:/sw/xorg/Monitors/Monitor1/hrefresh", KEY_END),
     214             :                              keyNew ("system:/sw/xorg/Monitors/Monitor2/vrefresh", KEY_END),
     215             :                              keyNew ("system:/sw/xorg/Monitors/Monitor2/hrefresh", KEY_END),
     216             :                              keyNew ("system:/sw/xorg/Devices/Device1/driver", KEY_END),
     217             :                              keyNew ("system:/sw/xorg/Devices/Device1/mode", KEY_END), KS_END);
     218             : 
     219           2 :         printf ("Test common parentname\n");
     220             : 
     221           2 :         succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) > 0, "could not find correct parentname");
     222           2 :         succeed_if_same_string (ret, "system:/sw/xorg");
     223           2 :         ksDel (ks);
     224             : 
     225           2 :         ks = ksNew (10, keyNew ("system:/", KEY_END), keyNew ("user:/", KEY_END), KS_END);
     226           2 :         succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) == 0, "could find correct parentname");
     227           2 :         succeed_if_same_string (ret, "");
     228           2 :         ksDel (ks);
     229             : 
     230           2 :         ks = ksNew (10, keyNew ("system:/some/thing", KEY_END), keyNew ("system:/other/thing", KEY_END), KS_END);
     231           2 :         succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) == 9, "could find correct parentname");
     232           2 :         succeed_if_same_string (ret, "system:/");
     233           2 :         ksDel (ks);
     234             : 
     235           2 :         ks = ksNew (10, keyNew ("system:/some/thing", KEY_END), keyNew ("system:/something", KEY_END), KS_END);
     236           2 :         succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) == 9, "could find correct parentname");
     237           2 :         succeed_if_same_string (ret, "system:/");
     238           2 :         ksDel (ks);
     239             : 
     240           2 :         ks = ksNew (10, keyNew ("system:/here/in/deep/goes/ok/thing", KEY_END),
     241             :                     keyNew ("system:/here/in/deep/goes/ok/other/thing", KEY_END), KS_END);
     242           2 :         succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) > 0, "could find correct parentname");
     243           2 :         succeed_if_same_string (ret, "system:/here/in/deep/goes/ok");
     244           2 :         ksDel (ks);
     245             : 
     246           2 :         ks = ksNew (10, keyNew ("system:/here/in/deep/goes/ok/thing", KEY_END),
     247             :                     keyNew ("system:/here/in/deep/goes/ok/other/thing", KEY_END), keyNew ("user:/unique/thing", KEY_END), KS_END);
     248           2 :         succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) == 0, "could find correct parentname");
     249           2 :         succeed_if_same_string (ret, "");
     250           2 :         ksDel (ks);
     251             : 
     252           2 :         ks = ksNew (10, keyNew ("user:/unique/thing", KEY_END), KS_END);
     253           2 :         succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) > 0, "could find correct parentname");
     254           2 :         succeed_if_same_string (ret, "user:/unique/thing");
     255           2 :         ksDel (ks);
     256           2 : }
     257             : 
     258             : 
     259           2 : int main (int argc, char ** argv)
     260             : {
     261           2 :         printf ("ELEKTRA PLUGIN TEST SUITE\n");
     262           2 :         printf ("========================================\n\n");
     263             : 
     264           2 :         init (argc, argv);
     265             : 
     266           2 :         test_key ();
     267           2 :         test_keyset ();
     268           2 :         test_ksCommonParentName ();
     269             : 
     270             :         /*
     271             :         test_readwrite();
     272             :         test_readwrite_hier();
     273             :         */
     274             : 
     275           6 :         print_result ("test_xml");
     276             : 
     277           2 :         return nbError;
     278             : }

Generated by: LCOV version 1.13