Branch data Line data Source code
1 : : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : : /*
3 : : Copyright (C) 2011 Collabora Ltd
4 : :
5 : : The Gnome Keyring Library is free software; you can redistribute it and/or
6 : : modify it under the terms of the GNU Library General Public License as
7 : : published by the Free Software Foundation; either version 2 of the
8 : : License, or (at your option) any later version.
9 : :
10 : : The Gnome Keyring Library is distributed in the hope that it will be useful,
11 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : : Library General Public License for more details.
14 : :
15 : : You should have received a copy of the GNU Library General Public
16 : : License along with the Gnome Library; see the file COPYING.LIB. If not,
17 : : see <http://www.gnu.org/licenses/>.
18 : :
19 : : Author: Stef Walter <stefw@collabora.co.uk>
20 : : */
21 : :
22 : : #include "config.h"
23 : :
24 : : #include "gcr/gcr.h"
25 : : #include "gcr/gcr-openpgp.h"
26 : : #include "gcr/gcr-record.h"
27 : :
28 : : #include "egg/egg-armor.h"
29 : : #include "egg/egg-testing.h"
30 : :
31 : : #include <gcrypt.h>
32 : : #include <glib.h>
33 : : #include <string.h>
34 : :
35 : : typedef struct {
36 : : const gchar *name;
37 : : const gchar **records;
38 : : const gchar *filename;
39 : : const gchar *version;
40 : : GcrOpenpgpParseFlags flags;
41 : : } Fixture;
42 : :
43 : : static const gchar *werner_koch_records[] = {
44 : : "pub:e:1024:17:68B7AB8957548DCD:899816990:1136043547::o:::sca:\n"
45 : : "fpr:::::::::6BD9050FD8FC941B43412DCC68B7AB8957548DCD:\n"
46 : : "uid:e::::1102866526::B712A25DC2ABEF1579696C2925859931078C2C3E::Werner Koch (gnupg sig) <dd9jn@gnu.org>:\n",
47 : :
48 : : "pub:e:1024:17:5DE249965B0358A2:921520361:1247335656::o:::sc:\n"
49 : : "fpr:::::::::A4D94E92B0986AB5EE9DCD755DE249965B0358A2:\n"
50 : : "uid:e::::1113145458::F5B5738FAFB7543A01BAB31A6D767FBC789FF8A8::Werner Koch <wk@gnupg.org>:\n"
51 : : "uid:e::::1113145466::60095F7DAD08129CCE39E15BEB6BBE21937E3AA6::Werner Koch <wk@g10code.com>:\n"
52 : : "uid:e::::921520362::392B892CF897AD0F03EB26343C4C20A48B36513E::Werner Koch:\n"
53 : : "uid:e::::1113145466::3E000C0F7D13A3C57C633C16ABDC97F12EAF16C1::Werner Koch <werner@fsfe.org>:\n"
54 : : "sub:e:1024:17:60784E94010A57ED:1079892559:1199124559:::::s:\n"
55 : : "sub:e:2048:1:7299C628B604F148:1079892777:1136052777:::::e:\n"
56 : : "sub:e:2048:1:35E52D69C3680A6E:1136137762:1199123362:::::e:\n",
57 : :
58 : : "pub:e:1024:1:53B620D01CE0C630:1136130759:1230738759::o:::sc:\n"
59 : : "fpr:::::::::7B96D396E6471601754BE4DB53B620D01CE0C630:\n"
60 : : "uid:e::::1136130760::142B958D9816ECF810DBB83BD257E5C7DB36C99A::Werner Koch (dist sig) <dd9jn@gnu.org>:\n",
61 : :
62 : : NULL
63 : : };
64 : :
65 : : static const gchar *werner_sig_records[] = {
66 : : "pub:e:1024:17:68B7AB8957548DCD:899816990:1136043547::o:::sca:\n"
67 : : "fpr:::::::::6BD9050FD8FC941B43412DCC68B7AB8957548DCD:\n"
68 : : "uid:e::::1102866526::B712A25DC2ABEF1579696C2925859931078C2C3E::Werner Koch (gnupg sig) <dd9jn@gnu.org>:\n"
69 : : "sig:::17:68B7AB8957548DCD:1102866526:::::13x:\n",
70 : :
71 : : "pub:e:1024:17:5DE249965B0358A2:921520361:1247335656::o:::sc:\n"
72 : : "fpr:::::::::A4D94E92B0986AB5EE9DCD755DE249965B0358A2:\n"
73 : : "uid:e::::1113145458::F5B5738FAFB7543A01BAB31A6D767FBC789FF8A8::Werner Koch <wk@gnupg.org>:\n"
74 : : "sig:::17:5DE249965B0358A2:1113145458:::::13x:\n"
75 : : "uid:e::::1113145466::60095F7DAD08129CCE39E15BEB6BBE21937E3AA6::Werner Koch <wk@g10code.com>:\n"
76 : : "sig:::17:5DE249965B0358A2:1113145466:::::13x:\n"
77 : : "uid:e::::921520362::392B892CF897AD0F03EB26343C4C20A48B36513E::Werner Koch:\n"
78 : : "sig:::17:5DE249965B0358A2:921520362:::::13x:\n"
79 : : "uid:e::::1113145466::3E000C0F7D13A3C57C633C16ABDC97F12EAF16C1::Werner Koch <werner@fsfe.org>:\n"
80 : : "sig:::17:5DE249965B0358A2:1113145466:::::13x:\n"
81 : : "sub:e:1024:17:60784E94010A57ED:1079892559:1199124559:::::s:\n"
82 : : "sig:::17:5DE249965B0358A2:1148562461:::::18x:\n"
83 : : "sub:e:2048:1:7299C628B604F148:1079892777:1136052777:::::e:\n"
84 : : "sig:::17:5DE249965B0358A2:1079892777:::::18x:\n"
85 : : "sub:e:2048:1:35E52D69C3680A6E:1136137762:1199123362:::::e:\n"
86 : : "sig:::17:5DE249965B0358A2:1136137762:::::18x:\n",
87 : :
88 : : "pub:e:1024:1:53B620D01CE0C630:1136130759:1230738759::o:::sc:\n"
89 : : "fpr:::::::::7B96D396E6471601754BE4DB53B620D01CE0C630:\n"
90 : : "uid:e::::1136130760::142B958D9816ECF810DBB83BD257E5C7DB36C99A::Werner Koch (dist sig) <dd9jn@gnu.org>:\n"
91 : : "sig:::1:53B620D01CE0C630:1136130760:::::13x:\n",
92 : :
93 : : NULL
94 : : };
95 : :
96 : : static const gchar *pubring_records[] = {
97 : : "pub:o:2048:1:4842D952AFC000FD:1305189489:::o:::scSCE:\n"
98 : : "fpr:::::::::61A6EA3E0115080227A32EC94842D952AFC000FD:\n"
99 : : "uid:o::::1305189489::D449F1605254754B0BBFA424FC34E50609103BBB::Test Number 1 (unlimited) <test-number-1@example.com>:\n"
100 : : "uid:o::::1305189849::D0A8FA7B15DC4BE3F8F03A49C372F2718C78AFC0::Dr. Strangelove <lovingbomb@example.com>:\n"
101 : : "sub:o:2048:1:4852132BBED15014:1305189489::::::e:\n",
102 : :
103 : : "pub:e:1024:1:268FEE686262C395:1305189628:1305276028::o:::sc:\n"
104 : : "fpr:::::::::A4853C22EA82C8ADC6692751268FEE686262C395:\n"
105 : : "uid:e::::1305189628::2E9D48BD771DA765D2B48A0233D0E8F393F6E839::Test Number 2 (all gone) <test-number-2@example.com>:\n"
106 : : "sub:e:1024:1:C5877FABF4772E4F:1305189628:1305276028:::::e:\n",
107 : :
108 : : "pub:e:1024:17:68B7AB8957548DCD:899816990:1136043547::o:::sca:\n"
109 : : "fpr:::::::::6BD9050FD8FC941B43412DCC68B7AB8957548DCD:\n"
110 : : "uid:e::::1102866526::B712A25DC2ABEF1579696C2925859931078C2C3E::Werner Koch (gnupg sig) <dd9jn@gnu.org>:\n",
111 : :
112 : : "pub:e:1024:17:5DE249965B0358A2:921520361:1247335656::o:::sc:\n"
113 : : "fpr:::::::::A4D94E92B0986AB5EE9DCD755DE249965B0358A2:\n"
114 : : "uid:e::::1113145458::F5B5738FAFB7543A01BAB31A6D767FBC789FF8A8::Werner Koch <wk@gnupg.org>:\n"
115 : : "uid:e::::1113145466::60095F7DAD08129CCE39E15BEB6BBE21937E3AA6::Werner Koch <wk@g10code.com>:\n"
116 : : "uid:e::::921520362::392B892CF897AD0F03EB26343C4C20A48B36513E::Werner Koch:\n"
117 : : "uid:e::::1113145466::3E000C0F7D13A3C57C633C16ABDC97F12EAF16C1::Werner Koch <werner@fsfe.org>:\n"
118 : : "sub:e:1024:17:60784E94010A57ED:1079892559:1199124559:::::s:\n"
119 : : "sub:e:2048:1:7299C628B604F148:1079892777:1136052777:::::e:\n"
120 : : "sub:e:2048:1:35E52D69C3680A6E:1136137762:1199123362:::::e:\n",
121 : :
122 : : "pub:o:1024:17:C7463639B2D7795E:978642983:::o:::scSCE:\n"
123 : : "fpr:::::::::055FC78F112193492C4F37AFC7463639B2D7795E:\n"
124 : : "rvk:o::17::::::3FC732041D23E9EA66DDB5009C9DBC21DF74DC61:80:\n"
125 : : "uid:o::::978642983::44C6F00AAE524A8955CAB76F2BB16126530BB203::Philip R. Zimmermann <prz@mit.edu>:\n"
126 : : "uid:o::::978643127::BD93DF0D0D564E85F73ECBECFFB1B5BA5FF2838D::Philip R. Zimmermann <prz@acm.org>:\n"
127 : : "uat:o::::978751266::E0F87F37495D4ED247BB66A08D7360D8D81F9976::1 3391:\n"
128 : : "uat:o::::1013326898::10A2C49F62C540090ECD679C518AACAA8E960BA5::1 3479:\n"
129 : : "uid:o::::1052692250::09D1F68A1C44AC42E7FCC5615EEDBB0FD581DCDE::Philip R. Zimmermann <prz@philzimmermann.com>:\n"
130 : : "sub:o:3072:16:C4EB1C56A8E92834:978642983::::::e:\n",
131 : :
132 : : "pub:o:4096:1:DB698D7199242560:1012189561:::o:::scSCEA:\n"
133 : : "fpr:::::::::7D92FD313AB6F3734CC59CA1DB698D7199242560:\n"
134 : : "uid:o::::1012189561::0E5FC22DD5518890217F20F1FF832597932B46C1::David M. Shaw <dshaw@jabberwocky.com>:\n"
135 : : "sub:o:2048:16:AE2827D11643B926:1012189956:1327549956:::::e:\n"
136 : : "sub:o:1024:17:E2665C8749E1CBC9:1012190171:1327550171:::::sca:\n",
137 : :
138 : : "pub:o:2048:1:9710B89BCA57AD7C:1102303986:::o:::scSC:\n"
139 : : "fpr:::::::::50BB6FFC9719DFD2BEBB04C69710B89BCA57AD7C:\n"
140 : : "uid:o::::1112650864::A96F758EFD5D67EA9450860C7D15A96DAA1B40E2::PGP Global Directory Verification Key:\n"
141 : : "uat:o::::1112650864::83B0B68B95892BBCE32F04BA0FBAC6CEAD4EDE49::1 3422:\n",
142 : :
143 : : "pub:e:1024:1:53B620D01CE0C630:1136130759:1230738759::o:::sc:\n"
144 : : "fpr:::::::::7B96D396E6471601754BE4DB53B620D01CE0C630:\n"
145 : : "uid:e::::1136130760::142B958D9816ECF810DBB83BD257E5C7DB36C99A::Werner Koch (dist sig) <dd9jn@gnu.org>:\n",
146 : :
147 : : NULL
148 : : };
149 : :
150 : : static const gchar *secring_records[] = {
151 : : "sec::2048:1:4842D952AFC000FD:1305189489::::::::::\n"
152 : : "fpr:::::::::61A6EA3E0115080227A32EC94842D952AFC000FD:\n"
153 : : "uid:::::::D449F1605254754B0BBFA424FC34E50609103BBB::Test Number 1 (unlimited) <test-number-1@example.com>:\n"
154 : : "uid:::::::D0A8FA7B15DC4BE3F8F03A49C372F2718C78AFC0::Dr. Strangelove <lovingbomb@example.com>:\n"
155 : : "ssb::2048:1:4852132BBED15014:1305189489::::::::::\n",
156 : :
157 : : "sec::1024:1:268FEE686262C395:1305189628:1305276028:::::::::\n"
158 : : "fpr:::::::::A4853C22EA82C8ADC6692751268FEE686262C395:\n"
159 : : "uid:::::::2E9D48BD771DA765D2B48A0233D0E8F393F6E839::Test Number 2 (all gone) <test-number-2@example.com>:\n"
160 : : "ssb::1024:1:C5877FABF4772E4F:1305189628::::::::::\n",
161 : :
162 : : NULL
163 : : };
164 : :
165 : : static Fixture fixtures[] = {
166 : : {
167 : : "werner_koch",
168 : : werner_koch_records,
169 : : SRCDIR "/gcr/fixtures/werner-koch.asc",
170 : : "GnuPG v1.4.11 (GNU/Linux)",
171 : : GCR_OPENPGP_PARSE_KEYS
172 : : },
173 : : {
174 : : "werner_koch_with_sigs",
175 : : werner_sig_records,
176 : : SRCDIR "/gcr/fixtures/werner-koch.asc",
177 : : "GnuPG v1.4.11 (GNU/Linux)",
178 : : GCR_OPENPGP_PARSE_KEYS | GCR_OPENPGP_PARSE_SIGNATURES
179 : : },
180 : : {
181 : : "pubring",
182 : : pubring_records,
183 : : SRCDIR "/gcr/fixtures/pubring.gpg",
184 : : NULL,
185 : : GCR_OPENPGP_PARSE_KEYS
186 : : },
187 : : {
188 : : "secring",
189 : : secring_records,
190 : : SRCDIR "/gcr/fixtures/secring.gpg",
191 : : NULL,
192 : : GCR_OPENPGP_PARSE_KEYS
193 : : }
194 : : };
195 : :
196 : : typedef struct {
197 : : const gchar **at;
198 : : const Fixture *fixture;
199 : : } Test;
200 : :
201 : : static void
202 : 4 : setup (Test *test,
203 : : gconstpointer data)
204 : : {
205 : 4 : const Fixture *fixture = data;
206 : 4 : test->fixture = fixture;
207 : 4 : test->at = fixture->records;
208 : 4 : }
209 : :
210 : : static void
211 : 4 : teardown (Test *test,
212 : : gconstpointer data)
213 : : {
214 : :
215 : 4 : }
216 : :
217 : : static void
218 : 16 : compare_fixture_with_records (const gchar *fixture,
219 : : GPtrArray *records)
220 : : {
221 : : gchar *record;
222 : : gchar **lines;
223 : : guint i;
224 : :
225 : 16 : lines = g_strsplit (fixture, "\n", -1);
226 [ + + ]: 106 : for (i = 0; i < records->len; i++) {
227 : 90 : record = _gcr_record_format (records->pdata[i]);
228 [ - + ]: 90 : g_assert_cmpstr (record, ==, lines[i]);
229 : 90 : g_free (record);
230 : : }
231 : :
232 [ - + ]: 16 : if (lines[i] == NULL) {
233 : 0 : g_test_message ("more openpgp records parsed than in fixture");
234 : 0 : g_assert_not_reached ();
235 : : }
236 : :
237 : 16 : g_strfreev (lines);
238 : 16 : }
239 : :
240 : : static void
241 : 16 : on_openpgp_packet (GPtrArray *records,
242 : : GBytes *outer,
243 : : gpointer user_data)
244 : : {
245 : 16 : Test *test = user_data;
246 : : guint seen;
247 : :
248 : : /* Should be parseable again */
249 : 16 : seen = _gcr_openpgp_parse (outer, test->fixture->flags |
250 : : GCR_OPENPGP_PARSE_NO_RECORDS, NULL, NULL);
251 [ - + ]: 16 : g_assert_cmpuint (seen, ==, 1);
252 : :
253 [ - + ]: 16 : if (*(test->at) == NULL) {
254 : 0 : g_test_message ("more openpgp packets parsed than in fixture");
255 : 0 : g_assert_not_reached ();
256 : : }
257 : :
258 : 16 : compare_fixture_with_records (*(test->at), records);
259 : 16 : test->at++;
260 : 16 : }
261 : :
262 : : static void
263 : 2 : on_armor_parsed (GQuark type,
264 : : GBytes *data,
265 : : GBytes *outer,
266 : : GHashTable *headers,
267 : : gpointer user_data)
268 : : {
269 : 2 : Test *test = user_data;
270 : : const gchar *value;
271 : : guint seen;
272 : :
273 [ + - ]: 2 : if (test->fixture->version) {
274 : 2 : value = g_hash_table_lookup (headers, "Version");
275 [ - + ]: 2 : g_assert_cmpstr (value, ==, test->fixture->version);
276 : : }
277 : :
278 : 2 : seen = _gcr_openpgp_parse (data, test->fixture->flags,
279 : : on_openpgp_packet, test);
280 [ - + ]: 2 : g_assert_cmpuint (seen, >, 0);
281 : :
282 [ - + ]: 2 : if (*(test->at) != NULL) {
283 : 0 : g_test_message ("less openpgp packets parsed than in fixture");
284 : 0 : g_assert_not_reached ();
285 : : }
286 : 2 : }
287 : :
288 : : static void
289 : 2 : test_openpgp_armor (Test *test,
290 : : gconstpointer data)
291 : : {
292 : 2 : GError *error = NULL;
293 : : GBytes *bytes;
294 : : gchar *armor;
295 : : gsize length;
296 : : guint parts;
297 : :
298 : 2 : g_file_get_contents (test->fixture->filename, &armor, &length, &error);
299 [ - + ]: 2 : g_assert_no_error (error);
300 : :
301 : 2 : bytes = g_bytes_new_take (armor, length);
302 : 2 : parts = egg_armor_parse (bytes, on_armor_parsed, test);
303 [ - + ]: 2 : g_assert_cmpuint (parts, ==, 1);
304 : :
305 : 2 : g_bytes_unref (bytes);
306 : 2 : }
307 : :
308 : : static void
309 : 2 : test_openpgp_binary (Test *test,
310 : : gconstpointer data)
311 : : {
312 : 2 : GError *error = NULL;
313 : : GBytes *bytes;
314 : : gchar *binary;
315 : : gsize length;
316 : : guint seen;
317 : :
318 : 2 : g_file_get_contents (test->fixture->filename, &binary, &length, &error);
319 [ - + ]: 2 : g_assert_no_error (error);
320 : :
321 : 2 : bytes = g_bytes_new_take (binary, length);
322 : 2 : seen = _gcr_openpgp_parse (bytes, test->fixture->flags, on_openpgp_packet, test);
323 [ - + ]: 2 : g_assert_cmpuint (seen, >, 0);
324 : :
325 [ - + ]: 2 : if (*(test->at) != NULL) {
326 : 0 : g_test_message ("less openpgp packets parsed than in fixture");
327 : 0 : g_assert_not_reached ();
328 : : }
329 : :
330 : 2 : g_bytes_unref (bytes);
331 : 2 : }
332 : :
333 : : int
334 : 1 : main (int argc, char **argv)
335 : : {
336 : : guint i;
337 : : gchar *test_path;
338 : :
339 : 1 : g_test_init (&argc, &argv, NULL);
340 : 1 : g_set_prgname ("test-openpgp");
341 : :
342 [ + + ]: 5 : for (i = 0; i < G_N_ELEMENTS (fixtures); i++) {
343 : 4 : test_path = g_strdup_printf ("/gcr/openpgp/%s", fixtures[i].name);
344 [ + + ]: 4 : if (g_str_has_suffix (fixtures[i].filename, ".asc"))
345 : 2 : g_test_add (test_path, Test, fixtures + i, setup, test_openpgp_armor, teardown);
346 : : else
347 : 2 : g_test_add (test_path, Test, fixtures + i, setup, test_openpgp_binary, teardown);
348 : 4 : g_free (test_path);
349 : : }
350 : :
351 : 1 : return g_test_run ();
352 : : }
|