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

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief A plugin that makes use of libaugeas to read and write configuration files
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #ifdef HAVE_KDBCONFIG_H
      11             : #include "kdbconfig.h"
      12             : #endif
      13             : 
      14             : #include <stdio.h>
      15             : #ifdef HAVE_STDLIB_H
      16             : #include <stdlib.h>
      17             : #endif
      18             : #ifdef HAVE_STRING_H
      19             : #include <string.h>
      20             : #endif
      21             : 
      22             : #include <kdbproposal.h>
      23             : 
      24             : #include <tests_plugin.h>
      25             : 
      26           2 : static void test_hostLensRead (char * fileName)
      27             : {
      28           2 :         Key * parentKey = keyNew ("user/tests/augeas-hosts", KEY_VALUE, srcdir_file (fileName), KEY_END);
      29           2 :         KeySet * conf = ksNew (20, keyNew ("system/lens", KEY_VALUE, "Hosts.lns", KEY_END), KS_END);
      30           2 :         PLUGIN_OPEN ("augeas");
      31             : 
      32           2 :         KeySet * ks = ksNew (0, KS_END);
      33             : 
      34           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
      35           2 :         succeed_if (output_error (parentKey), "error in kdbGet");
      36           2 :         succeed_if (output_warnings (parentKey), "warnings in kdbGet");
      37             : 
      38           2 :         Key * key = ksLookupByName (ks, "user/tests/augeas-hosts/1/ipaddr", 0);
      39           2 :         exit_if_fail (key, "ip address of localhost not found");
      40           2 :         succeed_if (strcmp ("127.0.0.1", keyValue (key)) == 0, "address of localhost not correct");
      41             : 
      42           2 :         key = ksLookupByName (ks, "user/tests/augeas-hosts/1/canonical", 0);
      43           2 :         exit_if_fail (key, "name of localhost not found");
      44           2 :         succeed_if (strcmp ("localhost", keyValue (key)) == 0, "name of localhost not correct");
      45             : 
      46           2 :         key = ksLookupByName (ks, "user/tests/augeas-hosts/2/ipaddr", 0);
      47           2 :         exit_if_fail (key, "ip address of host1 not found");
      48           2 :         succeed_if (strcmp ("192.168.0.1", keyValue (key)) == 0, "address of host1 not correct");
      49             : 
      50           2 :         key = ksLookupByName (ks, "user/tests/augeas-hosts/2/canonical", 0);
      51           2 :         exit_if_fail (key, "name of host1 not found");
      52           2 :         succeed_if (strcmp ("host1", keyValue (key)) == 0, "name of host1 not correct");
      53             : 
      54           2 :         key = ksLookupByName (ks, "user/tests/augeas-hosts/2/alias[1]", 0);
      55           2 :         exit_if_fail (key, "alias1 of host1 not found");
      56           2 :         succeed_if (strcmp ("alias1", keyValue (key)) == 0, "name of alias1 of host1 not correct");
      57             : 
      58           2 :         key = ksLookupByName (ks, "user/tests/augeas-hosts/2/alias[2]", 0);
      59           2 :         exit_if_fail (key, "alias2 of host1 not found");
      60           2 :         succeed_if (strcmp ("alias2", keyValue (key)) == 0, "name of alias2 of host1 not correct");
      61             : 
      62           2 :         PLUGIN_CLOSE ();
      63             : 
      64           2 :         ksDel (ks);
      65           2 :         keyDel (parentKey);
      66           2 : }
      67             : 
      68           2 : static void test_hostLensWrite (char * fileName)
      69             : {
      70           2 :         Key * parentKey = keyNew ("user/tests/augeas-hosts", KEY_VALUE, elektraFilename (), KEY_END);
      71           2 :         KeySet * conf = ksNew (20, keyNew ("system/lens", KEY_VALUE, "Hosts.lns", KEY_END), KS_END);
      72           2 :         PLUGIN_OPEN ("augeas");
      73             : 
      74             :         // clang-format off
      75           2 :         KeySet *ks = ksNew (30, keyNew ("user/tests/augeas-hosts/1", KEY_END),
      76             :                         keyNew ("user/tests/augeas-hosts/1/ipaddr", KEY_VALUE, "127.0.0.1",
      77             :                                         KEY_META, "order", "10", KEY_END),
      78             :                         keyNew ("user/tests/augeas-hosts/1/canonical", KEY_VALUE,
      79             :                                         "localhost", KEY_META, "order", "20", KEY_END),
      80             :                         keyNew ("user/tests/augeas-hosts/1/#comment", KEY_VALUE,
      81             :                                         "hostcomment", KEY_META, "order", "21", KEY_END),
      82             :                         keyNew ("user/tests/augeas-hosts/#comment", KEY_VALUE,
      83             :                                         "linecomment", KEY_META, "order", "22", KEY_END),
      84             :                         keyNew ("user/tests/augeas-hosts/2/ipaddr", KEY_VALUE,
      85             :                                         "192.168.0.1", KEY_META, "order", "30", KEY_END),
      86             :                         keyNew ("user/tests/augeas-hosts/2/canonical", KEY_VALUE, "host1",
      87             :                                         KEY_META, "order", "40", KEY_END),
      88             :                         keyNew ("user/tests/augeas-hosts/2/alias[1]", KEY_VALUE,
      89             :                                         "host1alias1", KEY_META, "order", "50", KEY_END),
      90             :                         keyNew ("user/tests/augeas-hosts/2/alias[2]", KEY_VALUE,
      91             :                                         "host1alias2", KEY_META, "order", "60", KEY_END),
      92             :                         keyNew ("user/tests/augeas-hosts/3/ipaddr", KEY_VALUE,
      93             :                                         "fd00::4711:4712:2::1", KEY_META, "order", "70", KEY_END),
      94             :                         keyNew ("user/tests/augeas-hosts/3/canonical", KEY_VALUE, "host2",
      95             :                                         KEY_META, "order", "80", KEY_END),
      96             :                         keyNew ("user/tests/augeas-hosts/3/alias[1]", KEY_VALUE,
      97             :                                         "host2alias1", KEY_META, "order", "90", KEY_END),
      98             :                         keyNew ("user/tests/augeas-hosts/3/alias[2]", KEY_VALUE,
      99             :                                         "host2alias2", KEY_META, "order", "100", KEY_END), KS_END);
     100             :         // clang-format on
     101             : 
     102           2 :         ksAppendKey (ks, parentKey);
     103             : 
     104           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
     105           2 :         succeed_if (output_error (parentKey), "error in kdbSet");
     106           2 :         succeed_if (output_warnings (parentKey), "warnings in kdbSet");
     107             : 
     108           2 :         succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
     109             : 
     110           2 :         elektraUnlink (keyString (parentKey));
     111             : 
     112           2 :         ksDel (ks);
     113             : 
     114           2 :         PLUGIN_CLOSE ();
     115           2 : }
     116             : 
     117           2 : static void test_hostLensDelete (char * sourceFile, char * compFile)
     118             : {
     119           2 :         Key * parentKey = keyNew ("user/tests/augeas-hosts", KEY_VALUE, srcdir_file (sourceFile), KEY_END);
     120           2 :         KeySet * conf = ksNew (20, keyNew ("system/lens", KEY_VALUE, "Hosts.lns", KEY_END), KS_END);
     121           2 :         PLUGIN_OPEN ("augeas");
     122             : 
     123           2 :         KeySet * ks = ksNew (0, KS_END);
     124             : 
     125           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
     126           2 :         succeed_if (output_error (parentKey), "error in kdbGet");
     127           2 :         succeed_if (output_warnings (parentKey), "warnings in kdbGet");
     128             : 
     129           2 :         Key * key = ksLookupByName (ks, "user/tests/augeas-hosts/1", 0);
     130           2 :         exit_if_fail (key, "localhost not found");
     131           2 :         ksPopAtCursor (ks, ksGetCursor (ks));
     132           2 :         keyDel (key);
     133             : 
     134           2 :         key = ksLookupByName (ks, "user/tests/augeas-hosts/1/ipaddr", 0);
     135           2 :         exit_if_fail (key, "ip address of localhost not found");
     136           2 :         ksPopAtCursor (ks, ksGetCursor (ks));
     137           2 :         keyDel (key);
     138             : 
     139           2 :         key = ksLookupByName (ks, "user/tests/augeas-hosts/1/canonical", 0);
     140           2 :         exit_if_fail (key, "canonical of localhost not found");
     141           2 :         ksPopAtCursor (ks, ksGetCursor (ks));
     142           2 :         keyDel (key);
     143             : 
     144           2 :         key = ksLookupByName (ks, "user/tests/augeas-hosts/1/#comment", 0);
     145           2 :         exit_if_fail (key, "comment of localhost not found");
     146           2 :         ksPopAtCursor (ks, ksGetCursor (ks));
     147           2 :         keyDel (key);
     148             : 
     149           2 :         keySetString (parentKey, elektraFilename ());
     150             : 
     151           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
     152           2 :         succeed_if (output_error (parentKey), "error in kdbSet");
     153           2 :         succeed_if (output_warnings (parentKey), "warnings in kdbSet");
     154             : 
     155           2 :         succeed_if (compare_line_files (srcdir_file (compFile), keyString (parentKey)), "files do not match as expected");
     156             : 
     157           2 :         ksDel (ks);
     158             : 
     159           2 :         elektraUnlink (keyString (parentKey));
     160           2 :         keyDel (parentKey);
     161             : 
     162           2 :         PLUGIN_CLOSE ();
     163           2 : }
     164             : 
     165           2 : static void test_hostLensModify (char * sourceFile, char * compFile)
     166             : {
     167           2 :         Key * parentKey = keyNew ("user/tests/augeas-hosts", KEY_VALUE, srcdir_file (sourceFile), KEY_END);
     168           2 :         KeySet * conf = ksNew (20, keyNew ("system/lens", KEY_VALUE, "Hosts.lns", KEY_END), KS_END);
     169           2 :         PLUGIN_OPEN ("augeas");
     170             : 
     171           2 :         KeySet * ks = ksNew (0, KS_END);
     172             : 
     173           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
     174           2 :         succeed_if (output_error (parentKey), "error in kdbGet");
     175           2 :         succeed_if (output_warnings (parentKey), "warnings in kdbGet");
     176             : 
     177           2 :         Key * key = ksLookupByName (ks, "user/tests/augeas-hosts/1/ipaddr", 0);
     178           2 :         exit_if_fail (key, "ip address of localhost not found");
     179           2 :         keySetString (key, "127.0.0.2");
     180             : 
     181           2 :         key = ksLookupByName (ks, "user/tests/augeas-hosts/3/ipaddr", 0);
     182           2 :         exit_if_fail (key, "ip address of host2 not found");
     183           2 :         keySetString (key, "fd00::4711:4712:2::2");
     184             : 
     185           2 :         key = ksLookupByName (ks, "user/tests/augeas-hosts/#comment", 0);
     186           2 :         exit_if_fail (key, "line comment not found");
     187           2 :         keySetString (key, "line comment modified");
     188             : 
     189           2 :         keySetString (parentKey, elektraFilename ());
     190             : 
     191           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
     192           2 :         succeed_if (output_error (parentKey), "error in kdbSet");
     193           2 :         succeed_if (output_warnings (parentKey), "warnings in kdbSet");
     194             : 
     195           2 :         succeed_if (compare_line_files (srcdir_file (compFile), keyString (parentKey)), "files do not match as expected");
     196             : 
     197           2 :         PLUGIN_CLOSE ();
     198             : 
     199           2 :         elektraUnlink (keyString (parentKey));
     200             : 
     201           2 :         ksDel (ks);
     202           2 :         keyDel (parentKey);
     203           2 : }
     204             : 
     205           2 : static void test_order (char * fileName)
     206             : {
     207           2 :         Key * parentKey = keyNew ("user/tests/augeas-hosts", KEY_VALUE, srcdir_file (fileName), KEY_END);
     208           2 :         KeySet * conf = ksNew (20, keyNew ("system/lens", KEY_VALUE, "Hosts.lns", KEY_END), KS_END);
     209           2 :         PLUGIN_OPEN ("augeas");
     210             : 
     211           2 :         KeySet * ks = ksNew (0, KS_END);
     212             : 
     213           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
     214           2 :         succeed_if (output_error (parentKey), "error in kdbGet");
     215           2 :         succeed_if (output_warnings (parentKey), "warnings in kdbGet");
     216             : 
     217             :         Key * key;
     218           2 :         size_t currentIndex = 0;
     219           2 :         size_t numKeys = ksGetSize (ks);
     220           2 :         long * usedOrders = elektraMalloc (numKeys * sizeof (long));
     221             : 
     222           2 :         exit_if_fail (usedOrders, "unable to allocate memory for order array");
     223             : 
     224             :         /* as 0 is a legit order we have to initialize the array manually */
     225         826 :         for (size_t index = 0; index < numKeys; index++)
     226             :         {
     227         826 :                 usedOrders[index] = -1;
     228             :         }
     229             : 
     230           2 :         ksRewind (ks);
     231         830 :         while ((key = ksNext (ks)) != 0)
     232             :         {
     233         826 :                 if (strcmp (keyName (key), keyName (parentKey)))
     234             :                 {
     235             :                         char errorMessage[150];
     236         824 :                         const Key * orderKey = keyGetMeta (key, "order");
     237             : 
     238         824 :                         snprintf (errorMessage, 150, "key %s has no order", keyName (key));
     239             : 
     240         824 :                         succeed_if (orderKey, errorMessage);
     241             : 
     242         824 :                         char * orderString = (char *) keyValue (orderKey);
     243             :                         long order;
     244             :                         char * end;
     245         824 :                         order = strtol (orderString, &end, 10);
     246         824 :                         snprintf (errorMessage, 150, "key %s has an unparseable order", keyName (key));
     247             : 
     248         824 :                         succeed_if (*end == 0, errorMessage);
     249             : 
     250         824 :                         snprintf (errorMessage, 150, "key %s has a negative order", keyName (key));
     251         824 :                         succeed_if (order >= 0, errorMessage);
     252             : 
     253         824 :                         snprintf (errorMessage, 150, "the order %ld exists more than once. Duplicate found in %s.", order, keyName (key));
     254             : 
     255             :                         // TODO: this is in O(n^2) where n is the number of keys
     256      170156 :                         for (size_t i = 0; i < currentIndex; i++)
     257             :                         {
     258      169332 :                                 succeed_if (usedOrders[i] != order, errorMessage);
     259             :                         }
     260             : 
     261         824 :                         usedOrders[currentIndex] = order;
     262         824 :                         ++currentIndex;
     263             :                 }
     264             :         }
     265             : 
     266           2 :         elektraFree (usedOrders);
     267           2 :         ksDel (ks);
     268           2 :         keyDel (parentKey);
     269             : 
     270           2 :         PLUGIN_CLOSE ();
     271           2 : }
     272             : 
     273           2 : static void test_hostLensFormatting (char * fileName)
     274             : {
     275           2 :         Key * parentKey = keyNew ("user/tests/augeas-hosts", KEY_VALUE, srcdir_file (fileName), KEY_END);
     276           2 :         KeySet * conf = ksNew (20, keyNew ("system/lens", KEY_VALUE, "Hosts.lns", KEY_END), KS_END);
     277           2 :         PLUGIN_OPEN ("augeas");
     278             : 
     279           2 :         KeySet * ks = ksNew (0, KS_END);
     280             : 
     281           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
     282           2 :         succeed_if (output_error (parentKey), "error in kdbGet");
     283           2 :         succeed_if (output_warnings (parentKey), "warnings in kdbGet");
     284             : 
     285           2 :         keySetString (parentKey, elektraFilename ());
     286             : 
     287           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
     288           2 :         succeed_if (output_error (parentKey), "error in kdbSet");
     289           2 :         succeed_if (output_warnings (parentKey), "warnings in kdbSet");
     290             : 
     291           2 :         succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
     292             : 
     293           2 :         elektraUnlink (keyString (parentKey));
     294           2 :         keyDel (parentKey);
     295           2 :         ksDel (ks);
     296             : 
     297             : 
     298           2 :         PLUGIN_CLOSE ();
     299           2 : }
     300             : 
     301           2 : int main (int argc, char ** argv)
     302             : {
     303           2 :         printf ("AUGEAS       TESTS\n");
     304           2 :         printf ("==================\n\n");
     305             : 
     306           2 :         init (argc, argv);
     307             : 
     308             :         /* output all lenses:
     309             :         KeySet * ks = ksNew (5, KS_END);
     310             :         elektraAugeasGenConf (ks, 0);
     311             :         output_keyset (ks);
     312             :         ksDel (ks);
     313             :         */
     314             : 
     315           2 :         test_hostLensRead ("augeas/hosts-read");
     316           2 :         test_hostLensWrite ("augeas/hosts-write");
     317           2 :         test_hostLensModify ("augeas/hosts-modify-in", "augeas/hosts-modify");
     318           2 :         test_hostLensDelete ("augeas/hosts-delete-in", "augeas/hosts-delete");
     319           2 :         test_hostLensFormatting ("augeas/hosts-formatting");
     320           2 :         test_order ("augeas/hosts-big");
     321             : 
     322           2 :         print_result ("test_augeas");
     323             : 
     324           2 :         return nbError;
     325             : }

Generated by: LCOV version 1.13