~ubuntu-branches/ubuntu/trusty/gcr/trusty-proposed

« back to all changes in this revision

Viewing changes to gcr/tests/test-record.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-03 10:18:39 UTC
  • Revision ID: package-import@ubuntu.com-20120503101839-wuvloldm7gmdsnij
Tags: upstream-3.4.1
ImportĀ upstreamĀ versionĀ 3.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
 
2
/*
 
3
   Copyright (C) 2011 Collabora Ltd.
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
19
 
 
20
   Author: Stef Walter <stefw@collabora.co.uk>
 
21
*/
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include "gcr/gcr-record.h"
 
26
 
 
27
#include "egg/egg-testing.h"
 
28
 
 
29
#include <glib.h>
 
30
 
 
31
typedef struct {
 
32
        GcrRecord *record;
 
33
} Test;
 
34
 
 
35
static void
 
36
setup (Test *test, gconstpointer unused)
 
37
{
 
38
        test->record = _gcr_record_parse_colons ("one:tab\\there::YW9ldTM4Mzg=:f\xfc""nf:3533333:-88", -1);
 
39
}
 
40
 
 
41
static void
 
42
teardown (Test *test, gconstpointer unused)
 
43
{
 
44
        _gcr_record_free (test->record);
 
45
}
 
46
 
 
47
static void
 
48
test_count (Test *test, gconstpointer unused)
 
49
{
 
50
        g_assert_cmpuint (_gcr_record_get_count (test->record), ==, 7);
 
51
}
 
52
 
 
53
static void
 
54
test_parse_colons (void)
 
55
{
 
56
        GcrRecord *record;
 
57
 
 
58
        record = _gcr_record_parse_colons ("one:two::four::six", -1);
 
59
        g_assert (record);
 
60
 
 
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);
 
68
 
 
69
        _gcr_record_free (record);
 
70
}
 
71
 
 
72
static void
 
73
test_parse_spaces (void)
 
74
{
 
75
        GcrRecord *record;
 
76
 
 
77
        record = _gcr_record_parse_spaces (" one two  four six   ", -1);
 
78
        g_assert (record);
 
79
 
 
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);
 
86
 
 
87
        _gcr_record_free (record);
 
88
}
 
89
 
 
90
static void
 
91
test_parse_part (void)
 
92
{
 
93
        GcrRecord *record;
 
94
 
 
95
        record = _gcr_record_parse_colons ("one:two::four::six", 8);
 
96
        g_assert (record);
 
97
 
 
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);
 
102
 
 
103
        _gcr_record_free (record);
 
104
}
 
105
 
 
106
static void
 
107
test_parse_too_long (void)
 
108
{
 
109
        GcrRecord *record;
 
110
 
 
111
        /* Too many columns */
 
112
        record = _gcr_record_parse_colons (":::::::::::::::::::::::::::::::::::::::::::::::::::::", -1);
 
113
        g_assert (record == NULL);
 
114
}
 
115
 
 
116
static void
 
117
test_find (void)
 
118
{
 
119
        GcrRecord *uid, *pub, *one, *check;
 
120
        GPtrArray *records;
 
121
 
 
122
        records = g_ptr_array_new_with_free_func (_gcr_record_free);
 
123
 
 
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);
 
130
 
 
131
        check = _gcr_records_find (records, GCR_RECORD_SCHEMA_PUB);
 
132
        g_assert (check == pub);
 
133
 
 
134
        check = _gcr_records_find (records, GCR_RECORD_SCHEMA_UID);
 
135
        g_assert (check == uid);
 
136
 
 
137
        g_ptr_array_unref (records);
 
138
}
 
139
 
 
140
static void
 
141
test_get_string (Test *test, gconstpointer unused)
 
142
{
 
143
        gchar *value = _gcr_record_get_string (test->record, 1);
 
144
        g_assert (value);
 
145
 
 
146
        g_assert_cmpstr (value, ==, "tab\there");
 
147
        g_free (value);
 
148
}
 
149
 
 
150
static void
 
151
test_get_string_null (Test *test, gconstpointer unused)
 
152
{
 
153
        gchar *value = _gcr_record_get_string (test->record, 35);
 
154
        g_assert (value == NULL);
 
155
}
 
156
 
 
157
static void
 
158
test_get_string_latin1 (Test *test, gconstpointer unused)
 
159
{
 
160
        gchar *value = _gcr_record_get_string (test->record, 4);
 
161
        g_assert (value);
 
162
 
 
163
        g_assert_cmpstr (value, ==, "f\xc3\xbc""nf");
 
164
        g_assert (g_utf8_validate (value, -1, NULL));
 
165
        g_free (value);
 
166
}
 
167
 
 
168
static void
 
169
test_get_uint (Test *test, gconstpointer unused)
 
170
{
 
171
        guint value = 0;
 
172
 
 
173
        if (!_gcr_record_get_uint (test->record, 5, &value))
 
174
                g_assert_not_reached ();
 
175
        g_assert_cmpuint (value, ==, 3533333);
 
176
}
 
177
 
 
178
static void
 
179
test_get_uint_range (Test *test, gconstpointer unused)
 
180
{
 
181
        guint value = 0;
 
182
 
 
183
        if (_gcr_record_get_uint (test->record, 6, &value))
 
184
                g_assert_not_reached ();
 
185
}
 
186
 
 
187
static void
 
188
test_get_uint_invalid (Test *test, gconstpointer unused)
 
189
{
 
190
        guint value = 0;
 
191
 
 
192
        if (_gcr_record_get_uint (test->record, 0, &value))
 
193
                g_assert_not_reached ();
 
194
}
 
195
 
 
196
static void
 
197
test_get_base64 (Test *test, gconstpointer unused)
 
198
{
 
199
        gchar *value;
 
200
        gsize n_value;
 
201
 
 
202
        value = _gcr_record_get_base64 (test->record, 3, &n_value);
 
203
        g_assert (value);
 
204
        egg_assert_cmpsize (n_value, ==, 8);
 
205
        g_assert (memcmp (value, "aoeu3838", n_value) == 0);
 
206
 
 
207
        g_free (value);
 
208
}
 
209
 
 
210
 
 
211
static void
 
212
test_free_null (void)
 
213
{
 
214
        _gcr_record_free (NULL);
 
215
}
 
216
 
 
217
static void
 
218
test_get_schema (Test *test, gconstpointer unused)
 
219
{
 
220
        GQuark schema;
 
221
        GQuark check;
 
222
 
 
223
        /* Initialize this quark */
 
224
        check = g_quark_from_static_string ("one");
 
225
 
 
226
        schema = _gcr_record_get_schema (test->record);
 
227
        g_assert (check == schema);
 
228
        g_assert_cmpstr (g_quark_to_string (schema), ==, "one");
 
229
}
 
230
 
 
231
static void
 
232
test_copy (Test *test, gconstpointer unused)
 
233
{
 
234
        GcrRecord *copy;
 
235
        guint count, i;
 
236
 
 
237
        copy = _gcr_record_copy (test->record);
 
238
 
 
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));
 
244
        }
 
245
 
 
246
        g_assert (_gcr_record_get_raw (copy, count) == NULL);
 
247
        _gcr_record_free (copy);
 
248
}
 
249
 
 
250
static void
 
251
test_boxed (Test *test, gconstpointer unused)
 
252
{
 
253
        GcrRecord *copy;
 
254
        guint count, i;
 
255
 
 
256
        copy = g_boxed_copy (GCR_TYPE_RECORD, test->record);
 
257
 
 
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));
 
263
        }
 
264
 
 
265
        g_assert (_gcr_record_get_raw (copy, count) == NULL);
 
266
        g_boxed_free (GCR_TYPE_RECORD, copy);
 
267
}
 
268
 
 
269
int
 
270
main (int argc, char **argv)
 
271
{
 
272
        g_type_init ();
 
273
        g_test_init (&argc, &argv, NULL);
 
274
 
 
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);
 
292
 
 
293
        return g_test_run ();
 
294
}