LCOV - code coverage report
Current view: top level - src/plugins/base64 - testmod_base64.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 148 149 99.3 %
Date: 2022-05-21 16:19:22 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief test suite for the base64 plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include <tests_plugin.h>
      11             : 
      12             : #include "base64.h"
      13             : 
      14             : // test vectors are defined in RFC4648
      15             : // see https://www.ietf.org/rfc/rfc4648.txt
      16             : static const char * decoded[] = { "", "f", "fo", "foo", "foob", "fooba", "foobar" };
      17             : static const char * encoded[] = { "", "Zg==", "Zm8=", "Zm9v", "Zm9vYg==", "Zm9vYmE=", "Zm9vYmFy" };
      18             : static const size_t testcaseCounter = sizeof (decoded) / sizeof (const char *);
      19             : 
      20             : static inline KeySet * newPluginConfiguration (void)
      21             : {
      22           9 :         return ksNew (0, KS_END);
      23             : }
      24             : 
      25           3 : static void test_init (void)
      26             : #ifdef __llvm__
      27             :         __attribute__ ((annotate ("oclint:suppress[high ncss method]")))
      28             : #endif
      29             : {
      30           3 :         Plugin * plugin = NULL;
      31           3 :         Key * parentKey = keyNew ("system:/", KEY_END);
      32           3 :         KeySet * modules = ksNew (0, KS_END);
      33           3 :         KeySet * configKs = newPluginConfiguration ();
      34           3 :         elektraModulesInit (modules, 0);
      35             : 
      36           3 :         plugin = elektraPluginOpen (ELEKTRA_PLUGIN_NAME, modules, configKs, 0);
      37           3 :         succeed_if (plugin != 0, "failed to open the plugin");
      38           3 :         if (plugin)
      39             :         {
      40           3 :                 succeed_if (strcmp (plugin->name, ELEKTRA_PLUGIN_NAME) == 0, "got wrong name");
      41             : 
      42           3 :                 KeySet * config = elektraPluginGetConfig (plugin);
      43           3 :                 succeed_if (config != 0, "there should be a config");
      44             : 
      45           3 :                 succeed_if (plugin->kdbGet != 0, "no get pointer");
      46           3 :                 succeed_if (plugin->kdbSet != 0, "no set pointer");
      47             : 
      48           3 :                 elektraPluginClose (plugin, 0);
      49             :         }
      50             : 
      51           3 :         elektraModulesClose (modules, 0);
      52           3 :         ksDel (modules);
      53           3 :         keyDel (parentKey);
      54           3 : }
      55             : 
      56             : static inline char testcase2char (size_t offset)
      57             : {
      58          96 :         return '0' + offset + 1;
      59             : }
      60             : 
      61           3 : static void test_base64_encoding (void)
      62             : #ifdef __llvm__
      63             :         __attribute__ ((annotate ("oclint:suppress[deep nested block]")))
      64             : #endif
      65             : {
      66           3 :         char errorAlloc[] = "Encoding #.: Memory allocation failed";
      67           3 :         char errorMismatch[] = "Encoding #.: returned unexpected result";
      68             : 
      69          24 :         for (size_t charOffset = 0; charOffset < testcaseCounter; charOffset++)
      70             :         {
      71          42 :                 errorAlloc[10] = testcase2char (charOffset);
      72          42 :                 errorMismatch[10] = testcase2char (charOffset);
      73             : 
      74          21 :                 char * encodedText = base64Encode ((kdb_octet_t *) decoded[charOffset], strlen (decoded[charOffset]));
      75          21 :                 succeed_if (encodedText, errorAlloc);
      76          21 :                 if (encodedText)
      77             :                 {
      78          21 :                         succeed_if (strcmp (encodedText, encoded[charOffset]) == 0, errorMismatch);
      79          21 :                         elektraFree (encodedText);
      80             :                 }
      81             :         }
      82           3 : }
      83             : 
      84           3 : static void test_base64_decoding (void)
      85             : #ifdef __llvm__
      86             :         __attribute__ ((annotate ("oclint:suppress[deep nested block]"), annotate ("oclint:suppress[high npath complexity]"),
      87             :                         annotate ("oclint:suppress[high ncss method]"), annotate ("oclint:suppress[high cyclomatic complexity]")))
      88             : #endif
      89             : {
      90           3 :         char errorFail[] = "Decoding #.: operation failed";
      91           3 :         char errorMismatch[] = "Decoding #.: returned unexpected result vector";
      92           3 :         char errorLength[] = "Decoding #.: returned unexpected result length";
      93             : 
      94           3 :         kdb_octet_t * buffer = NULL;
      95           3 :         size_t bufferLen = 0;
      96             : 
      97             :         // first test case is a little special because we expect NULL on success here
      98           3 :         succeed_if (base64Decode (encoded[0], &buffer, &bufferLen) == 1, "decoding of test vector 1 failed");
      99           3 :         succeed_if (buffer == NULL, "decoding of test vector 1 returned unexpected result vector");
     100           3 :         succeed_if (bufferLen == 0, "decoding of test vector 1 returned unexpected result length");
     101           3 :         if (buffer)
     102             :         {
     103           0 :                 elektraFree (buffer);
     104             :         }
     105             : 
     106          21 :         for (size_t i = 1; i < testcaseCounter; i++)
     107             :         {
     108          36 :                 errorMismatch[10] = testcase2char (i);
     109          36 :                 errorFail[10] = testcase2char (i);
     110          36 :                 errorLength[10] = testcase2char (i);
     111             : 
     112          18 :                 succeed_if (base64Decode (encoded[i], &buffer, &bufferLen) == 1, errorFail);
     113          18 :                 if (buffer)
     114             :                 {
     115          18 :                         succeed_if (bufferLen == strlen (decoded[i]), errorLength);
     116          18 :                         if (bufferLen == strlen (decoded[i]))
     117             :                         {
     118          18 :                                 succeed_if (memcmp (buffer, decoded[i], bufferLen) == 0, errorMismatch);
     119             :                         }
     120          18 :                         elektraFree (buffer);
     121          18 :                         buffer = NULL;
     122             :                 }
     123             :         }
     124           3 : }
     125             : 
     126           3 : static void test_base64_plugin_regular (void)
     127             : #ifdef __llvm__
     128             :         __attribute__ ((annotate ("oclint:suppress[deep nested block]"), annotate ("oclint:suppress[high ncss method]"),
     129             :                         annotate ("oclint:suppress[high npath complexity]"), annotate ("oclint:suppress[long method]"),
     130             :                         annotate ("oclint:suppress[high cyclomatic complexity]")))
     131             : #endif
     132             : {
     133           3 :         Plugin * plugin = NULL;
     134           3 :         Key * parentKey = keyNew ("system:/", KEY_END);
     135           3 :         KeySet * modules = ksNew (0, KS_END);
     136           3 :         KeySet * config = newPluginConfiguration ();
     137             : 
     138           3 :         elektraModulesInit (modules, 0);
     139           3 :         plugin = elektraPluginOpen (ELEKTRA_PLUGIN_NAME, modules, config, 0);
     140           3 :         succeed_if (plugin, "failed to open plugin handle");
     141           3 :         if (plugin)
     142             :         {
     143           3 :                 Key * key;
     144           3 :                 const kdb_octet_t sampleValue[] = { 0x31, 0x32, 0x33 };
     145             : 
     146           3 :                 KeySet * data = ksNew (4, keyNew ("/t/k1", KEY_VALUE, "Hello World", KEY_END),
     147             :                                        keyNew ("/t/k2", KEY_BINARY, KEY_SIZE, sizeof (sampleValue), KEY_VALUE, sampleValue, KEY_END),
     148             :                                        keyNew ("/t/k3", KEY_BINARY, KEY_SIZE, 0, KEY_END),
     149             :                                        keyNew ("/t/k4", KEY_VALUE, ELEKTRA_PLUGIN_BASE64_PREFIX, KEY_END), KS_END);
     150             : 
     151             :                 // test encoding
     152           3 :                 succeed_if (plugin->kdbSet (plugin, data, parentKey) == 1, "kdb set failed");
     153             : 
     154           3 :                 key = ksLookupByName (data, "/t/k1", 0);
     155           3 :                 succeed_if (key, "lost key in data KeySet");
     156           3 :                 if (key)
     157             :                 {
     158           3 :                         succeed_if (strcmp (keyString (key), "Hello World") == 0, "changed string value that does not require encoding");
     159             :                 }
     160             : 
     161           3 :                 key = ksLookupByName (data, "/t/k2", 0);
     162           3 :                 succeed_if (key, "lost key in data KeySet");
     163           3 :                 if (key)
     164             :                 {
     165           3 :                         succeed_if (strcmp (keyString (key), ELEKTRA_PLUGIN_BASE64_PREFIX "MTIz") == 0,
     166           3 :                                     "encoding binary key failed during kdb set");
     167             :                 }
     168             : 
     169           3 :                 key = ksLookupByName (data, "/t/k3", 0);
     170           3 :                 succeed_if (key, "lost key in data KeySet");
     171           3 :                 if (key)
     172             :                 {
     173           3 :                         succeed_if (strcmp (keyString (key), ELEKTRA_PLUGIN_BASE64_PREFIX "") == 0,
     174           3 :                                     "encoding NULL-key failed during kdb set");
     175             :                 }
     176             : 
     177           3 :                 key = ksLookupByName (data, "/t/k4", 0);
     178           3 :                 succeed_if (key, "lost key in data KeySet");
     179           3 :                 if (key)
     180             :                 {
     181           3 :                         succeed_if (strcmp (keyString (key), ELEKTRA_PLUGIN_BASE64_ESCAPE ELEKTRA_PLUGIN_BASE64_PREFIX) == 0,
     182           3 :                                     "encoding string starting with prefix " ELEKTRA_PLUGIN_BASE64_ESCAPE " failed during kdb set");
     183             :                 }
     184             : 
     185             :                 // test decoding
     186           3 :                 succeed_if (plugin->kdbGet (plugin, data, parentKey) == 1, "kdb get (pregetstorage) failed");
     187             : 
     188           3 :                 key = ksLookupByName (data, "/t/k1", 0);
     189           3 :                 succeed_if (key, "lost key in data KeySet");
     190           3 :                 if (key)
     191             :                 {
     192           3 :                         succeed_if (strcmp (keyString (key), "Hello World") == 0, "changed string value that does not require decoding");
     193             :                 }
     194             : 
     195           3 :                 key = ksLookupByName (data, "/t/k2", 0);
     196           3 :                 succeed_if (key, "lost key in data KeySet");
     197           3 :                 if (key)
     198             :                 {
     199           3 :                         succeed_if (keyGetValueSize (key) == sizeof (sampleValue), "decoding binary key failed during kdb get");
     200           3 :                         if (keyGetValueSize (key) == sizeof (sampleValue))
     201             :                         {
     202           3 :                                 succeed_if (memcmp (sampleValue, keyValue (key), sizeof (sampleValue)) == 0,
     203           3 :                                             "decoding binary key failed during kdb get");
     204             :                         }
     205             :                 }
     206             : 
     207           3 :                 key = ksLookupByName (data, "/t/k3", 0);
     208           3 :                 succeed_if (key, "lost key in data KeySet");
     209           3 :                 if (key)
     210             :                 {
     211           3 :                         succeed_if (keyGetValueSize (key) <= 0, "decoding NULL-key failed during kdb get");
     212             :                 }
     213             : 
     214           3 :                 key = ksLookupByName (data, "/t/k4", 0);
     215           3 :                 succeed_if (key, "lost key in data KeySet");
     216           3 :                 if (key)
     217             :                 {
     218           3 :                         succeed_if (strcmp (keyString (key), ELEKTRA_PLUGIN_BASE64_PREFIX) == 0,
     219           3 :                                     "decoding string starting with prefix " ELEKTRA_PLUGIN_BASE64_ESCAPE " failed during kdb get");
     220             :                 }
     221             : 
     222           3 :                 ksDel (data);
     223           3 :                 elektraPluginClose (plugin, 0);
     224             :         }
     225             : 
     226           3 :         elektraModulesClose (modules, 0);
     227           3 :         ksDel (modules);
     228           3 :         keyDel (parentKey);
     229           3 : }
     230             : 
     231           3 : static void test_base64_plugin_decoding_error (void)
     232             : #ifdef __llvm__
     233             :         __attribute__ ((annotate ("oclint:suppress[deep nested block]"), annotate ("oclint:suppress[high ncss method]")))
     234             : #endif
     235             : {
     236           3 :         Plugin * plugin = NULL;
     237           3 :         Key * parentKey = keyNew ("system:/", KEY_END);
     238           3 :         KeySet * modules = ksNew (0, KS_END);
     239           3 :         KeySet * config = newPluginConfiguration ();
     240             : 
     241           3 :         elektraModulesInit (modules, 0);
     242           3 :         plugin = elektraPluginOpen (ELEKTRA_PLUGIN_NAME, modules, config, 0);
     243           3 :         succeed_if (plugin, "failed to open plugin handle");
     244           3 :         if (plugin)
     245             :         {
     246           3 :                 Key * key;
     247           3 :                 KeySet * data = ksNew (1, keyNew ("/t/k1", KEY_VALUE, ELEKTRA_PLUGIN_BASE64_PREFIX "_$..", KEY_END), KS_END);
     248             : 
     249             :                 // test failing decoding
     250           3 :                 succeed_if (plugin->kdbGet (plugin, data, parentKey) == 1, "kdb get failed");
     251             : 
     252           3 :                 key = ksLookupByName (data, "/t/k1", 0);
     253           3 :                 succeed_if (key, "lost key in data KeySet");
     254           3 :                 if (key)
     255             :                 {
     256           3 :                         succeed_if (strcmp (keyString (key), ELEKTRA_PLUGIN_BASE64_PREFIX "_$..") == 0,
     257           3 :                                     "decoded string value that should have failed");
     258             :                 }
     259             : 
     260           3 :                 ksDel (data);
     261           3 :                 elektraPluginClose (plugin, 0);
     262             :         }
     263             : 
     264           3 :         elektraModulesClose (modules, 0);
     265           3 :         ksDel (modules);
     266           3 :         keyDel (parentKey);
     267           3 : }
     268             : 
     269           3 : int main (int argc, char ** argv)
     270             : {
     271           3 :         printf ("BASE64       TESTS\n");
     272           3 :         printf ("==================\n\n");
     273             : 
     274           3 :         init (argc, argv);
     275             : 
     276             :         // test the encoding and decoding process
     277           3 :         test_base64_encoding ();
     278           3 :         test_base64_decoding ();
     279             : 
     280             :         // test the plugin functionality
     281           3 :         test_init ();
     282           3 :         test_base64_plugin_regular ();
     283           3 :         test_base64_plugin_decoding_error ();
     284             : 
     285           9 :         print_result (ELEKTRA_PLUGIN_NAME);
     286           3 :         return nbError;
     287             : }

Generated by: LCOV version 1.13