LCOV - code coverage report
Current view: top level - tests/abi - testabi_key.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 1924 1926 99.9 %
Date: 2022-05-21 16:19:22 Functions: 25 26 96.2 %

          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.h>
      10             : 
      11             : #define NUMBER_OF_NAMESPACES 6
      12             : 
      13             : char * namespaces[] = { "/", "spec:/", "proc:/", "dir:/", "user:/", "system:/", 0 };
      14             : 
      15             : struct test
      16             : {
      17             :         char * testName;
      18             :         char * keyName;
      19             : 
      20             :         char * expectedKeyName;
      21             :         char * expectedBaseName;
      22             :         char * expectedFRootName;
      23             : };
      24             : 
      25             : struct test tstKeyName[] = { { "Normal key", "system:/foo/bar", "system:/foo/bar", "bar", "system" },
      26             : 
      27             :                              {
      28             :                                      "Key containing redundant & trailing separator", "system://foo//bar//",
      29             :                                      "system:/foo/bar", /* keyName    */
      30             :                                      "bar",           /* keyBaseName  */
      31             :                                      "system",                /* keyGetFullRootName   */
      32             :                              },
      33             : 
      34             :                              {
      35             :                                      "Normal user key", "user:/key", "user:/key", /* keyName      */
      36             :                                      "key",                                     /* keyBaseName        */
      37             :                                      "user",                                    /* keyGetFullRootName         */
      38             :                              },
      39             : 
      40             :                              {
      41             :                                      "Key with empty part", "user:///%", "user:/%", /* keyName    */
      42             :                                      "",                                          /* keyBaseName      */
      43             :                                      "",                                          /* keyGetFullRootName       */
      44             : 
      45             :                              },
      46             : 
      47             :                              {
      48             :                                      "Key with escaped %", "user:///\\%", "user:/\\%", /* keyName         */
      49             :                                      "%",                                            /* keyBaseName   */
      50             :                                      "",                                             /* keyGetFullRootName    */
      51             : 
      52             :                              },
      53             : 
      54             :                              {
      55             :                                      "Key with multi escaped %", "user:///\\\\%", "user:/\\\\%", /* keyName       */
      56             :                                      "\\%",                                                    /* keyBaseName         */
      57             :                                      "",                                                       /* keyGetFullRootName  */
      58             : 
      59             :                              },
      60             : 
      61             :                              {
      62             :                                      NULL, NULL, NULL, /* keyName       */
      63             :                                      NULL,             /* keyBaseName   */
      64             :                                      NULL,             /* keyGetFullRootName    */
      65             :                              } };
      66             : 
      67           3 : static void test_keyNewSpecial (void)
      68             : {
      69           3 :         printf ("Test special key creation\n");
      70             : 
      71           3 :         Key * k = keyNew ("/", KEY_END);
      72           3 :         succeed_if_same_string (keyName (k), "/");
      73           3 :         keyDel (k);
      74             : 
      75           3 :         k = keyNew (0, KEY_END);
      76           3 :         succeed_if (k == NULL, "should be invalid");
      77           3 :         keyDel (k);
      78             : 
      79           3 :         k = keyNew ("", KEY_END);
      80           3 :         succeed_if (k == NULL, "should be invalid");
      81           3 :         keyDel (k);
      82             : 
      83           3 :         k = keyNew ("invalid", KEY_END);
      84           3 :         succeed_if (k == NULL, "should be invalid");
      85           3 :         keyDel (k);
      86             : 
      87           3 :         k = keyNew ("other invalid", KEY_END);
      88           3 :         succeed_if (k == NULL, "should be invalid");
      89           3 :         keyDel (k);
      90             : 
      91           3 :         k = keyNew ("system spaces", KEY_END);
      92           3 :         succeed_if (k == NULL, "should be invalid");
      93           3 :         keyDel (k);
      94             : 
      95           3 :         k = keyNew ("system:/bin", KEY_BINARY, KEY_VALUE, "a 2d\0b", KEY_END);
      96           3 :         succeed_if_same_string (keyValue (k), "a 2d");
      97           3 :         succeed_if (keyGetValueSize (k) == sizeof ("a 2d"), "no KEY_SIZE given, so bin is truncated");
      98           3 :         succeed_if (keyIsBinary (k), "not a binary key");
      99           3 :         keyDel (k);
     100           3 : }
     101             : 
     102           3 : static void test_keyNewWrongName (void)
     103             : {
     104           3 :         printf ("Test key creation with wrong names\n");
     105           3 :         Key * k = keyNew ("systemx:/hello", KEY_END);
     106           3 :         succeed_if (k == NULL, "key with wrong name could be created");
     107           3 :         k = keyNew ("usx:/hello", KEY_END);
     108           3 :         succeed_if (k == NULL, "key with wrong name could be created");
     109           3 :         k = keyNew ("usxx:/hello", KEY_END);
     110           3 :         succeed_if (k == NULL, "key with wrong name could be created");
     111           3 :         k = keyNew ("abc:/hello", KEY_END);
     112           3 :         succeed_if (k == NULL, "key with wrong name could be created");
     113           3 :         k = keyNew ("use:/hello", KEY_END);
     114           3 :         succeed_if (k == NULL, "key with wrong name could be created");
     115           3 :         k = keyNew ("spexc:/hello", KEY_END);
     116           3 :         succeed_if (k == NULL, "key with wrong name could be created");
     117           3 : }
     118             : 
     119             : 
     120           3 : static void test_keyNewSystem (void)
     121             : {
     122           3 :         Key * key;
     123           3 :         char array[] = "here is some data stored";
     124           3 :         Key * k1;
     125           3 :         Key * k2;
     126           3 :         Key * k3;
     127           3 :         char * getBack;
     128             : 
     129           3 :         printf ("Test system key creation\n");
     130             : 
     131             :         // Empty key
     132           3 :         key = keyNew ("/", KEY_END);
     133           3 :         succeed_if (key != NULL, "keyNew: Unable to create a new empty key");
     134           3 :         succeed_if (keyDel (key) == 0, "keyDel: Unable to delete empty key");
     135             : 
     136             :         // Key with name
     137           3 :         key = keyNew ("system:/sw/test", KEY_END);
     138           3 :         succeed_if (key != NULL, "keyNew: Unable to create a key with name");
     139           3 :         succeed_if_same_string (keyName (key), "system:/sw/test");
     140           3 :         keyCopy (key, 0, KEY_CP_NAME);
     141           3 :         succeed_if_same_string (keyName (key), "/");
     142           3 :         succeed_if (keyDel (key) == 0, "keyDel: Unable to delete key with name");
     143             : 
     144             :         // Key with name
     145           3 :         key = keyNew ("system:/sw/test", KEY_END);
     146           3 :         succeed_if (key != NULL, "keyNew: Unable to create a key with name");
     147           3 :         succeed_if_same_string (keyName (key), "system:/sw/test");
     148           3 :         succeed_if (keyDel (key) == 0, "keyDel: Unable to delete key with name");
     149             : 
     150             :         // Key with name + value
     151           3 :         key = keyNew ("system:/sw/test", KEY_VALUE, "test", KEY_END);
     152           3 :         succeed_if (key != NULL, "keyNew: Unable to create a key with name + value of default type");
     153           3 :         succeed_if_same_string (keyValue (key), "test");
     154           3 :         succeed_if (keyDel (key) == 0, "keyDel: Unable to delete key with name + value");
     155           3 :         key = keyNew ("system:/valid/there", KEY_BINARY, KEY_SIZE, sizeof (array), KEY_VALUE, array, KEY_END);
     156           3 :         succeed_if (key != NULL, "keyNew: Unable to create a key with name + value of default type");
     157           3 :         succeed_if (keyIsBinary (key), "Could not set type to binary");
     158           3 :         succeed_if (keyGetValueSize (key) == sizeof (array), "Value size not correct");
     159           3 :         succeed_if (memcmp ((char *) keyValue (key), array, sizeof (array)) == 0, "could not get correct binary value");
     160           3 :         getBack = elektraMalloc (keyGetValueSize (key));
     161           3 :         keyGetBinary (key, getBack, keyGetValueSize (key));
     162           3 :         succeed_if (memcmp (getBack, array, sizeof (array)) == 0, "could not get correct value with keyGetBinary");
     163           3 :         elektraFree (getBack);
     164           3 :         succeed_if (keyDel (key) == 0, "keyDel: Unable to delete key with name + owner");
     165             : 
     166           3 :         key = keyNew ("system:/", KEY_END);
     167           3 :         succeed_if_same_string (keyName (key), "system:/");
     168           3 :         succeed_if (keyGetNameSize (key) == 9, "empty name size");
     169           3 :         succeed_if (keyValue (keyGetMeta (key, "owner")) == 0, "owner not null");
     170           3 :         keyDel (key);
     171             : 
     172             :         // testing multiple values at once
     173           3 :         k1 = keyNew ("system:/1", KEY_VALUE, "singlevalue", KEY_END);
     174           3 :         k2 = keyNew ("system:/2", KEY_VALUE, "myvalue", KEY_END);
     175           3 :         k3 = keyNew ("system:/3", KEY_VALUE, "syskey", KEY_END);
     176           3 :         succeed_if (k1 != NULL, "keyNew: Unable to create a key with name + value of default type");
     177           3 :         succeed_if (keyIsString (k1), "keyNew: Default key value isn't set to string");
     178           3 :         succeed_if_same_string (keyValue (k1), "singlevalue");
     179             : 
     180           3 :         succeed_if (k2 != NULL, "keyNew: Unable to create a key with name + value of default type");
     181           3 :         succeed_if (keyIsString (k2), "keyNew: Default key value isn't set to string");
     182           3 :         succeed_if_same_string (keyValue (k2), "myvalue");
     183             : 
     184           3 :         succeed_if (k3 != NULL, "keyNew: Unable to create a key with name + value of default type");
     185           3 :         succeed_if (keyIsString (k3), "keyNew: Default key value isn't set to string");
     186           3 :         succeed_if_same_string (keyValue (k3), "syskey");
     187             : 
     188           3 :         succeed_if (keyDel (k1) == 0, "keyDel: Unable to delete key with name + value");
     189           3 :         succeed_if (keyDel (k2) == 0, "keyDel: Unable to delete key with name + value");
     190           3 :         succeed_if (keyDel (k3) == 0, "keyDel: Unable to delete key with name + value");
     191           3 : }
     192             : 
     193           3 : static void test_keyNewUser (void)
     194             : {
     195           3 :         Key * k1;
     196           3 :         Key * k2;
     197           3 :         Key * k3;
     198             : 
     199           3 :         printf ("Test user key creation\n");
     200             :         // testing multiple values at once
     201           3 :         k1 = keyNew ("user:/1", KEY_VALUE, "singlevalue", KEY_END);
     202           3 :         k2 = keyNew ("user:/2", KEY_VALUE, "myvalue", KEY_END);
     203           3 :         k3 = keyNew ("user:/3", KEY_VALUE, "syskey", KEY_END);
     204           3 :         succeed_if (k1 != NULL, "keyNew: Unable to create a key with name + value of default type");
     205           3 :         succeed_if (keyIsString (k1), "keyNew: Default key value isn't set to string");
     206           3 :         succeed_if_same_string (keyValue (k1), "singlevalue");
     207             : 
     208           3 :         succeed_if (k2 != NULL, "keyNew: Unable to create a key with name + value of default type");
     209           3 :         succeed_if (keyIsString (k2), "keyNew: Default key value isn't set to string");
     210           3 :         succeed_if_same_string (keyValue (k2), "myvalue");
     211             : 
     212           3 :         succeed_if (k3 != NULL, "keyNew: Unable to create a key with name + value of default type");
     213           3 :         succeed_if (keyIsString (k3), "keyNew: Default key value isn't set to string");
     214           3 :         succeed_if_same_string (keyValue (k3), "syskey");
     215             : 
     216           3 :         succeed_if (keyDel (k1) == 0, "keyDel: Unable to delete key with name + value");
     217           3 :         succeed_if (keyDel (k2) == 0, "keyDel: Unable to delete key with name + value");
     218           3 :         succeed_if (keyDel (k3) == 0, "keyDel: Unable to delete key with name + value");
     219             : 
     220           3 :         k1 = keyNew ("invalid", KEY_END);
     221           3 :         succeed_if (k1 == 0, "should not construct key on invalid names");
     222           3 : }
     223             : 
     224           3 : static void test_keyReference (void)
     225             : {
     226           3 :         printf ("Test key reference\n");
     227             : 
     228           3 :         Key * key = keyNew ("user:/key", KEY_END);
     229           3 :         Key * c = keyNew ("user:/c", KEY_END);
     230           3 :         Key * d;
     231           3 :         KeySet *ks1, *ks2;
     232             : 
     233           3 :         succeed_if (keyGetRef (0) == UINT16_MAX, "No error on getting refcount of NULL Key");
     234           3 :         succeed_if (keyDecRef (0) == UINT16_MAX, "No error on decrementing NULL Key");
     235           3 :         succeed_if (keyIncRef (0) == UINT16_MAX, "No error on incrementing NULL Key");
     236             : 
     237           3 :         succeed_if (keyGetRef (key) == 0, "New created key reference");
     238             : 
     239           3 :         succeed_if (keyIncRef (key) == 1, "keyIncRef return value");
     240           3 :         succeed_if (keyGetRef (key) == 1, "After keyIncRef key reference");
     241           3 :         succeed_if (keyIncRef (key) == 2, "keyIncRef return value");
     242           3 :         succeed_if (keyGetRef (key) == 2, "After keyIncRef key reference");
     243           3 :         succeed_if (keyIncRef (key) == 3, "keyIncRef return value");
     244           3 :         succeed_if (keyGetRef (key) == 3, "After keyIncRef key reference");
     245           3 :         succeed_if (keyIncRef (key) == 4, "keyIncRef return value");
     246           3 :         succeed_if (keyGetRef (key) == 4, "After keyIncRef key reference");
     247             : 
     248           3 :         d = keyDup (key, KEY_CP_ALL);
     249           3 :         succeed_if (keyGetRef (d) == 0, "After keyDup key reference");
     250           3 :         succeed_if (keyIncRef (d) == 1, "keyIncRef return value");
     251           3 :         succeed_if (keyGetRef (key) == 4, "Reference should not change");
     252           3 :         succeed_if (keyDecRef (d) == 0, "decrement key");
     253           3 :         succeed_if (keyDel (d) == 0, "last keyDel d, key exist");
     254             : 
     255           3 :         keyCopy (c, key, KEY_CP_ALL);
     256           3 :         succeed_if (keyGetRef (c) == 0, "After keyCopy key reference");
     257           3 :         succeed_if (keyIncRef (c) == 1, "keyIncRef return value");
     258           3 :         succeed_if (keyGetRef (key) == 4, "Reference should not change");
     259             : 
     260           3 :         keyCopy (c, key, KEY_CP_ALL);
     261           3 :         succeed_if (keyGetRef (c) == 1, "After keyCopy key reference");
     262           3 :         succeed_if (keyDecRef (c) == 0, "keyDecRef return value");
     263           3 :         succeed_if (keyGetRef (key) == 4, "Reference should not change");
     264             : 
     265           3 :         succeed_if (keyIncRef (c) == 1, "keyIncRef return value");
     266           3 :         succeed_if (keyIncRef (c) == 2, "keyIncRef return value");
     267           3 :         keyCopy (c, key, KEY_CP_ALL);
     268           3 :         succeed_if (keyGetRef (c) == 2, "After keyCopy key reference");
     269           3 :         succeed_if (keyDecRef (c) == 1, "keyDecRef return value");
     270           3 :         succeed_if (keyDecRef (c) == 0, "keyDecRef return value");
     271           3 :         succeed_if (keyDel (c) == 0, "could not delete copy");
     272             : 
     273           3 :         succeed_if (keyGetRef (key) == 4, "After keyIncRef key reference");
     274           3 :         succeed_if (keyDecRef (key) == 3, "keyDel return value");
     275           3 :         succeed_if (keyDel (key) == 3, "should not do anything");
     276           3 :         succeed_if (keyGetRef (key) == 3, "After keyIncRef key reference");
     277           3 :         succeed_if (keyDecRef (key) == 2, "keyDel return value");
     278           3 :         succeed_if (keyDel (key) == 2, "should not do anything");
     279           3 :         succeed_if (keyGetRef (key) == 2, "After keyIncRef key reference");
     280           3 :         succeed_if (keyDecRef (key) == 1, "keyDel return value");
     281           3 :         succeed_if (keyDel (key) == 1, "should not do anything");
     282           3 :         succeed_if (keyGetRef (key) == 1, "Should have no more reference");
     283           3 :         succeed_if (keyDecRef (key) == 0, "last keyDel key, key exist");
     284           3 :         succeed_if (keyDel (key) == 0, "last keyDel key, key exist");
     285             : 
     286             :         /* From examples in ksNew () */
     287           3 :         key = keyNew ("/", KEY_END); // ref counter 0
     288           3 :         succeed_if (keyGetRef (key) == 0, "reference counter");
     289           3 :         keyIncRef (key); // ref counter of key 1
     290           3 :         succeed_if (keyGetRef (key) == 1, "reference counter");
     291           3 :         keyDel (key); // has no effect
     292           3 :         succeed_if (keyGetRef (key) == 1, "reference counter");
     293           3 :         keyDecRef (key); // ref counter back to 0
     294           3 :         succeed_if (keyGetRef (key) == 0, "reference counter");
     295           3 :         keyDel (key); // key is now deleted
     296             : 
     297           3 :         ks1 = ksNew (0, KS_END);
     298           3 :         ks2 = ksNew (0, KS_END);
     299           3 :         key = keyNew ("user:/key", KEY_END); // ref counter 0
     300           3 :         succeed_if (keyGetRef (key) == 0, "reference counter");
     301           3 :         ksAppendKey (ks1, key); // ref counter of key 1
     302           3 :         succeed_if (keyGetRef (key) == 1, "reference counter");
     303           3 :         ksAppendKey (ks2, key); // ref counter of key 2
     304           3 :         succeed_if (keyGetRef (key) == 2, "reference counter");
     305           3 :         ksDel (ks1); // ref counter of key 1
     306           3 :         succeed_if (keyGetRef (key) == 1, "reference counter");
     307           3 :         ksDel (ks2); // key is now deleted
     308             : 
     309           3 :         key = keyNew ("/", KEY_END); // ref counter 0
     310           3 :         succeed_if (keyGetRef (key) == 0, "reference counter");
     311           3 :         keyIncRef (key); // ref counter of key 1
     312           3 :         succeed_if (keyGetRef (key) == 1, "reference counter");
     313           3 :         keyDel (key); // has no effect
     314           3 :         succeed_if (keyGetRef (key) == 1, "reference counter");
     315           3 :         keyIncRef (key); // ref counter of key 2
     316           3 :         succeed_if (keyGetRef (key) == 2, "reference counter");
     317           3 :         keyDel (key); // has no effect
     318           3 :         succeed_if (keyGetRef (key) == 2, "reference counter");
     319           3 :         keyDecRef (key); // ref counter of key 1
     320           3 :         succeed_if (keyGetRef (key) == 1, "reference counter");
     321           3 :         keyDel (key); // has no effect
     322           3 :         succeed_if (keyGetRef (key) == 1, "reference counter");
     323           3 :         keyDecRef (key); // ref counter is now 0
     324           3 :         succeed_if (keyGetRef (key) == 0, "reference counter");
     325           3 :         keyDel (key); // key is now deleted
     326             : 
     327           3 :         Key * k = keyNew ("system:/proper_name", KEY_END); // ref counter = 0
     328           3 :         succeed_if (keyGetRef (k) == 0, "ref should be zero");
     329           3 :         KeySet * ks = ksNew (1, k, KS_END);
     330           3 :         succeed_if (keyGetRef (k) == 1, "ref should be one");
     331           3 :         succeed_if (keyDel (k) == 1, "key will not be deleted, because its in the keyset");
     332           3 :         succeed_if (keyGetRef (k) == 1, "ref should be one");
     333           3 :         succeed_if (ksDel (ks) == 0, "could not del"); // now the key will be deleted
     334             : 
     335           3 :         key = keyNew ("/", KEY_END); // ref counter 0
     336      196605 :         while (keyGetRef (key) < UINT16_MAX - 1)
     337      196602 :                 keyIncRef (key);
     338           3 :         succeed_if (keyGetRef (key) == UINT16_MAX - 1, "reference counter");
     339           3 :         succeed_if (keyIncRef (key) == UINT16_MAX, "should report error");
     340           3 :         succeed_if (keyGetRef (key) == UINT16_MAX - 1, "reference counter");
     341           3 :         succeed_if (keyIncRef (key) == UINT16_MAX, "should report error");
     342      196605 :         while (keyGetRef (key) > 0)
     343      196602 :                 keyDecRef (key);
     344           3 :         keyDel (key);
     345           3 : }
     346             : 
     347           3 : static void test_keyName (void)
     348             : {
     349           3 :         Key * key;
     350           3 :         char ret[1000];
     351           3 :         size_t i;
     352           3 :         char testName[] = "user:/name";
     353           3 :         char testBaseName[] = "name";
     354             : 
     355             : #ifdef HAVE_CLEARENV
     356           3 :         clearenv ();
     357             : #else
     358             :         unsetenv ("USER");
     359             : #endif
     360             : 
     361           3 :         printf ("Test Key Name\n");
     362             : 
     363           3 :         key = keyNew (testName, KEY_END);
     364           3 :         succeed_if (keyGetName (0, ret, 100) == -1, "null pointer");
     365           3 :         succeed_if (keyGetName (key, 0, 100) == -1, "string null pointer");
     366           3 :         succeed_if (keyGetName (key, ret, 0) == -1, "length checking");
     367          33 :         for (i = 1; i < sizeof (testName); i++)
     368             :         {
     369          30 :                 succeed_if (keyGetName (key, ret, i) == -1, "length checking too short");
     370             :         }
     371          36 :         for (i = sizeof (testName); i < sizeof (testName) * 2; i++)
     372             :         {
     373          33 :                 succeed_if (keyGetName (key, ret, i) == sizeof (testName), "length checking longer");
     374             :         }
     375           3 :         succeed_if (keyGetName (key, ret, (size_t) -1) == -1, "maxSize exceeded");
     376           3 :         keyDel (key);
     377             : 
     378           3 :         succeed_if (keyName (0) == 0, "null pointer");
     379             : 
     380           3 :         succeed_if (keySetName (0, ret) == -1, "Null pointer");
     381             : 
     382             : 
     383           3 :         printf ("Test Key Base Name\n");
     384             : 
     385           3 :         key = keyNew (testName, KEY_END);
     386             : 
     387           3 :         succeed_if (keyGetBaseName (0, ret, 100) == -1, "null pointer");
     388           3 :         succeed_if (keyGetBaseName (key, 0, 100) == -1, "string null pointer");
     389           3 :         succeed_if (keyGetBaseName (key, ret, 0) == -1, "length checking");
     390             : 
     391           3 :         succeed_if (keyGetBaseNameSize (0) == -1, "no error on passing NULL pointer");
     392             : 
     393          15 :         for (i = 1; i < sizeof (testBaseName); i++)
     394             :         {
     395          12 :                 succeed_if (keyGetBaseName (key, ret, i) == -1, "length checking too short");
     396             :         }
     397             : 
     398          18 :         for (i = sizeof (testBaseName); i < sizeof (testBaseName) * 2; i++)
     399             :         {
     400          15 :                 succeed_if (keyGetBaseName (key, ret, i) == sizeof (testBaseName), "length checking longer");
     401             :         }
     402             : 
     403           3 :         succeed_if (keyGetBaseName (key, ret, (size_t) -1) == -1, "maxSize exceeded");
     404           3 :         keyDel (key);
     405             : 
     406             : 
     407           3 :         succeed_if (keyBaseName (0) == 0, "null pointer");
     408             : 
     409           3 :         key = keyNew ("/", KEY_END);
     410           3 :         succeed_if_same_string (keyBaseName (key), "");
     411           3 :         succeed_if (keyGetBaseName (key, ret, 1000) == 1, "get empty name");
     412           3 :         succeed_if_same_string (ret, "");
     413           3 :         succeed_if (keyGetBaseName (key, ret, 0) == -1, "get empty name");
     414           3 :         keyDel (key);
     415             : 
     416           3 :         succeed_if (keySetName (0, ret) == -1, "Null pointer");
     417             : 
     418           3 :         key = keyNew ("user:/", KEY_END);
     419           3 :         succeed_if_same_string (keyName (key), "user:/");
     420           3 :         succeed_if (keyGetNameSize (key) == 7, "name length checking");
     421           3 :         succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
     422           3 :         succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
     423           3 :         succeed_if_same_string (ret, "");
     424           3 :         succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
     425           3 :         succeed_if_same_string (ret, "");
     426           3 :         keyDel (key);
     427             : 
     428           3 :         key = keyNew ("user:/", KEY_END);
     429           3 :         succeed_if_same_string (keyName (key), "user:/");
     430           3 :         succeed_if (keyGetNameSize (key) == 7, "name length checking");
     431           3 :         succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
     432           3 :         succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
     433           3 :         succeed_if_same_string (ret, "");
     434           3 :         succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
     435           3 :         succeed_if_same_string (ret, "");
     436           3 :         keyDel (key);
     437             : 
     438           3 :         key = keyNew ("user://", KEY_END);
     439           3 :         succeed_if_same_string (keyName (key), "user:/");
     440           3 :         succeed_if (keyGetNameSize (key) == 7, "name length checking");
     441           3 :         succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
     442           3 :         succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
     443           3 :         succeed_if_same_string (ret, "");
     444           3 :         succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
     445           3 :         succeed_if_same_string (ret, "");
     446           3 :         keyDel (key);
     447             : 
     448           3 :         key = keyNew ("system:/", KEY_END);
     449           3 :         succeed_if_same_string (keyName (key), "system:/");
     450           3 :         succeed_if (keyGetNameSize (key) == 9, "name length checking");
     451           3 :         succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
     452           3 :         succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
     453           3 :         succeed_if_same_string (ret, "");
     454           3 :         succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
     455           3 :         succeed_if_same_string (ret, "");
     456           3 :         keyDel (key);
     457             : 
     458           3 :         key = keyNew ("system:/", KEY_END);
     459           3 :         succeed_if_same_string (keyName (key), "system:/");
     460           3 :         succeed_if (keyGetNameSize (key) == 9, "name length checking");
     461           3 :         succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
     462           3 :         succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
     463           3 :         succeed_if_same_string (ret, "");
     464           3 :         succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
     465           3 :         succeed_if_same_string (ret, "");
     466           3 :         keyDel (key);
     467             : 
     468           3 :         key = keyNew ("dir:/", KEY_END);
     469           3 :         succeed_if_same_string (keyName (key), "dir:/");
     470           3 :         succeed_if (keyGetNameSize (key) == 6, "name length checking");
     471           3 :         succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
     472           3 :         succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
     473           3 :         succeed_if_same_string (ret, "");
     474           3 :         succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
     475           3 :         succeed_if_same_string (ret, "");
     476           3 :         keyDel (key);
     477             : 
     478           3 :         key = keyNew ("dir:/", KEY_END);
     479           3 :         succeed_if_same_string (keyName (key), "dir:/");
     480           3 :         succeed_if (keyGetNameSize (key) == 6, "name length checking");
     481           3 :         succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
     482           3 :         succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
     483           3 :         succeed_if_same_string (ret, "");
     484           3 :         succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
     485           3 :         succeed_if_same_string (ret, "");
     486           3 :         keyDel (key);
     487             : 
     488           3 :         key = keyNew ("proc:/", KEY_END);
     489           3 :         succeed_if_same_string (keyName (key), "proc:/");
     490           3 :         succeed_if (keyGetNameSize (key) == 7, "name length checking");
     491           3 :         succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
     492           3 :         succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
     493           3 :         succeed_if_same_string (ret, "");
     494           3 :         succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
     495           3 :         succeed_if_same_string (ret, "");
     496           3 :         keyDel (key);
     497             : 
     498           3 :         key = keyNew ("proc:/", KEY_END);
     499           3 :         succeed_if_same_string (keyName (key), "proc:/");
     500           3 :         succeed_if (keyGetNameSize (key) == 7, "name length checking");
     501           3 :         succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
     502           3 :         succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
     503           3 :         succeed_if_same_string (ret, "");
     504           3 :         succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
     505           3 :         succeed_if_same_string (ret, "");
     506           3 :         keyDel (key);
     507             : 
     508           3 :         key = keyNew ("spec:/", KEY_END);
     509           3 :         succeed_if_same_string (keyName (key), "spec:/");
     510           3 :         succeed_if (keyGetNameSize (key) == 7, "name length checking");
     511           3 :         succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
     512           3 :         succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
     513           3 :         succeed_if_same_string (ret, "");
     514           3 :         succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
     515           3 :         succeed_if_same_string (ret, "");
     516           3 :         keyDel (key);
     517             : 
     518           3 :         key = keyNew ("spec:/", KEY_END);
     519           3 :         succeed_if_same_string (keyName (key), "spec:/");
     520           3 :         succeed_if (keyGetNameSize (key) == 7, "name length checking");
     521           3 :         succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
     522           3 :         succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
     523           3 :         succeed_if_same_string (ret, "");
     524           3 :         succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
     525           3 :         succeed_if_same_string (ret, "");
     526           3 :         keyDel (key);
     527             : 
     528           3 :         key = keyNew ("meta:/", KEY_END);
     529           3 :         succeed_if_same_string (keyName (key), "meta:/");
     530           3 :         succeed_if (keyGetNameSize (key) == 7, "name length checking");
     531           3 :         succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
     532           3 :         succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
     533           3 :         succeed_if_same_string (ret, "");
     534           3 :         succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
     535           3 :         succeed_if_same_string (ret, "");
     536           3 :         keyDel (key);
     537             : 
     538           3 :         key = keyNew ("default:/", KEY_END);
     539           3 :         succeed_if_same_string (keyName (key), "default:/");
     540           3 :         succeed_if (keyGetNameSize (key) == 10, "name length checking");
     541           3 :         succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
     542           3 :         succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
     543           3 :         succeed_if_same_string (ret, "");
     544           3 :         succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
     545           3 :         succeed_if_same_string (ret, "");
     546           3 :         keyDel (key);
     547             : 
     548           3 :         key = keyNew ("/", KEY_END);
     549           3 :         succeed_if_same_string (keyName (key), "/");
     550           3 :         succeed_if (keyGetNameSize (key) == 2, "name length checking");
     551           3 :         succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
     552           3 :         succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
     553           3 :         succeed_if_same_string (ret, "");
     554           3 :         succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
     555           3 :         succeed_if_same_string (ret, "");
     556           3 :         keyDel (key);
     557             : 
     558           3 :         key = keyNew ("//", KEY_END);
     559           3 :         succeed_if_same_string (keyName (key), "/");
     560           3 :         succeed_if (keyGetNameSize (key) == 2, "name length checking");
     561           3 :         succeed_if (keyGetBaseNameSize (key) == 1, "length checking");
     562           3 :         succeed_if (keyGetBaseName (key, ret, 1) == 1, "GetBaseName for root key");
     563           3 :         succeed_if_same_string (ret, "");
     564           3 :         succeed_if (keyGetBaseName (key, ret, 2) == 1, "GetBaseName for root key");
     565           3 :         succeed_if_same_string (ret, "");
     566           3 :         keyDel (key);
     567           3 : }
     568             : 
     569             : 
     570           3 : static void test_keyNameSlashes (void)
     571             : {
     572           3 :         printf ("Test Slashes in Key Name\n");
     573           3 :         char * buf;
     574           3 :         char * getBack;
     575           3 :         Key * key = 0;
     576           3 :         int i;
     577             : 
     578             : 
     579           3 :         key = keyNew ("/", KEY_END);
     580           3 :         succeed_if (keyGetNameSize (key) == 2, "empty name size");
     581           3 :         keyDel (key);
     582             : 
     583           3 :         key = keyNew ("", KEY_END);
     584           3 :         succeed_if (key == 0, "key should be null!");
     585             : 
     586           3 :         key = keyNew ("/", KEY_END);
     587           3 :         keySetName (key, "user:/");
     588           3 :         succeed_if_same_string (keyName (key), "user:/");
     589           3 :         succeed_if (keyGetNameSize (key) == 7, "empty name size");
     590             : 
     591           3 :         keySetName (key, "system:/");
     592           3 :         succeed_if_same_string (keyName (key), "system:/");
     593           3 :         succeed_if (keyGetNameSize (key) == 9, "empty name size");
     594           3 :         keyDel (key);
     595             : 
     596           3 :         key = keyNew ("/", KEY_END);
     597           3 :         keySetName (key, "system:/");
     598           3 :         succeed_if_same_string (keyName (key), "system:/");
     599           3 :         succeed_if (keyGetNameSize (key) == 9, "empty name size");
     600             : 
     601           3 :         keySetName (key, "user:/");
     602           3 :         succeed_if_same_string (keyName (key), "user:/");
     603           3 :         succeed_if (keyGetNameSize (key) == 7, "empty name size");
     604           3 :         keyDel (key);
     605             : 
     606           3 :         key = keyNew ("/", KEY_END);
     607           3 :         succeed_if (keySetName (key, "user:/") == 7, "setting user:/ generates error");
     608           3 :         succeed_if_same_string (keyName (key), "user:/");
     609           3 :         succeed_if (keyGetNameSize (key) == 7, "empty name size");
     610           3 :         keyDel (key);
     611             : 
     612           3 :         key = keyNew ("/", KEY_END);
     613           3 :         succeed_if (keySetName (key, "no") == -1, "no error code setting invalid name");
     614           3 :         succeed_if_same_string (keyName (key), "/");
     615           3 :         succeed_if (keyGetNameSize (key) == 2, "empty name size");
     616           3 :         keyDel (key);
     617             : 
     618           3 :         key = keyNew ("user:/noname", KEY_END);
     619           3 :         succeed_if (keyGetNameSize (key) == 13, "size not correct after keyNew");
     620           3 :         getBack = elektraMalloc (13);
     621           3 :         succeed_if (keyGetName (key, getBack, 13), "could not get name");
     622           3 :         succeed_if_same_string (getBack, "user:/noname");
     623           3 :         elektraFree (getBack);
     624             : 
     625           3 :         keySetName (key, "user:/noname");
     626           3 :         succeed_if (keyGetNameSize (key) == 13, "size not correct after keySetName");
     627           3 :         getBack = elektraMalloc (13);
     628           3 :         succeed_if (keyGetName (key, getBack, 13), "could not get name");
     629           3 :         succeed_if_same_string (getBack, "user:/noname");
     630           3 :         elektraFree (getBack);
     631             : 
     632           3 :         keySetName (key, "no");
     633           3 :         succeed_if (keyGetNameSize (key) == 13, "size not correct after keySetName");
     634           3 :         getBack = elektraMalloc (13);
     635           3 :         succeed_if (keyGetName (key, getBack, 13), "could not get name");
     636           3 :         succeed_if_same_string (getBack, "user:/noname");
     637           3 :         elektraFree (getBack);
     638           3 :         keyDel (key);
     639             : 
     640           3 :         key = keyNew ("user:/noname", KEY_END);
     641             : 
     642           3 :         keySetName (key, "user://hidden");
     643           3 :         succeed_if_same_string (keyName (key), "user:/hidden");
     644           3 :         succeed_if (keyGetNameSize (key) == 13, "name size minus slashes");
     645             : 
     646           3 :         keySetName (key, "user:///hidden");
     647           3 :         succeed_if_same_string (keyName (key), "user:/hidden");
     648           3 :         succeed_if (keyGetNameSize (key) == 13, "name size minus slashes");
     649             : 
     650           3 :         keySetName (key, "user:////////////////////////////////////hidden");
     651           3 :         succeed_if_same_string (keyName (key), "user:/hidden");
     652           3 :         succeed_if (keyGetNameSize (key) == 13, "name size minus slashes");
     653             : 
     654           3 :         printf ("Test trailing Slashes in Key Name\n");
     655           3 :         keySetName (key, "user://hidden/");
     656           3 :         succeed_if_same_string (keyName (key), "user:/hidden");
     657           3 :         succeed_if (keyGetNameSize (key) == 13, "name size minus slashes");
     658             : 
     659           3 :         keySetName (key, "user://hidden//");
     660           3 :         succeed_if_same_string (keyName (key), "user:/hidden");
     661           3 :         succeed_if (keyGetNameSize (key) == 13, "name size minus slashes");
     662             : 
     663           3 :         keySetName (key, "user://hidden///////");
     664           3 :         succeed_if_same_string (keyName (key), "user:/hidden");
     665           3 :         succeed_if (keyGetNameSize (key) == 13, "name size minus slashes");
     666             : 
     667           3 :         keySetName (key, "user:/");
     668           3 :         succeed_if_same_string (keyName (key), "user:/");
     669             : 
     670           3 :         keySetName (key, "user:/");
     671           3 :         succeed_if_same_string (keyName (key), "user:/");
     672             : 
     673           3 :         keySetName (key, "user:/a");
     674           3 :         succeed_if_same_string (keyName (key), "user:/a");
     675             : 
     676           3 :         keySetName (key, "user://");
     677           3 :         succeed_if_same_string (keyName (key), "user:/");
     678             : 
     679           3 :         keySetName (key, "user://///////");
     680           3 :         succeed_if_same_string (keyName (key), "user:/");
     681             : 
     682           3 :         printf ("Test Dots in Key Name\n");
     683           3 :         keySetName (key, "user:/hidden/.");
     684           3 :         succeed_if_same_string (keyName (key), "user:/hidden");
     685             : 
     686           3 :         keySetName (key, "user:/.");
     687           3 :         succeed_if_same_string (keyName (key), "user:/");
     688             : 
     689           3 :         keySetName (key, "user:/./hidden");
     690           3 :         succeed_if_same_string (keyName (key), "user:/hidden");
     691             : 
     692           3 :         keySetName (key, "user:/.valid/.");
     693           3 :         succeed_if_same_string (keyName (key), "user:/.valid");
     694             : 
     695           3 :         keySetName (key, "user:/./.valid");
     696           3 :         succeed_if_same_string (keyName (key), "user:/.valid");
     697             : 
     698           3 :         keySetName (key, "user:/./.valid/.");
     699           3 :         succeed_if_same_string (keyName (key), "user:/.valid");
     700             : 
     701           3 :         keySetName (key, "user:/././././.valid/././././.");
     702           3 :         succeed_if_same_string (keyName (key), "user:/.valid");
     703             : 
     704           3 :         printf ("Test Double Dots in Key Name\n");
     705           3 :         keySetName (key, "user:/hidden/parent/..");
     706           3 :         succeed_if_same_string (keyName (key), "user:/hidden");
     707             : 
     708           3 :         keySetName (key, "user:/hidden/..");
     709           3 :         succeed_if_same_string (keyName (key), "user:/");
     710             : 
     711           3 :         keySetName (key, "user:/..");
     712           3 :         succeed_if_same_string (keyName (key), "user:/");
     713             : 
     714           3 :         keySetName (key, "user:/hidden/../..");
     715             :         // printf ("Name: %s\n", keyName(key));
     716           3 :         succeed_if_same_string (keyName (key), "user:/");
     717             : 
     718           3 :         succeed_if (keySetName (key, "user:///sw/../sw//././MyApp") == sizeof ("user:/sw/MyApp"), "could not set keySet example");
     719             :         // printf("%s %d\n", keyName(key), keyGetNameSize(key));
     720           3 :         succeed_if_same_string (keyName (key), "user:/sw/MyApp");
     721           3 :         succeed_if (keyGetNameSize (key) == sizeof ("user:/sw/MyApp"), "incorrect length for keySet example");
     722             : 
     723           3 :         printf ("Test Mixed Dots and Slashes in Key Name\n");
     724           3 :         keySetName (key, "user:/hidden/../.");
     725             : 
     726           3 :         keyDel (key);
     727             : 
     728             : 
     729           3 :         printf ("Test failure key creation\n");
     730             : 
     731           3 :         key = keyNew ("invalid", KEY_END);
     732           3 :         succeed_if (key == 0, "should be null");
     733           3 :         succeed_if (keyDel (key) == -1, "keyDel: should fail");
     734             : 
     735           3 :         key = keyNew ("nonhere/valid/there", KEY_END);
     736           3 :         succeed_if (key == 0, "should be null");
     737           3 :         succeed_if (keyDel (key) == -1, "keyDel: should fail");
     738             : 
     739           3 :         key = keyNew ("nonhere:y/valid/there", KEY_END);
     740           3 :         succeed_if (key == 0, "should be null");
     741           3 :         succeed_if (keyDel (key) == -1, "keyDel: should fail");
     742             : 
     743           3 :         key = keyNew ("user:/validname", KEY_END);
     744           3 :         succeed_if (key != NULL, "keyNew: Unable to create a key with name");
     745           3 :         succeed_if_same_string (keyName (key), "user:/validname");
     746             : 
     747           3 :         keySetName (key, "user:/validname");
     748           3 :         succeed_if_same_string (keyName (key), "user:/validname");
     749             : 
     750           3 :         keySetName (key, "user:/validname\\/t");
     751           3 :         succeed_if_same_string (keyName (key), "user:/validname\\/t");
     752             : 
     753             : #ifdef COMPAT
     754             :         keySetName (key, "user:/validname\\");
     755             :         succeed_if_same_string (keyName (key), "user:/validname\\");
     756             : #endif
     757             : 
     758           3 :         keySetName (key, "user:/validname\\/");
     759           3 :         succeed_if_same_string (keyName (key), "user:/validname\\/");
     760           3 :         succeed_if (keyDel (key) == 0, "keyDel: Unable to delete key with name");
     761             : 
     762           3 :         printf ("Test key's name manipulation\n");
     763             : 
     764           3 :         Key * copy = keyNew ("/", KEY_END);
     765             : 
     766          21 :         for (i = 0; tstKeyName[i].testName != NULL; i++)
     767             :         {
     768          18 :                 key = keyNew (tstKeyName[i].keyName, KEY_END);
     769             : 
     770          18 :                 succeed_if (keyGetRef (copy) == 0, "reference of copy not correct");
     771          18 :                 keyCopy (copy, key, KEY_CP_ALL);
     772          18 :                 succeed_if (keyGetRef (copy) == 0, "reference of copy not correct");
     773             : 
     774          18 :                 Key * dup = keyDup (key, KEY_CP_ALL);
     775          18 :                 succeed_if (keyGetRef (dup) == 0, "reference of dup not correct");
     776             : 
     777          18 :                 compare_key (copy, key);
     778          18 :                 compare_key (copy, dup);
     779          18 :                 compare_key (dup, key);
     780             : 
     781             :                 /* keyName */
     782          18 :                 succeed_if_same_string (keyName (key), tstKeyName[i].expectedKeyName);
     783          18 :                 succeed_if_same_string (keyName (copy), tstKeyName[i].expectedKeyName);
     784          18 :                 succeed_if_same_string (keyName (dup), tstKeyName[i].expectedKeyName);
     785             : 
     786             :                 /* keyBaseName */
     787          18 :                 succeed_if_same_string (keyBaseName (key), tstKeyName[i].expectedBaseName);
     788          18 :                 succeed_if_same_string (keyBaseName (copy), tstKeyName[i].expectedBaseName);
     789          18 :                 succeed_if_same_string (keyBaseName (dup), tstKeyName[i].expectedBaseName);
     790             : 
     791             :                 /* keyGetBaseNameSize */
     792          18 :                 size_t size = keyGetBaseNameSize (key);
     793          18 :                 succeed_if ((size == strlen (tstKeyName[i].expectedBaseName) + 1), "keyGetBaseNameSize");
     794             : 
     795             :                 /* keyGetBaseName */
     796          18 :                 size = keyGetBaseNameSize (key) + 1;
     797          18 :                 buf = elektraMalloc (size * sizeof (char));
     798          18 :                 keyGetBaseName (key, buf, size);
     799          18 :                 succeed_if_same_string (buf, tstKeyName[i].expectedBaseName);
     800          18 :                 elektraFree (buf);
     801             : 
     802             :                 /* keyGetNameSize */
     803          18 :                 size = keyGetNameSize (key);
     804          18 :                 succeed_if ((size == strlen (tstKeyName[i].expectedKeyName) + 1), "keyGetKeyNameSize");
     805             : 
     806             :                 /* keyGetName */
     807          18 :                 size = keyGetNameSize (key);
     808          18 :                 buf = elektraMalloc (size * sizeof (char));
     809          18 :                 keyGetName (key, buf, size);
     810          18 :                 succeed_if_same_string (buf, tstKeyName[i].expectedKeyName);
     811          18 :                 elektraFree (buf);
     812             : 
     813          18 :                 keyDel (key);
     814          18 :                 keyDel (dup);
     815             :         }
     816           3 :         keyDel (copy);
     817           3 : }
     818             : 
     819             : 
     820           3 : static void test_keyValue (void)
     821             : {
     822           3 :         Key * key;
     823           3 :         char ret[1000];
     824           3 :         size_t i;
     825           3 :         char testString[] = "teststring";
     826           3 :         char testBinary[] = "\0tes\1tbinary";
     827           3 :         testBinary[sizeof (testBinary) - 1] = 'T';
     828             : 
     829           3 :         printf ("Test value of keys\n");
     830             : 
     831           3 :         succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
     832           3 :         succeed_if (keyGetValueSize (key) == 1, "empty value size");
     833           3 :         succeed_if (keySetString (key, "perfectvalue") == 13, "could not set string");
     834           3 :         succeed_if (keyGetValueSize (key) == 13, "value size not correct");
     835           3 :         succeed_if_same_string (keyValue (key), "perfectvalue");
     836           3 :         succeed_if (keySetString (key, "perfectvalue") == 13, "could not re-set same string");
     837           3 :         succeed_if_same_string (keyValue (key), "perfectvalue");
     838           3 :         succeed_if (keySetString (key, "nearperfectvalue") == 17, "could not re-set other string");
     839           3 :         succeed_if (keyGetValueSize (key) == 17, "value size not correct");
     840           3 :         succeed_if_same_string (keyValue (key), "nearperfectvalue");
     841           3 :         succeed_if (keyGetString (key, ret, keyGetValueSize (key) >= 999 ? 999 : keyGetValueSize (key)) == 17, "could not get string");
     842           3 :         succeed_if_same_string (ret, "nearperfectvalue");
     843           3 :         succeed_if (keyDel (key) == 0, "could not delete key");
     844             : 
     845           3 :         succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
     846           3 :         succeed_if_same_string (keyValue (key), "");
     847           3 :         succeed_if (keyGetValueSize (key) == 1, "Empty value size problem");
     848           3 :         succeed_if (keySetString (key, "") == 1, "could not set empty string");
     849           3 :         succeed_if_same_string (keyValue (key), "");
     850           3 :         succeed_if (keyGetValueSize (key) == 1, "Empty value size problem");
     851           3 :         succeed_if (keyGetString (key, ret, 0) == -1, "Could not get empty value");
     852           3 :         succeed_if (keyGetString (key, ret, 1) == 1, "Could not get empty value");
     853           3 :         succeed_if (ret[0] == 0, "keyGetValue did not return empty value");
     854           3 :         succeed_if (keyDel (key) == 0, "could not delete key");
     855             : 
     856           3 :         succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
     857           3 :         succeed_if (keySetString (key, "a long long string") == 19, "could not set string");
     858           3 :         succeed_if (keyGetString (key, ret, 6) == -1, "string not truncated");
     859           3 :         succeed_if (keyGetBinary (key, ret, 999) == -1, "binary not mismatch");
     860           3 :         succeed_if (keyDel (key) == 0, "could not delete key");
     861             : 
     862           3 :         succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
     863           3 :         succeed_if (keySetBinary (key, "a", 1) == 1, "could not set binary");
     864           3 :         succeed_if (keyIsString (key) == 0, "is not a string");
     865           3 :         succeed_if (keyIsBinary (key) == 1, "is not a string");
     866           3 :         succeed_if (keyGetBinary (key, ret, 1) == 1, "binary not truncated");
     867           3 :         succeed_if (!strncmp (ret, "a", 1), "binary value wrong");
     868           3 :         succeed_if (keyGetString (key, ret, 999) == -1, "string not mismatch");
     869           3 :         succeed_if (keySetString (key, 0) == 1, "wrong error code for SetString");
     870           3 :         succeed_if (keyDel (key) == 0, "could not delete key");
     871             : 
     872           3 :         succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
     873           3 :         succeed_if (keySetBinary (key, NULL, 0) == 0, "could not set null binary");
     874           3 :         succeed_if (keyIsString (key) == 0, "is not a string");
     875           3 :         succeed_if (keyIsBinary (key) == 1, "is not a string");
     876           3 :         succeed_if (keyGetValueSize (key) == 0, "Empty value size problem");
     877           3 :         succeed_if (keyDel (key) == 0, "could not delete key");
     878             : 
     879           3 :         succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
     880           3 :         succeed_if (keySetString (key, "") == 1, "could not set empty string");
     881           3 :         succeed_if (keyIsString (key) == 1, "is not a string");
     882           3 :         succeed_if (keyIsBinary (key) == 0, "is a binary");
     883           3 :         succeed_if (keyGetValueSize (key) == 1, "Empty value size problem");
     884           3 :         succeed_if (keyDel (key) == 0, "could not delete key");
     885             : 
     886           3 :         succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
     887           3 :         succeed_if (keySetBinary (key, "a long long binary", 19) == 19, "could not set string");
     888           3 :         succeed_if (keyIsString (key) == 0, "is not a string");
     889           3 :         succeed_if (keyIsBinary (key) == 1, "is not a string");
     890           3 :         succeed_if (keyGetBinary (key, ret, 6) == -1, "binary not truncated");
     891           3 :         succeed_if (keyGetBinary (key, ret, 19) == 19, "could not get binary");
     892           3 :         succeed_if (!strncmp (ret, "a long long binary", 19), "binary value wrong");
     893           3 :         succeed_if (keyGetString (key, ret, 999) == -1, "string not mismatch");
     894           3 :         succeed_if (keyDel (key) == 0, "could not delete key");
     895             : 
     896           3 :         succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
     897         765 :         for (i = 1; i < 255; i++)
     898             :         {
     899         762 :                 ret[0] = i;
     900         762 :                 ret[1] = i;
     901         762 :                 ret[2] = 0;
     902             :                 // output_key (key);
     903         762 :                 succeed_if (keySetString (key, ret) == 3, "could not set string");
     904         762 :                 succeed_if_same_string (keyValue (key), ret);
     905             :         }
     906           3 :         succeed_if (keyDel (key) == 0, "could not delete key");
     907             : 
     908             : 
     909           3 :         succeed_if (key = keyNew ("/", KEY_END), "could not create new key");
     910         768 :         for (i = 0; i < 255; i++)
     911             :         {
     912         765 :                 ret[0] = i;
     913         765 :                 ret[1] = 255 - i;
     914         765 :                 ret[2] = i;
     915             :                 // output_key (key);
     916         765 :                 succeed_if (keySetBinary (key, ret, 3) == 3, "could not set string");
     917         765 :                 succeed_if (memcmp (keyValue (key), ret, 3) == 0, "String not same as set");
     918             :         }
     919           3 :         succeed_if (keyDel (key) == 0, "could not delete key");
     920             : 
     921             : 
     922           3 :         printf ("Test string of key\n");
     923             : 
     924           3 :         succeed_if (keyIsString (0) == -1, "no error on NULL key");
     925             : 
     926           3 :         succeed_if (keyValue (0) == 0, "null pointer");
     927           3 :         succeed_if (keyGetValueSize (0) == -1, "null pointer");
     928           3 :         succeed_if (keySetString (0, "") == -1, "null pointer");
     929           3 :         succeed_if_same_string (keyString (0), "(null)");
     930             : 
     931           3 :         key = keyNew ("/", KEY_END);
     932           3 :         succeed_if (keyGetValueSize (key) == 1, "empty value size");
     933           3 :         succeed_if_same_string (keyString (key), "");
     934             : 
     935           3 :         keySetString (key, testString);
     936           3 :         succeed_if (keyGetString (0, ret, 100) == -1, "null pointer");
     937           3 :         succeed_if (keyGetString (key, 0, 100) == -1, "string null pointer");
     938           3 :         succeed_if (keyGetString (key, ret, 0) == -1, "length checking");
     939           3 :         succeed_if_same_string (keyString (key), testString);
     940             : 
     941          33 :         for (i = 1; i < sizeof (testString); i++)
     942             :         {
     943          30 :                 succeed_if (keyGetString (key, ret, i) == -1, "length checking too short");
     944             :         }
     945          36 :         for (i = sizeof (testString); i < sizeof (testString) * 2; i++)
     946             :         {
     947          33 :                 succeed_if (keyGetString (key, ret, i) == sizeof (testString), "length checking longer");
     948             :         }
     949           3 :         succeed_if (keyGetString (key, ret, (size_t) -1) == -1, "maxSize exceeded");
     950             : 
     951           3 :         succeed_if (keySetString (key, 0) == 1, "delete string");
     952           3 :         succeed_if (keyGetString (key, ret, i) == 1, "length checking deleting");
     953           3 :         succeed_if_same_string (ret, "");
     954             : 
     955           3 :         succeed_if (keySetString (key, testString) == sizeof (testString), "set string");
     956           3 :         succeed_if (keyGetString (key, ret, i) == sizeof (testString), "length checking working");
     957           3 :         succeed_if_same_string (ret, testString);
     958             : 
     959           3 :         succeed_if (keySetString (key, "") == 1, "delete string");
     960           3 :         succeed_if (keyGetString (key, ret, i) == 1, "length checking deleting");
     961           3 :         succeed_if_same_string (ret, "");
     962             : 
     963           3 :         succeed_if (keySetString (key, testString) == sizeof (testString), "set string");
     964           3 :         succeed_if (keyGetString (key, ret, i) == sizeof (testString), "length checking working");
     965           3 :         succeed_if_same_string (ret, testString);
     966             : 
     967           3 :         succeed_if (keyGetValueSize (key) == sizeof (testString), "testString value size");
     968           3 :         succeed_if (strncmp (keyValue (key), testString, sizeof (testString)) == 0, "testString not same");
     969           3 :         keyDel (key);
     970             : 
     971             : 
     972           3 :         printf ("Test binary of key\n");
     973             : 
     974           3 :         succeed_if (keyValue (0) == 0, "null pointer");
     975           3 :         succeed_if (keyGetValueSize (0) == -1, "null pointer");
     976           3 :         succeed_if (keySetBinary (0, "", 1) == -1, "null pointer");
     977             : 
     978           3 :         key = keyNew ("/", KEY_END);
     979           3 :         succeed_if (keySetBinary (key, "", 0) == -1, "null size");
     980           3 :         succeed_if (keySetBinary (key, "b", 0) == -1, "null size");
     981           3 :         succeed_if (keySetBinary (key, "", SIZE_MAX) == -1, "max size");
     982           3 :         succeed_if (keySetBinary (key, "b", SIZE_MAX) == -1, "max size");
     983           3 :         succeed_if (keyGetValueSize (key) == 1, "empty value size");
     984             : 
     985           3 :         keySetBinary (key, testBinary, sizeof (testBinary));
     986           3 :         succeed_if (keyGetBinary (0, ret, 100) == -1, "null pointer");
     987           3 :         succeed_if (keyGetBinary (key, 0, 100) == -1, "binary null pointer");
     988           3 :         succeed_if (keyGetBinary (key, ret, 0) == -1, "length checking");
     989             : 
     990          39 :         for (i = 1; i < sizeof (testBinary); i++)
     991             :         {
     992          36 :                 succeed_if (keyGetBinary (key, ret, i) == -1, "length checking too short");
     993             :         }
     994          42 :         for (i = sizeof (testBinary); i < sizeof (testBinary) * 2; i++)
     995             :         {
     996          39 :                 succeed_if (keyGetBinary (key, ret, i) == sizeof (testBinary), "length checking longer");
     997             :         }
     998           3 :         succeed_if (keyGetBinary (key, ret, (size_t) -1) == -1, "maxSize exceeded");
     999             : 
    1000           3 :         succeed_if (keySetBinary (key, 0, 0) == 0, "delete binary");
    1001           3 :         succeed_if (keyGetBinary (key, ret, i) == 0, "length checking deleting");
    1002             : 
    1003           3 :         succeed_if (keySetBinary (key, testBinary, sizeof (testBinary)) == sizeof (testBinary), "set binary");
    1004           3 :         succeed_if (keyGetBinary (key, ret, i) == sizeof (testBinary), "length checking working");
    1005           3 :         succeed_if_same_string (ret, testBinary);
    1006             : 
    1007           3 :         succeed_if (keySetBinary (key, 0, 1) == 0, "delete binary");
    1008           3 :         succeed_if (keyGetBinary (key, ret, i) == 0, "length checking deleting");
    1009             : 
    1010           3 :         succeed_if (keySetBinary (key, testBinary, sizeof (testBinary)) == sizeof (testBinary), "set binary");
    1011           3 :         succeed_if (keyGetBinary (key, ret, i) == sizeof (testBinary), "length checking working");
    1012           3 :         succeed_if_same_string (ret, testBinary);
    1013             : 
    1014           3 :         succeed_if (keySetBinary (key, "", 1) == 1, "delete binary the string way");
    1015           3 :         succeed_if (keyGetBinary (key, ret, i) == 1, "length checking deleting string way");
    1016           3 :         succeed_if_same_string (ret, "");
    1017             : 
    1018           3 :         succeed_if (keySetBinary (key, testBinary, sizeof (testBinary)) == sizeof (testBinary), "set binary");
    1019           3 :         succeed_if (keyGetBinary (key, ret, i) == sizeof (testBinary), "length checking working");
    1020           3 :         succeed_if_same_string (ret, testBinary);
    1021             : 
    1022           3 :         succeed_if (keyGetValueSize (key) == sizeof (testBinary), "testBinary value size");
    1023           3 :         succeed_if (strncmp (keyValue (key), testBinary, sizeof (testBinary)) == 0, "testBinary not same");
    1024           3 :         keyDel (key);
    1025           3 : }
    1026             : 
    1027           3 : static void test_keyBinary (void)
    1028             : {
    1029           3 :         Key * key = 0;
    1030           3 :         char ret[1000];
    1031           3 :         int i;
    1032           3 :         char binaryData[] = "\0binary \1\34data";
    1033           3 :         binaryData[sizeof (binaryData) - 1] = 'T';
    1034             : 
    1035           3 :         printf ("Test binary special cases\n");
    1036             : 
    1037           3 :         succeed_if (keyIsBinary (0) == -1, "no error on checking NULL Key");
    1038             : 
    1039           3 :         key = keyNew ("user:/binary", KEY_BINARY, KEY_END);
    1040             : 
    1041           3 :         succeed_if (keyIsBinary (key) == 1, "should be binary");
    1042           3 :         succeed_if (keyIsString (key) == 0, "should not be string");
    1043           3 :         succeed_if (keyGetValueSize (key) == 0, "size not correct");
    1044           3 :         succeed_if (keyValue (key) == 0, "should be null key");
    1045           3 :         succeed_if (keyGetString (key, ret, 1000) == -1, "should be type mismatch");
    1046             : 
    1047           3 :         keyDel (key);
    1048             : 
    1049           3 :         key = keyNew ("user:/binary", KEY_BINARY, KEY_SIZE, sizeof (binaryData), KEY_VALUE, binaryData, KEY_END);
    1050             : 
    1051           3 :         succeed_if (keyIsBinary (key) == 1, "should be binary");
    1052           3 :         succeed_if (keyIsString (key) == 0, "should not be string");
    1053           3 :         succeed_if (keyGetValueSize (key) == sizeof (binaryData), "size not correct");
    1054           3 :         succeed_if (memcmp (binaryData, keyValue (key), sizeof (binaryData)) == 0, "memcmp");
    1055           3 :         succeed_if (keyGetBinary (key, ret, 1000) == sizeof (binaryData), "could not get binary data");
    1056           3 :         succeed_if (memcmp (binaryData, ret, sizeof (binaryData)) == 0, "memcmp");
    1057           3 :         succeed_if (keyGetString (key, ret, 1000) == -1, "should be type mismatch");
    1058           3 :         succeed_if_same_string (keyString (key), "(binary)");
    1059             : 
    1060           3 :         keyDel (key);
    1061             : 
    1062           3 :         key = keyNew ("/", KEY_END);
    1063           3 :         keySetBinary (key, binaryData, sizeof (binaryData));
    1064             : 
    1065           3 :         succeed_if (keyIsBinary (key) == 1, "should be binary");
    1066           3 :         succeed_if (keyIsString (key) == 0, "should not be string");
    1067           3 :         succeed_if (keyGetValueSize (key) == sizeof (binaryData), "size not correct");
    1068           3 :         succeed_if (memcmp (binaryData, keyValue (key), sizeof (binaryData)) == 0, "memcmp");
    1069           3 :         succeed_if (keyGetBinary (key, ret, 1000) == sizeof (binaryData), "could not get binary data");
    1070           3 :         succeed_if (memcmp (binaryData, ret, sizeof (binaryData)) == 0, "memcmp");
    1071           3 :         succeed_if (keyGetString (key, ret, 1000) == -1, "should be type mismatch");
    1072           3 :         succeed_if_same_string (keyString (key), "(binary)");
    1073             : 
    1074           3 :         keyDel (key);
    1075             : 
    1076           3 :         key = keyNew ("/", KEY_END);
    1077           3 :         keySetBinary (key, 0, 0);
    1078             : 
    1079           3 :         succeed_if (keyIsBinary (key) == 1, "should be binary");
    1080           3 :         succeed_if (keyIsString (key) == 0, "should not be string");
    1081           3 :         succeed_if (keyGetValueSize (key) == 0, "size not correct");
    1082           3 :         succeed_if (keyValue (key) == 0, "should be null pointer");
    1083           3 :         succeed_if (keyGetBinary (key, ret, 1000) == 0, "should write nothing because of no data");
    1084           3 :         succeed_if (keyGetString (key, ret, 1000) == -1, "should be type mismatch");
    1085             : 
    1086           3 :         keyDel (key);
    1087             : 
    1088           3 :         key = keyNew ("/", KEY_END);
    1089           3 :         keySetBinary (key, 0, 1);
    1090             : 
    1091           3 :         succeed_if (keyIsBinary (key) == 1, "should be binary");
    1092           3 :         succeed_if (keyIsString (key) == 0, "should not be string");
    1093           3 :         succeed_if (keyGetValueSize (key) == 0, "size not correct");
    1094           3 :         succeed_if (keyValue (key) == 0, "should be null pointer");
    1095           3 :         succeed_if (keyGetBinary (key, ret, 1000) == 0, "should write nothing because of no data");
    1096           3 :         succeed_if (keyGetString (key, ret, 1000) == -1, "should be type mismatch");
    1097             : 
    1098           3 :         keyDel (key);
    1099             : 
    1100           3 :         key = keyNew ("/", KEY_END);
    1101           3 :         keySetBinary (key, "", 1);
    1102           3 :         succeed_if (keySetBinary (key, 0, SIZE_MAX) == -1, "should do nothing and fail");
    1103           3 :         succeed_if (keySetBinary (key, 0, SSIZE_MAX) == 0, "should free data");
    1104             : 
    1105           3 :         succeed_if (keyIsBinary (key) == 1, "should be binary");
    1106           3 :         succeed_if (keyIsString (key) == 0, "should not be string");
    1107           3 :         succeed_if (keyGetValueSize (key) == 0, "size not correct");
    1108           3 :         succeed_if (keyValue (key) == 0, "should be null pointer");
    1109           3 :         succeed_if (keyGetBinary (key, ret, 1000) == 0, "should write nothing because of no data");
    1110           3 :         succeed_if (keyGetString (key, ret, 1000) == -1, "should be type mismatch");
    1111             : 
    1112           3 :         keyDel (key);
    1113             : 
    1114           3 :         key = keyNew ("/", KEY_END);
    1115           3 :         keySetBinary (key, "", 1);
    1116             : 
    1117           3 :         succeed_if (keyIsBinary (key) == 1, "should be binary");
    1118           3 :         succeed_if (keyIsString (key) == 0, "should not be string");
    1119           3 :         succeed_if (keyGetValueSize (key) == 1, "size not correct");
    1120           3 :         succeed_if (memcmp (binaryData, keyValue (key), 1) == 0, "memcmp");
    1121           3 :         succeed_if (keyGetBinary (key, ret, 1000) == 1, "could not get binary data");
    1122           3 :         succeed_if (memcmp (binaryData, ret, 1) == 0, "memcmp");
    1123           3 :         succeed_if (keyGetString (key, ret, 1000) == -1, "should be type mismatch");
    1124           3 :         succeed_if_same_string (keyString (key), "(binary)");
    1125             : 
    1126           3 :         keyDel (key);
    1127             : 
    1128           3 :         key = keyNew ("/", KEY_END);
    1129           3 :         i = 23;
    1130           3 :         keySetBinary (key, (void *) &i, sizeof (i));
    1131             : 
    1132           3 :         succeed_if (keyIsBinary (key) == 1, "should be binary");
    1133           3 :         succeed_if (keyIsString (key) == 0, "should not be string");
    1134           3 :         succeed_if (keyGetValueSize (key) == sizeof (i), "size not correct");
    1135           3 :         succeed_if (memcmp ((void *) &i, keyValue (key), sizeof (i)) == 0, "memcmp");
    1136           3 :         succeed_if (keyGetBinary (key, ret, 1000) == sizeof (i), "could not get binary data");
    1137           3 :         succeed_if (memcmp ((void *) &i, ret, sizeof (i)) == 0, "memcmp");
    1138           3 :         succeed_if (keyGetString (key, ret, 1000) == -1, "should be type mismatch");
    1139           3 :         succeed_if_same_string (keyString (key), "(binary)");
    1140             : 
    1141           3 :         i = *(int *) keyValue (key);
    1142           3 :         succeed_if (i == 23, "incorrect int");
    1143             : 
    1144           3 :         keyDel (key);
    1145           3 : }
    1146             : 
    1147           3 : static void test_keyBelow (void)
    1148             : {
    1149           3 :         Key * key1 = keyNew ("/", KEY_END);
    1150           3 :         Key * key2 = keyNew ("/", KEY_END);
    1151             : 
    1152           3 :         printf ("Test of relative positions of keys\n");
    1153             : 
    1154           3 :         succeed_if (keyIsBelow (key1, 0) == -1, "NULL pointer");
    1155           3 :         succeed_if (keyIsBelow (0, 0) == -1, "NULL pointer");
    1156           3 :         succeed_if (keyIsBelow (0, key1) == -1, "NULL pointer");
    1157             : 
    1158           3 :         succeed_if (keyIsDirectlyBelow (key1, 0) == -1, "NULL pointer");
    1159           3 :         succeed_if (keyIsDirectlyBelow (0, 0) == -1, "NULL pointer");
    1160           3 :         succeed_if (keyIsDirectlyBelow (0, key1) == -1, "NULL pointer");
    1161             : 
    1162           3 :         keySetName (key1, "user:/valid");
    1163           3 :         keySetName (key2, "user:/valid");
    1164           3 :         succeed_if (!keyIsBelow (key1, key2), "Key should not be below");
    1165           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1166             : 
    1167           3 :         keySetName (key1, "user:/valid");
    1168           3 :         keySetName (key2, "user:/valid/");
    1169           3 :         succeed_if (!keyIsBelow (key1, key2), "Key should not be below");
    1170           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1171             : 
    1172           3 :         keySetName (key1, "user:/valid");
    1173           3 :         keySetName (key2, "user:/valid/below");
    1174           3 :         succeed_if (keyIsBelow (key1, key2), "Key should be below");
    1175           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1176             : 
    1177           3 :         keySetName (key1, "user:/valid");
    1178           3 :         keySetName (key2, "user:/valid/b");
    1179           3 :         succeed_if (keyIsBelow (key1, key2), "Key should be below");
    1180           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1181             : 
    1182           3 :         keySetName (key1, "user:/valid");
    1183           3 :         keySetName (key2, "user:/valid/b");
    1184           3 :         succeed_if (keyIsBelow (key1, key2), "Key should be below");
    1185           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1186             : 
    1187           3 :         keySetName (key1, "user:/valid");
    1188           3 :         keySetName (key2, "user:/valid/b/e");
    1189           3 :         succeed_if (keyIsBelow (key1, key2), "Key should be below");
    1190           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1191             : 
    1192           3 :         keySetName (key1, "/valid");
    1193           3 :         keySetName (key2, "/valid/b/e");
    1194           3 :         succeed_if (keyIsBelow (key1, key2), "cascading Key should be below");
    1195           3 :         succeed_if (!keyIsBelow (key2, key1), "cascading Key should not be below");
    1196             : 
    1197           3 :         keySetName (key1, "/");
    1198           3 :         keySetName (key2, "/valid/b/e");
    1199           3 :         succeed_if (keyIsBelow (key1, key2), "cascading Key should be below");
    1200           3 :         succeed_if (!keyIsBelow (key2, key1), "cascading Key should not be below");
    1201             : 
    1202           3 :         keySetName (key1, "/");
    1203           3 :         keySetName (key2, "/v");
    1204           3 :         succeed_if (keyIsBelow (key1, key2), "cascading Key should be below");
    1205           3 :         succeed_if (!keyIsBelow (key2, key1), "cascading Key should not be below");
    1206             : 
    1207           3 :         keySetName (key1, "user:/");
    1208           3 :         keySetName (key2, "user:/");
    1209           3 :         succeed_if (!keyIsBelow (key1, key2), "root Key should not be below");
    1210           3 :         succeed_if (!keyIsBelow (key2, key1), "root Key should not be below");
    1211             : 
    1212           3 :         keySetName (key1, "dir:/");
    1213           3 :         keySetName (key2, "dir:/");
    1214           3 :         succeed_if (!keyIsBelow (key1, key2), "root Key should not be below");
    1215           3 :         succeed_if (!keyIsBelow (key2, key1), "root Key should not be below");
    1216             : 
    1217           3 :         keySetName (key1, "/");
    1218           3 :         keySetName (key2, "dir:/");
    1219           3 :         succeed_if (!keyIsBelow (key1, key2), "root Key should not be below");
    1220           3 :         succeed_if (!keyIsBelow (key2, key1), "root Key should not be below");
    1221             : 
    1222           3 :         keySetName (key1, "/");
    1223           3 :         keySetName (key2, "system:/");
    1224           3 :         succeed_if (!keyIsBelow (key1, key2), "root Key should not be below");
    1225           3 :         succeed_if (!keyIsBelow (key2, key1), "root Key should not be below");
    1226             : 
    1227           3 :         keySetName (key1, "user:/a");
    1228           3 :         keySetName (key2, "user:/a");
    1229           3 :         succeed_if (!keyIsBelow (key1, key2), "cascading Key should not be below");
    1230           3 :         succeed_if (!keyIsBelow (key2, key1), "cascading Key should not be below");
    1231             : 
    1232           3 :         keySetName (key1, "/");
    1233           3 :         keySetName (key2, "/");
    1234           3 :         succeed_if (!keyIsBelow (key1, key2), "cascading Key should not be below");
    1235           3 :         succeed_if (!keyIsBelow (key2, key1), "cascading Key should not be below");
    1236             : 
    1237           3 :         keySetName (key1, "user:/valide");
    1238           3 :         keySetName (key2, "user:/valid/e");
    1239           3 :         succeed_if (!keyIsBelow (key1, key2), "Key should not be below");
    1240           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1241             : 
    1242           3 :         keySetName (key1, "user:/valid/b");
    1243           3 :         keySetName (key2, "user:/valid/e");
    1244           3 :         succeed_if (!keyIsBelow (key1, key2), "Key should not be below");
    1245           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1246             : 
    1247           3 :         keySetName (key1, "user:/valide");
    1248           3 :         keySetName (key2, "user:/valid/valide");
    1249           3 :         succeed_if (!keyIsBelow (key1, key2), "Key should not be below");
    1250           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1251             : 
    1252           3 :         keySetName (key1, "system:/infos");
    1253           3 :         keySetName (key2, "/infos/constants");
    1254           3 :         succeed_if (keyIsBelow (key1, key2), "Key should be below");
    1255           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1256             : 
    1257           3 :         keySetName (key1, "/");
    1258           3 :         keySetName (key2, "/infos/constants");
    1259           3 :         succeed_if (keyIsBelow (key1, key2), "Key should be below");
    1260           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1261             : 
    1262           3 :         keySetName (key1, "system:/");
    1263           3 :         keySetName (key2, "/infos/constants");
    1264           3 :         succeed_if (keyIsBelow (key1, key2), "Key should be below");
    1265           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1266             : 
    1267           3 :         keySetName (key1, "/infos");
    1268           3 :         keySetName (key2, "system:/infos/constants");
    1269           3 :         succeed_if (keyIsBelow (key1, key2), "Key should be below");
    1270           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1271             : 
    1272           3 :         keySetName (key1, "system:/infos/constants");
    1273           3 :         keySetName (key2, "/infos/constants/syste");
    1274           3 :         succeed_if (keyIsBelow (key1, key2), "Key should be below");
    1275           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1276             : 
    1277           3 :         keySetName (key1, "/");
    1278           3 :         keySetName (key2, "system:/infos/constants");
    1279           3 :         succeed_if (keyIsBelow (key1, key2), "Key should be below");
    1280           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1281             : 
    1282           3 :         keySetName (key1, "/infos");
    1283           3 :         keySetName (key2, "/infos/constants");
    1284           3 :         succeed_if (keyIsBelow (key1, key2), "Key should be below");
    1285           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1286             : 
    1287           3 :         keySetName (key1, "/");
    1288           3 :         keySetName (key2, "system:/infos/constants/version/version");
    1289           3 :         succeed_if (keyIsBelow (key1, key2), "Key should be below");
    1290           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1291             : 
    1292           3 :         keySetName (key1, "user:/valid");
    1293           3 :         keySetName (key2, "user:/valid\\/e");
    1294           3 :         succeed_if (!keyIsBelow (key1, key2), "Key should not be below");
    1295           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1296             : 
    1297           3 :         keySetName (key1, "user:/valid\\/");
    1298           3 :         keySetName (key2, "user:/valid/e");
    1299           3 :         succeed_if (!keyIsBelow (key1, key2), "Key should not be below");
    1300           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1301             : 
    1302           3 :         keySetName (key1, "/valid\\/");
    1303           3 :         keySetName (key2, "user:/valid\\//valid");
    1304           3 :         succeed_if (keyIsBelow (key1, key2), "Key should be below");
    1305           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1306             : 
    1307           3 :         keySetName (key1, "/a/b/c");
    1308           3 :         keySetName (key2, "/a/b/d");
    1309           3 :         succeed_if (!keyIsBelow (key1, key2), "Key should be below");
    1310           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1311             : 
    1312           3 :         keySetName (key1, "/a");
    1313           3 :         keySetName (key2, "/b");
    1314           3 :         succeed_if (!keyIsBelow (key1, key2), "Key should be below");
    1315           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1316             : 
    1317           3 :         keySetName (key1, "/valid\\/");
    1318           3 :         keySetName (key2, "user:/valid\\/valid");
    1319           3 :         succeed_if (!keyIsBelow (key1, key2), "Key should be below");
    1320           3 :         succeed_if (!keyIsBelow (key2, key1), "Key should not be below");
    1321             : 
    1322             : 
    1323           3 :         keySetName (key1, "user:/valid");
    1324           3 :         keySetName (key2, "user:/valid/valide");
    1325           3 :         succeed_if (keyIsDirectlyBelow (key1, key2), "Key should be below");
    1326           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1327             : 
    1328           3 :         keySetName (key1, "user:/valid");
    1329           3 :         keySetName (key2, "user:/valid/non/valid");
    1330           3 :         succeed_if (!keyIsDirectlyBelow (key1, key2), "Key should not be below");
    1331           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1332             : 
    1333           3 :         keySetName (key1, "user:/valid/a");
    1334           3 :         keySetName (key2, "user:/valid/b");
    1335           3 :         succeed_if (!keyIsDirectlyBelow (key1, key2), "Key should not be below");
    1336           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1337             : 
    1338           3 :         keySetName (key1, "user:/valid\\/");
    1339           3 :         keySetName (key2, "user:/valid\\a");
    1340           3 :         succeed_if (!keyIsDirectlyBelow (key1, key2), "Key should not be below");
    1341           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1342             : 
    1343           3 :         keySetName (key1, "user:/valid\\/");
    1344           3 :         keySetName (key2, "user:/valid\\/a");
    1345           3 :         succeed_if (!keyIsDirectlyBelow (key1, key2), "Key should not be below");
    1346           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1347             : 
    1348           3 :         keySetName (key1, "user:/valid\\/");
    1349           3 :         keySetName (key2, "user:/valid\\/");
    1350           3 :         succeed_if (!keyIsDirectlyBelow (key1, key2), "Key should not be below");
    1351           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1352             : 
    1353           3 :         keySetName (key1, "user:/valid\\/");
    1354           3 :         keySetName (key2, "user:/valid\\/a");
    1355           3 :         succeed_if (!keyIsDirectlyBelow (key1, key2), "Key should not be below");
    1356           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1357             : 
    1358           3 :         keySetName (key2, "user:/valid\\/a");
    1359           3 :         succeed_if (!keyIsDirectlyBelow (key1, key2), "Key should not be below");
    1360           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1361             : 
    1362           3 :         keySetName (key1, "user:/valid\\/");
    1363           3 :         keySetName (key2, "user:/valid\\/\\/");
    1364           3 :         succeed_if (!keyIsDirectlyBelow (key1, key2), "Key should not be below");
    1365           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1366             : 
    1367           3 :         keySetName (key1, "user:/valid\\/");
    1368           3 :         keySetName (key2, "user:/valid\\/ab");
    1369           3 :         succeed_if (!keyIsDirectlyBelow (key1, key2), "Key should not be below");
    1370           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1371             : 
    1372           3 :         keySetName (key1, "user:/valid");
    1373           3 :         keySetName (key2, "user:/valid");
    1374           3 :         succeed_if (!keyIsDirectlyBelow (key1, key2), "Key should not be below");
    1375           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1376             : 
    1377           3 :         keySetName (key1, "user:/valid/a/b");
    1378           3 :         keySetName (key2, "user:/valid");
    1379           3 :         succeed_if (!keyIsDirectlyBelow (key1, key2), "Key should not be below");
    1380           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1381             : 
    1382           3 :         keySetName (key1, "user:/valid/a");
    1383           3 :         keySetName (key2, "user:/valid");
    1384           3 :         succeed_if (!keyIsDirectlyBelow (key1, key2), "Key should not be below");
    1385           3 :         succeed_if (keyIsDirectlyBelow (key2, key1), "Key should be below");
    1386             : 
    1387           3 :         keySetName (key1, "user:/tests/ini-section-write");
    1388           3 :         keySetName (key2, "user:/tests/ini-section-write/akey\\/looking\\/like\\/sections");
    1389           3 :         succeed_if (keyIsDirectlyBelow (key1, key2), "looking like sections not recognised");
    1390           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1391             : 
    1392           3 :         keySetName (key1, "user:/valid");
    1393           3 :         keySetName (key2, "/valid/valide");
    1394           3 :         succeed_if (keyIsDirectlyBelow (key1, key2), "Key should be below");
    1395           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1396             : 
    1397           3 :         keySetName (key1, "user:/valid");
    1398           3 :         keySetName (key2, "/valid/non/valid");
    1399           3 :         succeed_if (!keyIsDirectlyBelow (key1, key2), "Key should not be below");
    1400           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1401             : 
    1402           3 :         keySetName (key1, "/valid");
    1403           3 :         keySetName (key2, "user:/valid/valide");
    1404           3 :         succeed_if (keyIsDirectlyBelow (key1, key2), "Key should be below");
    1405           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1406             : 
    1407           3 :         keySetName (key1, "/valid");
    1408           3 :         keySetName (key2, "user:/valid/non/valid");
    1409           3 :         succeed_if (!keyIsDirectlyBelow (key1, key2), "Key should not be below");
    1410           3 :         succeed_if (!keyIsDirectlyBelow (key2, key1), "Key should not be below");
    1411             : 
    1412             : 
    1413           3 :         keyDel (key1);
    1414           3 :         keyDel (key2);
    1415           3 : }
    1416             : 
    1417           3 : static void test_keyDup (void)
    1418             : {
    1419           3 :         Key *orig, *copy;
    1420             : 
    1421           3 :         printf ("Test key duplication\n");
    1422             : 
    1423             :         // Create test key
    1424           3 :         orig = keyNew ("user:/foo/bar", KEY_BINARY, KEY_SIZE, 6, KEY_VALUE, "foobar", KEY_META, "comment/#0", "mycomment", KEY_END);
    1425             : 
    1426             : 
    1427             :         // Dup the key
    1428           3 :         succeed_if ((copy = keyDup (orig, KEY_CP_ALL)) != 0, "keyDup failed");
    1429           9 :         compare_key (orig, copy);
    1430           3 :         keyDel (orig); // everything independent from original!
    1431             : 
    1432             :         // Check the duplication
    1433           3 :         succeed_if_same_string (keyName (copy), "user:/foo/bar");
    1434           3 :         succeed_if (strncmp (keyValue (copy), "foobar", 6) == 0, "keyDup: key value copy error");
    1435           3 :         succeed_if (keyIsBinary (copy), "keyDup: key type copy error");
    1436             : 
    1437             :         // Dup the key again
    1438           3 :         Key * ccopy;
    1439           3 :         succeed_if ((ccopy = keyDup (copy, KEY_CP_ALL)) != 0, "keyDup failed");
    1440           9 :         compare_key (copy, ccopy);
    1441           3 :         keyDel (copy); // everything independent from original!
    1442             : 
    1443           3 :         succeed_if_same_string (keyName (ccopy), "user:/foo/bar");
    1444           3 :         succeed_if (strncmp (keyValue (ccopy), "foobar", 6) == 0, "keyDup: key value ccopy error");
    1445           3 :         succeed_if (keyIsBinary (ccopy), "keyDup: key type ccopy error");
    1446             : 
    1447           3 :         keyDel (ccopy);
    1448             : 
    1449           3 :         orig = keyNew ("/", KEY_END);
    1450           3 :         keySetName (orig, "invalid");
    1451             : 
    1452           3 :         succeed_if ((copy = keyDup (orig, KEY_CP_ALL)) != 0, "keyDup failed");
    1453           3 :         succeed_if_same_string (keyName (orig), "/");
    1454           3 :         succeed_if_same_string (keyName (copy), "/");
    1455           3 :         succeed_if (keyGetNameSize (orig) == 2, "orig name size");
    1456           3 :         succeed_if (keyGetNameSize (copy) == 2, "orig name size");
    1457           3 :         succeed_if (keyGetRef (orig) == 0, "orig ref counter should be 0");
    1458           3 :         succeed_if (keyGetRef (copy) == 0, "copy ref counter should be 0");
    1459             : 
    1460           3 :         keyDel (orig);
    1461           3 :         keyDel (copy);
    1462           3 : }
    1463             : 
    1464           3 : static void test_keyCopy (void)
    1465             : {
    1466           3 :         Key *orig, *copy;
    1467           3 :         char origBuffer[5], copyBuffer[5];
    1468             : 
    1469           3 :         printf ("Test key copy\n");
    1470             : 
    1471             :         // Create test key
    1472           3 :         orig = keyNew ("user:/foo/bar", KEY_BINARY, KEY_SIZE, 6, KEY_VALUE, "foobar", KEY_META, "comment/#0", "mycomment", KEY_END);
    1473             : 
    1474             : 
    1475             :         // Copy the key
    1476           3 :         copy = keyNew ("/", KEY_END);
    1477           3 :         succeed_if (keyCopy (copy, orig, KEY_CP_ALL) == copy, "keyCopy failed");
    1478           3 :         succeed_if (keyGetRef (orig) == 0, "orig ref counter should be 0");
    1479           3 :         succeed_if (keyGetRef (copy) == 0, "copy ref counter should be 0");
    1480           9 :         compare_key (orig, copy);
    1481             : 
    1482           3 :         succeed_if (keyCopy (copy, orig, KEY_CP_ALL) == copy, "keyCopy failed");
    1483           3 :         succeed_if (keyGetRef (orig) == 0, "orig ref counter should be 0");
    1484           3 :         succeed_if (keyGetRef (copy) == 0, "copy ref counter should be 0");
    1485           9 :         compare_key (orig, copy);
    1486           3 :         keyDel (orig); // everything independent from original!
    1487             : 
    1488             :         // Check the duplication
    1489           3 :         succeed_if_same_string (keyName (copy), "user:/foo/bar");
    1490           3 :         succeed_if (strncmp (keyValue (copy), "foobar", 6) == 0, "keyCopy: key value copy error");
    1491             : 
    1492           3 :         orig = keyNew ("/", KEY_END);
    1493           3 :         succeed_if (keyCopy (copy, orig, KEY_CP_ALL) == copy, "make a key copy of an unmodified key");
    1494           3 :         compare_key (orig, copy);
    1495             : 
    1496           3 :         succeed_if (keyCopy (copy, 0, KEY_CP_ALL) == copy, "make the key copy fresh");
    1497           3 :         compare_key (orig, copy);
    1498           3 :         keyDel (orig);
    1499             : 
    1500           3 :         keyDel (copy);
    1501             : 
    1502           3 :         orig = keyNew ("/", KEY_END);
    1503           3 :         keySetName (orig, "invalid");
    1504             : 
    1505           3 :         copy = keyNew ("/", KEY_END);
    1506           3 :         succeed_if (keyCopy (copy, orig, KEY_CP_ALL) == copy, "keyCopy failed");
    1507           3 :         succeed_if_same_string (keyName (orig), "/");
    1508           3 :         succeed_if_same_string (keyName (copy), "/");
    1509           3 :         succeed_if (keyGetNameSize (orig) == 2, "orig name size");
    1510           3 :         succeed_if (keyGetNameSize (copy) == 2, "orig name size");
    1511           3 :         succeed_if (keyGetRef (orig) == 0, "orig ref counter should be 0");
    1512           3 :         succeed_if (keyGetRef (copy) == 0, "copy ref counter should be 0");
    1513             : 
    1514           3 :         keyDel (orig);
    1515           3 :         keyDel (copy);
    1516             : 
    1517             : 
    1518             :         // check KEY_CP_NAME
    1519           3 :         orig = keyNew ("user:/orig", KEY_END);
    1520           3 :         keySetString (orig, "orig");
    1521           3 :         keySetMeta (orig, "orig", "orig");
    1522             : 
    1523           3 :         copy = keyNew ("user:/copy", KEY_END);
    1524           3 :         keySetString (copy, "copy");
    1525           3 :         keySetMeta (copy, "copy", "copy");
    1526             : 
    1527           3 :         succeed_if (keyCopy (copy, orig, KEY_CP_NAME) == copy, "keyCopy failed");
    1528           3 :         succeed_if_same_string (keyName (orig), "user:/orig");
    1529           3 :         succeed_if_same_string (keyName (copy), "user:/orig");
    1530           3 :         keyGetString (orig, origBuffer, sizeof (origBuffer));
    1531           3 :         keyGetString (copy, copyBuffer, sizeof (copyBuffer));
    1532           3 :         succeed_if_same_string (origBuffer, "orig");
    1533           3 :         succeed_if_same_string (copyBuffer, "copy");
    1534           3 :         succeed_if_same_string (keyValue (keyGetMeta (orig, "orig")), "orig");
    1535           3 :         succeed_if (keyGetMeta (orig, "copy") == 0, "Metadata not cleared on keyCopy with KEY_CP_NAME");
    1536           3 :         succeed_if_same_string (keyValue (keyGetMeta (copy, "copy")), "copy");
    1537           3 :         succeed_if (keyGetMeta (copy, "orig") == 0, "Metadata not cleared on keyCopy with KEY_CP_NAME");
    1538             : 
    1539           3 :         keyDel (orig);
    1540           3 :         keyDel (copy);
    1541             : 
    1542             :         // check KEY_CP_VALUE
    1543           3 :         orig = keyNew ("user:/orig", KEY_END);
    1544           3 :         keySetString (orig, "orig");
    1545           3 :         keySetMeta (orig, "orig", "orig");
    1546             : 
    1547           3 :         copy = keyNew ("user:/copy", KEY_END);
    1548           3 :         keySetString (copy, "copy");
    1549           3 :         keySetMeta (copy, "copy", "copy");
    1550             : 
    1551           3 :         succeed_if (keyCopy (copy, orig, KEY_CP_VALUE) == copy, "keyCopy failed");
    1552           3 :         succeed_if_same_string (keyName (orig), "user:/orig");
    1553           3 :         succeed_if_same_string (keyName (copy), "user:/copy");
    1554           3 :         keyGetString (orig, origBuffer, sizeof (origBuffer));
    1555           3 :         keyGetString (copy, copyBuffer, sizeof (copyBuffer));
    1556           3 :         succeed_if_same_string (origBuffer, "orig");
    1557           3 :         succeed_if_same_string (copyBuffer, "orig");
    1558           3 :         succeed_if_same_string (keyValue (keyGetMeta (orig, "orig")), "orig");
    1559           3 :         succeed_if (keyGetMeta (orig, "copy") == 0, "Metadata not cleared on keyCopy with KEY_CP_VALUE");
    1560           3 :         succeed_if_same_string (keyValue (keyGetMeta (copy, "copy")), "copy");
    1561           3 :         succeed_if (keyGetMeta (copy, "orig") == 0, "Metadata not cleared on keyCopy with KEY_CP_VALUE");
    1562             : 
    1563           3 :         keyDel (orig);
    1564           3 :         keyDel (copy);
    1565             : 
    1566             :         // check KEY_CP_META
    1567           3 :         orig = keyNew ("user:/orig", KEY_END);
    1568           3 :         keySetString (orig, "orig");
    1569           3 :         keySetMeta (orig, "orig", "orig");
    1570             : 
    1571           3 :         copy = keyNew ("user:/copy", KEY_END);
    1572           3 :         keySetString (copy, "copy");
    1573           3 :         keySetMeta (copy, "copy", "copy");
    1574             : 
    1575           3 :         succeed_if (keyCopy (copy, orig, KEY_CP_META) == copy, "keyCopy failed");
    1576           3 :         succeed_if_same_string (keyName (orig), "user:/orig");
    1577           3 :         succeed_if_same_string (keyName (copy), "user:/copy");
    1578           3 :         keyGetString (orig, origBuffer, sizeof (origBuffer));
    1579           3 :         keyGetString (copy, copyBuffer, sizeof (copyBuffer));
    1580           3 :         succeed_if_same_string (origBuffer, "orig");
    1581           3 :         succeed_if_same_string (copyBuffer, "copy");
    1582           3 :         succeed_if_same_string (keyValue (keyGetMeta (orig, "orig")), "orig");
    1583           3 :         succeed_if (keyGetMeta (orig, "copy") == 0, "Metadata not cleared on keyCopy with KEY_CP_META");
    1584           3 :         succeed_if_same_string (keyValue (keyGetMeta (copy, "orig")), "orig");
    1585           3 :         succeed_if (keyGetMeta (copy, "copy") == 0, "Metadata not cleared on keyCopy with KEY_CP_META");
    1586             : 
    1587           3 :         keyDel (orig);
    1588           3 :         keyDel (copy);
    1589             : 
    1590             : 
    1591           3 :         orig = keyNew ("user:/orig", KEY_END);
    1592           3 :         succeed_if (keyNeedSync (orig), "fresh key does not need sync?");
    1593           3 :         KeySet * ks = ksNew (20, KS_END);
    1594           3 :         ksAppendKey (ks, orig);
    1595           3 :         copy = keyNew ("user:/othername", KEY_END);
    1596           3 :         succeed_if (keyNeedSync (copy), "fresh key does not need sync?");
    1597           3 :         succeed_if (keyGetRef (orig) == 1, "orig ref counter should be 1");
    1598           3 :         succeed_if (keyGetRef (copy) == 0, "copy ref counter should be 0");
    1599           3 :         succeed_if (keyCopy (orig, copy, KEY_CP_ALL) == NULL, "copy should not be allowed when key is already referred to");
    1600           3 :         succeed_if (keyNeedSync (orig), "copied key does not need sync?");
    1601           3 :         succeed_if (keyNeedSync (copy), "copied key does not need sync?");
    1602             : 
    1603           3 :         succeed_if (keyGetRef (orig) == 1, "orig ref counter should be 1");
    1604           3 :         succeed_if (keyGetRef (copy) == 0, "copy ref counter should be 1");
    1605             : 
    1606           3 :         succeed_if_same_string (keyName (orig), "user:/orig");
    1607           3 :         succeed_if_same_string (keyName (copy), "user:/othername");
    1608             : 
    1609           3 :         keyDel (copy);
    1610           3 :         ksRewind (ks);
    1611           3 :         succeed_if_same_string (keyName (ksNext (ks)), "user:/orig");
    1612           3 :         ksDel (ks);
    1613           3 : }
    1614             : 
    1615             : 
    1616             : typedef void (*fun_t) (void);
    1617           0 : static void fun (void)
    1618             : {
    1619           0 : }
    1620             : 
    1621           3 : static void test_binary (void)
    1622             : {
    1623           3 :         printf ("Test binary values\n");
    1624             : 
    1625           3 :         Key * k = 0;
    1626             : 
    1627           3 :         int i = 20;
    1628           3 :         int * p = &i;
    1629             : 
    1630           3 :         k = keyNew ("/", KEY_END);
    1631           3 :         succeed_if (keySetBinary (k, &p, sizeof (p)) == sizeof (p), "could not set binary");
    1632             : 
    1633           3 :         int * q;
    1634           3 :         succeed_if (keyGetBinary (k, &q, sizeof (q)) == sizeof (q), "could not get binary");
    1635           3 :         succeed_if (p == q, "pointers to int are not equal");
    1636           3 :         succeed_if (*p == *q, "values are not equal");
    1637           3 :         succeed_if (*q == 20, "values are not equal");
    1638           3 :         succeed_if (*p == 20, "values are not equal");
    1639             : 
    1640           3 :         keyDel (k);
    1641             : 
    1642             : 
    1643           3 :         union
    1644             :         {
    1645             :                 void (*f) (void);
    1646             :                 void * v;
    1647             :         } conversation;
    1648             : 
    1649           3 :         k = keyNew ("/", KEY_END);
    1650           3 :         conversation.f = fun;
    1651           3 :         succeed_if (keySetBinary (k, &conversation.v, sizeof (conversation)) == sizeof (conversation), "could not set binary");
    1652             : 
    1653           3 :         conversation.v = 0;
    1654           3 :         conversation.f = 0;
    1655           3 :         void (*g) (void) = 0;
    1656           3 :         succeed_if (keyGetBinary (k, &conversation.v, sizeof (conversation)) == sizeof (conversation), "could not get binary");
    1657           3 :         g = conversation.f;
    1658           3 :         succeed_if (g == fun, "pointers to functions are not equal");
    1659             : 
    1660           3 :         keyDel (k);
    1661             : 
    1662             : 
    1663           3 :         conversation.f = fun;
    1664           3 :         k = keyNew ("system:/symbols/fun", KEY_BINARY, KEY_SIZE, sizeof (conversation), KEY_VALUE, &conversation.v, KEY_END);
    1665             : 
    1666           3 :         conversation.v = 0;
    1667           3 :         conversation.f = 0;
    1668           3 :         succeed_if (keyGetBinary (k, &conversation.v, sizeof (conversation)) == sizeof (conversation), "could not get binary");
    1669           3 :         g = conversation.f;
    1670           3 :         succeed_if (g == fun, "pointers to functions are not equal");
    1671             : 
    1672           3 :         keyDel (k);
    1673             : 
    1674             : 
    1675           3 :         fun_t tmp = fun;
    1676             : 
    1677           3 :         k = keyNew ("system:/symbol/fun", KEY_BINARY, KEY_SIZE, sizeof (fun_t), KEY_VALUE, &tmp, KEY_END);
    1678             : 
    1679           3 :         fun_t myfun = 0;
    1680           3 :         succeed_if (keyGetBinary (k, &myfun, sizeof (fun_t)) == sizeof (fun_t), "could not get binary");
    1681             : 
    1682           3 :         succeed_if (fun == myfun, "pointers not equal");
    1683             : 
    1684           3 :         keyDel (k);
    1685             : 
    1686             : 
    1687           3 :         k = keyNew ("system:/symbol/cool", KEY_FUNC, fun, KEY_END);
    1688             : 
    1689           3 :         succeed_if (keyGetBinary (k, &myfun, sizeof (fun_t)) == sizeof (fun_t), "could not get binary");
    1690             : 
    1691           3 :         succeed_if (fun == myfun, "pointers not equal");
    1692             : 
    1693           3 :         keyDel (k);
    1694             : 
    1695           3 :         char data[10];
    1696           3 :         k = keyNew ("system:/empty_binary", KEY_END);
    1697           3 :         succeed_if (keySetBinary (k, 0, 0) == 0, "could not set binary will null pointer");
    1698           3 :         succeed_if (keyIsBinary (k), "key is not binary");
    1699           3 :         succeed_if (keyGetBinary (k, data, 1) == 0, "could not get empty binary");
    1700           3 :         succeed_if (keyValue (k) == 0, "did not get back null pointer");
    1701             : 
    1702           3 :         succeed_if (keySetBinary (k, 0, 1) == 0, "could not set binary will null pointer");
    1703           3 :         succeed_if (keyIsBinary (k), "key is not binary");
    1704           3 :         succeed_if (keyGetBinary (k, data, 1) == 0, "could not get empty binary");
    1705           3 :         succeed_if (keyValue (k) == 0, "did not get back null pointer");
    1706             : 
    1707           3 :         succeed_if (keySetBinary (k, 0, 5) == 0, "could not set binary will null pointer");
    1708           3 :         succeed_if (keyIsBinary (k), "key is not binary");
    1709           3 :         succeed_if (keyGetBinary (k, data, 1) == 0, "could not get empty binary");
    1710           3 :         succeed_if (keyValue (k) == 0, "did not get back null pointer");
    1711             : 
    1712           3 :         succeed_if (keySetBinary (k, 0, -1) == -1, "misusage: this will fail");
    1713           3 :         succeed_if (keyIsBinary (k), "key is not binary (should be from previous calls)");
    1714           3 :         succeed_if (keyGetBinary (k, data, 1) == 0, "could not get empty binary");
    1715           3 :         succeed_if (keyValue (k) == 0, "did not get back null pointer");
    1716             : 
    1717           3 :         keyDel (k);
    1718           3 : }
    1719             : 
    1720           3 : static void test_keyBelowOrSame (void)
    1721             : {
    1722           3 :         Key * key1 = keyNew ("/", KEY_END);
    1723           3 :         Key * key2 = keyNew ("/", KEY_END);
    1724             : 
    1725           3 :         printf ("Test of keyBelowOrSame\n");
    1726             : 
    1727           3 :         succeed_if (keyIsBelowOrSame (key1, 0) == -1, "NULL pointer");
    1728           3 :         succeed_if (keyIsBelowOrSame (0, 0) == -1, "NULL pointer");
    1729           3 :         succeed_if (keyIsBelowOrSame (0, key1) == -1, "NULL pointer");
    1730             : 
    1731           3 :         keySetName (key1, "user:/valid");
    1732           3 :         keySetName (key2, "user:/valid");
    1733           3 :         succeed_if (keyIsBelowOrSame (key1, key2), "Key should be below");
    1734           3 :         succeed_if (keyIsBelowOrSame (key2, key1), "Key should be below");
    1735             : 
    1736           3 :         keySetName (key1, "user:/valid");
    1737           3 :         keySetName (key2, "user:/valid/");
    1738           3 :         succeed_if (keyIsBelowOrSame (key1, key2), "Key should be below");
    1739           3 :         succeed_if (keyIsBelowOrSame (key2, key1), "Key should be below");
    1740             : 
    1741           3 :         keySetName (key1, "user:/valid");
    1742           3 :         keySetName (key2, "user:/valid/below");
    1743           3 :         succeed_if (keyIsBelowOrSame (key1, key2), "Key should be below");
    1744           3 :         succeed_if (!keyIsBelowOrSame (key2, key1), "Key should not be below");
    1745             : 
    1746           3 :         keySetName (key1, "user:/valid");
    1747           3 :         keySetName (key2, "user:/valid/b");
    1748           3 :         succeed_if (keyIsBelowOrSame (key1, key2), "Key should be below");
    1749           3 :         succeed_if (!keyIsBelowOrSame (key2, key1), "Key should not be below");
    1750             : 
    1751           3 :         keySetName (key1, "user:/valid");
    1752           3 :         keySetName (key2, "user:/valid/b");
    1753           3 :         succeed_if (keyIsBelowOrSame (key1, key2), "Key should be below");
    1754           3 :         succeed_if (!keyIsBelowOrSame (key2, key1), "Key should not be below");
    1755             : 
    1756           3 :         keySetName (key1, "user:/valid");
    1757           3 :         keySetName (key2, "user:/valid/b/e");
    1758           3 :         succeed_if (keyIsBelowOrSame (key1, key2), "Key should be below");
    1759           3 :         succeed_if (!keyIsBelowOrSame (key2, key1), "Key should not be below");
    1760             : 
    1761           3 :         keySetName (key1, "/valid");
    1762           3 :         keySetName (key2, "/valid/b/e");
    1763           3 :         succeed_if (keyIsBelowOrSame (key1, key2), "Key should be below");
    1764           3 :         succeed_if (!keyIsBelowOrSame (key2, key1), "Key should not be below");
    1765             : 
    1766           3 :         keySetName (key1, "/");
    1767           3 :         keySetName (key2, "/valid/b/e");
    1768           3 :         succeed_if (keyIsBelowOrSame (key1, key2), "Key should be below");
    1769           3 :         succeed_if (!keyIsBelowOrSame (key2, key1), "Key should not be below");
    1770             : 
    1771           3 :         keySetName (key1, "/");
    1772           3 :         keySetName (key2, "/v");
    1773           3 :         succeed_if (keyIsBelowOrSame (key1, key2), "Key should be below");
    1774           3 :         succeed_if (!keyIsBelowOrSame (key2, key1), "Key should not be below");
    1775             : 
    1776           3 :         keySetName (key1, "/");
    1777           3 :         keySetName (key2, "/");
    1778           3 :         succeed_if (keyIsBelowOrSame (key1, key2), "Key should be below");
    1779           3 :         succeed_if (keyIsBelowOrSame (key2, key1), "Key should be below");
    1780             : 
    1781           3 :         keySetName (key1, "user:/valide");
    1782           3 :         keySetName (key2, "user:/valid/e");
    1783           3 :         succeed_if (!keyIsBelowOrSame (key1, key2), "Key should not be below");
    1784           3 :         succeed_if (!keyIsBelowOrSame (key2, key1), "Key should not be below");
    1785             : 
    1786           3 :         keySetName (key1, "user:/valid/b");
    1787           3 :         keySetName (key2, "user:/valid/e");
    1788           3 :         succeed_if (!keyIsBelowOrSame (key1, key2), "Key should not be below");
    1789           3 :         succeed_if (!keyIsBelowOrSame (key2, key1), "Key should not be below");
    1790             : 
    1791           3 :         keySetName (key1, "user:/export/a");
    1792           3 :         keySetName (key2, "user:/export-backup/b");
    1793           3 :         succeed_if (!keyIsBelowOrSame (key1, key2), "Key should not be below");
    1794           3 :         succeed_if (!keyIsBelowOrSame (key2, key1), "Key should not be below");
    1795             : 
    1796           3 :         keySetName (key1, "user:/export");
    1797           3 :         keySetName (key2, "user:/export-backup-2/x");
    1798           3 :         succeed_if (!keyIsBelowOrSame (key1, key2), "Key should not be below");
    1799           3 :         succeed_if (!keyIsBelowOrSame (key2, key1), "Key should not be below");
    1800             : 
    1801           3 :         keyDel (key1);
    1802           3 :         keyDel (key2);
    1803           3 : }
    1804             : 
    1805           3 : static void test_keyNameSpecial (void)
    1806             : {
    1807           3 :         printf ("Test special keynames\n");
    1808           3 :         Key * k = keyNew ("/", KEY_END);
    1809           3 :         succeed_if_same_string (keyName (k), "/");
    1810             : 
    1811           3 :         succeed_if (keySetName (k, "system:/"), "could not set key name with system");
    1812           3 :         succeed_if_same_string (keyName (k), "system:/");
    1813             : 
    1814           3 :         succeed_if (keySetName (k, 0) < 0, "could set key name with 0");
    1815           3 :         succeed_if_same_string (keyName (k), "system:/");
    1816             : 
    1817           3 :         succeed_if (keySetName (k, "system:/"), "could not set key name with system");
    1818           3 :         succeed_if_same_string (keyName (k), "system:/");
    1819             : 
    1820           3 :         succeed_if (keySetName (k, "") < 0, "could set key name with empty string");
    1821           3 :         succeed_if_same_string (keyName (k), "system:/");
    1822             : 
    1823           3 :         succeed_if (keySetName (k, "system:/"), "could not set key name with system");
    1824           3 :         succeed_if_same_string (keyName (k), "system:/");
    1825             : 
    1826           3 :         succeed_if (keySetName (k, "invalid") < 0, "could set key name invalid");
    1827           3 :         succeed_if_same_string (keyName (k), "system:/");
    1828             : 
    1829             : 
    1830           3 :         succeed_if (keySetName (k, "system:/something/.."), "could not set key name with ..");
    1831           3 :         succeed_if_same_string (keyName (k), "system:/");
    1832             : 
    1833           3 :         succeed_if (keySetName (k, "system:/something/.."), "could not set key name with ..");
    1834           3 :         succeed_if_same_string (keyName (k), "system:/");
    1835             : 
    1836           3 :         succeed_if (keySetName (k, "system:/something/../.."), "could not set key name with ..");
    1837           3 :         succeed_if_same_string (keyName (k), "system:/");
    1838             : 
    1839           3 :         succeed_if (keySetName (k, "system:/something/../../.."), "could not set key name with ..");
    1840           3 :         succeed_if_same_string (keyName (k), "system:/");
    1841             : 
    1842           3 :         succeed_if (keySetName (k, "system:/something/../../../.."), "could not set key name with ..");
    1843           3 :         succeed_if_same_string (keyName (k), "system:/");
    1844             : 
    1845           3 :         succeed_if (keySetName (k, "system:/something/../../../../.."), "could not set key name with ..");
    1846           3 :         succeed_if_same_string (keyName (k), "system:/");
    1847             : 
    1848             : 
    1849           3 :         succeed_if (keySetName (k, "system:/../something"), "could not set key name with too many ..");
    1850           3 :         succeed_if_same_string (keyName (k), "system:/something");
    1851             : 
    1852           3 :         succeed_if (keySetName (k, "system:/../../something"), "could not set key name with too many ..");
    1853           3 :         succeed_if_same_string (keyName (k), "system:/something");
    1854             : 
    1855           3 :         succeed_if (keySetName (k, "system:/../../../something"), "could not set key name with too many ..");
    1856           3 :         succeed_if_same_string (keyName (k), "system:/something");
    1857             : 
    1858           3 :         succeed_if (keySetName (k, "system:/../../../../something"), "could not set key name with too many ..");
    1859           3 :         succeed_if_same_string (keyName (k), "system:/something");
    1860             : 
    1861           3 :         succeed_if (keySetName (k, "system:/../../../../../something"), "could not set key name with too many ..");
    1862           3 :         succeed_if_same_string (keyName (k), "system:/something");
    1863             : 
    1864             : 
    1865           3 :         succeed_if (keySetName (k, "system:/a/b/c/.."), "could not set key name with ..");
    1866           3 :         succeed_if_same_string (keyName (k), "system:/a/b");
    1867             : 
    1868           3 :         succeed_if (keySetName (k, "system:/a/b/c/../.."), "could not set key name with ..");
    1869           3 :         succeed_if_same_string (keyName (k), "system:/a");
    1870             : 
    1871           3 :         succeed_if (keySetName (k, "system:/a/b/c/../../.."), "could not set key name with ..");
    1872           3 :         succeed_if_same_string (keyName (k), "system:/");
    1873             : 
    1874           3 :         succeed_if (keySetName (k, "system:/a/b/c/../../../.."), "could not set key name with ..");
    1875           3 :         succeed_if_same_string (keyName (k), "system:/");
    1876             : 
    1877           3 :         succeed_if (keySetName (k, "system:/a/b/c/../../../../.."), "could not set key name with ..");
    1878           3 :         succeed_if_same_string (keyName (k), "system:/");
    1879             : 
    1880             : 
    1881           3 :         succeed_if (keySetName (k, "system:/../a/b/c"), "could not set key name with too many ..");
    1882           3 :         succeed_if_same_string (keyName (k), "system:/a/b/c");
    1883             : 
    1884           3 :         succeed_if (keySetName (k, "system:/../../a/b/c"), "could not set key name with too many ..");
    1885           3 :         succeed_if_same_string (keyName (k), "system:/a/b/c");
    1886             : 
    1887           3 :         succeed_if (keySetName (k, "system:/../../../a/b/c"), "could not set key name with too many ..");
    1888           3 :         succeed_if_same_string (keyName (k), "system:/a/b/c");
    1889             : 
    1890           3 :         succeed_if (keySetName (k, "system:/../../../../a/b/c"), "could not set key name with too many ..");
    1891           3 :         succeed_if_same_string (keyName (k), "system:/a/b/c");
    1892             : 
    1893           3 :         succeed_if (keySetName (k, "system:/../../../../../a/b/c"), "could not set key name with too many ..");
    1894           3 :         succeed_if_same_string (keyName (k), "system:/a/b/c");
    1895             : 
    1896             : 
    1897           3 :         keyDel (k);
    1898             : 
    1899             : 
    1900           3 :         printf ("Test invalid names\n");
    1901           3 :         k = keyNew ("/", KEY_END);
    1902             : 
    1903           3 :         succeed_if (keySetName (k, 0) == -1, "no error code setting invalid name");
    1904           3 :         succeed_if_same_string (keyName (k), "/");
    1905             : 
    1906           3 :         succeed_if (keySetName (k, "") == -1, "no error code setting invalid name");
    1907           3 :         succeed_if_same_string (keyName (k), "/");
    1908             : 
    1909           3 :         succeed_if (keyGetNameSize (k) == 2, "empty name size");
    1910             : 
    1911           3 :         succeed_if (keyGetNameSize (0) == -1, "no error on getting name size of NULL pointer");
    1912             : 
    1913           3 :         keyDel (k);
    1914           3 : }
    1915             : 
    1916           3 : static void test_keyClear (void)
    1917             : {
    1918           3 :         printf ("Test clear of key\n");
    1919             : 
    1920           3 :         Key * k1 = keyNew ("system:/abc", KEY_END);
    1921           3 :         succeed_if_same_string (keyName (k1), "system:/abc");
    1922             : 
    1923           3 :         succeed_if (keyGetRef (k1) == 0, "New key reference");
    1924           3 :         keyIncRef (k1);
    1925           3 :         succeed_if (keyGetRef (k1) == 1, "Incremented key reference");
    1926           3 :         Key * k2 = k1; // create an alias for k1
    1927           3 :         succeed_if_same_string (keyName (k2), "system:/abc");
    1928           3 :         succeed_if (keyGetRef (k1) == 1, "Incremented key reference");
    1929           3 :         succeed_if (keyGetRef (k2) == 1, "Incremented key reference");
    1930             : 
    1931           3 :         keyIncRef (k1);
    1932           3 :         Key * k3 = k1; // create an alias for k1
    1933           3 :         succeed_if_same_string (keyName (k3), "system:/abc");
    1934           3 :         succeed_if (keyGetRef (k1) == 2, "Incremented key reference");
    1935           3 :         succeed_if (keyGetRef (k2) == 2, "Incremented key reference");
    1936           3 :         succeed_if (keyGetRef (k3) == 2, "Incremented key reference");
    1937             : 
    1938           3 :         keyClear (k1);
    1939           3 :         succeed_if_same_string (keyName (k1), "/");
    1940           3 :         succeed_if_same_string (keyName (k2), "/");
    1941           3 :         succeed_if_same_string (keyName (k3), "/");
    1942             : 
    1943           3 :         keySetMeta (k1, "test_meta", "test_value");
    1944           3 :         succeed_if_same_string (keyValue (keyGetMeta (k1, "test_meta")), "test_value");
    1945           3 :         succeed_if_same_string (keyValue (keyGetMeta (k2, "test_meta")), "test_value");
    1946           3 :         succeed_if_same_string (keyValue (keyGetMeta (k3, "test_meta")), "test_value");
    1947             : 
    1948           3 :         keyClear (k2);
    1949           3 :         succeed_if (keyGetMeta (k1, "test_meta") == 0, "there should be no meta after keyClear");
    1950           3 :         succeed_if (keyGetMeta (k2, "test_meta") == 0, "there should be no meta after keyClear");
    1951           3 :         succeed_if (keyGetMeta (k3, "test_meta") == 0, "there should be no meta after keyClear");
    1952             : 
    1953           3 :         keySetString (k1, "mystring");
    1954           3 :         succeed_if_same_string (keyValue (k1), "mystring");
    1955           3 :         succeed_if_same_string (keyValue (k2), "mystring");
    1956           3 :         succeed_if_same_string (keyValue (k3), "mystring");
    1957             : 
    1958           3 :         keyClear (k3);
    1959           3 :         succeed_if_same_string (keyValue (k1), "");
    1960           3 :         succeed_if_same_string (keyValue (k2), "");
    1961           3 :         succeed_if_same_string (keyValue (k3), "");
    1962             : 
    1963           3 :         succeed_if (keyGetRef (k1) == 2, "Incremented key reference");
    1964           3 :         succeed_if (keyGetRef (k2) == 2, "Incremented key reference");
    1965           3 :         succeed_if (keyGetRef (k3) == 2, "Incremented key reference");
    1966             : 
    1967           3 :         keyDel (k3); // does nothing
    1968           3 :         keyDecRef (k3);
    1969           3 :         k3 = 0; // remove alias
    1970           3 :         succeed_if (keyGetRef (k1) == 1, "Incremented key reference");
    1971           3 :         succeed_if (keyGetRef (k2) == 1, "Incremented key reference");
    1972             : 
    1973           3 :         keyDel (k2); // does nothing
    1974           3 :         keyDecRef (k2);
    1975           3 :         k2 = 0; // remove alias
    1976           3 :         succeed_if (keyGetRef (k1) == 0, "Incremented key reference");
    1977             : 
    1978           3 :         keyDel (k1);
    1979           3 : }
    1980             : 
    1981           3 : static void test_keyBaseName (void)
    1982             : {
    1983           3 :         printf ("Test basename\n");
    1984           3 :         Key * k = keyNew ("user:///foo\\///bar\\/foo_bar\\/", KEY_END);
    1985           3 :         succeed_if_same_string (keyName (k), "user:/foo\\//bar\\/foo_bar\\/");
    1986           3 :         succeed_if_same_string (keyBaseName (k), "bar/foo_bar/");
    1987           3 :         succeed_if (keyGetBaseNameSize (k) == 13, "wrong base name size");
    1988             : 
    1989           3 :         keySetName (k, "system:/");
    1990           3 :         succeed_if_same_string (keyBaseName (k), "");
    1991           3 :         succeed_if (keyGetBaseNameSize (k) == 1, "wrong base name size");
    1992             : 
    1993           3 :         keySetName (k, "system:/valid//////");
    1994           3 :         succeed_if_same_string (keyName (k), "system:/valid");
    1995           3 :         succeed_if_same_string (keyBaseName (k), "valid");
    1996           3 :         succeed_if (keyGetBaseNameSize (k) == 6, "wrong base name size");
    1997             : 
    1998           3 :         keySetName (k, "system://////valid//////");
    1999           3 :         succeed_if_same_string (keyName (k), "system:/valid");
    2000           3 :         succeed_if_same_string (keyBaseName (k), "valid");
    2001           3 :         succeed_if (keyGetBaseNameSize (k) == 6, "wrong base name size");
    2002             : 
    2003           3 :         keySetName (k, "system:///.///valid//.////");
    2004           3 :         succeed_if_same_string (keyName (k), "system:/valid");
    2005           3 :         succeed_if_same_string (keyBaseName (k), "valid");
    2006           3 :         succeed_if (keyGetBaseNameSize (k) == 6, "wrong base name size");
    2007             : 
    2008           3 :         keySetName (k, "user:/");
    2009           3 :         succeed_if_same_string (keyBaseName (k), "");
    2010           3 :         succeed_if (keyGetBaseNameSize (k) == 1, "wrong base name size");
    2011             : 
    2012           3 :         keySetName (k, "user:/valid//////");
    2013           3 :         succeed_if_same_string (keyName (k), "user:/valid");
    2014           3 :         succeed_if_same_string (keyBaseName (k), "valid");
    2015           3 :         succeed_if (keyGetBaseNameSize (k) == 6, "wrong base name size");
    2016             : 
    2017           3 :         keySetName (k, "user://////valid//////");
    2018           3 :         succeed_if_same_string (keyName (k), "user:/valid");
    2019           3 :         succeed_if_same_string (keyBaseName (k), "valid");
    2020           3 :         succeed_if (keyGetBaseNameSize (k) == 6, "wrong base name size");
    2021             : 
    2022           3 :         keySetName (k, "user:///.///valid//.////");
    2023           3 :         succeed_if_same_string (keyName (k), "user:/valid");
    2024           3 :         succeed_if_same_string (keyBaseName (k), "valid");
    2025           3 :         succeed_if (keyGetBaseNameSize (k) == 6, "wrong base name size");
    2026             : 
    2027           3 :         keySetName (k, "user:///foo\\///bar\\/foo_bar\\/");
    2028           3 :         succeed_if_same_string (keyName (k), "user:/foo\\//bar\\/foo_bar\\/");
    2029           3 :         succeed_if_same_string (keyBaseName (k), "bar/foo_bar/");
    2030           3 :         succeed_if (keyGetBaseNameSize (k) == 13, "wrong base name size");
    2031             : 
    2032           3 :         keySetName (k, "user://////foo_bar\\/");
    2033           3 :         succeed_if_same_string (keyName (k), "user:/foo_bar\\/");
    2034           3 :         succeed_if_same_string (keyBaseName (k), "foo_bar/");
    2035           3 :         succeed_if (keyGetBaseNameSize (k) == 9, "wrong base name size");
    2036             : 
    2037           3 :         keySetName (k, "user://////%");
    2038           3 :         succeed_if_same_string (keyName (k), "user:/%");
    2039           3 :         succeed_if_same_string (keyBaseName (k), "");
    2040           3 :         succeed_if (keyGetBaseNameSize (k) == 1, "wrong base name size");
    2041             : 
    2042           3 :         keySetName (k, "user://////\\%");
    2043           3 :         succeed_if_same_string (keyName (k), "user:/\\%");
    2044           3 :         succeed_if_same_string (keyBaseName (k), "%");
    2045           3 :         succeed_if (keyGetBaseNameSize (k) == 2, "wrong base name size");
    2046             : 
    2047           3 :         keySetName (k, "user://////\\\\%");
    2048           3 :         succeed_if_same_string (keyName (k), "user:/\\\\%");
    2049           3 :         succeed_if_same_string (keyBaseName (k), "\\%");
    2050           3 :         succeed_if (keyGetBaseNameSize (k) == 3, "wrong base name size");
    2051             : 
    2052           3 :         keySetName (k, "system:/\\\\valid/\\\\base");
    2053           3 :         succeed_if_same_string (keyName (k), "system:/\\\\valid/\\\\base");
    2054           3 :         succeed_if_same_string (keyBaseName (k), "\\base");
    2055           3 :         succeed_if (keyGetBaseNameSize (k) == 6, "wrong base name size");
    2056             : 
    2057           3 :         keySetName (k, "system:/\\/valid/\\/base");
    2058           3 :         succeed_if_same_string (keyName (k), "system:/\\/valid/\\/base");
    2059           3 :         succeed_if_same_string (keyBaseName (k), "/base"); // wanted?
    2060           3 :         succeed_if (keyGetBaseNameSize (k) == 6, "wrong base name size");
    2061             : 
    2062           3 :         keySetName (k, "system:/valid\\\\/base");
    2063           3 :         succeed_if_same_string (keyName (k), "system:/valid\\\\/base");
    2064           3 :         succeed_if_same_string (keyBaseName (k), "base");
    2065           3 :         succeed_if (keyGetBaseNameSize (k) == 5, "wrong base name size");
    2066             : 
    2067           3 :         keySetName (k, "user://////\\\\%");
    2068           3 :         succeed_if_same_string (keyName (k), "user:/\\\\%");
    2069           3 :         succeed_if_same_string (keyBaseName (k), "\\%");
    2070           3 :         keyDel (k);
    2071           3 : }
    2072             : 
    2073           3 : static void test_keySetBaseName (void)
    2074             : {
    2075           3 :         printf ("Test set basename\n");
    2076             : 
    2077           3 :         Key * k = keyNew ("/", KEY_END);
    2078             : 
    2079           3 :         succeed_if (keySetBaseName (0, "abc") == -1, "NULL key");
    2080           3 :         succeed_if_same_string (keyName (k), "/");
    2081           3 :         succeed_if_same_string (keyBaseName (k), "");
    2082             : 
    2083           3 :         succeed_if (keySetBaseName (k, "abc") == -1, "invalid key");
    2084           3 :         succeed_if_same_string (keyName (k), "/");
    2085           3 :         succeed_if_same_string (keyBaseName (k), "");
    2086             : 
    2087           3 :         keySetName (k, "spec:/");
    2088           3 :         succeed_if (keySetBaseName (k, 0) == -1, "could remove root name");
    2089           3 :         succeed_if_same_string (keyName (k), "spec:/");
    2090           3 :         succeed_if_same_string (keyBaseName (k), "");
    2091             : 
    2092           3 :         keySetName (k, "proc:/");
    2093           3 :         succeed_if (keySetBaseName (k, 0) == -1, "could remove root name");
    2094           3 :         succeed_if_same_string (keyName (k), "proc:/");
    2095           3 :         succeed_if_same_string (keyBaseName (k), "");
    2096             : 
    2097           3 :         keySetName (k, "dir:/");
    2098           3 :         succeed_if (keySetBaseName (k, 0) == -1, "could remove root name");
    2099           3 :         succeed_if_same_string (keyName (k), "dir:/");
    2100           3 :         succeed_if_same_string (keyBaseName (k), "");
    2101             : 
    2102           3 :         keySetName (k, "user:/");
    2103           3 :         succeed_if (keySetBaseName (k, 0) == -1, "could remove root name");
    2104           3 :         succeed_if_same_string (keyName (k), "user:/");
    2105           3 :         succeed_if_same_string (keyBaseName (k), "");
    2106             : 
    2107           3 :         keySetName (k, "system:/");
    2108           3 :         succeed_if (keySetBaseName (k, 0) == -1, "could remove root name");
    2109           3 :         succeed_if_same_string (keyName (k), "system:/");
    2110           3 :         succeed_if_same_string (keyBaseName (k), "");
    2111             : 
    2112           3 :         keySetName (k, "/");
    2113           3 :         succeed_if (keySetBaseName (k, 0) == -1, "could remove root name");
    2114           3 :         succeed_if_same_string (keyName (k), "/");
    2115           3 :         succeed_if_same_string (keyBaseName (k), "");
    2116             : 
    2117           3 :         keySetName (k, "/x");
    2118           3 :         succeed_if (keySetBaseName (k, 0) == 2, "removing single character basename of cascading key with depth 1 failed");
    2119           3 :         succeed_if_same_string (keyName (k), "/");
    2120           3 :         succeed_if_same_string (keyBaseName (k), "");
    2121             : 
    2122           3 :         keySetName (k, "/cascading");
    2123           3 :         succeed_if (keySetBaseName (k, 0) == 2, "removing basename of cascading key with depth 1 failed");
    2124           3 :         succeed_if_same_string (keyName (k), "/");
    2125           3 :         succeed_if_same_string (keyBaseName (k), "");
    2126             : 
    2127           3 :         keySetName (k, "system:/notCascading");
    2128           3 :         succeed_if (keySetBaseName (k, 0) >= 0, "removing basename of non cascading key with depth 1 failed");
    2129           3 :         succeed_if_same_string (keyName (k), "system:/");
    2130           3 :         succeed_if_same_string (keyBaseName (k), "");
    2131             : 
    2132           3 :         keySetName (k, "system:/foo/bar");
    2133           3 :         succeed_if (keySetBaseName (k, 0) >= 0, "removing basename of non cascading key with depth 2 failed");
    2134           3 :         succeed_if_same_string (keyName (k), "system:/foo");
    2135           3 :         succeed_if_same_string (keyBaseName (k), "foo");
    2136           3 :         succeed_if (keySetBaseName (k, 0) >= 0, "second removing basename of non cascading key with depth 2 failed");
    2137           3 :         succeed_if_same_string (keyName (k), "system:/");
    2138           3 :         succeed_if_same_string (keyBaseName (k), "");
    2139             : 
    2140           3 :         keySetName (k, "/foo/bar");
    2141           3 :         succeed_if (keySetBaseName (k, 0) >= 0, "removing basename of cascading key with depth 2 failed");
    2142           3 :         succeed_if_same_string (keyName (k), "/foo");
    2143           3 :         succeed_if_same_string (keyBaseName (k), "foo");
    2144           3 :         succeed_if (keySetBaseName (k, 0) == 2, "second removing basename of cascading key with depth 2 failed");
    2145           3 :         succeed_if_same_string (keyName (k), "/");
    2146           3 :         succeed_if_same_string (keyBaseName (k), "");
    2147           3 :         succeed_if (keySetBaseName (k, 0) == -1, "third removing basename of cascading key with depth 2 was possible");
    2148           3 :         succeed_if_same_string (keyName (k), "/");
    2149           3 :         succeed_if_same_string (keyBaseName (k), "");
    2150             : 
    2151           3 :         keySetName (k, "/\\/");
    2152           3 :         succeed_if (keySetBaseName (k, 0) == 2, "removing basename of single character escaped cascading key with depth 1 failed");
    2153           3 :         succeed_if_same_string (keyName (k), "/");
    2154           3 :         succeed_if_same_string (keyBaseName (k), "");
    2155             : 
    2156           3 :         keySetName (k, "/\\.");
    2157           3 :         succeed_if (keySetBaseName (k, 0) == 2, "removing basename of single character escaped dot cascading key with depth 1 failed");
    2158           3 :         succeed_if_same_string (keyName (k), "/");
    2159           3 :         succeed_if_same_string (keyBaseName (k), "");
    2160             : 
    2161           3 :         keySetName (k, "/\\..");
    2162           3 :         succeed_if (keySetBaseName (k, 0) == 2, "removing basename of escaped dot dot cascading key with depth 1 failed");
    2163           3 :         succeed_if_same_string (keyName (k), "/");
    2164           3 :         succeed_if_same_string (keyBaseName (k), "");
    2165             : 
    2166           3 :         keySetName (k, "/\\%");
    2167           3 :         succeed_if (keySetBaseName (k, 0) == 2, "removing basename of single character escaped % cascading key with depth 1 failed");
    2168           3 :         succeed_if_same_string (keyName (k), "/");
    2169           3 :         succeed_if_same_string (keyBaseName (k), "");
    2170             : 
    2171           3 :         keySetName (k, "/#1");
    2172           3 :         succeed_if (keySetBaseName (k, 0) == 2, "removing basename of array cascading key with depth 1 failed");
    2173           3 :         succeed_if_same_string (keyName (k), "/");
    2174           3 :         succeed_if_same_string (keyBaseName (k), "");
    2175             : 
    2176           3 :         keySetName (k, "system:/");
    2177           3 :         succeed_if (keySetBaseName (k, "valid") == -1, "add root name, but set was used");
    2178           3 :         succeed_if_same_string (keyName (k), "system:/");
    2179           3 :         succeed_if_same_string (keyBaseName (k), "");
    2180             : 
    2181           3 :         keySetName (k, "proc:/");
    2182           3 :         succeed_if (keySetBaseName (k, "a") == -1, "add root name, but set was used");
    2183           3 :         succeed_if_same_string (keyName (k), "proc:/");
    2184           3 :         succeed_if_same_string (keyBaseName (k), "");
    2185             : 
    2186           3 :         keySetName (k, "system:/valid");
    2187           3 :         succeed_if (keySetBaseName (k, 0) == sizeof ("system:/"), "could not remove base name");
    2188           3 :         succeed_if_same_string (keyName (k), "system:/");
    2189           3 :         succeed_if_same_string (keyBaseName (k), "");
    2190             : 
    2191           3 :         keySetName (k, "system:/valid");
    2192           3 :         succeed_if (keySetBaseName (k, "") > 0, "could not set empty name");
    2193           3 :         succeed_if_same_string (keyName (k), "system:/%"); // is an empty name
    2194           3 :         succeed_if_same_string (keyBaseName (k), "");
    2195             : 
    2196           3 :         keySetName (k, "system:/valid");
    2197           3 :         succeed_if (keySetBaseName (k, "a") >= 0, "escaped slash ok");
    2198           3 :         succeed_if_same_string (keyName (k), "system:/a");
    2199           3 :         succeed_if_same_string (keyBaseName (k), "a");
    2200             : 
    2201           3 :         keySetName (k, "system:/valid");
    2202           3 :         succeed_if (keySetBaseName (k, "/") >= 0, "escaped slash ok");
    2203           3 :         succeed_if_same_string (keyName (k), "system:/\\/");
    2204           3 :         succeed_if_same_string (keyBaseName (k), "/");
    2205             : 
    2206           3 :         keySetName (k, "system:/valid");
    2207           3 :         succeed_if (keySetBaseName (k, "\\/") >= 0, "escaped slash ok");
    2208           3 :         succeed_if_same_string (keyName (k), "system:/\\\\\\/");
    2209           3 :         succeed_if_same_string (keyBaseName (k), "\\/");
    2210             : 
    2211           3 :         keySetName (k, "system:/valid");
    2212           3 :         succeed_if (keySetBaseName (k, "\\\\/") >= 0, "backslash escaped, but slash unescaped");
    2213           3 :         succeed_if_same_string (keyName (k), "system:/\\\\\\\\\\/");
    2214           3 :         succeed_if_same_string (keyBaseName (k), "\\\\/");
    2215             : 
    2216           3 :         keySetName (k, "system:/valid");
    2217           3 :         succeed_if (keySetBaseName (k, "\\\\\\/") >= 0, "backslash escaped, slash escaped");
    2218           3 :         succeed_if_same_string (keyName (k), "system:/\\\\\\\\\\\\\\/");
    2219           3 :         succeed_if_same_string (keyBaseName (k), "\\\\\\/");
    2220             : 
    2221           3 :         keySetName (k, "system:/valid");
    2222           3 :         succeed_if (keySetBaseName (k, "%") == sizeof ("system:/\\%"), "could not set basename");
    2223           3 :         succeed_if_same_string (keyBaseName (k), "%");
    2224           3 :         succeed_if_same_string (keyName (k), "system:/\\%");
    2225           3 :         succeed_if_same_string (keyBaseName (k), "%");
    2226             : 
    2227           3 :         keySetName (k, "system:/valid");
    2228           3 :         succeed_if (keySetBaseName (k, ".") == sizeof ("system:/\\%"), "could not set basename");
    2229           3 :         succeed_if_same_string (keyBaseName (k), ".");
    2230           3 :         succeed_if_same_string (keyName (k), "system:/\\.");
    2231           3 :         succeed_if_same_string (keyBaseName (k), ".");
    2232             : 
    2233           3 :         keySetName (k, "system:/valid");
    2234           3 :         succeed_if (keySetBaseName (k, "..") == sizeof ("system:/\\.."), "could not set basename");
    2235           3 :         succeed_if_same_string (keyBaseName (k), "..");
    2236           3 :         succeed_if_same_string (keyName (k), "system:/\\..");
    2237           3 :         succeed_if_same_string (keyBaseName (k), "..");
    2238             : 
    2239           3 :         keySetName (k, "system:/valid");
    2240           3 :         succeed_if (keySetBaseName (k, "\\\\\\\\") >= 0, "backslash escaped, backslash escaped");
    2241           3 :         succeed_if_same_string (keyName (k), "system:/\\\\\\\\\\\\\\\\");
    2242           3 :         succeed_if_same_string (keyBaseName (k), "\\\\\\\\");
    2243             : 
    2244           3 :         keySetName (k, "system:/valid");
    2245           3 :         succeed_if (keySetBaseName (k, "\\\\") >= 0, "escaped backslash ok");
    2246           3 :         succeed_if_same_string (keyName (k), "system:/\\\\\\\\");
    2247           3 :         succeed_if_same_string (keyBaseName (k), "\\\\");
    2248             : 
    2249           3 :         keySetName (k, "system:/valid");
    2250           3 :         succeed_if (keySetBaseName (k, "\\.") >= 0, "escaped dot");
    2251           3 :         succeed_if_same_string (keyName (k), "system:/\\\\.");
    2252           3 :         succeed_if_same_string (keyBaseName (k), "\\.");
    2253             : 
    2254           3 :         keySetName (k, "system:/valid");
    2255           3 :         succeed_if (keySetBaseName (k, "\\..") >= 0, "escaped dot-dot");
    2256           3 :         succeed_if_same_string (keyName (k), "system:/\\\\..");
    2257           3 :         succeed_if_same_string (keyBaseName (k), "\\..");
    2258             : 
    2259           3 :         keySetName (k, "system:/valid");
    2260           3 :         succeed_if (keySetBaseName (k, "%") == sizeof ("system:/\\%"), "add some char");
    2261           3 :         succeed_if_same_string (keyName (k), "system:/\\%");
    2262           3 :         succeed_if_same_string (keyBaseName (k), "%");
    2263             : 
    2264           3 :         keySetName (k, "system:/valid");
    2265           3 :         succeed_if (keySetBaseName (k, "#1") == sizeof ("system:/#1"), "valid array entry");
    2266           3 :         succeed_if_same_string (keyName (k), "system:/#1");
    2267           3 :         succeed_if_same_string (keyBaseName (k), "#1");
    2268             : 
    2269           3 :         keySetName (k, "system:/valid");
    2270           3 :         succeed_if (keySetBaseName (k, "#_10") >= 0, "valid array entry");
    2271           3 :         succeed_if_same_string (keyName (k), "system:/#_10");
    2272           3 :         succeed_if_same_string (keyBaseName (k), "#_10");
    2273             : 
    2274           3 :         keySetName (k, "user:/tests/yajl/___empty_map");
    2275           3 :         succeed_if_same_string (keyBaseName (k), "___empty_map");
    2276           3 :         keySetBaseName (k, "#0");
    2277           3 :         succeed_if_same_string (keyBaseName (k), "#0");
    2278             : 
    2279           3 :         keySetBaseName (k, "nullkey");
    2280           3 :         succeed_if_same_string (keyBaseName (k), "nullkey");
    2281           3 :         succeed_if_same_string (keyName (k), "user:/tests/yajl/nullkey");
    2282             : 
    2283             :         //! [base1]
    2284           3 :         keySetName (k, "system:/valid");
    2285           3 :         keySetBaseName (k, ".hiddenkey");
    2286           3 :         succeed_if_same_string (keyName (k), "system:/.hiddenkey");
    2287           3 :         succeed_if_same_string (keyBaseName (k), ".hiddenkey");
    2288             :         //! [base1]
    2289             : 
    2290             :         //! [base2]
    2291           3 :         keySetName (k, "system:/valid");
    2292           3 :         keySetBaseName (k, "");
    2293           3 :         succeed_if_same_string (keyName (k), "system:/%");
    2294           3 :         succeed_if_same_string (keyBaseName (k), "");
    2295             :         //! [base2]
    2296             : 
    2297             :         //! [base3]
    2298           3 :         keySetName (k, "system:/valid");
    2299           3 :         keySetBaseName (k, "%");
    2300           3 :         succeed_if_same_string (keyName (k), "system:/\\%");
    2301           3 :         succeed_if_same_string (keyBaseName (k), "%");
    2302             :         //! [base3]
    2303             : 
    2304           3 :         keyDel (k);
    2305           3 : }
    2306             : 
    2307           3 : static void test_keyAddBaseName (void)
    2308             : {
    2309           3 :         printf ("Test add basename\n");
    2310             : 
    2311           3 :         Key * k = keyNew ("/", KEY_END);
    2312             : 
    2313             :         //![base0 empty]
    2314           3 :         keySetName (k, "");
    2315           3 :         succeed_if_same_string (keyBaseName (k), "");
    2316           3 :         keySetName (k, "user:/");
    2317           3 :         succeed_if_same_string (keyBaseName (k), "");
    2318             :         //![base0 empty]
    2319             : 
    2320             :         //![base1 empty]
    2321           3 :         keySetName (k, "system:/valid");
    2322           3 :         succeed_if (keyAddBaseName (k, "") >= 0, "could not add a base name");
    2323           3 :         succeed_if_same_string (keyName (k), "system:/valid/%");
    2324           3 :         succeed_if_same_string (keyBaseName (k), "");
    2325             :         //![base1 empty]
    2326             : 
    2327           3 :         keySetName (k, "system:/valid");
    2328           3 :         succeed_if (keyAddBaseName (k, "%") >= 0, "could not add a base name");
    2329           3 :         succeed_if_same_string (keyName (k), "system:/valid/\\%");
    2330           3 :         succeed_if_same_string (keyBaseName (k), "%");
    2331             : 
    2332           3 :         keySetName (k, "system:/valid");
    2333           3 :         succeed_if (keyAddBaseName (k, "#") == sizeof ("system:/valid/#"), "could not add a base name");
    2334           3 :         succeed_if_same_string (keyName (k), "system:/valid/#");
    2335           3 :         succeed_if_same_string (keyBaseName (k), "#");
    2336             : 
    2337           3 :         keySetName (k, "system:/valid");
    2338           3 :         succeed_if (keyAddBaseName (k, "#2") >= 0, "could not add a base name");
    2339           3 :         succeed_if_same_string (keyName (k), "system:/valid/#2");
    2340           3 :         succeed_if_same_string (keyBaseName (k), "#2");
    2341             : 
    2342             :         //![base1 add]
    2343           3 :         keySetName (k, "system:/valid");
    2344           3 :         succeed_if (keyAddBaseName (k, ".") >= 0, "could not add a base name");
    2345           3 :         succeed_if_same_string (keyName (k), "system:/valid/\\.");
    2346           3 :         succeed_if_same_string (keyBaseName (k), ".");
    2347             :         //![base1 add]
    2348             : 
    2349           3 :         keySetName (k, "system:/valid");
    2350           3 :         succeed_if (keyAddBaseName (k, "..") >= 0, "could not add a base name");
    2351           3 :         succeed_if_same_string (keyName (k), "system:/valid/\\..");
    2352           3 :         succeed_if_same_string (keyBaseName (k), "..");
    2353             : 
    2354             : 
    2355           3 :         keySetName (k, "system:/valid");
    2356           3 :         succeed_if (keyAddBaseName (k, "hello%#") >= 0, "could not add a base name");
    2357           3 :         succeed_if_same_string (keyName (k), "system:/valid/hello%#");
    2358           3 :         succeed_if_same_string (keyBaseName (k), "hello%#");
    2359             : 
    2360           3 :         keySetName (k, "system:/valid");
    2361           3 :         succeed_if (keyAddBaseName (k, "hello..") >= 0, "could not add a base name");
    2362           3 :         succeed_if_same_string (keyName (k), "system:/valid/hello..");
    2363           3 :         succeed_if_same_string (keyBaseName (k), "hello..");
    2364             : 
    2365           3 :         keySetName (k, "system:/valid");
    2366           3 :         succeed_if (keyAddBaseName (k, "..hello..") >= 0, "could not add a base name");
    2367           3 :         succeed_if_same_string (keyName (k), "system:/valid/..hello..");
    2368           3 :         succeed_if_same_string (keyBaseName (k), "..hello..");
    2369             : 
    2370           3 :         keySetName (k, "system:/valid");
    2371           3 :         succeed_if (keyAddBaseName (k, "has/slash") >= 0, "could not add a base name");
    2372           3 :         succeed_if_same_string (keyName (k), "system:/valid/has\\/slash");
    2373           3 :         succeed_if_same_string (keyBaseName (k), "has/slash");
    2374             : 
    2375           3 :         keySetName (k, "system:/valid");
    2376           3 :         keyAddBaseName (k, "#0");
    2377           3 :         succeed_if_same_string (keyName (k), "system:/valid/#0");
    2378           3 :         succeed_if_same_string (keyBaseName (k), "#0");
    2379             : 
    2380           3 :         keyDel (k);
    2381           3 : }
    2382             : 
    2383           3 : static void test_keyDirectBelow (void)
    2384             : {
    2385           3 :         printf ("Test direct below check\n");
    2386             : 
    2387           3 :         Key * k1 = keyNew ("/dir", KEY_END);
    2388           3 :         Key * k2 = keyNew ("/dir/directbelow", KEY_END);
    2389           3 :         succeed_if (keyIsDirectlyBelow (k1, k2) == 1, "not direct below");
    2390             : 
    2391           3 :         keySetName (k1, "user:/dir");
    2392           3 :         keySetName (k2, "user:/dir/directbelow");
    2393           3 :         succeed_if (keyIsDirectlyBelow (k1, k2) == 1, "not direct below");
    2394             : 
    2395           3 :         keySetName (k1, "user:/dir");
    2396           3 :         keySetName (k2, "user:/dir/direct\\/below");
    2397           3 :         succeed_if (keyIsDirectlyBelow (k1, k2) == 1, "not direct below");
    2398             : 
    2399           3 :         keySetName (k1, "user:/dir");
    2400           3 :         keySetName (k2, "user:/dir/direct\\/");
    2401           3 :         succeed_if (keyIsDirectlyBelow (k1, k2) == 1, "not direct below");
    2402             : 
    2403           3 :         keySetName (k1, "user:/dir");
    2404           3 :         succeed_if (keySetName (k2, "user:/dir/direct\\\\") > -1, "could not set correct name");
    2405           3 :         succeed_if (keyIsDirectlyBelow (k1, k2) == 1, "not direct below");
    2406             : 
    2407           3 :         keySetName (k1, "user:/dir");
    2408           3 :         keySetName (k2, "user:/dir/direct\\\\\\/below");
    2409           3 :         succeed_if (keyIsDirectlyBelow (k1, k2) == 1, "not direct below");
    2410             : 
    2411           3 :         keySetName (k1, "user:/dir");
    2412           3 :         keySetName (k2, "user:/dir/direct\\\\below");
    2413           3 :         succeed_if (keyIsBelow (k1, k2) == 1, "below");
    2414           3 :         succeed_if (keyIsDirectlyBelow (k1, k2) == 1, "not direct below");
    2415             : 
    2416           3 :         keySetName (k1, "user:/dir");
    2417           3 :         keySetName (k2, "user:/dir/direct\\\\/b");
    2418           3 :         succeed_if_same_string (keyName (k2), "user:/dir/direct\\\\/b");
    2419           3 :         succeed_if (keyIsBelow (k1, k2) == 1, "below");
    2420           3 :         succeed_if (keyIsDirectlyBelow (k1, k2) == 0, "direct below, but shouldnt be");
    2421             : 
    2422           3 :         keySetName (k1, "user:/dir");
    2423           3 :         keySetName (k2, "user:/dir/direct\\\\/below");
    2424           3 :         succeed_if (keyIsBelow (k1, k2) == 1, "below");
    2425           3 :         succeed_if (keyIsDirectlyBelow (k1, k2) == 0, "direct below, but shouldnt be");
    2426             : 
    2427           3 :         keyDel (k1);
    2428           3 :         keyDel (k2);
    2429           3 : }
    2430             : 
    2431           3 : static void test_keyEscape (void)
    2432             : {
    2433           3 :         printf ("test escape in basename\n");
    2434             : 
    2435           3 :         Key * k = keyNew ("/valid", KEY_END);
    2436           3 :         char buffer[500];
    2437             : 
    2438             : #define TEST_ESCAPE_PART(A, S)                                                                                                             \
    2439             :         do                                                                                                                                 \
    2440             :         {                                                                                                                                  \
    2441             :                 succeed_if (keySetBaseName (k, A) != -1, "keySetBaseName returned an error");                                              \
    2442             :                 succeed_if_same_string (keyBaseName (k), A);                                                                               \
    2443             :                 succeed_if (keyGetBaseName (k, buffer, 499) != -1, "keyGetBaseName returned an error");                                    \
    2444             :                 succeed_if_same_string (buffer, A);                                                                                        \
    2445             :         } while (0)
    2446             : 
    2447             : #include <data_escape.c>
    2448             : 
    2449           3 :         keySetName (k, "spec:/valid");
    2450             : 
    2451             : #include <data_escape.c>
    2452             : 
    2453           3 :         keySetName (k, "proc:/valid");
    2454             : 
    2455             : #include <data_escape.c>
    2456             : 
    2457           3 :         keySetName (k, "dir:/valid");
    2458             : 
    2459             : #include <data_escape.c>
    2460             : 
    2461           3 :         keySetName (k, "user:/valid");
    2462             : 
    2463             : #include <data_escape.c>
    2464             : 
    2465           3 :         keySetName (k, "system:/valid");
    2466             : 
    2467             : #include <data_escape.c>
    2468             : 
    2469             : #undef TEST_ESCAPE_PART
    2470             : #define TEST_ESCAPE_PART(A, S)                                                                                                             \
    2471             :         do                                                                                                                                 \
    2472             :         {                                                                                                                                  \
    2473             :                 succeed_if (keySetBaseName (k, A) == -1, "keySetBaseName should have returned an error");                                  \
    2474             :                 succeed_if_same_string (keyBaseName (k), "");                                                                              \
    2475             :                 succeed_if (keyGetBaseName (k, buffer, 499) != -1, "keyGetBaseName returned an error");                                    \
    2476             :                 succeed_if_same_string (buffer, "");                                                                                       \
    2477             :         } while (0)
    2478             : 
    2479          21 :         for (int i = 0; i < NUMBER_OF_NAMESPACES; ++i)
    2480             :         {
    2481          18 :                 keySetName (k, namespaces[i]);
    2482             : 
    2483             : #include <data_escape.c>
    2484             :         }
    2485             : 
    2486           3 :         keySetName (k, "/");
    2487           3 :         succeed_if (keyAddBaseName (k, "valid") != -1, "keyAddBaseName returned an error");
    2488           3 :         succeed_if_same_string (keyBaseName (k), "valid");
    2489           3 :         succeed_if_same_string (keyName (k), "/valid");
    2490             : 
    2491             : 
    2492             : // generates huge key (but fits within 500)
    2493             : #undef TEST_ESCAPE_PART
    2494             : #define TEST_ESCAPE_PART(A, S)                                                                                                             \
    2495             :         do                                                                                                                                 \
    2496             :         {                                                                                                                                  \
    2497             :                 succeed_if (keyAddBaseName (k, A) != -1, "keyAddBaseName returned an error");                                              \
    2498             :                 succeed_if_same_string (keyBaseName (k), A);                                                                               \
    2499             :                 succeed_if (keyGetBaseName (k, buffer, 499) != -1, "keyGetBaseName (for keyAddBaseName) returned an error");               \
    2500             :                 succeed_if_same_string (buffer, A);                                                                                        \
    2501             :         } while (0)
    2502             : 
    2503          21 :         for (int i = 0; i < NUMBER_OF_NAMESPACES; ++i)
    2504             :         {
    2505          18 :                 keySetName (k, namespaces[i]);
    2506             : 
    2507             : #include <data_escape.c>
    2508             :         }
    2509             : 
    2510           3 :         keyDel (k);
    2511           3 : }
    2512             : 
    2513         252 : static void test_keyAdd_test (Key * k, const char * escaped, const char * unescaped)
    2514             : {
    2515         252 :         char buffer[500];
    2516         252 :         succeed_if_fmt (keyAddName (k, escaped) != -1, "keyAddName returned an error for '%s'", escaped);
    2517         252 :         succeed_if_same_string (keyBaseName (k), unescaped);
    2518         252 :         succeed_if (keyGetBaseName (k, buffer, 499) != -1, "keyGetBaseName returned an error");
    2519         252 :         succeed_if_same_string (buffer, unescaped);
    2520         252 : }
    2521             : 
    2522           3 : static void test_keyAdd (void)
    2523             : {
    2524           3 :         printf ("test keyAdd\n");
    2525             : 
    2526           3 :         Key * k = keyNew ("/", KEY_END);
    2527           3 :         succeed_if (keyAddName (0, "valid") == -1, "cannot add to null name");
    2528             : 
    2529           3 :         keySetName (k, "/");
    2530           3 :         succeed_if (keyAddName (k, 0) < 0, "could add null pointer");
    2531           3 :         succeed_if (keyAddName (k, "") == sizeof ("/"), "cannot add empty name");
    2532           3 :         succeed_if (keyAddName (k, "/") == sizeof ("/"), "cannot add slashes");
    2533           3 :         succeed_if (keyAddName (k, "//") == sizeof ("/"), "cannot add slashes");
    2534           3 :         succeed_if (keyAddName (k, "////") == sizeof ("/"), "cannot add slashes");
    2535           3 :         succeed_if (keyAddName (k, "invalid\\") < 0, "added invalid name");
    2536           3 :         succeed_if (keyAddName (k, "valid") == sizeof ("/valid"), "added valid name");
    2537             : 
    2538           3 :         keySetName (k, "user:/");
    2539           3 :         succeed_if (keyAddName (k, 0) < 0, "could add null pointer");
    2540           3 :         succeed_if (keyAddName (k, "") == sizeof ("user:/"), "cannot add empty name");
    2541           3 :         succeed_if (keyAddName (k, "/") == sizeof ("user:/"), "cannot add slashes");
    2542           3 :         succeed_if (keyAddName (k, "//") == sizeof ("user:/"), "cannot add slashes");
    2543           3 :         succeed_if (keyAddName (k, "////") == sizeof ("user:/"), "cannot add slashes");
    2544           3 :         succeed_if (keyAddName (k, "invalid\\") < 0, "added invalid name");
    2545           3 :         succeed_if (keyAddName (k, "valid") == sizeof ("user:/valid"), "added valid name");
    2546             : 
    2547          21 :         for (int i = 0; i < NUMBER_OF_NAMESPACES; ++i)
    2548             :         {
    2549          18 :                 keySetName (k, namespaces[i]);
    2550             : 
    2551          18 :                 test_keyAdd_test (k, "a", "a");
    2552          18 :                 test_keyAdd_test (k, "$", "$");
    2553          18 :                 test_keyAdd_test (k, "€", "€");
    2554          18 :                 test_keyAdd_test (k, "\x01", "\x01");
    2555          18 :                 test_keyAdd_test (k, "\xFF", "\xFF");
    2556          18 :                 test_keyAdd_test (k, "\xFF\xFF\xFF\xFF", "\xFF\xFF\xFF\xFF");
    2557          18 :                 test_keyAdd_test (k, "\xFF\xFF/\xFF\xFF", "\xFF\xFF");
    2558          18 :                 test_keyAdd_test (k, "test", "test");
    2559          18 :                 test_keyAdd_test (k, "test/name", "name");
    2560          18 :                 test_keyAdd_test (k, "a/b/c/d/e/f/g/h/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z", "z");
    2561          18 :                 test_keyAdd_test (k, "a\\/b\\/c\\/d\\/e\\/f\\/g\\/h\\/j\\/k\\/l\\/m\\/n\\/o\\/p\\/q\\/r\\/s\\/t\\/u\\/v\\/w\\/x\\/y\\/z",
    2562             :                                   "a/b/c/d/e/f/g/h/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z");
    2563          18 :                 test_keyAdd_test (k, "\\\\%", "\\%");
    2564          18 :                 test_keyAdd_test (k, "a/test", "test");
    2565          18 :                 test_keyAdd_test (k, "a\\/test", "a/test");
    2566             :         }
    2567             : 
    2568           3 :         keyDel (k);
    2569           3 : }
    2570             : 
    2571           3 : void test_keyCascading (void)
    2572             : {
    2573           3 :         printf ("test cascading\n");
    2574             : 
    2575           3 :         Key * k = keyNew ("/", KEY_END);
    2576           3 :         succeed_if (keyGetNameSize (k) == 2, "size not correct");
    2577           3 :         succeed_if_same_string (keyName (k), "/");
    2578           3 :         succeed_if_same_string (keyBaseName (k), "");
    2579             : 
    2580           3 :         succeed_if (keyAddName (k, "valid") > 0, "could not add valid");
    2581           3 :         succeed_if (keyGetNameSize (k) == 7, "size not correct");
    2582           3 :         succeed_if_same_string (keyName (k), "/valid");
    2583           3 :         succeed_if_same_string (keyBaseName (k), "valid");
    2584             : 
    2585           3 :         keySetName (k, "/");
    2586           3 :         succeed_if (keyGetNameSize (k) == 2, "size not correct");
    2587           3 :         succeed_if (keyAddName (k, "/valid") == 7, "could not add valid with starting slash");
    2588           3 :         succeed_if (keyGetNameSize (k) == 7, "size not correct");
    2589           3 :         succeed_if_same_string (keyName (k), "/valid");
    2590           3 :         succeed_if_same_string (keyBaseName (k), "valid");
    2591             : 
    2592           3 :         keySetName (k, "////");
    2593           3 :         succeed_if (keyGetNameSize (k) == 2, "size not correct");
    2594           3 :         succeed_if (keyAddName (k, "/////valid") == 7, "could not add valid with starting slash");
    2595           3 :         succeed_if (keyGetNameSize (k) == 7, "size not correct");
    2596           3 :         succeed_if_same_string (keyName (k), "/valid");
    2597           3 :         succeed_if_same_string (keyBaseName (k), "valid");
    2598             : 
    2599           3 :         keySetName (k, "/");
    2600           3 :         succeed_if (keyGetNameSize (k) == 2, "size not correct");
    2601           3 :         succeed_if (keyAddName (k, "/////more/valid") > 0, "could not add valid with starting slash");
    2602           3 :         succeed_if_same_string (keyName (k), "/more/valid");
    2603           3 :         succeed_if_same_string (keyBaseName (k), "valid");
    2604             : 
    2605           3 :         keySetName (k, "/");
    2606           3 :         succeed_if (keyGetNameSize (k) == 2, "size not correct");
    2607           3 :         succeed_if (keyAddName (k, "/////./valid") == 7, "could not add valid with starting slash");
    2608           3 :         succeed_if (keyGetNameSize (k) == 7, "size not correct");
    2609           3 :         succeed_if_same_string (keyName (k), "/valid");
    2610           3 :         succeed_if_same_string (keyBaseName (k), "valid");
    2611             : 
    2612           3 :         keySetName (k, "/");
    2613           3 :         succeed_if (keyGetNameSize (k) == 2, "size not correct");
    2614           3 :         succeed_if (keyAddName (k, "/////more/../valid") == 7, "could not add valid with ..");
    2615           3 :         succeed_if (keyGetNameSize (k) == 7, "size not correct");
    2616           3 :         succeed_if_same_string (keyName (k), "/valid");
    2617           3 :         succeed_if_same_string (keyBaseName (k), "valid");
    2618             : 
    2619           3 :         keySetName (k, "/");
    2620           3 :         succeed_if (keyGetNameSize (k) == 2, "size not correct");
    2621           3 :         succeed_if (keyAddName (k, "/////..") == 2, "could not add nothing with ..");
    2622           3 :         succeed_if (keyGetNameSize (k) == 2, "size not correct");
    2623           3 :         succeed_if_same_string (keyName (k), "/");
    2624           3 :         succeed_if_same_string (keyBaseName (k), "");
    2625             : 
    2626           3 :         keySetName (k, "/");
    2627           3 :         succeed_if (keyGetNameSize (k) == 2, "size not correct");
    2628           3 :         succeed_if (keyAddName (k, "/////../more") == 6, "could not add more with ..");
    2629           3 :         succeed_if (keyGetNameSize (k) == 6, "size not correct");
    2630           3 :         succeed_if_same_string (keyName (k), "/more");
    2631           3 :         succeed_if_same_string (keyBaseName (k), "more");
    2632             : 
    2633             : 
    2634           3 :         keySetName (k, "/");
    2635           3 :         succeed_if (keyGetNameSize (k) == 2, "size not correct");
    2636           3 :         succeed_if (keyAddName (k, "/////more/..") == 2, "could not add nothing with ..");
    2637           3 :         succeed_if (keyGetNameSize (k) == 2, "size not correct");
    2638           3 :         succeed_if_same_string (keyName (k), "/");
    2639           3 :         succeed_if_same_string (keyBaseName (k), "");
    2640             : 
    2641             : 
    2642           3 :         keySetName (k, "/");
    2643           3 :         succeed_if (keyAddName (k, "/is//../a//../complex/..///.") == 2, "could not add complex stuff");
    2644           3 :         succeed_if_same_string (keyName (k), "/");
    2645           3 :         succeed_if_same_string (keyBaseName (k), "");
    2646             : 
    2647             :         // printf ("%s\n", keyName(k));
    2648             : 
    2649           3 :         keyDel (k);
    2650           3 : }
    2651             : 
    2652           3 : int main (int argc, char ** argv)
    2653             : {
    2654           3 :         printf ("KEY ABI  TESTS\n");
    2655           3 :         printf ("==================\n\n");
    2656             : 
    2657           3 :         init (argc, argv);
    2658             : 
    2659           3 :         test_keyBelow ();
    2660           3 :         test_keyBelowOrSame ();
    2661           3 :         test_keyDirectBelow ();
    2662             : 
    2663           3 :         test_keyNewSpecial ();
    2664           3 :         test_keyNewWrongName ();
    2665           3 :         test_keyNewSystem ();
    2666           3 :         test_keyNewUser ();
    2667           3 :         test_keyReference ();
    2668           3 :         test_keyName ();
    2669           3 :         test_keyNameSlashes ();
    2670           3 :         test_keyValue ();
    2671           3 :         test_keyBinary ();
    2672           3 :         test_keyDup ();
    2673           3 :         test_keyCopy ();
    2674           3 :         test_binary ();
    2675           3 :         test_keyNameSpecial ();
    2676           3 :         test_keyClear ();
    2677           3 :         test_keyBaseName ();
    2678           3 :         test_keySetBaseName ();
    2679           3 :         test_keyAddBaseName ();
    2680           3 :         test_keyEscape ();
    2681           3 :         test_keyAdd ();
    2682           3 :         test_keyCascading ();
    2683             : 
    2684           3 :         printf ("\ntestabi_key RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);
    2685             : 
    2686           3 :         return nbError;
    2687             : }

Generated by: LCOV version 1.13