LCOV - code coverage report
Current view: top level - tests/ctest - test_backend.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 132 132 100.0 %
Date: 2019-09-12 12:28:41 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Test cases for how to build a backend out of system/elektra/mountpoints/<name>
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include <../../src/libs/elektra/backend.c>
      10             : #include <tests_internal.h>
      11             : 
      12             : 
      13           2 : KeySet * set_simple (void)
      14             : {
      15           2 :         return ksNew (50, keyNew ("system/elektra/mountpoints/simple", KEY_END),
      16             : 
      17             :                       keyNew ("system/elektra/mountpoints/simple/config", KEY_END),
      18             :                       keyNew ("system/elektra/mountpoints/simple/config/anything", KEY_VALUE, "backend", KEY_END),
      19             :                       keyNew ("system/elektra/mountpoints/simple/config/more", KEY_END),
      20             :                       keyNew ("system/elektra/mountpoints/simple/config/more/config", KEY_END),
      21             :                       keyNew ("system/elektra/mountpoints/simple/config/more/config/below", KEY_END),
      22             :                       keyNew ("system/elektra/mountpoints/simple/config/path", KEY_END),
      23             : 
      24             :                       keyNew ("system/elektra/mountpoints/simple/errorplugins", KEY_END),
      25             :                       keyNew ("system/elektra/mountpoints/simple/errorplugins/#1" KDB_DEFAULT_STORAGE, KEY_END),
      26             : 
      27             :                       keyNew ("system/elektra/mountpoints/simple/getplugins", KEY_END),
      28             :                       keyNew ("system/elektra/mountpoints/simple/getplugins/#1" KDB_DEFAULT_STORAGE, KEY_END),
      29             :                       keyNew ("system/elektra/mountpoints/simple/getplugins/#1" KDB_DEFAULT_STORAGE "/config", KEY_END),
      30             :                       keyNew ("system/elektra/mountpoints/simple/getplugins/#1" KDB_DEFAULT_STORAGE "/config/anything", KEY_VALUE, "plugin",
      31             :                               KEY_END),
      32             :                       keyNew ("system/elektra/mountpoints/simple/getplugins/#1" KDB_DEFAULT_STORAGE "/config/more", KEY_END),
      33             :                       keyNew ("system/elektra/mountpoints/simple/getplugins/#1" KDB_DEFAULT_STORAGE "/config/more/config", KEY_END),
      34             :                       keyNew ("system/elektra/mountpoints/simple/getplugins/#1" KDB_DEFAULT_STORAGE "/config/more/config/below", KEY_END),
      35             :                       keyNew ("system/elektra/mountpoints/simple/getplugins/#1" KDB_DEFAULT_STORAGE "/config/path", KEY_END),
      36             : 
      37             :                       keyNew ("system/elektra/mountpoints/simple/mountpoint", KEY_VALUE, "user/tests/backend/simple", KEY_END),
      38             : 
      39             :                       keyNew ("system/elektra/mountpoints/simple/setplugins", KEY_END),
      40             :                       keyNew ("system/elektra/mountpoints/simple/setplugins/#1" KDB_DEFAULT_STORAGE, KEY_END),
      41             : 
      42             :                       keyNew ("system/elektra/mountpoints/simple/errorplugins", KEY_END),
      43             :                       keyNew ("system/elektra/mountpoints/simple/errorplugins/#1" KDB_DEFAULT_STORAGE, KEY_END), KS_END);
      44             : }
      45             : 
      46           8 : KeySet * set_pluginconf (void)
      47             : {
      48           8 :         return ksNew (10, keyNew ("system/anything", KEY_VALUE, "backend", KEY_END), keyNew ("system/more", KEY_END),
      49             :                       keyNew ("system/more/config", KEY_END), keyNew ("system/more/config/below", KEY_END), keyNew ("system/path", KEY_END),
      50             :                       keyNew ("user/anything", KEY_VALUE, "plugin", KEY_END), keyNew ("user/more", KEY_END),
      51             :                       keyNew ("user/more/config", KEY_END), keyNew ("user/more/config/below", KEY_END), keyNew ("user/path", KEY_END),
      52             :                       KS_END);
      53             : }
      54             : 
      55           2 : static void test_simple (void)
      56             : {
      57           2 :         printf ("Test simple building of backend\n");
      58             : 
      59           2 :         KeySet * modules = ksNew (0, KS_END);
      60           2 :         elektraModulesInit (modules, 0);
      61             : 
      62           2 :         KeySet * global = ksNew (0, KS_END);
      63           2 :         Key * errorKey = 0;
      64           2 :         Backend * backend = backendOpen (set_simple (), modules, global, errorKey);
      65           2 :         succeed_if (backend->errorplugins[0] == 0, "there should be no plugin");
      66           2 :         succeed_if (backend->errorplugins[2] == 0, "there should be no plugin");
      67           2 :         succeed_if (backend->errorplugins[3] == 0, "there should be no plugin");
      68           2 :         succeed_if (backend->errorplugins[4] == 0, "there should be no plugin");
      69           2 :         succeed_if (backend->errorplugins[5] == 0, "there should be no plugin");
      70           2 :         succeed_if (backend->errorplugins[6] == 0, "there should be no plugin");
      71           2 :         succeed_if (backend->errorplugins[7] == 0, "there should be no plugin");
      72           2 :         succeed_if (backend->errorplugins[8] == 0, "there should be no plugin");
      73           2 :         succeed_if (backend->errorplugins[9] == 0, "there should be no plugin");
      74           2 :         exit_if_fail (backend->errorplugins[1] != 0, "there should be a plugin");
      75             : 
      76           2 :         succeed_if (backend->getplugins[0] == 0, "there should be no plugin");
      77           2 :         succeed_if (backend->getplugins[2] == 0, "there should be no plugin");
      78           2 :         succeed_if (backend->getplugins[3] == 0, "there should be no plugin");
      79           2 :         succeed_if (backend->getplugins[4] == 0, "there should be no plugin");
      80           2 :         succeed_if (backend->getplugins[5] == 0, "there should be no plugin");
      81           2 :         succeed_if (backend->getplugins[6] == 0, "there should be no plugin");
      82           2 :         succeed_if (backend->getplugins[7] == 0, "there should be no plugin");
      83           2 :         succeed_if (backend->getplugins[8] == 0, "there should be no plugin");
      84           2 :         succeed_if (backend->getplugins[9] == 0, "there should be no plugin");
      85           2 :         exit_if_fail (backend->getplugins[1] != 0, "there should be a plugin");
      86             : 
      87           2 :         succeed_if (backend->setplugins[0] == 0, "there should be no plugin");
      88           2 :         succeed_if (backend->setplugins[2] == 0, "there should be no plugin");
      89           2 :         succeed_if (backend->setplugins[3] == 0, "there should be no plugin");
      90           2 :         succeed_if (backend->setplugins[4] == 0, "there should be no plugin");
      91           2 :         succeed_if (backend->setplugins[5] == 0, "there should be no plugin");
      92           2 :         succeed_if (backend->setplugins[6] == 0, "there should be no plugin");
      93           2 :         succeed_if (backend->setplugins[7] == 0, "there should be no plugin");
      94           2 :         succeed_if (backend->setplugins[8] == 0, "there should be no plugin");
      95           2 :         succeed_if (backend->setplugins[9] == 0, "there should be no plugin");
      96           2 :         exit_if_fail (backend->setplugins[1] != 0, "there should be a plugin");
      97             : 
      98             :         Key * mp;
      99           2 :         succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found");
     100           2 :         succeed_if_same_string (keyName (mp), "user/tests/backend/simple");
     101           2 :         succeed_if_same_string (keyString (mp), "simple");
     102             : 
     103           2 :         Plugin * plugin = backend->getplugins[1];
     104             : 
     105           2 :         KeySet * test_config = set_pluginconf ();
     106           2 :         KeySet * config = elektraPluginGetConfig (plugin);
     107           2 :         succeed_if (config != 0, "there should be a config");
     108           2 :         compare_keyset (config, test_config);
     109           2 :         ksDel (test_config);
     110             : 
     111           2 :         succeed_if (plugin->kdbGet != 0, "no get pointer");
     112           2 :         succeed_if (plugin->kdbSet != 0, "no set pointer");
     113             : 
     114           2 :         backendClose (backend, errorKey);
     115           2 :         elektraModulesClose (modules, 0);
     116           2 :         ksDel (modules);
     117           2 :         ksDel (global);
     118           2 : }
     119             : 
     120           2 : static void test_default (void)
     121             : {
     122           2 :         printf ("Test default " KDB_DEFAULT_STORAGE "\n");
     123             : 
     124           2 :         KeySet * modules = ksNew (0, KS_END);
     125           2 :         elektraModulesInit (modules, 0);
     126             : 
     127             : 
     128           2 :         Plugin * plugin = elektraPluginOpen (KDB_DEFAULT_STORAGE, modules, set_pluginconf (), 0);
     129           2 :         exit_if_fail (plugin, "KDB_DEFAULT_STORAGE: " KDB_DEFAULT_STORAGE " plugin could not be loaded");
     130             : 
     131           2 :         KeySet * test_config = set_pluginconf ();
     132           2 :         KeySet * config = elektraPluginGetConfig (plugin);
     133           2 :         succeed_if (config != 0, "there should be a config");
     134           2 :         compare_keyset (config, test_config);
     135           2 :         ksDel (test_config);
     136             : 
     137           2 :         succeed_if (plugin->kdbGet != 0, "no get pointer");
     138           2 :         succeed_if (plugin->kdbSet != 0, "no set pointer");
     139             : 
     140           2 :         elektraPluginClose (plugin, 0);
     141             : 
     142           2 :         KeySet * global = ksNew (0, KS_END);
     143           2 :         Backend * backend = backendOpenDefault (modules, global, KDB_DB_FILE, 0);
     144             : 
     145             :         Key * mp;
     146           2 :         succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found");
     147           2 :         succeed_if_same_string (keyName (mp), "");
     148           2 :         succeed_if_same_string (keyString (mp), "default");
     149             : 
     150           2 :         backendClose (backend, 0);
     151           2 :         elektraModulesClose (modules, 0);
     152           2 :         ksDel (modules);
     153           2 :         ksDel (global);
     154           2 : }
     155             : 
     156             : 
     157           2 : KeySet * set_backref (void)
     158             : {
     159           2 :         return ksNew (
     160             :                 50, keyNew ("system/elektra/mountpoints/backref", KEY_END),
     161             : 
     162             :                 keyNew ("system/elektra/mountpoints/backref/config", KEY_END),
     163             :                 keyNew ("system/elektra/mountpoints/backref/config/anything", KEY_VALUE, "backend", KEY_END),
     164             :                 keyNew ("system/elektra/mountpoints/backref/config/more", KEY_END),
     165             :                 keyNew ("system/elektra/mountpoints/backref/config/more/config", KEY_END),
     166             :                 keyNew ("system/elektra/mountpoints/backref/config/more/config/below", KEY_END),
     167             :                 keyNew ("system/elektra/mountpoints/backref/config/path", KEY_END),
     168             : 
     169             :                 keyNew ("system/elektra/mountpoints/backref/errorplugins", KEY_END),
     170             :                 keyNew ("system/elektra/mountpoints/backref/errorplugins/#1#" KDB_DEFAULT_STORAGE "#" KDB_DEFAULT_STORAGE "#", KEY_VALUE,
     171             :                         "introduce reference", KEY_END),
     172             :                 keyNew ("system/elektra/mountpoints/backref/errorplugins/#1#" KDB_DEFAULT_STORAGE "#" KDB_DEFAULT_STORAGE "#/config",
     173             :                         KEY_END),
     174             :                 keyNew ("system/elektra/mountpoints/backref/errorplugins/#1#" KDB_DEFAULT_STORAGE "#" KDB_DEFAULT_STORAGE
     175             :                         "#/config/anything",
     176             :                         KEY_VALUE, "plugin", KEY_END),
     177             :                 keyNew ("system/elektra/mountpoints/backref/errorplugins/#1#" KDB_DEFAULT_STORAGE "#" KDB_DEFAULT_STORAGE "#/config/more",
     178             :                         KEY_END),
     179             :                 keyNew ("system/elektra/mountpoints/backref/errorplugins/#1#" KDB_DEFAULT_STORAGE "#" KDB_DEFAULT_STORAGE
     180             :                         "#/config/more/config",
     181             :                         KEY_END),
     182             :                 keyNew ("system/elektra/mountpoints/backref/errorplugins/#1#" KDB_DEFAULT_STORAGE "#" KDB_DEFAULT_STORAGE
     183             :                         "#/config/more/config/below",
     184             :                         KEY_END),
     185             :                 keyNew ("system/elektra/mountpoints/backref/errorplugins/#1#" KDB_DEFAULT_STORAGE "#" KDB_DEFAULT_STORAGE "#/config/path",
     186             :                         KEY_END),
     187             : 
     188             :                 keyNew ("system/elektra/mountpoints/backref/getplugins", KEY_END),
     189             :                 keyNew ("system/elektra/mountpoints/backref/getplugins/#1#" KDB_DEFAULT_STORAGE, KEY_VALUE, "backend", KEY_END),
     190             : 
     191             :                 keyNew ("system/elektra/mountpoints/backref/mountpoint", KEY_VALUE, "user/tests/backend/backref", KEY_END),
     192             : 
     193             :                 keyNew ("system/elektra/mountpoints/backref/setplugins", KEY_END),
     194             :                 keyNew ("system/elektra/mountpoints/backref/setplugins/#1#" KDB_DEFAULT_STORAGE, KEY_VALUE, "reference to other default",
     195             :                         KEY_END),
     196             :                 KS_END);
     197             : }
     198             : 
     199           2 : static void test_backref (void)
     200             : {
     201           2 :         printf ("Test back references\n");
     202             : 
     203           2 :         KeySet * modules = ksNew (0, KS_END);
     204           2 :         elektraModulesInit (modules, 0);
     205             : 
     206           2 :         KeySet * global = ksNew (0, KS_END);
     207           2 :         Backend * backend = backendOpen (set_backref (), modules, global, 0);
     208           2 :         succeed_if (backend != 0, "there should be a backend");
     209           2 :         succeed_if (backend->getplugins[0] == 0, "there should be no plugin");
     210           2 :         exit_if_fail (backend->getplugins[1] != 0, "there should be a plugin");
     211           2 :         succeed_if (backend->getplugins[2] == 0, "there should be no plugin");
     212             : 
     213           2 :         succeed_if (backend->setplugins[0] == 0, "there should be no plugin");
     214           2 :         exit_if_fail (backend->setplugins[1] != 0, "there should be a plugin");
     215           2 :         succeed_if (backend->setplugins[2] == 0, "there should be no plugin");
     216             : 
     217             :         Key * mp;
     218           2 :         succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found");
     219           2 :         succeed_if_same_string (keyName (mp), "user/tests/backend/backref");
     220           2 :         succeed_if_same_string (keyString (mp), "backref");
     221             : 
     222           2 :         Plugin * plugin1 = backend->getplugins[1];
     223           2 :         Plugin * plugin2 = backend->setplugins[1];
     224           2 :         Plugin * plugin3 = backend->errorplugins[1];
     225             : 
     226           2 :         succeed_if (plugin1 != 0, "there should be a plugin");
     227           2 :         succeed_if (plugin2 != 0, "there should be a plugin");
     228           2 :         succeed_if (plugin3 != 0, "there should be a plugin");
     229             : 
     230           2 :         succeed_if (plugin1 == plugin2, "it should be the same plugin");
     231           2 :         succeed_if (plugin2 == plugin3, "it should be the same plugin");
     232           2 :         succeed_if (plugin1 == plugin3, "it should be the same plugin");
     233             : 
     234           2 :         succeed_if (plugin1->refcounter == 3, "ref counter should be 3");
     235             : 
     236           2 :         KeySet * test_config = set_pluginconf ();
     237           2 :         KeySet * config = elektraPluginGetConfig (plugin1);
     238           2 :         succeed_if (config != 0, "there should be a config");
     239           2 :         compare_keyset (config, test_config);
     240           2 :         ksDel (test_config);
     241             : 
     242           2 :         succeed_if (plugin1->kdbGet != 0, "no get pointer");
     243           2 :         succeed_if (plugin1->kdbSet != 0, "no set pointer");
     244           2 :         succeed_if (plugin2->kdbGet != 0, "no get pointer");
     245           2 :         succeed_if (plugin2->kdbSet != 0, "no set pointer");
     246             : 
     247           2 :         backendClose (backend, 0);
     248           2 :         elektraModulesClose (modules, 0);
     249           2 :         ksDel (modules);
     250           2 :         ksDel (global);
     251           2 : }
     252             : 
     253           2 : int main (int argc, char ** argv)
     254             : {
     255           2 :         printf ("  BACKEND   TESTS\n");
     256           2 :         printf ("====================\n\n");
     257             : 
     258           2 :         init (argc, argv);
     259             : 
     260           2 :         test_simple ();
     261           2 :         test_default ();
     262           2 :         test_backref ();
     263             : 
     264           2 :         printf ("\ntest_backend RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);
     265             : 
     266           2 :         return nbError;
     267             : }

Generated by: LCOV version 1.13