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