Branch data Line data Source code
1 : : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : : /* unit-test-pkix-parser.c: Test PKIX parser
3 : :
4 : : Copyright (C) 2007 Stefan Walter
5 : :
6 : : The Gnome Keyring Library is free software; you can redistribute it and/or
7 : : modify it under the terms of the GNU Library General Public License as
8 : : published by the Free Software Foundation; either version 2 of the
9 : : License, or (at your option) any later version.
10 : :
11 : : The Gnome Keyring Library is distributed in the hope that it will be useful,
12 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : Library General Public License for more details.
15 : :
16 : : You should have received a copy of the GNU Library General Public
17 : : License along with the Gnome Library; see the file COPYING.LIB. If not,
18 : : see <http://www.gnu.org/licenses/>.
19 : :
20 : : Author: Stef Walter <stef@memberwebs.com>
21 : : */
22 : :
23 : : #include "config.h"
24 : :
25 : : #include "egg/egg-error.h"
26 : : #include "egg/egg-secure-memory.h"
27 : : #include "egg/egg-testing.h"
28 : :
29 : : #include "gcr/gcr.h"
30 : : #include "gcr/gcr-internal.h"
31 : :
32 : : #include "gck/gck.h"
33 : :
34 : : #include <glib.h>
35 : : #include <gcrypt.h>
36 : :
37 : : #include <errno.h>
38 : : #include <stdlib.h>
39 : : #include <stdio.h>
40 : : #include <string.h>
41 : :
42 : : /*
43 : : * Each test looks like (on one line):
44 : : * void unit_test_xxxxx (CuTest* cu)
45 : : *
46 : : * Each setup looks like (on one line):
47 : : * void unit_setup_xxxxx (void);
48 : : *
49 : : * Each teardown looks like (on one line):
50 : : * void unit_teardown_xxxxx (void);
51 : : *
52 : : * Tests be run in the order specified here.
53 : : */
54 : :
55 : : typedef struct {
56 : : GcrParser *parser;
57 : : const gchar* filedesc;
58 : : } Test;
59 : :
60 : : static void
61 : 186 : ensure_block_can_be_parsed (GcrDataFormat format,
62 : : GBytes *block)
63 : : {
64 : : GcrParser *parser;
65 : : gboolean result;
66 : 186 : GError *error = NULL;
67 : :
68 [ - + ]: 186 : g_assert (block != NULL);
69 : :
70 : 186 : parser = gcr_parser_new ();
71 : 186 : g_object_add_weak_pointer (G_OBJECT (parser), (gpointer *)&parser);
72 : 186 : gcr_parser_format_disable (parser, -1);
73 : 186 : gcr_parser_format_enable (parser, format);
74 : 186 : result = gcr_parser_parse_bytes (parser, block, &error);
75 : :
76 [ - + ]: 186 : if (!result) {
77 : 0 : g_critical ("The data returned from gcr_parser_get_parsed_block() "
78 : : "cannot be parsed: %s", error->message);
79 : 0 : g_assert_not_reached ();
80 : : }
81 : :
82 : 186 : g_object_unref (parser);
83 [ - + ]: 186 : g_assert (parser == NULL);
84 : 186 : }
85 : :
86 : : static void
87 : 186 : parsed_item (GcrParser *par, gpointer user_data)
88 : : {
89 : : GckAttributes *attrs;
90 : : const gchar *description;
91 : : const gchar *label;
92 : 186 : Test *test = user_data;
93 : : GBytes *block;
94 : : GcrDataFormat format;
95 : :
96 [ - + + - : 186 : g_assert (GCR_IS_PARSER (par));
+ - - + ]
97 [ - + ]: 186 : g_assert (par == test->parser);
98 : :
99 : 186 : attrs = gcr_parser_get_parsed_attributes (test->parser);
100 [ - + ]: 186 : g_assert (attrs);
101 : :
102 : 186 : description = gcr_parser_get_parsed_description (test->parser);
103 : 186 : label = gcr_parser_get_parsed_label (test->parser);
104 : 186 : block = gcr_parser_get_parsed_bytes (test->parser);
105 : 186 : format = gcr_parser_get_parsed_format (test->parser);
106 : 186 : ensure_block_can_be_parsed (format, block);
107 : :
108 [ - + ]: 186 : if (g_test_verbose ())
109 : 0 : g_print ("%s: '%s'\n", description, label);
110 : 186 : }
111 : :
112 : : static gboolean
113 : 14 : authenticate (GcrParser *par, gint state, gpointer user_data)
114 : : {
115 : 14 : Test *test = user_data;
116 : :
117 [ - + + - : 14 : g_assert (GCR_IS_PARSER (par));
+ - - + ]
118 [ - + ]: 14 : g_assert (par == test->parser);
119 : :
120 [ + + - ]: 14 : switch (state) {
121 : 13 : case 0:
122 : 13 : gcr_parser_add_password (test->parser, "booo");
123 : 13 : return TRUE;
124 : 1 : case 1:
125 : 1 : gcr_parser_add_password (test->parser, "usr0052");
126 : 1 : return TRUE;
127 : 0 : default:
128 : 0 : g_printerr ("decryption didn't work for: %s", test->filedesc);
129 : 0 : g_assert_not_reached ();
130 : : return FALSE;
131 : : };
132 : : }
133 : :
134 : : static void
135 : 71 : setup (Test *test, gconstpointer unused)
136 : : {
137 : 71 : test->parser = gcr_parser_new ();
138 : 71 : g_signal_connect (test->parser, "parsed", G_CALLBACK (parsed_item), test);
139 : 71 : g_signal_connect (test->parser, "authenticate", G_CALLBACK (authenticate), test);
140 : 71 : }
141 : :
142 : : static void
143 : 71 : teardown (Test *test, gconstpointer unused)
144 : : {
145 : 71 : g_object_unref (test->parser);
146 : 71 : }
147 : :
148 : : static void
149 : 71 : test_parse_one (Test *test,
150 : : gconstpointer user_data)
151 : : {
152 : 71 : const gchar *path = user_data;
153 : : gchar *contents;
154 : 71 : GError *error = NULL;
155 : : gboolean result;
156 : : GBytes *bytes;
157 : : gsize len;
158 : :
159 [ - + ]: 71 : if (!g_file_get_contents (path, &contents, &len, NULL))
160 : 0 : g_assert_not_reached ();
161 : :
162 : 71 : test->filedesc = path;
163 : 71 : bytes = g_bytes_new_take (contents, len);
164 : 71 : result = gcr_parser_parse_bytes (test->parser, bytes, &error);
165 [ - + ]: 71 : g_assert_no_error (error);
166 [ - + ]: 71 : g_assert (result);
167 : :
168 : 71 : g_bytes_unref (bytes);
169 : 71 : }
170 : :
171 : : static void
172 : 2 : on_parsed_compare_bytes (GcrParser *parser,
173 : : gpointer user_data)
174 : : {
175 : 2 : GBytes *original = user_data;
176 : : GBytes *bytes;
177 : : gconstpointer data;
178 : : gsize n_data;
179 : : GcrParsed *parsed;
180 : :
181 : 2 : bytes = gcr_parser_get_parsed_bytes (parser);
182 [ - + ]: 2 : g_assert (bytes != NULL);
183 [ - + ]: 2 : g_assert (g_bytes_equal (original, bytes));
184 : :
185 : 2 : data = gcr_parser_get_parsed_block (parser, &n_data);
186 [ - + ]: 2 : g_assert (data != NULL);
187 [ - + ]: 2 : g_assert_cmpint (n_data, ==, g_bytes_get_size (original));
188 [ - + ]: 2 : g_assert (memcmp (data, g_bytes_get_data (original, NULL), n_data) == 0);
189 : :
190 : 2 : parsed = gcr_parser_get_parsed (parser);
191 [ - + ]: 2 : g_assert (parsed != NULL);
192 : 2 : bytes = gcr_parsed_get_bytes (parsed);
193 [ - + ]: 2 : g_assert (bytes != NULL);
194 [ - + ]: 2 : g_assert (g_bytes_equal (original, bytes));
195 : :
196 : 2 : data = gcr_parsed_get_data (parsed, &n_data);
197 [ - + ]: 2 : g_assert (data != NULL);
198 [ - + ]: 2 : g_assert_cmpint (n_data, ==, g_bytes_get_size (original));
199 [ - + ]: 2 : g_assert (memcmp (data, g_bytes_get_data (original, NULL), n_data) == 0);
200 : 2 : }
201 : :
202 : : static void
203 : 1 : test_parsed_bytes (void)
204 : : {
205 : 1 : GcrParser *parser = gcr_parser_new ();
206 : : gchar *contents;
207 : 1 : GError *error = NULL;
208 : : gboolean result;
209 : : GBytes *bytes;
210 : : gsize len;
211 : :
212 [ - + ]: 1 : if (!g_file_get_contents (SRCDIR "/gcr/fixtures/cacert.org.cer", &contents, &len, NULL))
213 : 0 : g_assert_not_reached ();
214 : :
215 : 1 : bytes = g_bytes_new_take (contents, len);
216 : 1 : g_signal_connect (parser, "parsed", G_CALLBACK (on_parsed_compare_bytes), bytes);
217 : 1 : result = gcr_parser_parse_bytes (parser, bytes, &error);
218 [ - + ]: 1 : g_assert_no_error (error);
219 [ - + ]: 1 : g_assert (result);
220 : :
221 : 1 : g_bytes_unref (bytes);
222 : 1 : g_object_unref (parser);
223 : 1 : }
224 : :
225 : : static void
226 : 1 : test_parse_null (void)
227 : : {
228 : 1 : GcrParser *parser = gcr_parser_new ();
229 : 1 : GError *error = NULL;
230 : : gboolean result;
231 : :
232 : 1 : result = gcr_parser_parse_data (parser, NULL, 0, &error);
233 [ + - + - : 1 : g_assert_error (error, GCR_DATA_ERROR, GCR_ERROR_UNRECOGNIZED);
- + ]
234 [ - + ]: 1 : g_assert (!result);
235 : 1 : g_error_free (error);
236 : :
237 : 1 : g_object_unref (parser);
238 : 1 : }
239 : :
240 : : static void
241 : 1 : test_parse_empty (void)
242 : : {
243 : 1 : GcrParser *parser = gcr_parser_new ();
244 : 1 : GError *error = NULL;
245 : : gboolean result;
246 : :
247 : 1 : result = gcr_parser_parse_data (parser, (const guchar *)"", 0, &error);
248 [ + - + - : 1 : g_assert_error (error, GCR_DATA_ERROR, GCR_ERROR_UNRECOGNIZED);
- + ]
249 [ - + ]: 1 : g_assert (!result);
250 : 1 : g_error_free (error);
251 : :
252 : 1 : g_object_unref (parser);
253 : 1 : }
254 : :
255 : : static void
256 : 1 : test_parse_stream (void)
257 : : {
258 : 1 : GcrParser *parser = gcr_parser_new ();
259 : 1 : GError *error = NULL;
260 : : gboolean result;
261 : : GFile *file;
262 : : GFileInputStream *fis;
263 : : gchar *contents;
264 : : gsize len;
265 : : GBytes *bytes;
266 : :
267 : 1 : file = g_file_new_for_path (SRCDIR "/gcr/fixtures/cacert.org.cer");
268 : 1 : fis = g_file_read (file, NULL, &error);
269 [ - + ]: 1 : g_assert_no_error (error);
270 : :
271 [ - + ]: 1 : if (!g_file_get_contents (SRCDIR "/gcr/fixtures/cacert.org.cer", &contents, &len, NULL))
272 : 0 : g_assert_not_reached ();
273 : 1 : bytes = g_bytes_new_take (contents, len);
274 : 1 : g_signal_connect (parser, "parsed", G_CALLBACK (on_parsed_compare_bytes), bytes);
275 : :
276 : 1 : result = gcr_parser_parse_stream (parser, G_INPUT_STREAM (fis), NULL, &error);
277 [ - + ]: 1 : g_assert_no_error (error);
278 [ - + ]: 1 : g_assert (result);
279 : :
280 : 1 : g_bytes_unref (bytes);
281 : 1 : g_object_unref (fis);
282 : 1 : g_object_unref (file);
283 : 1 : g_object_unref (parser);
284 : 1 : }
285 : :
286 : : static void
287 : 1 : on_parsed_ref (GcrParser *parser,
288 : : gpointer user_data)
289 : : {
290 : 1 : GcrParsed **parsed = user_data;
291 [ - + ]: 1 : g_assert (parsed != NULL);
292 [ - + ]: 1 : g_assert (*parsed == NULL);
293 : 1 : *parsed = gcr_parsed_ref (gcr_parser_get_parsed (parser));
294 : 1 : }
295 : :
296 : : static void
297 : 1 : test_parse_filename (void)
298 : : {
299 : 1 : GcrParser *parser = gcr_parser_new ();
300 : 1 : GcrParsed *parsed = NULL;
301 : 1 : GError *error = NULL;
302 : : gboolean result;
303 : : gchar *contents;
304 : : gsize len;
305 : : GBytes *bytes;
306 : :
307 [ - + ]: 1 : if (!g_file_get_contents (SRCDIR "/gcr/fixtures/cacert.org.cer", &contents, &len, NULL))
308 : 0 : g_assert_not_reached ();
309 : :
310 : 1 : bytes = g_bytes_new_take (contents, len);
311 : 1 : gcr_parser_set_filename (parser, "cacert.org.cer");
312 : 1 : g_signal_connect (parser, "parsed", G_CALLBACK (on_parsed_ref), &parsed);
313 : :
314 : 1 : result = gcr_parser_parse_bytes (parser, bytes, &error);
315 [ - + ]: 1 : g_assert_cmpstr (gcr_parser_get_filename (parser), ==, "cacert.org.cer");
316 [ - + ]: 1 : g_assert_no_error (error);
317 [ - + ]: 1 : g_assert (result);
318 : :
319 : 1 : g_bytes_unref (bytes);
320 : 1 : g_object_unref (parser);
321 : :
322 [ - + ]: 1 : g_assert (parsed != NULL);
323 [ - + ]: 1 : g_assert_cmpstr (gcr_parsed_get_filename (parsed), ==, "cacert.org.cer");
324 : 1 : gcr_parsed_unref (parsed);
325 : 1 : }
326 : :
327 : : int
328 : 1 : main (int argc, char **argv)
329 : : {
330 : : const gchar *filename;
331 : 1 : GError *error = NULL;
332 : : GPtrArray *strings;
333 : : GDir *dir;
334 : : gchar *path;
335 : : gchar *lower;
336 : : gchar *test;
337 : : int ret;
338 : :
339 : 1 : g_test_init (&argc, &argv, NULL);
340 : 1 : g_set_prgname ("test-parser");
341 : :
342 : 1 : strings = g_ptr_array_new_with_free_func (g_free);
343 : 1 : dir = g_dir_open (SRCDIR "/gcr/fixtures", 0, &error);
344 [ - + ]: 1 : g_assert_no_error (error);
345 : :
346 : : for (;;) {
347 : 76 : filename = g_dir_read_name (dir);
348 [ + + ]: 76 : if (!filename)
349 : 1 : break;
350 [ - + ]: 75 : if (filename[0] == '.')
351 : 0 : continue;
352 : :
353 : 75 : path = g_build_filename (SRCDIR "/gcr/fixtures", filename, NULL);
354 : :
355 [ + + ]: 75 : if (g_file_test (path, G_FILE_TEST_IS_DIR)) {
356 : 4 : g_free (path);
357 : 4 : continue;
358 : : }
359 : :
360 : 71 : lower = g_ascii_strdown (filename, -1);
361 : 71 : test = g_strdup_printf ("/gcr/parser/%s",
362 : : g_strcanon (lower, "abcdefghijklmnopqrstuvwxyz0123456789", '_'));
363 : 71 : g_free (lower);
364 : :
365 : 71 : g_test_add (test, Test, path, setup, test_parse_one, teardown);
366 : 71 : g_ptr_array_add (strings, path);
367 : 71 : g_ptr_array_add (strings, test);
368 : : }
369 : :
370 : 1 : g_dir_close (dir);
371 : :
372 : 1 : g_test_add_func ("/gcr/parser/parse_null", test_parse_null);
373 : 1 : g_test_add_func ("/gcr/parser/parse_empty", test_parse_empty);
374 : 1 : g_test_add_func ("/gcr/parser/parse_stream", test_parse_stream);
375 : 1 : g_test_add_func ("/gcr/parser/parsed_bytes", test_parsed_bytes);
376 : 1 : g_test_add_func ("/gcr/parser/filename", test_parse_filename);
377 : :
378 : 1 : ret = g_test_run ();
379 : 1 : g_ptr_array_free (strings, TRUE);
380 : 1 : return ret;
381 : : }
|