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