forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testmod_base64.c
287 lines (242 loc) · 8.71 KB
/
testmod_base64.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* @file
*
* @brief test suite for the base64 plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include <tests_plugin.h>
#include "base64.h"
// test vectors are defined in RFC4648
// see https://www.ietf.org/rfc/rfc4648.txt
static const char * decoded[] = { "", "f", "fo", "foo", "foob", "fooba", "foobar" };
static const char * encoded[] = { "", "Zg==", "Zm8=", "Zm9v", "Zm9vYg==", "Zm9vYmE=", "Zm9vYmFy" };
static const size_t testcaseCounter = sizeof (decoded) / sizeof (const char *);
static inline KeySet * newPluginConfiguration (void)
{
return ksNew (0, KS_END);
}
static void test_init (void)
#ifdef __llvm__
__attribute__ ((annotate ("oclint:suppress[high ncss method]")))
#endif
{
Plugin * plugin = NULL;
Key * parentKey = keyNew ("system:/", KEY_END);
KeySet * modules = ksNew (0, KS_END);
KeySet * configKs = newPluginConfiguration ();
elektraModulesInit (modules, 0);
plugin = elektraPluginOpen (ELEKTRA_PLUGIN_NAME, modules, configKs, 0);
succeed_if (plugin != 0, "failed to open the plugin");
if (plugin)
{
succeed_if (strcmp (plugin->name, ELEKTRA_PLUGIN_NAME) == 0, "got wrong name");
KeySet * config = elektraPluginGetConfig (plugin);
succeed_if (config != 0, "there should be a config");
succeed_if (plugin->kdbGet != 0, "no get pointer");
succeed_if (plugin->kdbSet != 0, "no set pointer");
elektraPluginClose (plugin, 0);
}
elektraModulesClose (modules, 0);
ksDel (modules);
keyDel (parentKey);
}
static inline char testcase2char (size_t offset)
{
return '0' + offset + 1;
}
static void test_base64_encoding (void)
#ifdef __llvm__
__attribute__ ((annotate ("oclint:suppress[deep nested block]")))
#endif
{
char errorAlloc[] = "Encoding #.: Memory allocation failed";
char errorMismatch[] = "Encoding #.: returned unexpected result";
for (size_t charOffset = 0; charOffset < testcaseCounter; charOffset++)
{
errorAlloc[10] = testcase2char (charOffset);
errorMismatch[10] = testcase2char (charOffset);
char * encodedText = base64Encode ((kdb_octet_t *) decoded[charOffset], strlen (decoded[charOffset]));
succeed_if (encodedText, errorAlloc);
if (encodedText)
{
succeed_if (strcmp (encodedText, encoded[charOffset]) == 0, errorMismatch);
elektraFree (encodedText);
}
}
}
static void test_base64_decoding (void)
#ifdef __llvm__
__attribute__ ((annotate ("oclint:suppress[deep nested block]"), annotate ("oclint:suppress[high npath complexity]"),
annotate ("oclint:suppress[high ncss method]"), annotate ("oclint:suppress[high cyclomatic complexity]")))
#endif
{
char errorFail[] = "Decoding #.: operation failed";
char errorMismatch[] = "Decoding #.: returned unexpected result vector";
char errorLength[] = "Decoding #.: returned unexpected result length";
kdb_octet_t * buffer = NULL;
size_t bufferLen = 0;
// first test case is a little special because we expect NULL on success here
succeed_if (base64Decode (encoded[0], &buffer, &bufferLen) == 1, "decoding of test vector 1 failed");
succeed_if (buffer == NULL, "decoding of test vector 1 returned unexpected result vector");
succeed_if (bufferLen == 0, "decoding of test vector 1 returned unexpected result length");
if (buffer)
{
elektraFree (buffer);
}
for (size_t i = 1; i < testcaseCounter; i++)
{
errorMismatch[10] = testcase2char (i);
errorFail[10] = testcase2char (i);
errorLength[10] = testcase2char (i);
succeed_if (base64Decode (encoded[i], &buffer, &bufferLen) == 1, errorFail);
if (buffer)
{
succeed_if (bufferLen == strlen (decoded[i]), errorLength);
if (bufferLen == strlen (decoded[i]))
{
succeed_if (memcmp (buffer, decoded[i], bufferLen) == 0, errorMismatch);
}
elektraFree (buffer);
buffer = NULL;
}
}
}
static void test_base64_plugin_regular (void)
#ifdef __llvm__
__attribute__ ((annotate ("oclint:suppress[deep nested block]"), annotate ("oclint:suppress[high ncss method]"),
annotate ("oclint:suppress[high npath complexity]"), annotate ("oclint:suppress[long method]"),
annotate ("oclint:suppress[high cyclomatic complexity]")))
#endif
{
Plugin * plugin = NULL;
Key * parentKey = keyNew ("system:/", KEY_END);
KeySet * modules = ksNew (0, KS_END);
KeySet * config = newPluginConfiguration ();
elektraModulesInit (modules, 0);
plugin = elektraPluginOpen (ELEKTRA_PLUGIN_NAME, modules, config, 0);
succeed_if (plugin, "failed to open plugin handle");
if (plugin)
{
Key * key;
const kdb_octet_t sampleValue[] = { 0x31, 0x32, 0x33 };
KeySet * data = ksNew (4, keyNew ("/t/k1", KEY_VALUE, "Hello World", KEY_END),
keyNew ("/t/k2", KEY_BINARY, KEY_SIZE, sizeof (sampleValue), KEY_VALUE, sampleValue, KEY_END),
keyNew ("/t/k3", KEY_BINARY, KEY_SIZE, 0, KEY_END),
keyNew ("/t/k4", KEY_VALUE, ELEKTRA_PLUGIN_BASE64_PREFIX, KEY_END), KS_END);
// test encoding
succeed_if (plugin->kdbSet (plugin, data, parentKey) == 1, "kdb set failed");
key = ksLookupByName (data, "/t/k1", 0);
succeed_if (key, "lost key in data KeySet");
if (key)
{
succeed_if (strcmp (keyString (key), "Hello World") == 0, "changed string value that does not require encoding");
}
key = ksLookupByName (data, "/t/k2", 0);
succeed_if (key, "lost key in data KeySet");
if (key)
{
succeed_if (strcmp (keyString (key), ELEKTRA_PLUGIN_BASE64_PREFIX "MTIz") == 0,
"encoding binary key failed during kdb set");
}
key = ksLookupByName (data, "/t/k3", 0);
succeed_if (key, "lost key in data KeySet");
if (key)
{
succeed_if (strcmp (keyString (key), ELEKTRA_PLUGIN_BASE64_PREFIX "") == 0,
"encoding NULL-key failed during kdb set");
}
key = ksLookupByName (data, "/t/k4", 0);
succeed_if (key, "lost key in data KeySet");
if (key)
{
succeed_if (strcmp (keyString (key), ELEKTRA_PLUGIN_BASE64_ESCAPE ELEKTRA_PLUGIN_BASE64_PREFIX) == 0,
"encoding string starting with prefix " ELEKTRA_PLUGIN_BASE64_ESCAPE " failed during kdb set");
}
// test decoding
succeed_if (plugin->kdbGet (plugin, data, parentKey) == 1, "kdb get (pregetstorage) failed");
key = ksLookupByName (data, "/t/k1", 0);
succeed_if (key, "lost key in data KeySet");
if (key)
{
succeed_if (strcmp (keyString (key), "Hello World") == 0, "changed string value that does not require decoding");
}
key = ksLookupByName (data, "/t/k2", 0);
succeed_if (key, "lost key in data KeySet");
if (key)
{
succeed_if (keyGetValueSize (key) == sizeof (sampleValue), "decoding binary key failed during kdb get");
if (keyGetValueSize (key) == sizeof (sampleValue))
{
succeed_if (memcmp (sampleValue, keyValue (key), sizeof (sampleValue)) == 0,
"decoding binary key failed during kdb get");
}
}
key = ksLookupByName (data, "/t/k3", 0);
succeed_if (key, "lost key in data KeySet");
if (key)
{
succeed_if (keyGetValueSize (key) <= 0, "decoding NULL-key failed during kdb get");
}
key = ksLookupByName (data, "/t/k4", 0);
succeed_if (key, "lost key in data KeySet");
if (key)
{
succeed_if (strcmp (keyString (key), ELEKTRA_PLUGIN_BASE64_PREFIX) == 0,
"decoding string starting with prefix " ELEKTRA_PLUGIN_BASE64_ESCAPE " failed during kdb get");
}
ksDel (data);
elektraPluginClose (plugin, 0);
}
elektraModulesClose (modules, 0);
ksDel (modules);
keyDel (parentKey);
}
static void test_base64_plugin_decoding_error (void)
#ifdef __llvm__
__attribute__ ((annotate ("oclint:suppress[deep nested block]"), annotate ("oclint:suppress[high ncss method]")))
#endif
{
Plugin * plugin = NULL;
Key * parentKey = keyNew ("system:/", KEY_END);
KeySet * modules = ksNew (0, KS_END);
KeySet * config = newPluginConfiguration ();
elektraModulesInit (modules, 0);
plugin = elektraPluginOpen (ELEKTRA_PLUGIN_NAME, modules, config, 0);
succeed_if (plugin, "failed to open plugin handle");
if (plugin)
{
Key * key;
KeySet * data = ksNew (1, keyNew ("/t/k1", KEY_VALUE, ELEKTRA_PLUGIN_BASE64_PREFIX "_$..", KEY_END), KS_END);
// test failing decoding
succeed_if (plugin->kdbGet (plugin, data, parentKey) == 1, "kdb get failed");
key = ksLookupByName (data, "/t/k1", 0);
succeed_if (key, "lost key in data KeySet");
if (key)
{
succeed_if (strcmp (keyString (key), ELEKTRA_PLUGIN_BASE64_PREFIX "_$..") == 0,
"decoded string value that should have failed");
}
ksDel (data);
elektraPluginClose (plugin, 0);
}
elektraModulesClose (modules, 0);
ksDel (modules);
keyDel (parentKey);
}
int main (int argc, char ** argv)
{
printf ("BASE64 TESTS\n");
printf ("==================\n\n");
init (argc, argv);
// test the encoding and decoding process
test_base64_encoding ();
test_base64_decoding ();
// test the plugin functionality
test_init ();
test_base64_plugin_regular ();
test_base64_plugin_decoding_error ();
print_result (ELEKTRA_PLUGIN_NAME);
return nbError;
}