minimal build
[platform/upstream/gcr.git] / gcr / tests / test-parser.c
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    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.
20
21    Author: Stef Walter <stef@memberwebs.com>
22 */
23
24 #include "config.h"
25
26 #include "egg/egg-error.h"
27 #include "egg/egg-secure-memory.h"
28 #include "egg/egg-testing.h"
29
30 #include "gcr/gcr-base.h"
31 #include "gcr/gcr-internal.h"
32
33 #include "gck/gck.h"
34
35 #include <glib.h>
36 #include <gcrypt.h>
37
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42
43 /*
44  * Each test looks like (on one line):
45  *     void unit_test_xxxxx (CuTest* cu)
46  *
47  * Each setup looks like (on one line):
48  *     void unit_setup_xxxxx (void);
49  *
50  * Each teardown looks like (on one line):
51  *     void unit_teardown_xxxxx (void);
52  *
53  * Tests be run in the order specified here.
54  */
55
56 typedef struct {
57         GcrParser *parser;
58         const gchar* filedesc;
59 } Test;
60
61 static void
62 ensure_block_can_be_parsed (GcrDataFormat format,
63                             gconstpointer block,
64                             gsize n_block)
65 {
66         GcrParser *parser;
67         gboolean result;
68         GError *error = NULL;
69
70         g_assert (block);
71         g_assert (n_block);
72
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);
77
78         if (!result) {
79                 g_critical ("The data returned from gcr_parser_get_parsed_block() "
80                             "cannot be parsed: %s", error->message);
81                 g_assert_not_reached ();
82         }
83
84         g_object_unref (parser);
85         egg_assert_not_object (parser);
86 }
87
88 static void
89 parsed_item (GcrParser *par, gpointer user_data)
90 {
91         GckAttributes *attrs;
92         const gchar *description;
93         const gchar *label;
94         Test *test = user_data;
95         gconstpointer block;
96         gsize n_block;
97         GcrDataFormat format;
98
99         g_assert (GCR_IS_PARSER (par));
100         g_assert (par == test->parser);
101
102         attrs = gcr_parser_get_parsed_attributes (test->parser);
103         g_assert (attrs);
104
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);
110
111         if (g_test_verbose ())
112                 g_print ("%s: '%s'\n", description, label);
113 }
114
115 static gboolean
116 authenticate (GcrParser *par, gint state, gpointer user_data)
117 {
118         Test *test = user_data;
119
120         g_assert (GCR_IS_PARSER (par));
121         g_assert (par == test->parser);
122
123         switch (state) {
124         case 0:
125                 gcr_parser_add_password (test->parser, "booo");
126                 return TRUE;
127         case 1:
128                 gcr_parser_add_password (test->parser, "usr0052");
129                 return TRUE;
130         default:
131                 g_printerr ("decryption didn't work for: %s", test->filedesc);
132                 g_assert_not_reached ();
133                 return FALSE;
134         };
135 }
136
137 static void
138 setup (Test *test, gconstpointer unused)
139 {
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);
143 }
144
145 static void
146 teardown (Test *test, gconstpointer unused)
147 {
148         g_object_unref (test->parser);
149 }
150
151 static void
152 test_parse_one (Test *test,
153                 gconstpointer user_data)
154 {
155         const gchar *path = user_data;
156         gchar *contents;
157         GError *error = NULL;
158         gboolean result;
159         gsize len;
160
161         if (!g_file_get_contents (path, &contents, &len, NULL))
162                 g_assert_not_reached ();
163
164         test->filedesc = path;
165         result = gcr_parser_parse_data (test->parser, (const guchar *)contents, len, &error);
166         g_assert_no_error (error);
167         g_assert (result);
168
169         g_free (contents);
170 }
171
172 static void
173 test_parse_null (void)
174 {
175         GcrParser *parser = gcr_parser_new ();
176         GError *error = NULL;
177         gboolean result;
178
179         result = gcr_parser_parse_data (parser, NULL, 0, &error);
180         g_assert_error (error, GCR_DATA_ERROR, GCR_ERROR_UNRECOGNIZED);
181         g_assert (!result);
182         g_error_free (error);
183
184         g_object_unref (parser);
185 }
186
187 static void
188 test_parse_empty (void)
189 {
190         GcrParser *parser = gcr_parser_new ();
191         GError *error = NULL;
192         gboolean result;
193
194         result = gcr_parser_parse_data (parser, (const guchar *)"", 0, &error);
195         g_assert_error (error, GCR_DATA_ERROR, GCR_ERROR_UNRECOGNIZED);
196         g_assert (!result);
197         g_error_free (error);
198
199         g_object_unref (parser);
200 }
201
202 int
203 main (int argc, char **argv)
204 {
205         const gchar *filename;
206         GError *error = NULL;
207         GPtrArray *strings;
208         GDir *dir;
209         gchar *path;
210         gchar *lower;
211         gchar *test;
212         int ret;
213
214 #if !GLIB_CHECK_VERSION(2,35,0)
215         g_type_init ();
216 #endif
217         g_test_init (&argc, &argv, NULL);
218         g_set_prgname ("test-parser");
219
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);
223
224         for (;;) {
225                 filename = g_dir_read_name (dir);
226                 if (!filename)
227                         break;
228                 if (filename[0] == '.')
229                         continue;
230
231                 path = g_build_filename (SRCDIR "/files", filename, NULL);
232
233                 if (g_file_test (path, G_FILE_TEST_IS_DIR)) {
234                         g_free (path);
235                         continue;
236                 }
237
238                 lower = g_ascii_strdown (filename, -1);
239                 test = g_strdup_printf ("/gcr/parser/%s",
240                                         g_strcanon (lower, "abcdefghijklmnopqrstuvwxyz012345789", '_'));
241                 g_free (lower);
242
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);
246         }
247
248         g_dir_close (dir);
249
250         g_test_add_func ("/gcr/parser/parse_null", test_parse_null);
251         g_test_add_func ("/gcr/parser/parse_empty", test_parse_empty);
252
253         ret = g_test_run ();
254         g_ptr_array_free (strings, TRUE);
255         return ret;
256 }