LCOV - code coverage report
Current view: top level - src/bindings/glib/tests - testglib_key.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 202 202 100.0 %
Date: 2022-05-21 16:19:22 Functions: 11 11 100.0 %

          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 "gelektra-key.h"
      10             : #include <glib-object.h>
      11             : #include <tests.h>
      12             : 
      13           2 : static void test_ctor (void)
      14             : {
      15           2 :         GElektraKey * key;
      16             : 
      17           2 :         key = gelektra_key_new (NULL);
      18           2 :         succeed_if (key != NULL, "unable to create key");
      19           2 :         succeed_if (gelektra_key_isvalid (key), "key should be valid");
      20           2 :         g_object_unref (key);
      21             : 
      22           2 :         key = g_object_new (GELEKTRA_TYPE_KEY, NULL);
      23           2 :         succeed_if (key != NULL, "unable to create key");
      24           2 :         succeed_if (gelektra_key_isvalid (key), "key should be valid");
      25           2 :         g_object_unref (key);
      26             : 
      27           2 :         key = gelektra_key_new ("wrongname", GELEKTRA_KEY_END);
      28           2 :         succeed_if (key == NULL, "created invalid key");
      29             : 
      30           2 :         Key * ckey = keyNew ("/", KEY_END);
      31           2 :         key = gelektra_key_make (ckey);
      32           2 :         succeed_if (key->key == ckey, "new key not wrapped");
      33           2 :         g_object_unref (key);
      34             : 
      35           2 :         key = gelektra_key_new ("user:/foo", GELEKTRA_KEY_END);
      36           2 :         succeed_if (key != NULL, "unable to create key");
      37           2 :         succeed_if (gelektra_key_isvalid (key), "key should be valid");
      38           2 :         succeed_if (gelektra_key_getref (key) == 1, "refcount should be 1");
      39           2 :         g_object_unref (key);
      40             : 
      41           2 :         key = gelektra_key_new ("/cascading", GELEKTRA_KEY_END);
      42           2 :         succeed_if (key != NULL, "unable to create key");
      43           2 :         succeed_if (gelektra_key_isvalid (key), "key should be valid");
      44           2 :         succeed_if (gelektra_key_getref (key) == 1, "refcount should be 1");
      45           2 :         g_object_unref (key);
      46             : 
      47           2 :         key = gelektra_key_new ("spec:/key", GELEKTRA_KEY_END);
      48           2 :         succeed_if (key != NULL, "unable to create key");
      49           2 :         succeed_if (gelektra_key_isvalid (key), "key should be valid");
      50           2 :         succeed_if (gelektra_key_getref (key) == 1, "refcount should be 1");
      51           2 :         g_object_unref (key);
      52             : 
      53           2 :         key = gelektra_key_new ("proc:/key", GELEKTRA_KEY_END);
      54           2 :         succeed_if (key != NULL, "unable to create key");
      55           2 :         succeed_if (gelektra_key_isvalid (key), "key should be valid");
      56           2 :         succeed_if (gelektra_key_getref (key) == 1, "refcount should be 1");
      57           2 :         g_object_unref (key);
      58             : 
      59           2 :         key = gelektra_key_new ("dir:/key", GELEKTRA_KEY_END);
      60           2 :         succeed_if (key != NULL, "unable to create key");
      61           2 :         succeed_if (gelektra_key_isvalid (key), "key should be valid");
      62           2 :         succeed_if (gelektra_key_getref (key) == 1, "refcount should be 1");
      63           2 :         g_object_unref (key);
      64             : 
      65           2 :         key = gelektra_key_new ("user:/key", GELEKTRA_KEY_END);
      66           2 :         succeed_if (key != NULL, "unable to create key");
      67           2 :         succeed_if (gelektra_key_isvalid (key), "key should be valid");
      68           2 :         succeed_if (gelektra_key_getref (key) == 1, "refcount should be 1");
      69           2 :         g_object_unref (key);
      70             : 
      71           2 :         key = gelektra_key_new ("system:/key", GELEKTRA_KEY_END);
      72           2 :         succeed_if (key != NULL, "unable to create key");
      73           2 :         succeed_if (gelektra_key_isvalid (key), "key should be valid");
      74           2 :         succeed_if (gelektra_key_getref (key) == 1, "refcount should be 1");
      75           2 :         g_object_unref (key);
      76           2 : }
      77             : 
      78             : static GElektraKey * g_key = NULL;
      79             : static GElektraKey * g_bkey = NULL;
      80             : 
      81           2 : static void create_global_keys (void)
      82             : {
      83           2 :         g_key = gelektra_key_new ("user:/key", GELEKTRA_KEY_VALUE, "value", GELEKTRA_KEY_META, "comment/#0", "mycomment", GELEKTRA_KEY_META,
      84             :                                   "by", "manuel", GELEKTRA_KEY_END);
      85           2 :         succeed_if (g_key != NULL, "unable to create key");
      86           2 :         succeed_if (gelektra_key_isvalid (g_key), "key should be valid");
      87           2 :         succeed_if (gelektra_key_getref (g_key) == 1, "refcount should be 1");
      88             : 
      89           2 :         g_bkey = gelektra_key_new ("system:/bkey", GELEKTRA_KEY_BINARY, GELEKTRA_KEY_VALUE, "bvalue\0\0", GELEKTRA_KEY_END);
      90           2 :         succeed_if (g_bkey != NULL, "unable to create key");
      91           2 :         succeed_if (gelektra_key_isvalid (g_bkey), "key should be valid");
      92           2 :         succeed_if (gelektra_key_getref (g_bkey) == 1, "refcount should be 1");
      93           2 : }
      94             : 
      95           2 : static void test_props (void)
      96             : {
      97           2 :         gchar *name, *basename;
      98           2 :         g_object_get (g_key, "name", &name, "basename", &basename, NULL);
      99           2 :         succeed_if (!strcmp (name, "user:/key"), "wrong value");
     100           2 :         succeed_if (!strcmp (basename, "key"), "wrong value");
     101           2 :         g_free (name);
     102           2 :         g_free (basename);
     103             : 
     104           2 :         GElektraKey * key = g_object_new (GELEKTRA_TYPE_KEY, NULL);
     105           2 :         gelektra_key_setname (key, "user:/foo");
     106           2 :         gelektra_key_setbasename (key, "bar");
     107           2 :         g_object_get (key, "name", &name, NULL);
     108           2 :         succeed_if (!strcmp (name, "user:/bar"), "wrong value");
     109           2 :         g_free (name);
     110           2 :         g_object_unref (key);
     111           2 : }
     112             : 
     113           2 : static void test_basic (void)
     114             : {
     115           2 :         GElektraKey * key;
     116             : 
     117           2 :         key = gelektra_key_dup (g_key, KEY_CP_ALL);
     118           2 :         gelektra_key_incref (key);
     119           2 :         succeed_if (gelektra_key_getref (key) == 2, "refcount should be 2");
     120           2 :         succeed_if (gelektra_key_getref (g_key) == 1, "refcount should be still 1");
     121           2 :         gelektra_key_decref (key);
     122           2 :         succeed_if (gelektra_key_getref (key) == 1, "refcount should be 1 again");
     123           2 :         g_object_unref (key);
     124             : 
     125           2 :         gchar * name;
     126           2 :         key = gelektra_key_new ("user:/bar", GELEKTRA_KEY_END);
     127           2 :         gelektra_key_copy (g_key, key, KEY_CP_ALL);
     128           2 :         g_object_get (key, "name", &name, NULL);
     129           2 :         succeed_if (!strcmp (name, "user:/key"), "wrong value");
     130           2 :         g_free (name);
     131             : 
     132           2 :         gelektra_key_clear (key);
     133           2 :         g_object_get (key, "name", &name, NULL);
     134           2 :         succeed_if (!strcmp (name, "/"), "wrong value");
     135           2 :         g_free (name);
     136             : 
     137           2 :         g_object_unref (key);
     138           2 : }
     139             : 
     140           2 : static void test_operators (void)
     141             : {
     142           2 :         succeed_if (!gelektra_key_equal (g_key, g_bkey), "keys shouldn't be equal");
     143           2 :         succeed_if (gelektra_key_cmp (g_key, g_bkey) != 0, "keys shouldn't be equal");
     144             : 
     145           2 :         GElektraKey * key = gelektra_key_dup (g_key, KEY_CP_ALL);
     146           2 :         succeed_if (gelektra_key_equal (g_key, key), "keys should be equal");
     147           2 :         succeed_if (gelektra_key_cmp (g_key, key) == 0, "keys should be equal");
     148           2 :         g_object_unref (key);
     149           2 : }
     150             : 
     151           2 : static void test_name_manipulation (void)
     152             : {
     153           2 :         succeed_if (gelektra_key_getnamesize (g_key) == sizeof ("user:/key"), "wrong size");
     154           2 :         succeed_if (gelektra_key_getbasenamesize (g_key) == sizeof ("key"), "wrong size");
     155           2 : }
     156             : 
     157             : 
     158           2 : static void test_value_operations (void)
     159             : {
     160           2 :         gchar * data;
     161           2 :         void * bdata;
     162           2 :         gssize size;
     163             : 
     164             :         /* string value tests */
     165           2 :         size = gelektra_key_getvaluesize (g_key);
     166           2 :         succeed_if (size > 0, "wrong value size");
     167           2 :         data = g_malloc0 (size);
     168           2 :         gelektra_key_getstring (g_key, data, size);
     169           2 :         succeed_if (!strncmp (data, "value", size), "invalid key value");
     170           2 :         g_free (data);
     171             : 
     172           2 :         succeed_if (!memcmp (gelektra_key_getvalue (g_key), "value", sizeof ("value")), "invalid key value");
     173             : 
     174           2 :         gelektra_key_setstring (g_key, "value2");
     175             : 
     176           2 :         data = gelektra_key_gi_getstring (g_key);
     177           2 :         succeed_if (!strcmp (data, "value2"), "invalid key value");
     178           2 :         g_free (data);
     179             : 
     180             :         /* binary value tests */
     181           2 :         size = gelektra_key_getvaluesize (g_bkey);
     182           2 :         bdata = g_malloc0 (size);
     183           2 :         gelektra_key_getbinary (g_bkey, bdata, size);
     184           2 :         succeed_if (!memcmp (bdata, "bvalue\0\0", size), "invalid key value");
     185           2 :         g_free (bdata);
     186             : 
     187           2 :         succeed_if (!memcmp (gelektra_key_getvalue (g_bkey), "bvalue\0\0", size), "invalid key value");
     188             : 
     189           2 :         gelektra_key_setbinary (g_bkey, "bvalue2\0\0", sizeof ("bvalue2\0\0"));
     190             : 
     191           2 :         bdata = gelektra_key_gi_getbinary (g_bkey, &size);
     192           2 :         succeed_if (size == sizeof ("bvalue2\0\0"), "invalid key size");
     193           2 :         succeed_if (!memcmp (bdata, "bvalue2\0\0", size), "invalid key value");
     194           2 :         g_free (bdata);
     195           2 : }
     196             : 
     197           2 : static void test_meta_data (void)
     198             : {
     199           2 :         GElektraKey *meta, *key;
     200             : 
     201             :         /* get tests */
     202           2 :         succeed_if (gelektra_key_hasmeta (g_key, "by"), "no meta");
     203             : 
     204           2 :         meta = gelektra_key_getmeta (g_key, "by");
     205           2 :         succeed_if (gelektra_key_getref (meta) == 2, "refcount should be 2");
     206           2 :         succeed_if (!memcmp (gelektra_key_getvalue (meta), "manuel", sizeof ("manuel")), "invalid metavalue");
     207           2 :         g_object_unref (meta);
     208             : 
     209           2 :         guint metacnt = 0;
     210           2 :         gelektra_key_rewindmeta (g_key);
     211           6 :         while ((meta = gelektra_key_nextmeta (g_key)) != NULL)
     212             :         {
     213           4 :                 GElektraKey * curmeta = gelektra_key_currentmeta (g_key);
     214           4 :                 succeed_if (meta->key == curmeta->key, "meta iterators returned different keys");
     215           4 :                 g_object_unref (curmeta);
     216             : 
     217           4 :                 ++metacnt;
     218           4 :                 g_object_unref (meta);
     219             :         }
     220           2 :         succeed_if_fmt (metacnt == 2, "incorrect number of metadata: %d", metacnt);
     221             : 
     222           2 :         gelektra_key_setmeta (g_key, "by", "gelektra");
     223           2 :         meta = gelektra_key_getmeta (g_key, "by");
     224           2 :         succeed_if (!memcmp (gelektra_key_getvalue (meta), "gelektra", sizeof ("gelektra")), "invalid metavalue");
     225           2 :         g_object_unref (meta);
     226             : 
     227             :         /* set tests */
     228           2 :         key = g_object_new (GELEKTRA_TYPE_KEY, NULL);
     229             : 
     230           2 :         gelektra_key_copymeta (g_key, key, "by");
     231           2 :         meta = gelektra_key_getmeta (g_key, "by");
     232           2 :         succeed_if (!memcmp (gelektra_key_getvalue (meta), "gelektra", sizeof ("gelektra")), "invalid metavalue");
     233           2 :         g_object_unref (meta);
     234             : 
     235           2 :         g_object_unref (key);
     236           2 : }
     237             : 
     238           2 : static void test_validating (void)
     239             : {
     240           2 :         succeed_if (!gelektra_key_isnull (g_key), "key is null");
     241           2 :         succeed_if (gelektra_key_isvalid (g_key), "key is not valid");
     242           2 :         succeed_if (gelektra_key_isuser (g_key), "key is not user");
     243           2 :         succeed_if (!gelektra_key_issystem (g_key), "key is system");
     244           2 :         succeed_if (gelektra_key_isstring (g_key), "key is not string");
     245           2 :         succeed_if (!gelektra_key_isbinary (g_key), "key is binary");
     246             : 
     247           2 :         succeed_if (!gelektra_key_isuser (g_bkey), "key is user");
     248           2 :         succeed_if (gelektra_key_issystem (g_bkey), "key is not system");
     249           2 :         succeed_if (!gelektra_key_isstring (g_bkey), "key is string");
     250           2 :         succeed_if (gelektra_key_isbinary (g_bkey), "key is not binary");
     251             : 
     252           2 :         GElektraKey * key = gelektra_key_new ("user:/key/glib/edy", GELEKTRA_KEY_END);
     253           2 :         succeed_if (gelektra_key_isbelow (key, g_key), "key not below g_key");
     254           2 :         succeed_if (gelektra_key_isbeloworsame (key, g_key), "key not below g_key");
     255           2 :         succeed_if (gelektra_key_isbeloworsame (g_key, g_key), "key is not key :)");
     256           2 :         succeed_if (!gelektra_key_isdirectbelow (key, g_key), "key not direct below g_key");
     257           2 :         g_object_unref (key);
     258           2 : }
     259             : 
     260           2 : static void destroy_global_keys (void)
     261             : {
     262           2 :         g_object_unref (g_key);
     263           2 :         g_object_unref (g_bkey);
     264           2 : }
     265             : 
     266           2 : int main (int argc, char ** argv)
     267             : {
     268           2 :         printf ("KEY TESTS\n");
     269           2 :         printf ("===============\n\n");
     270             : 
     271           2 :         init (argc, argv);
     272             : 
     273           2 :         test_ctor ();
     274             : 
     275           2 :         create_global_keys ();
     276           2 :         test_props ();
     277           2 :         test_basic ();
     278           2 :         test_operators ();
     279           2 :         test_name_manipulation ();
     280           2 :         test_value_operations ();
     281           2 :         test_meta_data ();
     282           2 :         test_validating ();
     283           2 :         destroy_global_keys ();
     284             : 
     285           2 :         printf ("\n%s RESULTS: %d test(s) done. %d error(s).\n", argv[0], nbTest, nbError);
     286           2 :         return nbError;
     287             : }

Generated by: LCOV version 1.13