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

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for xerces plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include <stdlib.h>
      10             : #include <string.h>
      11             : 
      12             : #include <kdbconfig.h>
      13             : 
      14             : #include <tests_plugin.h>
      15             : 
      16             : #define ELEKTRA_XERCES_ORIGINAL_ROOT_NAME "xerces/rootname"
      17             : 
      18           1 : static void test_basics (void)
      19             : {
      20           1 :         printf ("test basics\n");
      21           1 :         fflush (stdout);
      22             : 
      23           1 :         Key * parentKey = keyNew ("system/elektra/modules/xerces", KEY_END);
      24           1 :         Key * invalidKey = keyNew (0, KEY_END);
      25           1 :         KeySet * conf = ksNew (0, KS_END);
      26           1 :         PLUGIN_OPEN ("xerces");
      27             : 
      28           1 :         KeySet * ks = ksNew (0, KS_END);
      29             : 
      30           1 :         succeed_if (plugin->kdbOpen (plugin, parentKey) == 1, "call to kdbOpen was not successful");
      31             : 
      32           1 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "call to kdbGet was not successful");
      33           1 :         succeed_if (plugin->kdbGet (plugin, ks, invalidKey) == 0, "call to kdbGet was successful though the parentKey is invalid");
      34             : 
      35             :         // Will return 0 as we have no destination file set, we test serialization in detail later
      36           1 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 0, "call to kdbSet was not successful");
      37           1 :         succeed_if (plugin->kdbGet (plugin, ks, invalidKey) == 0, "call to kdbSet was successful though the parentKey is invalid");
      38             : 
      39           1 :         succeed_if (plugin->kdbClose (plugin, parentKey) == 1, "call to kdbClose was not successful");
      40             : 
      41           1 :         keyDel (invalidKey);
      42           1 :         keyDel (parentKey);
      43           1 :         ksDel (ks);
      44           1 :         PLUGIN_CLOSE ();
      45             : 
      46           1 :         printf ("test basics finished\n");
      47           1 :         fflush (stdout);
      48           1 : }
      49             : 
      50           1 : static void test_simple_read (void)
      51             : {
      52           1 :         printf ("test simple read\n");
      53           1 :         fflush (stdout);
      54             : 
      55           1 :         Key * parentKey = keyNew ("/sw/elektra/tests/xerces", KEY_VALUE, srcdir_file ("xerces/simple.xml"), KEY_END);
      56           1 :         KeySet * conf = ksNew (0, KS_END);
      57           1 :         PLUGIN_OPEN ("xerces");
      58             : 
      59           1 :         KeySet * ks = ksNew (0, KS_END);
      60             :         Key * current;
      61             : 
      62           1 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "call to kdbGet was not successful");
      63             : 
      64           1 :         succeed_if (current = ksLookupByName (ks, "/sw/elektra/tests/xerces", 0), "xerces key not found");
      65           1 :         if (current)
      66             :         {
      67           1 :                 succeed_if (strcmp (keyName (current), "/sw/elektra/tests/xerces") == 0, "wrong name");
      68           1 :                 succeed_if (strcmp (keyValue (current), "  value of xerces  ") == 0, "value not correct");
      69           1 :                 succeed_if (!keyGetMeta (current, "ELEKTRA_XERCES_ORIGINAL_ROOT_NAME"), "original root name metadata exists");
      70             :         }
      71             : 
      72           1 :         output_keyset (ks);
      73             : 
      74           1 :         succeed_if (current = ksLookupByName (ks, "/sw/elektra/tests/xerces/fizz/#0", 0), "first fizz key not found");
      75           1 :         if (current)
      76             :         {
      77             :                 const Key * meta;
      78           1 :                 succeed_if (meta = keyGetMeta (current, "buzz"), "no metadata exists");
      79           1 :                 if (meta)
      80             :                 {
      81           1 :                         succeed_if (strcmp (keyName (meta), "buzz") == 0, "wrong metadata name");
      82           1 :                         succeed_if (strcmp (keyValue (meta), "fizzBuzz") == 0, "wrong metadata value");
      83             :                 }
      84             :         }
      85             : 
      86           1 :         succeed_if (current = ksLookupByName (ks, "/sw/elektra/tests/xerces/fizz/#1", 0), "second fizz key not found");
      87           1 :         if (current)
      88             :         {
      89           1 :                 succeed_if (!keyGetMeta (current, "buzz"), "metadata exists");
      90           1 :                 succeed_if (!keyGetMeta (current, "without"), "metadata exists");
      91             :         }
      92             : 
      93           1 :         succeed_if (current = ksLookupByName (ks, "/sw/elektra/tests/xerces/fizz/#2", 0), "third fizz key not found");
      94           1 :         if (current)
      95             :         {
      96             :                 const Key * meta;
      97           1 :                 succeed_if (meta = keyGetMeta (current, "without"), "no metadata exists");
      98           1 :                 if (meta)
      99             :                 {
     100           1 :                         succeed_if (strcmp (keyName (meta), "without") == 0, "wrong metadata name");
     101           1 :                         succeed_if (strcmp (keyValue (meta), "buzz") == 0, "wrong metadata value");
     102             :                 }
     103             :         }
     104             : 
     105           1 :         succeed_if (current = ksLookupByName (ks, "/sw/elektra/tests/xerces/userKey", 0), "userKey key not found");
     106           1 :         if (current)
     107             :         {
     108           1 :                 succeed_if (strcmp (keyName (current), "/sw/elektra/tests/xerces/userKey") == 0, "wrong name");
     109           1 :                 succeed_if (strcmp (keyValue (current), "withValue") == 0, "value not correct");
     110             : 
     111             :                 const Key * meta;
     112           1 :                 succeed_if (meta = keyGetMeta (current, "user"), "no metadata exists");
     113           1 :                 if (meta)
     114             :                 {
     115           1 :                         succeed_if (strcmp (keyName (meta), "user") == 0, "wrong metadata name");
     116           1 :                         succeed_if (strcmp (keyValue (meta), "key") == 0, "wrong metadata value");
     117             :                 }
     118             :         }
     119             : 
     120           1 :         succeed_if (current = ksLookupByName (ks, "/sw/elektra/tests/xerces/späciöl_-keüs1", 0), "späciöl_-keüs1 key not found");
     121           1 :         if (current)
     122             :         {
     123           1 :                 succeed_if (strcmp (keyName (current), "/sw/elektra/tests/xerces/späciöl_-keüs1") == 0, "wrong name");
     124           1 :                 succeed_if (strcmp (keyValue (current), ">\"&<'") == 0, "value not correct");
     125             : 
     126             :                 const Key * meta;
     127           1 :                 succeed_if (meta = keyGetMeta (current, "attr"), "no metadata exists");
     128           1 :                 if (meta)
     129             :                 {
     130           1 :                         succeed_if (strcmp (keyName (meta), "attr") == 0, "wrong metadata name");
     131           1 :                         succeed_if (strcmp (keyValue (meta), "\"") == 0, "wrong metadata value");
     132             :                 }
     133           1 :                 succeed_if (meta = keyGetMeta (current, "attr2"), "no metadata exists");
     134           1 :                 if (meta)
     135             :                 {
     136           1 :                         succeed_if (strcmp (keyName (meta), "attr2") == 0, "wrong metadata name");
     137           1 :                         succeed_if (strcmp (keyValue (meta), "$%(){}``äüö²[/\\'>\"<'&") == 0, "wrong metadata value");
     138             :                 }
     139             :         }
     140             : 
     141           1 :         succeed_if (current = ksLookupByName (ks, "/sw/elektra/tests/xerces/cdata", 0), "cdata key not found");
     142           1 :         if (current)
     143             :         {
     144           1 :                 succeed_if (strcmp (keyName (current), "/sw/elektra/tests/xerces/cdata") == 0, "wrong name");
     145             : 
     146           1 :                 succeed_if (strcmp (keyValue (current), "this is some cdata text \"'<>&ä \"") == 0, "value not correct");
     147             : 
     148             :                 const Key * meta;
     149           1 :                 succeed_if (meta = keyGetMeta (current, "more-s_päcials"), "no metadata exists");
     150           1 :                 if (meta)
     151             :                 {
     152           1 :                         succeed_if (strcmp (keyName (meta), "more-s_päcials") == 0, "wrong metadata name");
     153           1 :                         succeed_if (strcmp (keyValue (meta), "1 & 2 are < 3 \n") == 0, "wrong metadata value");
     154             :                 }
     155             :         }
     156             : 
     157           1 :         keyDel (parentKey);
     158           1 :         ksDel (ks);
     159           1 :         PLUGIN_CLOSE ();
     160             : 
     161           1 :         printf ("test simple read finished\n");
     162           1 :         fflush (stdout);
     163           1 : }
     164             : 
     165           1 : static void test_simple_write (void)
     166             : {
     167           1 :         printf ("test simple write\n");
     168           1 :         fflush (stdout);
     169             : 
     170           1 :         Key * parentKey = keyNew ("/sw/elektra/tests/xerces", KEY_VALUE, elektraFilename (), KEY_END);
     171           1 :         KeySet * conf = ksNew (0, KS_END);
     172           1 :         PLUGIN_OPEN ("xerces");
     173             : 
     174           1 :         Key * root = keyNew ("/sw/elektra/tests/xerces", KEY_VALUE, "value of xerces", KEY_END);
     175           1 :         keySetMeta (root, ELEKTRA_XERCES_ORIGINAL_ROOT_NAME, "root");
     176             : 
     177           1 :         Key * keyWithMeta = keyNew ("/sw/elektra/tests/xerces/fizz", KEY_END);
     178           1 :         keySetMeta (keyWithMeta, "buzz", "fizzBuzz");
     179             : 
     180           1 :         Key * specialKeys = keyNew ("/sw/elektra/tests/xerces/späciöl_-keüs1", KEY_VALUE, ">\"&<'", KEY_END);
     181           1 :         keySetMeta (specialKeys, "attr", "\"");
     182           1 :         keySetMeta (specialKeys, "attr2", "$%(){}``äüö²[/\\'>&<'&");
     183             : 
     184           1 :         Key * moreSpecialKeys =
     185             :                 keyNew ("/sw/elektra/tests/xerces/cdata", KEY_VALUE, "<![CDATA[this is some cdata text \"'<>&ä \"]]>", KEY_END);
     186           1 :         keySetMeta (moreSpecialKeys, "more-s_päcials", "1 & 2 are < 3 \n");
     187             : 
     188           1 :         KeySet * ks = ksNew (5, root, keyNew ("/sw/elektra/tests/xerces/userKey", KEY_VALUE, "withValue", KEY_END), keyWithMeta,
     189             :                              specialKeys, moreSpecialKeys, KS_END);
     190           1 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "call to kdbSet was not successful");
     191           1 :         succeed_if (output_error (parentKey), "error in kdbSet");
     192           1 :         succeed_if (output_warnings (parentKey), "warnings in kdbSet");
     193             : 
     194           1 :         succeed_if (compare_line_files (srcdir_file ("xerces/escaping.xml"), keyString (parentKey)), "files do not match as expected")
     195             :                 // Its also another good deserialization test
     196           1 :                 Key * resultParentKey = keyNew ("/sw/elektra/tests/xerces", KEY_VALUE, srcdir_file ("xerces/escaping.xml"), KEY_END);
     197           1 :         KeySet * result = ksNew (2, KS_END);
     198           1 :         succeed_if (plugin->kdbGet (plugin, result, resultParentKey) == 1, "call to kdbGet was not successful");
     199           1 :         compare_keyset (ks, result);
     200             : 
     201           1 :         elektraUnlink (keyString (parentKey));
     202             : 
     203           1 :         keyDel (parentKey);
     204           1 :         ksDel (ks);
     205           1 :         keyDel (resultParentKey);
     206           1 :         ksDel (result);
     207           1 :         PLUGIN_CLOSE ();
     208             : 
     209           1 :         printf ("test simple write finished\n");
     210           1 :         fflush (stdout);
     211           1 : }
     212             : 
     213           1 : static void test_maven_pom (void)
     214             : {
     215           1 :         printf ("test maven pom\n");
     216           1 :         fflush (stdout);
     217             : 
     218           1 :         Key * parentKey = keyNew ("/sw/elektra/tests/xerces", KEY_VALUE, srcdir_file ("xerces/pom.xml"), KEY_END);
     219           1 :         KeySet * conf = ksNew (0, KS_END);
     220           1 :         PLUGIN_OPEN ("xerces");
     221             : 
     222           1 :         KeySet * ks = ksNew (64, KS_END);
     223           1 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "call to kdbGet was not successful");
     224             : 
     225             :         // Its also another good deserialization test
     226           1 :         Key * serializationParentKey = keyNew ("/sw/elektra/tests/xerces", KEY_VALUE, elektraFilename (), KEY_END);
     227           1 :         succeed_if (plugin->kdbSet (plugin, ks, serializationParentKey) >= 1, "call to kdbSet was not successful");
     228           1 :         succeed_if (output_error (serializationParentKey), "error in kdbSet");
     229           1 :         succeed_if (output_warnings (serializationParentKey), "warnings in kdbSet");
     230             : 
     231           1 :         Key * resultParentKey = keyNew ("/sw/elektra/tests/xerces", KEY_VALUE, keyString (serializationParentKey), KEY_END);
     232           1 :         KeySet * result = ksNew (64, KS_END);
     233           1 :         succeed_if (plugin->kdbGet (plugin, result, resultParentKey) == 1, "call to kdbGet was not successful");
     234             : 
     235           1 :         succeed_if (64 == ksGetSize (ks), "pom file is expected to contain 64 keys");
     236             : 
     237           1 :         compare_keyset (ks, result); // Should be the same
     238             : 
     239           1 :         elektraUnlink (keyString (serializationParentKey));
     240             : 
     241           1 :         keyDel (parentKey);
     242           1 :         ksDel (ks);
     243           1 :         keyDel (serializationParentKey);
     244           1 :         keyDel (resultParentKey);
     245           1 :         ksDel (result);
     246           1 :         PLUGIN_CLOSE ();
     247             : 
     248           1 :         printf ("test maven pom finished\n");
     249           1 :         fflush (stdout);
     250           1 : }
     251             : 
     252           1 : static void test_jenkins_config (void)
     253             : {
     254           1 :         printf ("test jenkins config\n");
     255           1 :         fflush (stdout);
     256             : 
     257           1 :         Key * parentKey = keyNew ("/sw/elektra/tests/xerces", KEY_VALUE, srcdir_file ("xerces/jenkins.xml"), KEY_END);
     258           1 :         KeySet * conf = ksNew (0, KS_END);
     259           1 :         PLUGIN_OPEN ("xerces");
     260             : 
     261           1 :         KeySet * ks = ksNew (64, KS_END);
     262           1 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "call to kdbGet was not successful");
     263             : 
     264             :         // Its also another good deserialization test
     265           1 :         Key * serializationParentKey = keyNew ("/sw/elektra/tests/xerces", KEY_VALUE, elektraFilename (), KEY_END);
     266           1 :         succeed_if (plugin->kdbSet (plugin, ks, serializationParentKey) >= 1, "call to kdbSet was not successful");
     267           1 :         succeed_if (output_error (serializationParentKey), "error in kdbSet");
     268           1 :         succeed_if (output_warnings (serializationParentKey), "warnings in kdbSet");
     269             : 
     270           1 :         Key * resultParentKey = keyNew ("/sw/elektra/tests/xerces", KEY_VALUE, keyString (serializationParentKey), KEY_END);
     271           1 :         KeySet * result = ksNew (64, KS_END);
     272           1 :         succeed_if (plugin->kdbGet (plugin, result, resultParentKey) == 1, "call to kdbGet was not successful");
     273             : 
     274             :         Key * current;
     275             : 
     276           1 :         succeed_if (
     277             :                 current = ksLookupByName (
     278             :                         ks, "/sw/elektra/tests/xerces/temporaryOfflineCause/user/properties/jenkins.security.ApiTokenProperty/apiToken", 0),
     279             :                 "failed to find apiToken key");
     280           1 :         succeed_if (strcmp (keyValue (current), "bee4ahGhOqua3ahzsai2Eef5quie5ohK/eiSe4eav+JhVlerBftAil8Ow5AejahBe9oiksKAlla/kk1/1=") == 0,
     281             :                     "api token is wrong");
     282             : 
     283           1 :         succeed_if (86 == ksGetSize (ks), "pom file is expected to contain 86 keys");
     284             : 
     285           1 :         compare_keyset (ks, result); // Should be the same
     286             : 
     287           1 :         elektraUnlink (keyString (serializationParentKey));
     288             : 
     289           1 :         keyDel (parentKey);
     290           1 :         ksDel (ks);
     291           1 :         keyDel (serializationParentKey);
     292           1 :         keyDel (resultParentKey);
     293           1 :         ksDel (result);
     294           1 :         PLUGIN_CLOSE ();
     295             : 
     296           1 :         printf ("test jenkins config finished\n");
     297           1 :         fflush (stdout);
     298           1 : }
     299             : 
     300           1 : int main (int argc, char ** argv)
     301             : {
     302           1 :         printf ("XERCES    TESTS\n");
     303           1 :         printf ("==================\n\n");
     304             : 
     305           1 :         init (argc, argv);
     306             : 
     307           1 :         test_basics ();
     308           1 :         test_simple_read ();
     309           1 :         test_simple_write ();
     310           1 :         test_maven_pom ();
     311           1 :         test_jenkins_config ();
     312             : 
     313           1 :         print_result ("testmod_xerces");
     314             : 
     315           1 :         return nbError;
     316             : }

Generated by: LCOV version 1.13