1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3
Copyright (C) 2011 Collabora Ltd.
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.
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.
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
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
Boston, MA 02111-1307, USA.
20
Author: Stef Walter <stefw@collabora.co.uk>
25
#include "gcr/gcr-record.h"
27
#include "egg/egg-testing.h"
36
setup (Test *test, gconstpointer unused)
38
test->record = _gcr_record_parse_colons ("one:tab\\there::YW9ldTM4Mzg=:f\xfc""nf:3533333:-88", -1);
42
teardown (Test *test, gconstpointer unused)
44
_gcr_record_free (test->record);
48
test_count (Test *test, gconstpointer unused)
50
g_assert_cmpuint (_gcr_record_get_count (test->record), ==, 7);
54
test_parse_colons (void)
58
record = _gcr_record_parse_colons ("one:two::four::six", -1);
61
g_assert_cmpstr (_gcr_record_get_raw (record, 0), ==, "one");
62
g_assert_cmpstr (_gcr_record_get_raw (record, 1), ==, "two");
63
g_assert_cmpstr (_gcr_record_get_raw (record, 2), ==, "");
64
g_assert_cmpstr (_gcr_record_get_raw (record, 3), ==, "four");
65
g_assert_cmpstr (_gcr_record_get_raw (record, 4), ==, "");
66
g_assert_cmpstr (_gcr_record_get_raw (record, 5), ==, "six");
67
g_assert (_gcr_record_get_raw (record, 6) == NULL);
69
_gcr_record_free (record);
73
test_parse_spaces (void)
77
record = _gcr_record_parse_spaces (" one two four six ", -1);
80
g_assert_cmpstr (_gcr_record_get_raw (record, 0), ==, "one");
81
g_assert_cmpstr (_gcr_record_get_raw (record, 1), ==, "two");
82
g_assert_cmpstr (_gcr_record_get_raw (record, 2), ==, "four");
83
g_assert_cmpstr (_gcr_record_get_raw (record, 3), ==, "six");
84
g_assert (_gcr_record_get_raw (record, 4) == NULL);
85
g_assert_cmpuint (_gcr_record_get_count (record), ==, 4);
87
_gcr_record_free (record);
91
test_parse_part (void)
95
record = _gcr_record_parse_colons ("one:two::four::six", 8);
98
g_assert_cmpstr (_gcr_record_get_raw (record, 0), ==, "one");
99
g_assert_cmpstr (_gcr_record_get_raw (record, 1), ==, "two");
100
g_assert_cmpstr (_gcr_record_get_raw (record, 2), ==, "");
101
g_assert (_gcr_record_get_raw (record, 3) == NULL);
103
_gcr_record_free (record);
107
test_parse_too_long (void)
111
/* Too many columns */
112
record = _gcr_record_parse_colons (":::::::::::::::::::::::::::::::::::::::::::::::::::::", -1);
113
g_assert (record == NULL);
119
GcrRecord *uid, *pub, *one, *check;
122
records = g_ptr_array_new_with_free_func (_gcr_record_free);
124
one = _gcr_record_parse_colons ("one:two::four::six", -1);
125
g_ptr_array_add (records, one);
126
pub = _gcr_record_parse_colons ("pub:two", -1);
127
g_ptr_array_add (records, pub);
128
uid = _gcr_record_parse_colons ("uid:two", -1);
129
g_ptr_array_add (records, uid);
131
check = _gcr_records_find (records, GCR_RECORD_SCHEMA_PUB);
132
g_assert (check == pub);
134
check = _gcr_records_find (records, GCR_RECORD_SCHEMA_UID);
135
g_assert (check == uid);
137
g_ptr_array_unref (records);
141
test_get_string (Test *test, gconstpointer unused)
143
gchar *value = _gcr_record_get_string (test->record, 1);
146
g_assert_cmpstr (value, ==, "tab\there");
151
test_get_string_null (Test *test, gconstpointer unused)
153
gchar *value = _gcr_record_get_string (test->record, 35);
154
g_assert (value == NULL);
158
test_get_string_latin1 (Test *test, gconstpointer unused)
160
gchar *value = _gcr_record_get_string (test->record, 4);
163
g_assert_cmpstr (value, ==, "f\xc3\xbc""nf");
164
g_assert (g_utf8_validate (value, -1, NULL));
169
test_get_uint (Test *test, gconstpointer unused)
173
if (!_gcr_record_get_uint (test->record, 5, &value))
174
g_assert_not_reached ();
175
g_assert_cmpuint (value, ==, 3533333);
179
test_get_uint_range (Test *test, gconstpointer unused)
183
if (_gcr_record_get_uint (test->record, 6, &value))
184
g_assert_not_reached ();
188
test_get_uint_invalid (Test *test, gconstpointer unused)
192
if (_gcr_record_get_uint (test->record, 0, &value))
193
g_assert_not_reached ();
197
test_get_base64 (Test *test, gconstpointer unused)
202
value = _gcr_record_get_base64 (test->record, 3, &n_value);
204
egg_assert_cmpsize (n_value, ==, 8);
205
g_assert (memcmp (value, "aoeu3838", n_value) == 0);
212
test_free_null (void)
214
_gcr_record_free (NULL);
218
test_get_schema (Test *test, gconstpointer unused)
223
/* Initialize this quark */
224
check = g_quark_from_static_string ("one");
226
schema = _gcr_record_get_schema (test->record);
227
g_assert (check == schema);
228
g_assert_cmpstr (g_quark_to_string (schema), ==, "one");
232
test_copy (Test *test, gconstpointer unused)
237
copy = _gcr_record_copy (test->record);
239
count = _gcr_record_get_count (test->record);
240
g_assert_cmpuint (_gcr_record_get_count (copy), ==, count);
241
for (i = 0; i < count; i++) {
242
g_assert_cmpstr (_gcr_record_get_raw (copy, i), ==,
243
_gcr_record_get_raw (test->record, i));
246
g_assert (_gcr_record_get_raw (copy, count) == NULL);
247
_gcr_record_free (copy);
251
test_boxed (Test *test, gconstpointer unused)
256
copy = g_boxed_copy (GCR_TYPE_RECORD, test->record);
258
count = _gcr_record_get_count (test->record);
259
g_assert_cmpuint (_gcr_record_get_count (copy), ==, count);
260
for (i = 0; i < count; i++) {
261
g_assert_cmpstr (_gcr_record_get_raw (copy, i), ==,
262
_gcr_record_get_raw (test->record, i));
265
g_assert (_gcr_record_get_raw (copy, count) == NULL);
266
g_boxed_free (GCR_TYPE_RECORD, copy);
270
main (int argc, char **argv)
273
g_test_init (&argc, &argv, NULL);
275
g_test_add_func ("/gcr/record/parse_colons", test_parse_colons);
276
g_test_add_func ("/gcr/record/parse_colons", test_parse_spaces);
277
g_test_add_func ("/gcr/record/parse_part", test_parse_part);
278
g_test_add_func ("/gcr/record/parse_too_long", test_parse_too_long);
279
g_test_add_func ("/gcr/record/free_null", test_free_null);
280
g_test_add_func ("/gcr/record/find", test_find);
281
g_test_add ("/gcr/record/count", Test, NULL, setup, test_count, teardown);
282
g_test_add ("/gcr/record/copy", Test, NULL, setup, test_copy, teardown);
283
g_test_add ("/gcr/record/boxed", Test, NULL, setup, test_boxed, teardown);
284
g_test_add ("/gcr/record/get_string", Test, NULL, setup, test_get_string, teardown);
285
g_test_add ("/gcr/record/get_string_null", Test, NULL, setup, test_get_string_null, teardown);
286
g_test_add ("/gcr/record/get_string_latin1", Test, NULL, setup, test_get_string_latin1, teardown);
287
g_test_add ("/gcr/record/get_uint", Test, NULL, setup, test_get_uint, teardown);
288
g_test_add ("/gcr/record/get_uint_invalid", Test, NULL, setup, test_get_uint_invalid, teardown);
289
g_test_add ("/gcr/record/get_uint_range", Test, NULL, setup, test_get_uint_range, teardown);
290
g_test_add ("/gcr/record/get_base64", Test, NULL, setup, test_get_base64, teardown);
291
g_test_add ("/gcr/record/get_schema", Test, NULL, setup, test_get_schema, teardown);
293
return g_test_run ();