LCOV - code coverage report
Current view: top level - src/plugins/blockresolver - testmod_blockresolver.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 59 59 100.0 %
Date: 2019-09-12 12:28:41 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for blockresolver plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include <stdio.h>
      11             : #include <stdlib.h>
      12             : #include <string.h>
      13             : 
      14             : #include <kdbconfig.h>
      15             : #include <kdbinternal.h>
      16             : 
      17             : #include <tests_plugin.h>
      18             : 
      19           2 : static void test_BlockresolverRead (char * fileName)
      20             : {
      21           2 :         Key * parentKey = keyNew ("system/test/blockresolver-read", KEY_VALUE, srcdir_file (fileName), KEY_END);
      22           2 :         KeySet * conf = ksNew (10, keyNew ("system/path", KEY_VALUE, srcdir_file (fileName), KEY_END),
      23             :                                keyNew ("system/identifier", KEY_VALUE, "### block config", KEY_END), KS_END);
      24           2 :         KeySet * modules = ksNew (0, KS_END);
      25           2 :         KeySet * ks = ksNew (0, KS_END);
      26           2 :         elektraModulesInit (modules, 0);
      27           2 :         Plugin * resolver = elektraPluginOpen ("blockresolver", modules, ksDup (conf), 0);
      28           2 :         succeed_if (resolver->kdbGet (resolver, ks, parentKey) >= 0, "blockresolver->kdbGet failed");
      29           2 :         output_warnings (parentKey);
      30           2 :         output_error (parentKey);
      31           2 :         Plugin * storage = elektraPluginOpen ("ini", modules, ksNew (0, KS_END), 0);
      32           2 :         succeed_if (storage->kdbGet (storage, ks, parentKey) >= 0, "storage->kdbGet failed");
      33           2 :         succeed_if (!strcmp (keyString (ksLookupByName (ks, "system/test/blockresolver-read/section/key", 0)), "inside block"),
      34             :                     "blockresolver failed to resolve requested block");
      35           2 :         elektraPluginClose (storage, 0);
      36           2 :         elektraPluginClose (resolver, 0);
      37           2 :         ksDel (conf);
      38           2 :         ksDel (ks);
      39           2 :         elektraModulesClose (modules, 0);
      40           2 :         ksDel (modules);
      41           2 :         keyDel (parentKey);
      42           2 : }
      43             : 
      44           2 : static void test_BlockresolverWrite (char * fileName, char * compareName)
      45             : {
      46           2 :         FILE * fin = fopen (srcdir_file (fileName), "r");
      47             :         char buffer[1024];
      48           2 :         const char * foutname = elektraFilename ();
      49           2 :         FILE * fout = fopen (foutname, "w");
      50          20 :         while (fgets (buffer, sizeof (buffer), fin))
      51             :         {
      52          16 :                 fputs (buffer, fout);
      53             :         }
      54           2 :         fclose (fin);
      55           2 :         fclose (fout);
      56             : 
      57           2 :         Key * parentKey = keyNew ("system/test/blockresolver-write", KEY_VALUE, foutname, KEY_END);
      58           2 :         KeySet * conf = ksNew (10, keyNew ("system/path", KEY_VALUE, foutname, KEY_END),
      59             :                                keyNew ("system/identifier", KEY_VALUE, "### block config", KEY_END), KS_END);
      60           2 :         KeySet * modules = ksNew (0, KS_END);
      61           2 :         KeySet * ks = ksNew (0, KS_END);
      62           2 :         elektraModulesInit (modules, 0);
      63           2 :         Plugin * resolver = elektraPluginOpen ("blockresolver", modules, ksDup (conf), 0);
      64           2 :         succeed_if (resolver->kdbGet (resolver, ks, parentKey) >= 0, "blockresolver->kdbGet failed");
      65           2 :         Plugin * storage = elektraPluginOpen ("ini", modules, ksNew (0, KS_END), 0);
      66           2 :         succeed_if (storage->kdbGet (storage, ks, parentKey) >= 0, "storage->kdbGet failed");
      67           2 :         keySetString (ksLookupByName (ks, "system/test/blockresolver-write/section/key", 0), "only the inside has changed");
      68           2 :         succeed_if (storage->kdbSet (storage, ks, parentKey) >= 0, "storage->kdbSet failed");
      69           2 :         succeed_if (resolver->kdbSet (resolver, ks, parentKey) >= 0, "blockresolver->kdbSet failed");
      70           2 :         succeed_if (resolver->kdbSet (resolver, ks, parentKey) >= 0, "blockresolver->kdbSet failed");
      71             : 
      72           2 :         succeed_if (compare_line_files (srcdir_file (compareName), foutname), "files do not match as expected");
      73             : 
      74           2 :         elektraPluginClose (storage, 0);
      75           2 :         elektraPluginClose (resolver, 0);
      76           2 :         ksDel (conf);
      77           2 :         ksDel (ks);
      78           2 :         elektraModulesClose (modules, 0);
      79           2 :         ksDel (modules);
      80           2 :         keyDel (parentKey);
      81           2 : }
      82             : 
      83           2 : int main (int argc, char ** argv)
      84             : {
      85           2 :         printf ("BLOCKRESOLVER     TESTS\n");
      86           2 :         printf ("==================\n\n");
      87             : 
      88           2 :         init (argc, argv);
      89             : 
      90           2 :         test_BlockresolverRead ("blockresolver/test.block");
      91           2 :         test_BlockresolverWrite ("blockresolver/test.block", "blockresolver/compare.block");
      92             : 
      93           2 :         print_result ("testmod_blockresolver");
      94             : 
      95           2 :         return nbError;
      96             : }

Generated by: LCOV version 1.13