forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_internals.h
280 lines (231 loc) · 9.74 KB
/
test_internals.h
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
/**
* @file
*
* @brief test suite for the crypto plugin.
* Contains shared functions for all compile variants.
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include "crypto.h"
#include "gpg.h"
#include "helper.h"
#include <kdb.h>
#include <kdbinternal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tests_internal.h>
#include <tests_plugin.h>
#include "common_gpg_tests.c"
#include "gpgagent_teardown.h"
#include "test_key.h"
#define TEST_KEY_ID "DDEBEF9EE2DC931701338212DAF635B17F230E8D"
static KeySet * newPluginConfiguration (void);
#define TEST_SUITE(PLUGIN_NAME) \
if (gpg_available (newPluginConfiguration ())) \
{ \
test_gpg (); \
test_init (PLUGIN_NAME); \
test_incomplete_config (PLUGIN_NAME); \
test_crypto_operations (PLUGIN_NAME); \
test_teardown (); \
} \
else \
{ \
printf ("The test was disabled because gpg could not be found on the system.\n"); \
}
typedef int (*checkConfPtr) (Key *, KeySet *);
static const char strVal[] = "abcde";
static const char strValLong[] = "Oh loooooooooooooooooooong Johnson";
static const char strFullBlockSingle[] = "abcdefghijklmno";
static const char strFullBlockDouble[] = "I am root!!!!!!!!!!!!!!!!!!!!!?";
static const kdb_octet_t binVal[] = { 0x01, 0x02, 0x03, 0x04 };
static inline ssize_t MIN (ssize_t a, ssize_t b)
{
return (a < b) ? a : b;
}
static int isMarkedForEncryption (const Key * k)
{
const Key * metaEncrypt = keyGetMeta (k, ELEKTRA_CRYPTO_META_ENCRYPT);
if (metaEncrypt && strcmp (keyString (metaEncrypt), "1") == 0)
{
return 1;
}
return 0;
}
/**
* @brief create a new KeySet holding sample data for encryption and decryption.
*/
static KeySet * newTestdataKeySet (void)
{
Key * kUnchanged1 = keyNew ("user/crypto/test/nochange", KEY_END);
Key * kUnchanged2 = keyNew ("user/crypto/test/nochange2", KEY_END);
Key * kNull = keyNew ("user/crypto/test/mynull", KEY_END);
Key * kString = keyNew ("user/crypto/test/mystring", KEY_END);
Key * kStringLong = keyNew ("user/crypto/test/myextralongstring", KEY_END);
Key * kStringFullBlockSingle = keyNew ("user/crypto/test/myfullblocksingle", KEY_END);
Key * kStringFullBlockDouble = keyNew ("user/crypto/test/myfullblockdouble", KEY_END);
Key * kBin = keyNew ("user/crypto/test/mybin", KEY_END);
keySetString (kUnchanged1, strVal);
keySetString (kUnchanged2, strVal);
keySetMeta (kUnchanged2, ELEKTRA_CRYPTO_META_ENCRYPT, "0");
keySetBinary (kNull, 0, 0);
keySetMeta (kNull, ELEKTRA_CRYPTO_META_ENCRYPT, "1");
keySetString (kString, strVal);
keySetMeta (kString, ELEKTRA_CRYPTO_META_ENCRYPT, "1");
keySetString (kStringLong, strValLong);
keySetMeta (kStringLong, ELEKTRA_CRYPTO_META_ENCRYPT, "1");
keySetString (kStringFullBlockSingle, strFullBlockSingle);
keySetMeta (kStringFullBlockSingle, ELEKTRA_CRYPTO_META_ENCRYPT, "1");
keySetString (kStringFullBlockDouble, strFullBlockDouble);
keySetMeta (kStringFullBlockDouble, ELEKTRA_CRYPTO_META_ENCRYPT, "1");
keySetBinary (kBin, binVal, sizeof (binVal));
keySetMeta (kBin, ELEKTRA_CRYPTO_META_ENCRYPT, "1");
return ksNew (8, kUnchanged1, kUnchanged2, kNull, kString, kStringLong, kStringFullBlockSingle, kStringFullBlockDouble, kBin,
KS_END);
}
static inline void setPluginShutdown (KeySet * config)
{
ksAppendKey (config, keyNew (ELEKTRA_CRYPTO_PARAM_SHUTDOWN, KEY_VALUE, "1", 0));
}
static KeySet * newPluginConfiguration (void)
{
return ksNew (2, keyNew (ELEKTRA_RECIPIENT_KEY, KEY_VALUE, TEST_KEY_ID, KEY_END),
keyNew (ELEKTRA_CRYPTO_PARAM_GPG_UNIT_TEST, KEY_VALUE, "1", KEY_END), KS_END);
}
static void test_init (const char * pluginName)
{
Plugin * plugin = NULL;
Key * parentKey = keyNew ("system", KEY_END);
KeySet * modules = ksNew (0, KS_END);
KeySet * configKs = newPluginConfiguration ();
elektraModulesInit (modules, 0);
plugin = elektraPluginOpen (pluginName, modules, configKs, 0);
succeed_if (plugin != 0, "failed to open the plugin");
if (plugin)
{
succeed_if (!strcmp (plugin->name, pluginName), "got wrong name");
KeySet * config = elektraPluginGetConfig (plugin);
succeed_if (config != 0, "there should be a config");
succeed_if (plugin->kdbOpen != 0, "no open pointer");
succeed_if (plugin->kdbClose != 0, "no close pointer");
succeed_if (plugin->kdbGet != 0, "no get pointer");
succeed_if (plugin->kdbSet != 0, "no set pointer");
// try re-opening the plugin
succeed_if (plugin->kdbClose (plugin, parentKey) == 1, "kdb close failed");
succeed_if (plugin->kdbOpen (plugin, parentKey) == 1, "re-opening the plugin failed");
succeed_if (plugin->kdbClose (plugin, parentKey) == 1, "kdb close failed");
elektraPluginClose (plugin, 0);
}
elektraModulesClose (modules, 0);
ksDel (modules);
keyDel (parentKey);
}
static void test_incomplete_config (const char * pluginName)
{
Plugin * plugin = NULL;
Key * parentKey = keyNew ("system", KEY_END);
KeySet * modules = ksNew (0, KS_END);
KeySet * configKs = ksNew (0, KS_END);
elektraModulesInit (modules, 0);
plugin = elektraPluginOpen (pluginName, modules, configKs, 0);
succeed_if (plugin != 0, "failed to open the plugin");
if (plugin)
{
KeySet * data = newTestdataKeySet ();
succeed_if (plugin->kdbSet (plugin, data, parentKey) == -1, "kdb set succeeded with incomplete configuration");
ksDel (data);
elektraPluginClose (plugin, 0);
}
elektraModulesClose (modules, 0);
ksDel (modules);
keyDel (parentKey);
}
static void test_crypto_operations (const char * pluginName)
{
union
{
checkConfPtr f;
void * v;
} conversation;
Plugin * plugin = NULL;
Key * parentKey = keyNew ("system", KEY_END);
KeySet * modules = ksNew (0, KS_END);
KeySet * config = newPluginConfiguration ();
setPluginShutdown (config);
elektraModulesInit (modules, 0);
plugin = elektraPluginOpen (pluginName, modules, config, 0);
if (plugin)
{
Key * k;
KeySet * data = newTestdataKeySet ();
KeySet * original = ksDup (data);
// read and check the contract
KeySet * contract = ksNew (0, KS_END);
Key * contractParent = keyNew ("system/elektra/modules/" ELEKTRA_PLUGIN_NAME, KEY_END);
succeed_if (plugin->kdbGet (plugin, contract, contractParent) == 1, "kdb get for contract failed");
// run checkconf to generate the master password
Key * function = ksLookupByName (contract, "system/elektra/modules/" ELEKTRA_PLUGIN_NAME "/exports/checkconf", 0);
succeed_if (function, "no symbol exported for the checkconf function");
if (function)
{
succeed_if (keyGetBinary (function, &conversation.v, sizeof (conversation)) == sizeof (conversation),
"type mismatch in function pointer to checkconf");
succeed_if (conversation.f, "exported NULL pointer as checkconf function");
if (conversation.f)
{
KeySet * pluginConfig = elektraPluginGetConfig (plugin);
succeed_if (conversation.f (parentKey, pluginConfig) != -1, "checkconf call failed");
}
}
keyDel (contractParent);
ksDel (contract);
// test encryption with kdb set
succeed_if (plugin->kdbSet (plugin, data, parentKey) == 1, "kdb set failed");
// verify key set
ksRewind (data);
while ((k = ksNext (data)) != 0)
{
if (isMarkedForEncryption (k))
{
succeed_if (keyIsBinary (k), "Key value is not binary although it should have been encrypted");
succeed_if (keyGetValueSize (k) > 0, "NULL Key must have encrypted metadata and can not have length 0");
succeed_if (memcmp (keyValue (k), binVal, MIN (keyGetValueSize (k), (ssize_t) sizeof (binVal))),
"encryption failed");
succeed_if (memcmp (keyValue (k), strVal, MIN (keyGetValueSize (k), (ssize_t) sizeof (strVal))),
"encryption failed");
succeed_if (memcmp (keyValue (k), strValLong, MIN (keyGetValueSize (k), (ssize_t) sizeof (strValLong))),
"encryption failed");
}
else
{
succeed_if (!strcmp (keyString (k), strVal), "Key value changed without being marked for encryption");
}
}
// test decryption with kdb get
succeed_if (plugin->kdbGet (plugin, data, parentKey) == 1, "kdb get failed");
compare_keyset (data, original);
ksDel (original);
ksDel (data);
elektraPluginClose (plugin, 0);
}
elektraModulesClose (modules, 0);
ksDel (modules);
keyDel (parentKey);
}
static void test_gpg (void)
{
// Plugin configuration
KeySet * conf = newPluginConfiguration ();
Key * errorKey = keyNew (0);
// install the gpg key
char * argv[] = { "", "-a", "--import", NULL };
const size_t argc = 4;
Key * msg = keyNew (0);
keySetBinary (msg, test_key_asc, test_key_asc_len);
succeed_if (ELEKTRA_PLUGIN_FUNCTION (gpgCall) (conf, errorKey, msg, argv, argc) == 1, "failed to install the GPG test key");
keyDel (msg);
keyDel (errorKey);
ksDel (conf);
}