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

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #ifdef HAVE_KDBCONFIG_H
      10             : #include "kdbconfig.h"
      11             : #endif
      12             : 
      13             : #include <stdio.h>
      14             : #ifdef HAVE_STDLIB_H
      15             : #include <stdlib.h>
      16             : #endif
      17             : #ifdef HAVE_STRING_H
      18             : #include <string.h>
      19             : #endif
      20             : 
      21             : #include "hexcode.h"
      22             : 
      23             : #include <tests_internal.h>
      24             : 
      25             : const char encoded_string[] = "a\\20value\\20with\\3D\\3B\\23\\20and\\20\\5C\\20itself";
      26             : const char decoded_string[] = "a value with=;# and \\ itself";
      27             : 
      28           2 : void test_encode (void)
      29             : {
      30           2 :         printf ("test encode\n");
      31             : 
      32           2 :         CHexData * hd = calloc (1, sizeof (CHexData));
      33           2 :         hd->hd['\0'] = 1;
      34           2 :         hd->hd['\n'] = 1;
      35           2 :         hd->hd['\\'] = 1;
      36           2 :         hd->hd[' '] = 1;
      37           2 :         hd->hd['='] = 1;
      38           2 :         hd->hd[';'] = 1;
      39           2 :         hd->hd['#'] = 1;
      40           2 :         hd->escape = '\\';
      41             : 
      42             :         char buf[1000];
      43           2 :         hd->buf = buf;
      44             : 
      45           2 :         Key * test = keyNew ("user/test", KEY_VALUE, decoded_string, KEY_END);
      46           2 :         elektraHexcodeEncode (test, hd);
      47           2 :         succeed_if (!memcmp (keyValue (test), encoded_string, sizeof (encoded_string) - 1), "string not correctly encoded");
      48             : 
      49           2 :         elektraFree (hd);
      50           2 :         keyDel (test);
      51           2 : }
      52             : 
      53           2 : void test_decode (void)
      54             : {
      55           2 :         printf ("test decode\n");
      56             : 
      57           2 :         CHexData * hd = calloc (1, sizeof (CHexData));
      58           2 :         hd->escape = '\\';
      59             : 
      60             :         char buf[1000];
      61           2 :         hd->buf = buf;
      62             : 
      63           2 :         Key * test = keyNew ("user/test", KEY_SIZE, sizeof (encoded_string) - 1, KEY_VALUE, encoded_string, KEY_END);
      64           2 :         elektraHexcodeDecode (test, hd);
      65           2 :         succeed_if (!strcmp (keyString (test), decoded_string), "string not correctly encoded");
      66             : 
      67           2 :         elektraFree (hd);
      68           2 :         keyDel (test);
      69           2 : }
      70             : 
      71          22 : void check_reversibility (const char * msg)
      72             : {
      73          22 :         Key * decode = keyNew ("user/test", KEY_VALUE, msg, KEY_END);
      74             : 
      75          22 :         CHexData * hd = calloc (1, sizeof (CHexData));
      76          22 :         hd->hd['\0'] = 1;
      77          22 :         hd->hd['\n'] = 1;
      78          22 :         hd->hd['\\'] = 1;
      79          22 :         hd->hd[' '] = 1;
      80          22 :         hd->hd['='] = 1;
      81          22 :         hd->hd[';'] = 1;
      82          22 :         hd->hd['#'] = 1;
      83          22 :         hd->escape = '\\';
      84             : 
      85             :         char buf[1000];
      86          22 :         hd->buf = buf;
      87             : 
      88          22 :         Key * encode = keyDup (decode);
      89          22 :         elektraHexcodeEncode (encode, hd);
      90             : 
      91          22 :         elektraHexcodeDecode (encode, hd);
      92          22 :         compare_key (encode, decode);
      93             : 
      94          22 :         elektraFree (hd);
      95          22 :         keyDel (decode);
      96          22 :         keyDel (encode);
      97          22 : }
      98             : 
      99           2 : void test_reversibility (void)
     100             : {
     101           2 :         printf ("test reversibility\n");
     102             : 
     103           2 :         check_reversibility ("hello world");
     104           2 :         check_reversibility ("hello world!\nnew line");
     105           2 :         check_reversibility ("\0");
     106           2 :         check_reversibility ("\n");
     107           2 :         check_reversibility ("\\");
     108           2 :         check_reversibility (" ");
     109           2 :         check_reversibility ("=");
     110           2 :         check_reversibility (";");
     111           2 :         check_reversibility ("#");
     112           2 :         check_reversibility (" =;#");
     113           2 :         check_reversibility ("\n\\");
     114           2 : }
     115             : 
     116           2 : void test_config (void)
     117             : {
     118           2 :         KeySet * config =
     119           2 :                 ksNew (20, keyNew ("user/chars", KEY_END), keyNew ("user/chars/20", KEY_END), keyNew ("user/chars/23", KEY_END),
     120             :                        keyNew ("user/chars/5C", KEY_END), keyNew ("user/chars/3D", KEY_END), keyNew ("user/chars/3B", KEY_END), KS_END);
     121             : 
     122           2 :         KeySet * returned = ksNew (20, keyNew ("user/something", KEY_VALUE, decoded_string, KEY_END), KS_END);
     123             : 
     124           2 :         Plugin * p = calloc (1, sizeof (Plugin));
     125           2 :         p->config = config;
     126             : 
     127           2 :         elektraHexcodeOpen (p, 0);
     128             : 
     129           2 :         elektraHexcodeSet (p, returned, 0);
     130             : 
     131           2 :         Key * test = ksLookupByName (returned, "user/something", 0);
     132           2 :         succeed_if (!memcmp (keyValue (test), encoded_string, sizeof (encoded_string) - 1), "string not correctly encoded");
     133             : 
     134           2 :         elektraHexcodeClose (p, 0);
     135             : 
     136           2 :         ksDel (returned);
     137           2 :         ksDel (p->config);
     138           2 :         elektraFree (p);
     139           2 : }
     140             : 
     141             : 
     142           2 : int main (int argc, char ** argv)
     143             : {
     144           2 :         printf ("HEXCODE   TESTS\n");
     145           2 :         printf ("===============\n\n");
     146             : 
     147           2 :         init (argc, argv);
     148             : 
     149           2 :         test_encode ();
     150           2 :         test_decode ();
     151           2 :         test_reversibility ();
     152           2 :         test_config ();
     153             : 
     154           2 :         print_result ("testmod_hexcode");
     155             : 
     156           2 :         return nbError;
     157             : }

Generated by: LCOV version 1.13