LCOV - code coverage report
Current view: top level - tests/abi - testabi_meta.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 410 411 99.8 %
Date: 2022-05-21 16:19:22 Functions: 13 13 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Test suite for meta information.
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include <tests.h>
      10             : 
      11           3 : static void test_basic (void)
      12             : {
      13           3 :         Key * key;
      14           3 :         key = keyNew ("user:/key_with_meta", KEY_END);
      15           3 :         exit_if_fail (key, "could not create new key");
      16           3 :         succeed_if (keyGetMeta (key, "hello") == 0, "hello was not set up to now");
      17           3 :         succeed_if (keyGetMeta (key, "error") == 0, "hello was not set up to now");
      18             : 
      19           3 :         keySetMeta (key, "hello", "hello_world");
      20           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "hello")), "hello_world");
      21             : 
      22           3 :         keySetMeta (key, "mode", "0644");
      23           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "hello")), "hello_world");
      24             : 
      25           3 :         keySetMeta (key, "time", "1271234264");
      26           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "hello")), "hello_world");
      27           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "mode")), "0644");
      28           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "time")), "1271234264");
      29             : 
      30           3 :         keySetMeta (key, "hello", "between");
      31           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "hello")), "between");
      32             : 
      33           3 :         keySetMeta (key, "hello", 0);
      34           3 :         succeed_if (keyValue (keyGetMeta (key, "hello")) == 0, "could not remove metadata");
      35             : 
      36           3 :         keySetMeta (key, "hello", "goodbye");
      37           3 :         succeed_if (!strcmp (keyValue (keyGetMeta (key, "hello")), "goodbye"), "could not set meta information again (2x)");
      38             : 
      39           3 :         keySetMeta (key, "empty", "");
      40           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "empty")), "");
      41             : 
      42           3 :         keySetMeta (key, "owner", "hugo");
      43           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "owner")), "hugo");
      44             : 
      45           3 :         keySetMeta (key, "mode", "775");
      46           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "owner")), "hugo");
      47           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "mode")), "775");
      48             : 
      49           3 :         keySetMeta (key, "", "empty");
      50           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "")), "empty");
      51             : 
      52           3 :         keySetMeta (key, "", "");
      53           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "")), "");
      54             : 
      55           3 :         keySetMeta (key, "", 0);
      56           3 :         succeed_if (keyValue (keyGetMeta (key, "")) == 0, "could not remove empty metadata");
      57             : 
      58             : 
      59           3 :         keyDel (key);
      60           3 : }
      61             : 
      62           3 : static void test_null_pointer (void)
      63             : {
      64           3 :         Key * key;
      65             : 
      66           3 :         key = keyNew ("user:/test1", KEY_END);
      67           3 :         exit_if_fail (key, "could not create new key");
      68             : 
      69           3 :         succeed_if (keyRewindMeta (0) == -1, "Could rewind NULL Key");
      70             : 
      71           3 :         succeed_if (keyGetMeta (0, "test") == 0, "Could get meta of NULL Key");
      72           3 :         succeed_if (keyGetMeta (key, 0) == 0, "Could get meta of NULL metaName");
      73             : 
      74           3 :         succeed_if (keyMeta (0) == 0, "Could get metadata of NULL Key");
      75           3 :         succeed_if (keyCurrentMeta (0) == 0, "Could get current meta Key of NULL key");
      76           3 :         succeed_if (keyNextMeta (0) == 0, "Could get next meta Key of NULL key");
      77             : 
      78           3 :         succeed_if (keyCopyMeta (0, key, "test") == -1, "Could copy metadata to NULL Key");
      79           3 :         succeed_if (keyCopyMeta (key, 0, "test") == -1, "Could copy metadata from NULL Key");
      80             : 
      81           3 :         succeed_if (keyCopyAllMeta (0, key) == -1, "Could copy all metadata to NULL Key");
      82           3 :         succeed_if (keyCopyAllMeta (key, 0) == -1, "Could copy all metadata from NULL Key");
      83             : 
      84           3 :         succeed_if (keySetMeta (0, "test", "test"), "Could set metadata to NULL Key");
      85           3 :         succeed_if (keySetMeta (key, 0, "test"), "Could set metadata with NULL metaName");
      86             : 
      87           3 :         keyDel (key);
      88           3 : }
      89             : 
      90           3 : static void test_iterate (void)
      91             : {
      92           3 :         Key * key;
      93             : 
      94           3 :         key = keyNew ("user:/test", KEY_END);
      95           3 :         exit_if_fail (key, "could not create new key");
      96           3 :         succeed_if (keyRewindMeta (key) == 0, "Could not rewind empty key");
      97           3 :         succeed_if (keyNextMeta (key) == 0, "Could get next metaname, even if it is empty");
      98           3 :         succeed_if (keyCurrentMeta (key) == 0, "Could get next metavalue, even if it is empty");
      99             : 
     100           3 :         keySetMeta (key, "meta1", "meta_value");
     101           3 :         succeed_if (keyRewindMeta (key) == 0, "Could not rewind key");
     102           3 :         succeed_if_same_string (keyName (keyNextMeta (key)), "meta:/meta1");
     103           3 :         succeed_if_same_string (keyValue (keyCurrentMeta (key)), "meta_value");
     104             : 
     105           3 :         succeed_if (keyNextMeta (key) == 0, "Could get next metaname, even if it is empty at 2. iteration");
     106           3 :         succeed_if (keyCurrentMeta (key) == 0, "Could get next metavalue, even if it is empty at 2. iteration");
     107             : 
     108           3 :         succeed_if (keyNextMeta (key) == 0, "Could get next metaname, even if it is empty at 3. iteration");
     109           3 :         succeed_if (keyCurrentMeta (key) == 0, "Could get next metavalue, even if it is empty at 3. iteration");
     110             : 
     111           3 :         succeed_if (keyNextMeta (key) == 0, "Could get next metaname, even if it is empty at 4. iteration");
     112           3 :         succeed_if (keyCurrentMeta (key) == 0, "Could get next metavalue, even if it is empty at 4. iteration");
     113             : 
     114           3 :         keyDel (key);
     115           3 : }
     116             : 
     117           3 : static void test_size (void)
     118             : {
     119           3 :         Key * key;
     120           3 :         char * buffer;
     121             : 
     122           3 :         key = keyNew ("user:/test", KEY_END);
     123           3 :         exit_if_fail (key, "could not create new key");
     124           3 :         succeed_if (keyValue (keyGetMeta (key, "hello")) == 0, "hello was not set up to now");
     125           3 :         succeed_if (keyGetValueSize (keyGetMeta (key, "hello")) == -1, "got wrong size for empty metavalue");
     126             : 
     127           3 :         keySetMeta (key, "hello", "hello_world");
     128           3 :         succeed_if (!strcmp (keyValue (keyGetMeta (key, "hello")), "hello_world"), "could not receive previously set meta information");
     129           3 :         succeed_if (keyGetValueSize (keyGetMeta (key, "hello")) == sizeof ("hello_world"), "got wrong size");
     130             : 
     131           3 :         keySetMeta (key, "mode", "0644");
     132           3 :         keySetMeta (key, "time", "1271234264");
     133           3 :         succeed_if (!strcmp (keyValue (keyGetMeta (key, "hello")), "hello_world"), "meta info changed unexpectly");
     134           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "mode")), "0644");
     135           3 :         succeed_if (keyGetValueSize (keyGetMeta (key, "mode")) == sizeof ("0644"), "got wrong size");
     136           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "time")), "1271234264");
     137           3 :         succeed_if (keyGetValueSize (keyGetMeta (key, "time")) == sizeof ("1271234264"), "got wrong size");
     138             : 
     139           3 :         keySetMeta (key, "hello", "between");
     140           3 :         succeed_if (!strcmp (keyValue (keyGetMeta (key, "hello")), "between"), "could not set meta information again");
     141           3 :         succeed_if (keyGetValueSize (keyGetMeta (key, "hello")) == sizeof ("between"), "got wrong size");
     142           3 :         buffer = calloc (1, keyGetValueSize (keyGetMeta (key, "hello")));
     143           3 :         succeed_if (keyGetString (keyGetMeta (key, "hello"), buffer, keyGetValueSize (keyGetMeta (key, "hello"))) ==
     144             :                             keyGetValueSize (keyGetMeta (key, "hello")),
     145           3 :                     "could not get meta");
     146           3 :         succeed_if_same_string (buffer, "between");
     147           3 :         elektraFree (buffer);
     148             : 
     149             : 
     150           3 :         keySetMeta (key, "hello", 0);
     151           3 :         succeed_if (keyValue (keyGetMeta (key, "hello")) == 0, "could not remove metadata");
     152           3 :         succeed_if (keyGetValueSize (keyGetMeta (key, "hello")) == -1, "got wrong size");
     153             : 
     154           3 :         keySetMeta (key, "hello", "goodbye");
     155           3 :         succeed_if (!strcmp (keyValue (keyGetMeta (key, "hello")), "goodbye"), "could not set meta information again (2x)");
     156           3 :         succeed_if (keyGetValueSize (keyGetMeta (key, "hello")) == sizeof ("goodbye"), "got wrong size");
     157           3 :         buffer = calloc (1, keyGetValueSize (keyGetMeta (key, "hello")));
     158           3 :         succeed_if (keyGetString (keyGetMeta (key, "hello"), buffer, keyGetValueSize (keyGetMeta (key, "hello"))) ==
     159             :                             keyGetValueSize (keyGetMeta (key, "hello")),
     160           3 :                     "could not get meta");
     161           3 :         succeed_if_same_string (buffer, "goodbye");
     162           3 :         elektraFree (buffer);
     163             : 
     164           3 :         keySetMeta (key, "empty", "");
     165           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "empty")), "");
     166           3 :         succeed_if (keyGetValueSize (keyGetMeta (key, "empty")) == sizeof (""), "got wrong size");
     167           3 :         buffer = calloc (1, keyGetValueSize (keyGetMeta (key, "empty")));
     168           3 :         succeed_if (keyGetString (keyGetMeta (key, "empty"), buffer, keyGetValueSize (keyGetMeta (key, "empty"))) ==
     169             :                             keyGetValueSize (keyGetMeta (key, "empty")),
     170           3 :                     "could not get meta");
     171           3 :         succeed_if_same_string (buffer, "");
     172           3 :         elektraFree (buffer);
     173             : 
     174           3 :         keySetMeta (key, "", "empty");
     175           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "")), "empty");
     176           3 :         succeed_if (keyGetValueSize (keyGetMeta (key, "")) == sizeof ("empty"), "got wrong size");
     177           3 :         buffer = calloc (1, keyGetValueSize (keyGetMeta (key, "")));
     178           3 :         succeed_if (keyGetString (keyGetMeta (key, ""), buffer, keyGetValueSize (keyGetMeta (key, ""))) ==
     179             :                             keyGetValueSize (keyGetMeta (key, "")),
     180           3 :                     "could not get meta");
     181           3 :         succeed_if_same_string (buffer, "empty");
     182           3 :         elektraFree (buffer);
     183             : 
     184           3 :         keySetMeta (key, "", "");
     185           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "")), "");
     186           3 :         succeed_if (keyGetValueSize (keyGetMeta (key, "")) == sizeof (""), "got wrong size");
     187           3 :         buffer = calloc (1, keyGetValueSize (keyGetMeta (key, "")));
     188           3 :         succeed_if (keyGetString (keyGetMeta (key, ""), buffer, keyGetValueSize (keyGetMeta (key, ""))) ==
     189             :                             keyGetValueSize (keyGetMeta (key, "")),
     190           3 :                     "could not get meta");
     191           3 :         succeed_if_same_string (buffer, "");
     192           3 :         elektraFree (buffer);
     193             : 
     194           3 :         keySetMeta (key, "", 0);
     195           3 :         succeed_if (keyValue (keyGetMeta (key, "")) == 0, "could not remove empty metadata");
     196           3 :         succeed_if (keyGetValueSize (keyGetMeta (key, "")) == -1, "got wrong size");
     197             : 
     198             : 
     199           3 :         keyDel (key);
     200           3 : }
     201             : 
     202           3 : static void test_dup (void)
     203             : {
     204           3 :         Key * key;
     205           3 :         Key * dup;
     206             : 
     207           3 :         key = keyNew ("user:/orig", KEY_END);
     208           3 :         succeed_if (keySetMeta (key, "test", "some_meta_test") == sizeof ("some_meta_test"), "could not set meta");
     209           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "test")), "some_meta_test");
     210             : 
     211           3 :         dup = keyDup (key, KEY_CP_ALL);
     212           3 :         succeed_if_same_string (keyValue (keyGetMeta (dup, "test")), "some_meta_test");
     213           3 :         succeed_if (keySetMeta (dup, "test", "some_other_meta_test") == sizeof ("some_other_meta_test"), "sizeof meta test wrong");
     214           3 :         succeed_if_same_string (keyValue (keyGetMeta (dup, "test")), "some_other_meta_test");
     215           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "test")), "some_meta_test");
     216           3 :         keyDel (dup);
     217             : 
     218           3 :         keyDel (key);
     219           3 : }
     220             : 
     221             : Key * g_c;
     222             : 
     223           3 : static void j (Key * k)
     224             : {
     225           3 :         size_t size = keyGetValueSize (k);
     226           3 :         char * value = elektraMalloc (size);
     227           3 :         int bstring = keyIsString (k);
     228             : 
     229             :         // receive key g_c
     230           3 :         memcpy (value, keyValue (k), size);
     231           3 :         keyCopy (k, g_c, KEY_CP_ALL);
     232           3 :         if (bstring)
     233           3 :                 keySetString (k, value);
     234             :         else
     235           0 :                 keySetBinary (k, value, size);
     236           3 :         elektraFree (value);
     237             :         // the caller will see the changed key k
     238             :         // with the metadata from g_c
     239           3 : }
     240             : 
     241             : static void l (Key * k)
     242             : {
     243             :         // receive g_c
     244           3 :         keyCopyMeta (k, g_c, "type");
     245             :         // the caller will see the changed key k
     246             :         // with the metadata "type" from g_c
     247             : }
     248             : 
     249           3 : static void test_examples (void)
     250             : {
     251           3 :         Key * key;
     252           3 :         key = keyNew ("/", KEY_END);
     253           3 :         keySetMeta (key, "def", "abc");
     254           3 :         keySetMeta (key, "nop", "cup");
     255             : 
     256           3 :         g_c = keyNew ("/", KEY_END);
     257           3 :         keySetMeta (g_c, "xef", "ybc");
     258           3 :         keySetMeta (g_c, "xop", "yup");
     259             : 
     260           3 :         j (key);
     261             : 
     262           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "xef")), "ybc");
     263           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "xop")), "yup");
     264           3 :         succeed_if (keyValue (keyGetMeta (key, "def")) == 0, "old metadata remained");
     265           3 :         succeed_if (keyValue (keyGetMeta (key, "nop")) == 0, "old metadata remained");
     266             : 
     267           3 :         keyDel (key);
     268           3 :         keyDel (g_c);
     269             : 
     270           3 :         key = keyNew ("/", KEY_END);
     271           3 :         keySetMeta (key, "def", "abc");
     272           3 :         keySetMeta (key, "nop", "cup");
     273             : 
     274           3 :         g_c = keyNew ("/", KEY_END);
     275           3 :         keySetMeta (g_c, "type", "boolean");
     276           3 :         keySetMeta (g_c, "xop", "yup");
     277             : 
     278           3 :         l (key);
     279             : 
     280           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "def")), "abc");
     281           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "nop")), "cup");
     282           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "type")), "boolean");
     283           3 :         succeed_if (keyValue (keyGetMeta (key, "xop")) == 0, "this metadata was not requested to be copied");
     284             : 
     285           3 :         keyDel (key);
     286           3 :         keyDel (g_c);
     287           3 : }
     288             : 
     289           3 : static void test_copy (void)
     290             : {
     291           3 :         printf ("Test key meta copy\n");
     292             : 
     293           3 :         Key * key1;
     294           3 :         Key * key2;
     295             : 
     296           3 :         succeed_if (key1 = keyNew ("/", KEY_END), "could not create key");
     297           3 :         succeed_if (key2 = keyNew ("/", KEY_END), "could not create key");
     298             : 
     299           3 :         succeed_if (keyCopyMeta (key2, key1, "nonexist") == 0, "could not do anything");
     300             : 
     301           3 :         succeed_if (keyValue (keyGetMeta (key2, "nonexist")) == 0, "should not be there");
     302             : 
     303           3 :         keyDel (key1);
     304           3 :         keyDel (key2);
     305             : 
     306           3 :         succeed_if (key1 = keyNew ("/", KEY_END), "could not create key");
     307           3 :         succeed_if (key2 = keyNew ("/", KEY_END), "could not create key");
     308             : 
     309           3 :         succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue") == sizeof ("a longer metavalue"), "could not set metavalue");
     310           3 :         succeed_if (keyCopyMeta (key2, key1, "mymeta") == 1, "could not copy metavalue");
     311           3 :         succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
     312           3 :         succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
     313           3 :         succeed_if (keyGetMeta (key1, "mymeta") == keyGetMeta (key2, "mymeta"), "reference to the same key");
     314             : 
     315           3 :         succeed_if (keyCopyMeta (key1, key2, "mymeta") == 1, "did nothing in the end");
     316           3 :         succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
     317           3 :         succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
     318           3 :         succeed_if (keyGetMeta (key1, "mymeta") == keyGetMeta (key2, "mymeta"), "reference to the same key");
     319             : 
     320           3 :         keyDel (key1);
     321           3 :         keyDel (key2);
     322             : 
     323             : 
     324           3 :         succeed_if (key1 = keyNew ("/", KEY_END), "could not create key");
     325           3 :         succeed_if (key2 = keyNew ("/", KEY_END), "could not create key");
     326             : 
     327           3 :         succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue") == sizeof ("a longer metavalue"), "could not set metavalue");
     328           3 :         succeed_if (keyCopyMeta (key2, key1, "mymeta") == 1, "could not copy metavalue");
     329           3 :         succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
     330           3 :         succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
     331           3 :         succeed_if (keyGetMeta (key1, "mymeta") == keyGetMeta (key2, "mymeta"), "reference to the same key");
     332             : 
     333           3 :         succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue") == sizeof ("a longer metavalue"), "could not set metavalue");
     334           3 :         succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
     335           3 :         succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
     336           3 :         succeed_if (keyGetMeta (key1, "mymeta") != keyGetMeta (key2, "mymeta"), "reference to another key");
     337             : 
     338           3 :         succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue2") == sizeof ("a longer metavalue2"), "could not set metavalue2");
     339           3 :         succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue2");
     340           3 :         succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
     341           3 :         succeed_if (keyGetMeta (key1, "mymeta") != keyGetMeta (key2, "mymeta"), "reference to another key (with another value)");
     342             : 
     343           3 :         keyDel (key1);
     344           3 :         keyDel (key2);
     345             : 
     346           3 :         Key * k;
     347           3 :         Key * c;
     348             : 
     349             :         // clang-format off
     350           3 :         k=keyNew ("user:/metakey",
     351             :                 KEY_META, "t", "test1",
     352             :                 KEY_META, "a", "another",
     353             :                 KEY_META, "cya", "see the metadata later",
     354             :                 KEY_META, "mode", "0775",
     355             :                 KEY_END);
     356             :         // clang-format on
     357           3 :         c = keyNew ("user:/metacopy", KEY_END);
     358             : 
     359           3 :         succeed_if (keyGetMeta (k, "t") != 0, "could not get metakey");
     360           3 :         succeed_if (keyGetMeta (k, "a") != 0, "could not get metakey");
     361             : 
     362           3 :         succeed_if (keyGetMeta (c, "t") == 0, "could get metakey not there");
     363           3 :         succeed_if (keyGetMeta (c, "a") == 0, "could get metakey not there");
     364             : 
     365           3 :         succeed_if (keyCopyMeta (c, k, "t") == 1, "could not copy metadata");
     366           3 :         succeed_if (keyGetMeta (k, "t") == keyGetMeta (c, "t"), "not the same metadata after copy");
     367             : 
     368           3 :         succeed_if (keyCopyMeta (c, k, "a") == 1, "could not copy metadata");
     369           3 :         succeed_if (keyGetMeta (k, "a") == keyGetMeta (c, "a"), "not the same metadata after copy");
     370             : 
     371           3 :         keyDel (k);
     372           3 :         keyDel (c);
     373             : 
     374             : 
     375             :         // clang-format off
     376           3 :         k=keyNew ("user:/metakey",
     377             :                 KEY_META, "t", "test1",
     378             :                 KEY_META, "a", "another",
     379             :                 KEY_META, "cya", "see the metadata later",
     380             :                 KEY_META, "mode", "0775",
     381             :                 KEY_END);
     382           3 :         c=keyNew ("user:/metacopy",
     383             :                 KEY_META, "t", "test1",
     384             :                 KEY_META, "a", "wrong",
     385             :                 KEY_META, "old", "will stay",
     386             :                 KEY_END);
     387             :         // clang-format on
     388             : 
     389           3 :         succeed_if (keyGetMeta (k, "t") != 0, "could not get metakey");
     390           3 :         succeed_if (keyGetMeta (k, "a") != 0, "could not get metakey");
     391             : 
     392           3 :         succeed_if (keyCopyMeta (c, k, "t") == 1, "could not copy metadata");
     393           3 :         succeed_if (keyGetMeta (k, "t") == keyGetMeta (c, "t"), "not the same metadata after copy");
     394           3 :         succeed_if_same_string (keyValue (keyGetMeta (k, "t")), "test1");
     395           3 :         succeed_if_same_string (keyValue (keyGetMeta (c, "t")), "test1");
     396             : 
     397           3 :         succeed_if (keyCopyMeta (c, k, "a") == 1, "could not copy metadata");
     398           3 :         succeed_if (keyGetMeta (k, "a") == keyGetMeta (c, "a"), "not the same metadata after copy");
     399           3 :         succeed_if_same_string (keyValue (keyGetMeta (k, "a")), "another");
     400           3 :         succeed_if_same_string (keyValue (keyGetMeta (c, "a")), "another");
     401             : 
     402           3 :         succeed_if_same_string (keyValue (keyGetMeta (c, "old")), "will stay");
     403           3 :         succeed_if (keyGetMeta (c, "cya") == 0, "metakey should not be there");
     404           3 :         succeed_if (keyGetMeta (c, "mode") == 0, "metakey should not be there");
     405             : 
     406           3 :         keyDel (k);
     407           3 :         keyDel (c);
     408             : 
     409           3 :         succeed_if (key1 = keyNew ("/", KEY_END), "could not create key");
     410           3 :         succeed_if (key2 = keyNew ("/", KEY_END), "could not create key");
     411             : 
     412           3 :         succeed_if (keySetMeta (key2, "mymeta", "a longer metavalue") == sizeof ("a longer metavalue"), "could not set metavalue");
     413           3 :         succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
     414             : 
     415           3 :         succeed_if (keyCopyMeta (key2, key1, "mymeta") == 0, "could not copy metavalue");
     416             : 
     417           3 :         succeed_if (keyGetMeta (key1, "mymeta") == 0, "value of mymeta is not NULL");
     418           3 :         succeed_if (keyGetMeta (key2, "mymeta") == 0, "value of mymeta has not been cleared");
     419             : 
     420           3 :         keyDel (key1);
     421           3 :         keyDel (key2);
     422           3 : }
     423             : 
     424           3 : static void test_new (void)
     425             : {
     426           3 :         Key * key;
     427             :         // clang-format off
     428           3 :         key = keyNew ("user:/test",
     429             :                 KEY_META, "hello", "hello_world",
     430             :                 KEY_META, "mode", "0644",
     431             :                 KEY_META, "time", "1271234264",
     432             :                 KEY_META, "empty", "",
     433             :                 KEY_META, "", "empty",
     434             :                 KEY_END);
     435             :         // clang-format on
     436             : 
     437           3 :         succeed_if (!strcmp (keyValue (keyGetMeta (key, "hello")), "hello_world"), "could not receive previously set meta information");
     438           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "mode")), "0644");
     439           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "time")), "1271234264");
     440           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "empty")), "");
     441           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "")), "empty");
     442             : 
     443           3 :         keySetMeta (key, "", "full");
     444           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "")), "full");
     445             : 
     446           3 :         keySetMeta (key, "", 0);
     447           3 :         succeed_if (keyValue (keyGetMeta (key, "")) == 0, "could not remove empty metadata");
     448             : 
     449           3 :         keyDel (key);
     450             : 
     451             :         // clang-format off
     452           3 :         key = keyNew ("user:/test",
     453             :                 KEY_META, "hello", "goodbye",
     454             :                 KEY_META, "mode", "0775",
     455             :                 KEY_META, "time", "1271939923",
     456             :                 KEY_META, "empty", "",
     457             :                 KEY_META, "", "",
     458             :                 KEY_END);
     459             :         // clang-format on
     460             : 
     461           3 :         succeed_if (!strcmp (keyValue (keyGetMeta (key, "hello")), "goodbye"), "could not receive previously set meta information");
     462           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "mode")), "0775");
     463           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "time")), "1271939923");
     464           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "empty")), "");
     465           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "")), "");
     466             : 
     467           3 :         keySetMeta (key, "", "full");
     468           3 :         succeed_if_same_string (keyValue (keyGetMeta (key, "")), "full");
     469             : 
     470           3 :         keySetMeta (key, "", 0);
     471           3 :         succeed_if (keyValue (keyGetMeta (key, "")) == 0, "could not remove empty metadata");
     472             : 
     473           3 :         keyDel (key);
     474           3 : }
     475             : 
     476             : 
     477           3 : static void test_copyall (void)
     478             : {
     479           3 :         printf ("Test key meta copy all\n");
     480             : 
     481           3 :         Key * key1;
     482           3 :         Key * key2;
     483             : 
     484           3 :         succeed_if (key1 = keyNew ("/", KEY_END), "could not create key");
     485           3 :         succeed_if (key2 = keyNew ("/", KEY_END), "could not create key");
     486             : 
     487           3 :         succeed_if (keyCopyAllMeta (key2, key1) == 0, "could not do anything");
     488             : 
     489           3 :         succeed_if (keyValue (keyGetMeta (key2, "nonexist")) == 0, "should not be there");
     490             : 
     491           3 :         keyDel (key1);
     492           3 :         keyDel (key2);
     493             : 
     494             : 
     495           3 :         succeed_if (key1 = keyNew ("/", KEY_END), "could not create key");
     496           3 :         succeed_if (key2 = keyNew ("/", KEY_END), "could not create key");
     497             : 
     498           3 :         succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue") == sizeof ("a longer metavalue"), "could not set metavalue");
     499           3 :         succeed_if (keyCopyAllMeta (key2, key1) == 1, "could not copy metavalue");
     500           3 :         succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
     501           3 :         succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
     502           3 :         succeed_if (keyGetMeta (key1, "mymeta") == keyGetMeta (key2, "mymeta"), "reference to the same key");
     503             : 
     504           3 :         succeed_if (keyCopyAllMeta (key1, key2) == 1, "did nothing in the end");
     505           3 :         succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
     506           3 :         succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
     507           3 :         succeed_if (keyGetMeta (key1, "mymeta") == keyGetMeta (key2, "mymeta"), "reference to the same key");
     508             : 
     509           3 :         keyDel (key1);
     510           3 :         keyDel (key2);
     511             : 
     512             : 
     513           3 :         succeed_if (key1 = keyNew ("/", KEY_END), "could not create key");
     514           3 :         succeed_if (key2 = keyNew ("/", KEY_END), "could not create key");
     515             : 
     516           3 :         succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue") == sizeof ("a longer metavalue"), "could not set metavalue");
     517           3 :         succeed_if (keyCopyAllMeta (key2, key1) == 1, "could not copy metavalue");
     518           3 :         succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
     519           3 :         succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
     520           3 :         succeed_if (keyGetMeta (key1, "mymeta") == keyGetMeta (key2, "mymeta"), "reference to the same key");
     521             : 
     522           3 :         succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue") == sizeof ("a longer metavalue"), "could not set metavalue");
     523           3 :         succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue");
     524           3 :         succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
     525           3 :         succeed_if (keyGetMeta (key1, "mymeta") != keyGetMeta (key2, "mymeta"), "reference to another key");
     526             : 
     527           3 :         succeed_if (keySetMeta (key1, "mymeta", "a longer metavalue2") == sizeof ("a longer metavalue2"), "could not set metavalue2");
     528           3 :         succeed_if_same_string (keyValue (keyGetMeta (key1, "mymeta")), "a longer metavalue2");
     529           3 :         succeed_if_same_string (keyValue (keyGetMeta (key2, "mymeta")), "a longer metavalue");
     530           3 :         succeed_if (keyGetMeta (key1, "mymeta") != keyGetMeta (key2, "mymeta"), "reference to another key (with another value)");
     531             : 
     532           3 :         keyDel (key1);
     533           3 :         keyDel (key2);
     534             : 
     535           3 :         Key * k;
     536           3 :         Key * c;
     537             : 
     538             :         // clang-format off
     539           3 :         k=keyNew ("user:/metakey",
     540             :                 KEY_META, "t", "test1",
     541             :                 KEY_META, "a", "another",
     542             :                 KEY_META, "cya", "see the metadata later",
     543             :                 KEY_META, "mode", "0775",
     544             :                 KEY_END);
     545             :         // clang-format on
     546           3 :         c = keyNew ("user:/metacopy", KEY_END);
     547             : 
     548           3 :         succeed_if (keyGetMeta (k, "t") != 0, "could not get metakey");
     549           3 :         succeed_if (keyGetMeta (k, "a") != 0, "could not get metakey");
     550             : 
     551           3 :         succeed_if (keyGetMeta (c, "t") == 0, "could get metakey not there");
     552           3 :         succeed_if (keyGetMeta (c, "a") == 0, "could get metakey not there");
     553             : 
     554           3 :         succeed_if (keyCopyAllMeta (c, k) == 1, "could not copy metadata");
     555           3 :         succeed_if (keyGetMeta (k, "t") == keyGetMeta (c, "t"), "not the same metadata after copy");
     556           3 :         succeed_if (keyGetMeta (k, "a") == keyGetMeta (c, "a"), "not the same metadata after copy");
     557           3 :         succeed_if (keyGetMeta (k, "cya") == keyGetMeta (c, "cya"), "not the same metadata after copy");
     558           3 :         succeed_if (keyGetMeta (k, "mode") == keyGetMeta (c, "mode"), "not the same metadata after copy");
     559           3 :         succeed_if (keyValue (keyGetMeta (k, "nonexist")) == 0, "should not be there");
     560           3 :         succeed_if (keyValue (keyGetMeta (c, "nonexist")) == 0, "should not be there");
     561             : 
     562           3 :         succeed_if (keyCopyAllMeta (c, k) == 1, "could not copy metadata (again)");
     563           3 :         succeed_if (keyGetMeta (k, "t") == keyGetMeta (c, "t"), "not the same metadata after copy");
     564           3 :         succeed_if (keyGetMeta (k, "a") == keyGetMeta (c, "a"), "not the same metadata after copy");
     565           3 :         succeed_if (keyGetMeta (k, "cya") == keyGetMeta (c, "cya"), "not the same metadata after copy");
     566           3 :         succeed_if (keyGetMeta (k, "mode") == keyGetMeta (c, "mode"), "not the same metadata after copy");
     567           3 :         succeed_if (keyValue (keyGetMeta (k, "nonexist")) == 0, "should not be there");
     568           3 :         succeed_if (keyValue (keyGetMeta (c, "nonexist")) == 0, "should not be there");
     569             : 
     570           3 :         keyDel (k);
     571           3 :         keyDel (c);
     572           3 : }
     573             : 
     574           3 : static void test_type (void)
     575             : {
     576           3 :         Key * key;
     577             : 
     578           3 :         succeed_if (key = keyNew ("/", KEY_END), "could not create a new key");
     579           3 :         succeed_if (keyValue (keyGetMeta (key, "binary")) == 0, "wrong type after key creation");
     580           3 :         succeed_if (keySetString (key, "mystring") == sizeof ("mystring"), "could not set string");
     581           3 :         succeed_if (keyValue (keyGetMeta (key, "binary")) == 0, "wrong type after setting string");
     582           3 :         succeed_if (keySetBinary (key, "mystring", sizeof ("mystring")) == sizeof ("mystring"), "could not set binary");
     583           3 :         succeed_if (keyValue (keyGetMeta (key, "binary")) != 0, "wrong type after setting string");
     584             : 
     585           3 :         keyDel (key);
     586           3 : }
     587             : 
     588           3 : static void test_keyMeta (void)
     589             : {
     590           3 :         Key * key = keyNew ("/", KEY_END);
     591             : 
     592           3 :         KeySet * meta = keyMeta (key);
     593             : 
     594           3 :         succeed_if (meta, "meta keyset is null");
     595           3 :         succeed_if (ksGetSize (meta) == 0, "meta keyset is not empty");
     596           3 :         keyDel (key);
     597             : 
     598             :         // clang-format off
     599           3 :         key = keyNew ("user:/test",
     600             :                 KEY_META, "hello", "hello_world",
     601             :                 KEY_META, "mode", "0644",
     602             :                 KEY_META, "time", "1271234264",
     603             :                 KEY_META, "empty", "",
     604             :                 KEY_META, "", "empty",
     605             :                 KEY_END);
     606             :         // clang-format on
     607             : 
     608           3 :         meta = keyMeta (key);
     609             : 
     610           3 :         const char * value = keyString (ksLookupByName (meta, "meta:/hello", 0));
     611           3 :         succeed_if (!strcmp (value, "hello_world"), "unexpected value");
     612             : 
     613           3 :         value = keyString (ksLookupByName (meta, "meta:/mode", 0));
     614           3 :         succeed_if (!strcmp (value, "0644"), "unexpected value");
     615             : 
     616           3 :         value = keyString (ksLookupByName (meta, "meta:/time", 0));
     617           3 :         succeed_if (!strcmp (value, "1271234264"), "unexpected value");
     618             : 
     619           3 :         value = keyString (ksLookupByName (meta, "meta:/empty", 0));
     620           3 :         succeed_if (!strcmp (value, ""), "unexpected value");
     621             : 
     622           3 :         value = keyString (ksLookupByName (meta, "meta:/", 0));
     623           3 :         succeed_if (!strcmp (value, "empty"), "unexpected value");
     624             : 
     625           3 :         succeed_if (ksGetSize (meta) == 5, "unexpected meta keyset size");
     626             : 
     627           3 :         keyDel (key);
     628           3 : }
     629             : 
     630           3 : int main (int argc, char ** argv)
     631             : {
     632           3 :         printf ("KEY META ABI TESTS\n");
     633           3 :         printf ("==================\n\n");
     634             : 
     635           3 :         init (argc, argv);
     636             : 
     637           3 :         test_basic ();
     638           3 :         test_null_pointer ();
     639           3 :         test_iterate ();
     640           3 :         test_size ();
     641           3 :         test_dup ();
     642           3 :         test_examples ();
     643           3 :         test_type ();
     644           3 :         test_copy ();
     645           3 :         test_new ();
     646           3 :         test_copyall ();
     647           3 :         test_keyMeta ();
     648             : 
     649             : 
     650           3 :         printf ("\ntestabi_meta RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);
     651             : 
     652           3 :         return nbError;
     653             : }

Generated by: LCOV version 1.13