summaryrefslogtreecommitdiff
path: root/gcr/tests/test-parser.c
blob: 1fa3bc1f5a2ca9d162c1da676a9c68387f8102b0 (plain)
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* unit-test-pkix-parser.c: Test PKIX parser

   Copyright (C) 2007 Stefan Walter

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Stef Walter <stef@memberwebs.com>
*/

#include "config.h"

#include "egg/egg-error.h"
#include "egg/egg-secure-memory.h"

#include "gcr/gcr-base.h"
#include "gcr/gcr-internal.h"

#include "gck/gck.h"

#include <glib.h>
#include <gcrypt.h>

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/*
 * Each test looks like (on one line):
 *     void unit_test_xxxxx (CuTest* cu)
 *
 * Each setup looks like (on one line):
 *     void unit_setup_xxxxx (void);
 *
 * Each teardown looks like (on one line):
 *     void unit_teardown_xxxxx (void);
 *
 * Tests be run in the order specified here.
 */

typedef struct {
	GcrParser *parser;
	const gchar* filedesc;
} Test;

static void
ensure_block_can_be_parsed (GcrDataFormat format,
                            gconstpointer block,
                            gsize n_block)
{
	GcrParser *parser;
	gboolean result;
	GError *error = NULL;

	g_assert (block);
	g_assert (n_block);

	parser = gcr_parser_new ();
	gcr_parser_format_disable (parser, -1);
	gcr_parser_format_enable (parser, format);
	result = gcr_parser_parse_data (parser, block, n_block, &error);

	if (!result) {
		g_critical ("The data returned from gcr_parser_get_parsed_block() "
		            "cannot be parsed: %s", error->message);
		g_assert_not_reached ();
	}

	g_object_unref (parser);
}

static void
parsed_item (GcrParser *par, gpointer user_data)
{
	GckAttributes *attrs;
	const gchar *description;
	const gchar *label;
	Test *test = user_data;
	gconstpointer block;
	gsize n_block;
	GcrDataFormat format;

	g_assert (GCR_IS_PARSER (par));
	g_assert (par == test->parser);

	attrs = gcr_parser_get_parsed_attributes (test->parser);
	g_assert (attrs);

	description = gcr_parser_get_parsed_description (test->parser);
	label = gcr_parser_get_parsed_label (test->parser);
	block = gcr_parser_get_parsed_block (test->parser, &n_block);
	format = gcr_parser_get_parsed_format (test->parser);
	ensure_block_can_be_parsed (format, block, n_block);

	if (g_test_verbose ())
		g_print ("%s: '%s'\n", description, label);
}

static gboolean
authenticate (GcrParser *par, gint state, gpointer user_data)
{
	Test *test = user_data;

	g_assert (GCR_IS_PARSER (par));
	g_assert (par == test->parser);

	switch (state) {
	case 0:
		gcr_parser_add_password (test->parser, "booo");
		return TRUE;
	default:
		g_printerr ("decryption didn't work for: %s", test->filedesc);
		g_assert_not_reached ();
		return FALSE;
	};
}

static void
setup (Test *test, gconstpointer unused)
{
	test->parser = gcr_parser_new ();
	g_signal_connect (test->parser, "parsed", G_CALLBACK (parsed_item), test);
	g_signal_connect (test->parser, "authenticate", G_CALLBACK (authenticate), test);
}

static void
teardown (Test *test, gconstpointer unused)
{
	g_object_unref (test->parser);
}

static void
test_parse_one (Test *test,
                gconstpointer user_data)
{
	const gchar *path = user_data;
	gchar *contents;
	GError *error = NULL;
	gboolean result;
	gsize len;

	if (!g_file_get_contents (path, &contents, &len, NULL))
		g_assert_not_reached ();

	test->filedesc = path;
	result = gcr_parser_parse_data (test->parser, contents, len, &error);
	g_assert_no_error (error);
	g_assert (result);

	g_free (contents);
}

static void
test_parse_null (void)
{
	GcrParser *parser = gcr_parser_new ();
	GError *error = NULL;
	gboolean result;

	result = gcr_parser_parse_data (parser, NULL, 0, &error);
	g_assert_error (error, GCR_DATA_ERROR, GCR_ERROR_UNRECOGNIZED);
	g_assert (!result);
	g_error_free (error);

	g_object_unref (parser);
}

static void
test_parse_empty (void)
{
	GcrParser *parser = gcr_parser_new ();
	GError *error = NULL;
	gboolean result;

	result = gcr_parser_parse_data (parser, "", 0, &error);
	g_assert_error (error, GCR_DATA_ERROR, GCR_ERROR_UNRECOGNIZED);
	g_assert (!result);
	g_error_free (error);

	g_object_unref (parser);
}

int
main (int argc, char **argv)
{
	const gchar *filename;
	GError *error = NULL;
	GPtrArray *strings;
	GDir *dir;
	gchar *path;
	gchar *lower;
	gchar *test;
	int ret;

	g_type_init ();
	g_test_init (&argc, &argv, NULL);
	g_set_prgname ("test-parser");

	strings = g_ptr_array_new_with_free_func (g_free);
	dir = g_dir_open (SRCDIR "/files", 0, &error);
	g_assert_no_error (error);

	for (;;) {
		filename = g_dir_read_name (dir);
		if (!filename)
			break;
		if (filename[0] == '.')
			continue;

		path = g_build_filename (SRCDIR "/files", filename, NULL);

		if (g_file_test (path, G_FILE_TEST_IS_DIR)) {
			g_free (path);
			continue;
		}

		lower = g_ascii_strdown (filename, -1);
		test = g_strdup_printf ("/gcr/parser/%s",
		                        g_strcanon (lower, "abcdefghijklmnopqrstuvwxyz012345789", '_'));
		g_free (lower);

		g_test_add (test, Test, path, setup, test_parse_one, teardown);
		g_ptr_array_add (strings, path);
		g_ptr_array_add (strings, test);
	}

	g_dir_close (dir);

	g_test_add_func ("/gcr/parser/parse_null", test_parse_null);
	g_test_add_func ("/gcr/parser/parse_empty", test_parse_empty);

	ret = g_test_run ();
	g_ptr_array_free (strings, TRUE);
	return ret;
}