forked from ostreedev/ostree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-gpg-verify-result.c
448 lines (379 loc) · 14.1 KB
/
test-gpg-verify-result.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* SPDX-License-Identifier: LGPL-2.0+
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <gpgme.h>
#include "libglnx.h"
#include "ostree-gpg-verify-result-private.h"
#define assert_no_gpg_error(err, filename) \
G_STMT_START { \
if (err != GPG_ERR_NO_ERROR) { \
g_autoptr(GString) string = g_string_new ("assertion failed "); \
g_string_append_printf (string, "%s: %s ", gpgme_strsource (err), gpgme_strerror (err)); \
g_string_append (string, filename ? filename : ""); \
g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, string->str); \
} \
} G_STMT_END
typedef struct {
OstreeGpgVerifyResult *result;
} TestFixture;
static OstreeGpgSignatureAttr some_attributes[] = {
OSTREE_GPG_SIGNATURE_ATTR_VALID,
OSTREE_GPG_SIGNATURE_ATTR_SIG_EXPIRED,
OSTREE_GPG_SIGNATURE_ATTR_KEY_EXPIRED,
OSTREE_GPG_SIGNATURE_ATTR_KEY_REVOKED,
OSTREE_GPG_SIGNATURE_ATTR_KEY_MISSING,
OSTREE_GPG_SIGNATURE_ATTR_KEY_EXP_TIMESTAMP,
};
static void
test_fixture_setup (TestFixture *fixture,
gconstpointer user_data)
{
gpgme_error_t gpg_error;
gpgme_data_t data_buffer;
gpgme_data_t signature_buffer;
OstreeGpgVerifyResult *result;
g_autofree char *homedir = NULL;
g_autofree char *filename = NULL;
GError *local_error = NULL;
/* Mimic what OstreeGpgVerifier does to create OstreeGpgVerifyResult.
* We don't use OstreeGpgVerifier directly because we don't need the
* multiple-keyring workaround and because we want the trust database
* taken into account, which contains additional data like revocation
* certificates for certain test cases. */
homedir = g_test_build_filename (G_TEST_DIST, "tests/gpg-verify-data", NULL);
g_setenv ("GNUPGHOME", homedir, TRUE);
result = g_initable_new (OSTREE_TYPE_GPG_VERIFY_RESULT,
NULL, &local_error, NULL);
g_assert_no_error (local_error);
filename = g_build_filename (homedir, "lgpl2", NULL);
gpg_error = gpgme_data_new_from_file (&data_buffer, filename, 1);
assert_no_gpg_error (gpg_error, filename);
g_clear_pointer (&filename, g_free);
filename = g_build_filename (homedir, "lgpl2.sig", NULL);
gpg_error = gpgme_data_new_from_file (&signature_buffer, filename, 1);
assert_no_gpg_error (gpg_error, filename);
gpg_error = gpgme_op_verify (result->context,
signature_buffer, data_buffer, NULL);
assert_no_gpg_error (gpg_error, NULL);
result->details = gpgme_op_verify_result (result->context);
gpgme_result_ref (result->details);
gpgme_data_release (data_buffer);
gpgme_data_release (signature_buffer);
fixture->result = result;
}
static void
test_fixture_teardown (TestFixture *fixture,
gconstpointer user_data)
{
g_clear_object (&fixture->result);
}
static void
test_check_counts (TestFixture *fixture,
gconstpointer user_data)
{
guint count_all;
guint count_valid;
count_all = ostree_gpg_verify_result_count_all (fixture->result);
count_valid = ostree_gpg_verify_result_count_valid (fixture->result);
g_assert_cmpint (count_all, ==, 5);
g_assert_cmpint (count_valid, ==, 2);
}
static void
test_signature_lookup (TestFixture *fixture,
gconstpointer user_data)
{
/* Checking the signature with the revoked key for this case. */
guint expected_signature_index = GPOINTER_TO_UINT (user_data);
/* Lowercase letters to ensure OstreeGpgVerifyResult handles it. */
const char *fingerprint = "68dcc2db4bec5811c2573590bd9d2a44b7f541a6";
guint signature_index;
gboolean signature_found;
/* Lookup full fingerprint. */
signature_index = 999999;
signature_found = ostree_gpg_verify_result_lookup (fixture->result,
fingerprint,
&signature_index);
g_assert_true (signature_found);
g_assert_cmpint (signature_index, ==, expected_signature_index);
/* Lookup abbreviated key ID. */
signature_index = 999999;
signature_found = ostree_gpg_verify_result_lookup (fixture->result,
fingerprint + 32,
&signature_index);
g_assert_true (signature_found);
g_assert_cmpint (signature_index, ==, expected_signature_index);
/* Bogus fingerprint, index should remain unchanged. */
signature_index = expected_signature_index = 999999;
fingerprint = "CAFEBABECAFEBABECAFEBABECAFEBABECAFEBABE";
signature_found = ostree_gpg_verify_result_lookup (fixture->result,
fingerprint,
&signature_index);
g_assert_false (signature_found);
g_assert_cmpint (signature_index, ==, expected_signature_index);
}
static void
test_attribute_basics (TestFixture *fixture,
gconstpointer user_data)
{
guint n_signatures, ii;
n_signatures = ostree_gpg_verify_result_count_valid (fixture->result);
for (ii = 0; ii < n_signatures; ii++)
{
g_autoptr(GVariant) tuple = NULL;
const char *attr_string;
const char *type_string;
gboolean key_missing;
tuple = ostree_gpg_verify_result_get_all (fixture->result, ii);
type_string = g_variant_get_type_string (tuple);
g_assert_cmpstr (type_string, ==, "(bbbbbsxxsssssxx)");
/* Check attributes which should be common to all signatures. */
g_variant_get_child (tuple,
OSTREE_GPG_SIGNATURE_ATTR_PUBKEY_ALGO_NAME,
"&s", &attr_string);
g_assert_cmpstr (attr_string, ==, "RSA");
g_variant_get_child (tuple,
OSTREE_GPG_SIGNATURE_ATTR_HASH_ALGO_NAME,
"&s", &attr_string);
g_assert_cmpstr (attr_string, ==, "SHA1");
g_variant_get_child (tuple,
OSTREE_GPG_SIGNATURE_ATTR_KEY_MISSING,
"b", &key_missing);
g_variant_get_child (tuple,
OSTREE_GPG_SIGNATURE_ATTR_USER_NAME,
"&s", &attr_string);
if (key_missing)
g_assert_cmpstr (attr_string, ==, "[unknown name]");
else
g_assert_cmpstr (attr_string, ==, "J. Random User");
g_variant_get_child (tuple,
OSTREE_GPG_SIGNATURE_ATTR_USER_EMAIL,
"&s", &attr_string);
if (key_missing)
g_assert_cmpstr (attr_string, ==, "[unknown email]");
else
g_assert_cmpstr (attr_string, ==, "[email protected]");
}
}
static void
test_valid_signature (TestFixture *fixture,
gconstpointer user_data)
{
guint signature_index = GPOINTER_TO_UINT (user_data);
g_autoptr(GVariant) tuple = NULL;
gboolean valid;
gboolean sig_expired;
gboolean key_expired;
gboolean key_revoked;
gboolean key_missing;
gint64 key_exp_timestamp;
tuple = ostree_gpg_verify_result_get (fixture->result,
signature_index,
some_attributes,
G_N_ELEMENTS (some_attributes));
g_variant_get (tuple, "(bbbbbx)",
&valid,
&sig_expired,
&key_expired,
&key_revoked,
&key_missing,
&key_exp_timestamp);
g_assert_true (valid);
g_assert_false (sig_expired);
g_assert_false (key_expired);
g_assert_false (key_revoked);
g_assert_false (key_missing);
g_assert_cmpint (key_exp_timestamp, ==, 0);
}
static void
test_expired_key (TestFixture *fixture,
gconstpointer user_data)
{
guint signature_index = GPOINTER_TO_UINT (user_data);
g_autoptr(GVariant) tuple = NULL;
gboolean valid;
gboolean sig_expired;
gboolean key_expired;
gboolean key_revoked;
gboolean key_missing;
gint64 key_exp_timestamp;
tuple = ostree_gpg_verify_result_get (fixture->result,
signature_index,
some_attributes,
G_N_ELEMENTS (some_attributes));
g_variant_get (tuple, "(bbbbbx)",
&valid,
&sig_expired,
&key_expired,
&key_revoked,
&key_missing,
&key_exp_timestamp);
g_assert_false (valid);
g_assert_false (sig_expired);
g_assert_true (key_expired);
g_assert_false (key_revoked);
g_assert_false (key_missing);
g_assert_cmpint (key_exp_timestamp, ==, 1426782201);
}
static void
test_revoked_key (TestFixture *fixture,
gconstpointer user_data)
{
guint signature_index = GPOINTER_TO_UINT (user_data);
g_autoptr(GVariant) tuple = NULL;
gboolean valid;
gboolean sig_expired;
gboolean key_expired;
gboolean key_revoked;
gboolean key_missing;
gint64 key_exp_timestamp;
tuple = ostree_gpg_verify_result_get (fixture->result,
signature_index,
some_attributes,
G_N_ELEMENTS (some_attributes));
g_variant_get (tuple, "(bbbbbx)",
&valid,
&sig_expired,
&key_expired,
&key_revoked,
&key_missing,
&key_exp_timestamp);
g_assert_false (valid);
g_assert_false (sig_expired);
g_assert_false (key_expired);
g_assert_true (key_revoked);
g_assert_false (key_missing);
g_assert_cmpint (key_exp_timestamp, ==, 0);
}
static void
test_missing_key (TestFixture *fixture,
gconstpointer user_data)
{
guint signature_index = GPOINTER_TO_UINT (user_data);
g_autoptr(GVariant) tuple = NULL;
gboolean valid;
gboolean sig_expired;
gboolean key_expired;
gboolean key_revoked;
gboolean key_missing;
gint64 key_exp_timestamp;
tuple = ostree_gpg_verify_result_get (fixture->result,
signature_index,
some_attributes,
G_N_ELEMENTS (some_attributes));
g_variant_get (tuple, "(bbbbbx)",
&valid,
&sig_expired,
&key_expired,
&key_revoked,
&key_missing,
&key_exp_timestamp);
g_assert_false (valid);
g_assert_false (sig_expired);
g_assert_false (key_expired);
g_assert_false (key_revoked);
g_assert_true (key_missing);
g_assert_cmpint (key_exp_timestamp, ==, 0);
}
static void
test_expired_signature (TestFixture *fixture,
gconstpointer user_data)
{
guint signature_index = GPOINTER_TO_UINT (user_data);
g_autoptr(GVariant) tuple = NULL;
gboolean valid;
gboolean sig_expired;
gboolean key_expired;
gboolean key_revoked;
gboolean key_missing;
gint64 key_exp_timestamp;
tuple = ostree_gpg_verify_result_get (fixture->result,
signature_index,
some_attributes,
G_N_ELEMENTS (some_attributes));
g_variant_get (tuple, "(bbbbbx)",
&valid,
&sig_expired,
&key_expired,
&key_revoked,
&key_missing,
&key_exp_timestamp);
g_assert_true (valid);
g_assert_true (sig_expired);
g_assert_false (key_expired);
g_assert_false (key_revoked);
g_assert_false (key_missing);
g_assert_cmpint (key_exp_timestamp, ==, 0);
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
(void) gpgme_check_version (NULL);
g_test_add ("/gpg-verify-result/check-counts",
TestFixture,
NULL,
test_fixture_setup,
test_check_counts,
test_fixture_teardown);
g_test_add ("/gpg-verify-result/signature-lookup",
TestFixture,
GINT_TO_POINTER (2),
test_fixture_setup,
test_signature_lookup,
test_fixture_teardown);
g_test_add ("/gpg-verify-result/attribute-basics",
TestFixture,
NULL,
test_fixture_setup,
test_attribute_basics,
test_fixture_teardown);
g_test_add ("/gpg-verify-result/valid-signature",
TestFixture,
GINT_TO_POINTER (0), /* signature index */
test_fixture_setup,
test_valid_signature,
test_fixture_teardown);
g_test_add ("/gpg-verify-result/expired-key",
TestFixture,
GINT_TO_POINTER (1), /* signature index */
test_fixture_setup,
test_expired_key,
test_fixture_teardown);
g_test_add ("/gpg-verify-result/revoked-key",
TestFixture,
GINT_TO_POINTER (2), /* signature index */
test_fixture_setup,
test_revoked_key,
test_fixture_teardown);
g_test_add ("/gpg-verify-result/missing-key",
TestFixture,
GINT_TO_POINTER (3), /* signature index */
test_fixture_setup,
test_missing_key,
test_fixture_teardown);
g_test_add ("/gpg-verify-result/expired-signature",
TestFixture,
GINT_TO_POINTER (4), /* signature index */
test_fixture_setup,
test_expired_signature,
test_fixture_teardown);
return g_test_run ();
}