1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* unit-test-pkix-parser.c: Test PKIX parser
4 Copyright (C) 2007 Stefan Walter
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.
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.
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 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Author: Stef Walter <stef@memberwebs.com>
26 #include "egg/egg-error.h"
27 #include "egg/egg-secure-memory.h"
28 #include "egg/egg-testing.h"
30 #include "gcr/gcr-base.h"
31 #include "gcr/gcr-internal.h"
44 * Each test looks like (on one line):
45 * void unit_test_xxxxx (CuTest* cu)
47 * Each setup looks like (on one line):
48 * void unit_setup_xxxxx (void);
50 * Each teardown looks like (on one line):
51 * void unit_teardown_xxxxx (void);
53 * Tests be run in the order specified here.
58 const gchar* filedesc;
62 ensure_block_can_be_parsed (GcrDataFormat format,
73 parser = gcr_parser_new ();
74 gcr_parser_format_disable (parser, -1);
75 gcr_parser_format_enable (parser, format);
76 result = gcr_parser_parse_data (parser, block, n_block, &error);
79 g_critical ("The data returned from gcr_parser_get_parsed_block() "
80 "cannot be parsed: %s", error->message);
81 g_assert_not_reached ();
84 g_object_unref (parser);
85 egg_assert_not_object (parser);
89 parsed_item (GcrParser *par, gpointer user_data)
92 const gchar *description;
94 Test *test = user_data;
99 g_assert (GCR_IS_PARSER (par));
100 g_assert (par == test->parser);
102 attrs = gcr_parser_get_parsed_attributes (test->parser);
105 description = gcr_parser_get_parsed_description (test->parser);
106 label = gcr_parser_get_parsed_label (test->parser);
107 block = gcr_parser_get_parsed_block (test->parser, &n_block);
108 format = gcr_parser_get_parsed_format (test->parser);
109 ensure_block_can_be_parsed (format, block, n_block);
111 if (g_test_verbose ())
112 g_print ("%s: '%s'\n", description, label);
116 authenticate (GcrParser *par, gint state, gpointer user_data)
118 Test *test = user_data;
120 g_assert (GCR_IS_PARSER (par));
121 g_assert (par == test->parser);
125 gcr_parser_add_password (test->parser, "booo");
128 gcr_parser_add_password (test->parser, "usr0052");
131 g_printerr ("decryption didn't work for: %s", test->filedesc);
132 g_assert_not_reached ();
138 setup (Test *test, gconstpointer unused)
140 test->parser = gcr_parser_new ();
141 g_signal_connect (test->parser, "parsed", G_CALLBACK (parsed_item), test);
142 g_signal_connect (test->parser, "authenticate", G_CALLBACK (authenticate), test);
146 teardown (Test *test, gconstpointer unused)
148 g_object_unref (test->parser);
152 test_parse_one (Test *test,
153 gconstpointer user_data)
155 const gchar *path = user_data;
157 GError *error = NULL;
161 if (!g_file_get_contents (path, &contents, &len, NULL))
162 g_assert_not_reached ();
164 test->filedesc = path;
165 result = gcr_parser_parse_data (test->parser, (const guchar *)contents, len, &error);
166 g_assert_no_error (error);
173 test_parse_null (void)
175 GcrParser *parser = gcr_parser_new ();
176 GError *error = NULL;
179 result = gcr_parser_parse_data (parser, NULL, 0, &error);
180 g_assert_error (error, GCR_DATA_ERROR, GCR_ERROR_UNRECOGNIZED);
182 g_error_free (error);
184 g_object_unref (parser);
188 test_parse_empty (void)
190 GcrParser *parser = gcr_parser_new ();
191 GError *error = NULL;
194 result = gcr_parser_parse_data (parser, (const guchar *)"", 0, &error);
195 g_assert_error (error, GCR_DATA_ERROR, GCR_ERROR_UNRECOGNIZED);
197 g_error_free (error);
199 g_object_unref (parser);
203 main (int argc, char **argv)
205 const gchar *filename;
206 GError *error = NULL;
214 #if !GLIB_CHECK_VERSION(2,35,0)
217 g_test_init (&argc, &argv, NULL);
218 g_set_prgname ("test-parser");
220 strings = g_ptr_array_new_with_free_func (g_free);
221 dir = g_dir_open (SRCDIR "/files", 0, &error);
222 g_assert_no_error (error);
225 filename = g_dir_read_name (dir);
228 if (filename[0] == '.')
231 path = g_build_filename (SRCDIR "/files", filename, NULL);
233 if (g_file_test (path, G_FILE_TEST_IS_DIR)) {
238 lower = g_ascii_strdown (filename, -1);
239 test = g_strdup_printf ("/gcr/parser/%s",
240 g_strcanon (lower, "abcdefghijklmnopqrstuvwxyz012345789", '_'));
243 g_test_add (test, Test, path, setup, test_parse_one, teardown);
244 g_ptr_array_add (strings, path);
245 g_ptr_array_add (strings, test);
250 g_test_add_func ("/gcr/parser/parse_null", test_parse_null);
251 g_test_add_func ("/gcr/parser/parse_empty", test_parse_empty);
254 g_ptr_array_free (strings, TRUE);