~noskcaj/ubuntu/vivid/gnome-keyring/3.15.90

« back to all changes in this revision

Viewing changes to pkcs11/secret-store/tests/test-secret-fields.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-14 22:13:02 UTC
  • mfrom: (1.3.1)
  • mto: (80.2.8 experimental) (1.1.77)
  • mto: This revision was merged to the branch mainline in revision 148.
  • Revision ID: package-import@ubuntu.com-20120514221302-0l3gjmqpe6xopond
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
/* test-secret-fields.c: Test secret fields
 
3
 
 
4
   Copyright (C) 2009 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 "secret-store/gkm-secret-fields.h"
 
27
 
 
28
#include "pkcs11/pkcs11i.h"
 
29
 
 
30
#include <glib.h>
 
31
 
 
32
#include <stdlib.h>
 
33
#include <stdio.h>
 
34
#include <string.h>
 
35
 
 
36
static void
 
37
test_new (void)
 
38
{
 
39
        GHashTable *fields = gkm_secret_fields_new ();
 
40
        g_hash_table_unref (fields);
 
41
}
 
42
 
 
43
static void
 
44
test_boxed (void)
 
45
{
 
46
        GType boxed = gkm_secret_fields_boxed_type ();
 
47
        GType check = gkm_secret_fields_boxed_type ();
 
48
        g_assert (boxed == check);
 
49
}
 
50
 
 
51
static void
 
52
test_add_get_values (void)
 
53
{
 
54
        GHashTable *fields = gkm_secret_fields_new ();
 
55
        const gchar *value;
 
56
 
 
57
        gkm_secret_fields_add (fields, "one", "value1");
 
58
        gkm_secret_fields_take (fields, g_strdup ("two"), g_strdup ("value2"));
 
59
        gkm_secret_fields_add (fields, "three", NULL);
 
60
 
 
61
        value = gkm_secret_fields_get (fields, "one");
 
62
        g_assert_cmpstr (value, ==, "value1");
 
63
 
 
64
        value = gkm_secret_fields_get (fields, "two");
 
65
        g_assert_cmpstr (value, ==, "value2");
 
66
 
 
67
        value = gkm_secret_fields_get (fields, "three");
 
68
        g_assert_cmpstr (value, ==, "");
 
69
 
 
70
        g_hash_table_unref (fields);
 
71
}
 
72
 
 
73
static void
 
74
test_parse (void)
 
75
{
 
76
        CK_ATTRIBUTE attr = { CKA_G_FIELDS, "one\0value1\0two\0value2\0three\0value3\0", 35 };
 
77
        GHashTable *fields;
 
78
        const gchar *value;
 
79
        CK_RV rv;
 
80
 
 
81
        rv = gkm_secret_fields_parse (&attr, &fields);
 
82
        g_assert (rv == CKR_OK);
 
83
 
 
84
        g_assert_cmpuint (g_hash_table_size (fields), ==, 3);
 
85
        value = g_hash_table_lookup (fields, "one");
 
86
        g_assert_cmpstr (value, ==, "value1");
 
87
        value = g_hash_table_lookup (fields, "two");
 
88
        g_assert_cmpstr (value, ==, "value2");
 
89
        value = g_hash_table_lookup (fields, "three");
 
90
        g_assert_cmpstr (value, ==, "value3");
 
91
 
 
92
        g_hash_table_unref (fields);
 
93
}
 
94
 
 
95
static void
 
96
test_parse_empty (void)
 
97
{
 
98
        CK_ATTRIBUTE attr = { CKA_G_FIELDS, "", 0 };
 
99
        GHashTable *fields;
 
100
        CK_RV rv;
 
101
 
 
102
        rv = gkm_secret_fields_parse (&attr, &fields);
 
103
        g_assert (rv == CKR_OK);
 
104
 
 
105
        g_assert_cmpuint (g_hash_table_size (fields), == , 0);
 
106
 
 
107
        g_hash_table_unref (fields);
 
108
}
 
109
 
 
110
static void
 
111
test_parse_null_invalid (void)
 
112
{
 
113
        CK_ATTRIBUTE attr = { CKA_G_FIELDS, NULL, 5 };
 
114
        GHashTable *fields;
 
115
        CK_RV rv;
 
116
 
 
117
        rv = gkm_secret_fields_parse (&attr, &fields);
 
118
        g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID);
 
119
}
 
120
 
 
121
static void
 
122
test_parse_missing_value (void)
 
123
{
 
124
        CK_ATTRIBUTE attr = { CKA_G_FIELDS, "one", 3 };
 
125
        GHashTable *fields;
 
126
        CK_RV rv;
 
127
 
 
128
        rv = gkm_secret_fields_parse (&attr, &fields);
 
129
        g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID);
 
130
}
 
131
 
 
132
static void
 
133
test_parse_missing_terminator (void)
 
134
{
 
135
        CK_ATTRIBUTE attr = { CKA_G_FIELDS, "one\0value", 9 };
 
136
        GHashTable *fields;
 
137
        CK_RV rv;
 
138
 
 
139
        rv = gkm_secret_fields_parse (&attr, &fields);
 
140
        g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID);
 
141
}
 
142
 
 
143
static void
 
144
test_parse_not_utf8 (void)
 
145
{
 
146
        CK_ATTRIBUTE attr = { CKA_G_FIELDS, "one\0not\234utf8\0", 13 };
 
147
        GHashTable *fields;
 
148
        CK_RV rv;
 
149
 
 
150
        rv = gkm_secret_fields_parse (&attr, &fields);
 
151
        g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID);
 
152
}
 
153
 
 
154
static void
 
155
test_serialize (void)
 
156
{
 
157
        gchar buffer[32];
 
158
        CK_ATTRIBUTE attr = { CKA_G_FIELDS, buffer, 32 };
 
159
        GHashTable *fields;
 
160
        CK_RV rv;
 
161
 
 
162
        fields = gkm_secret_fields_new ();
 
163
        gkm_secret_fields_add (fields, "one", "value1");
 
164
 
 
165
        rv = gkm_secret_fields_serialize (&attr, fields);
 
166
        g_assert (rv == CKR_OK);
 
167
        g_assert (attr.ulValueLen == 11);
 
168
        g_assert (memcmp (buffer, "one\0value1\0", 11) == 0);
 
169
 
 
170
        g_hash_table_unref (fields);
 
171
}
 
172
 
 
173
static void
 
174
test_serialize_length (void)
 
175
{
 
176
        CK_ATTRIBUTE attr = { CKA_G_FIELDS, NULL, 0 };
 
177
        GHashTable *fields;
 
178
        CK_RV rv;
 
179
 
 
180
        fields = gkm_secret_fields_new ();
 
181
        gkm_secret_fields_add (fields, "one", "value1");
 
182
 
 
183
        rv = gkm_secret_fields_serialize (&attr, fields);
 
184
        g_assert (rv == CKR_OK);
 
185
        g_assert (attr.ulValueLen == 11);
 
186
 
 
187
        g_hash_table_unref (fields);
 
188
}
 
189
 
 
190
static void
 
191
test_add_get_compat_uint32 (void)
 
192
{
 
193
        GHashTable *fields;
 
194
        gboolean ret;
 
195
        guint32 value;
 
196
 
 
197
        fields = gkm_secret_fields_new ();
 
198
        gkm_secret_fields_add_compat_uint32 (fields, "field", 992);
 
199
 
 
200
        ret = gkm_secret_fields_get_compat_uint32 (fields, "field", &value);
 
201
        g_assert (ret == TRUE);
 
202
        g_assert_cmpuint (value, ==, 992);
 
203
 
 
204
        g_hash_table_unref (fields);
 
205
}
 
206
 
 
207
static void
 
208
test_get_compat_uint32_fail (void)
 
209
{
 
210
        GHashTable *fields;
 
211
        gboolean ret;
 
212
        guint32 value;
 
213
 
 
214
        fields = gkm_secret_fields_new ();
 
215
        gkm_secret_fields_add (fields, "test", "value");
 
216
 
 
217
        ret = gkm_secret_fields_get_compat_uint32 (fields, "test", &value);
 
218
        g_assert (ret == FALSE);
 
219
 
 
220
        g_hash_table_unref (fields);
 
221
}
 
222
 
 
223
static void
 
224
test_get_compat_hashed_string (void)
 
225
{
 
226
        GHashTable *fields;
 
227
        gboolean ret;
 
228
        gchar *value;
 
229
 
 
230
        fields = gkm_secret_fields_new ();
 
231
        gkm_secret_fields_add (fields, "test", "value");
 
232
 
 
233
        ret = gkm_secret_fields_get_compat_hashed_string (fields, "test", &value);
 
234
        g_assert (ret == TRUE); /* Must be the same as the old gnome-keyring code */
 
235
        g_assert_cmpstr (value, ==, "2063c1608d6e0baf80249c42e2be5804");
 
236
        g_free (value);
 
237
 
 
238
        g_hash_table_unref (fields);
 
239
}
 
240
 
 
241
static void
 
242
test_get_compat_hashed_already (void)
 
243
{
 
244
        GHashTable *fields;
 
245
        gboolean ret;
 
246
        gchar *value;
 
247
 
 
248
        fields = gkm_secret_fields_new ();
 
249
        gkm_secret_fields_add_compat_hashed_string (fields, "test", "e991664529cd0caeb6e9fce8fac3d611");
 
250
 
 
251
        ret = gkm_secret_fields_get_compat_hashed_string (fields, "test", &value);
 
252
        g_assert (ret == TRUE); /* What went in comes out */
 
253
        g_assert_cmpstr (value, ==, "e991664529cd0caeb6e9fce8fac3d611");
 
254
        g_free (value);
 
255
 
 
256
        g_hash_table_unref (fields);
 
257
}
 
258
 
 
259
static void
 
260
test_get_compat_hashed_uint32 (void)
 
261
{
 
262
        GHashTable *fields;
 
263
        gboolean ret;
 
264
        guint32 value;
 
265
        guint32 val = 992;
 
266
 
 
267
        fields = gkm_secret_fields_new ();
 
268
        gkm_secret_fields_add_compat_uint32 (fields, "test", val);
 
269
 
 
270
        ret = gkm_secret_fields_get_compat_hashed_uint32 (fields, "test", &value);
 
271
        g_assert (ret == TRUE); /* Must be the same as the old gnome-keyring code */
 
272
        g_assert_cmpuint (value, ==, 0x18273645 ^ val ^ (val << 16 | val >> 16));
 
273
 
 
274
        g_hash_table_unref (fields);
 
275
}
 
276
 
 
277
static void
 
278
test_get_compat_hashed_uint32_already (void)
 
279
{
 
280
        GHashTable *fields;
 
281
        gboolean ret;
 
282
        guint32 value;
 
283
        guint32 val = 0x1bc735a5;
 
284
 
 
285
        fields = gkm_secret_fields_new ();
 
286
        gkm_secret_fields_add_compat_hashed_uint32 (fields, "test", val);
 
287
 
 
288
        ret = gkm_secret_fields_get_compat_hashed_uint32 (fields, "test", &value);
 
289
        g_assert (ret == TRUE); /* What went in comes out */
 
290
        g_assert_cmpuint (value, ==, val);
 
291
 
 
292
        g_hash_table_unref (fields);
 
293
}
 
294
 
 
295
static void
 
296
test_get_names (void)
 
297
{
 
298
        GHashTable *fields;
 
299
        GList *names, *l;
 
300
 
 
301
        fields = gkm_secret_fields_new ();
 
302
 
 
303
        gkm_secret_fields_add (fields, "one", "11111");
 
304
        gkm_secret_fields_add_compat_uint32 (fields, "two", 2);
 
305
        gkm_secret_fields_add_compat_hashed_string (fields, "test", "2063c1608d6e0baf80249c42e2be5804");
 
306
 
 
307
        names = gkm_secret_fields_get_names (fields);
 
308
        g_assert_cmpuint (g_list_length (names), ==, 3);
 
309
 
 
310
        for (l = names; l; l = g_list_next (l)) {
 
311
                g_assert (l->data);
 
312
                if (!g_str_equal (l->data, "one") &&
 
313
                    !g_str_equal (l->data, "two") &&
 
314
                    !g_str_equal (l->data, "test"))
 
315
                        g_assert_not_reached ();
 
316
        }
 
317
 
 
318
        g_list_free (names);
 
319
        g_hash_table_unref (fields);
 
320
}
 
321
 
 
322
static void
 
323
test_match (void)
 
324
{
 
325
        GHashTable *haystack;
 
326
        GHashTable *needle;
 
327
        gboolean ret;
 
328
 
 
329
        haystack = gkm_secret_fields_new ();
 
330
        gkm_secret_fields_add (haystack, "one", "11111");
 
331
        gkm_secret_fields_add_compat_uint32 (haystack, "two", 2);
 
332
        gkm_secret_fields_add_compat_hashed_string (haystack, "test", "2063c1608d6e0baf80249c42e2be5804"); /* Hashed 'value' */
 
333
        gkm_secret_fields_add_compat_hashed_uint32 (haystack, "number", 0x1bc735a5); /* Hashed 992 */
 
334
        gkm_secret_fields_add (haystack, "extra", "an extra value");
 
335
 
 
336
        needle = gkm_secret_fields_new ();
 
337
        gkm_secret_fields_add (needle, "one", "11111");
 
338
        gkm_secret_fields_add (needle, "two", "2");
 
339
        gkm_secret_fields_add (needle, "test", "value");
 
340
        gkm_secret_fields_add (needle, "number", "992");
 
341
 
 
342
        ret = gkm_secret_fields_match (haystack, needle);
 
343
        g_assert (ret == TRUE);
 
344
 
 
345
        g_hash_table_unref (haystack);
 
346
        g_hash_table_unref (needle);
 
347
}
 
348
 
 
349
static void
 
350
test_match_mismatch_value (void)
 
351
{
 
352
        GHashTable *haystack;
 
353
        GHashTable *needle;
 
354
        gboolean ret;
 
355
 
 
356
        haystack = gkm_secret_fields_new ();
 
357
        gkm_secret_fields_add (haystack, "field", "value");
 
358
 
 
359
        needle = gkm_secret_fields_new ();
 
360
        gkm_secret_fields_add (needle, "field", "another");
 
361
 
 
362
        ret = gkm_secret_fields_match (haystack, needle);
 
363
        g_assert (ret == FALSE);
 
364
 
 
365
        g_hash_table_unref (haystack);
 
366
        g_hash_table_unref (needle);
 
367
}
 
368
 
 
369
static void
 
370
test_match_mismatch_field (void)
 
371
{
 
372
        GHashTable *haystack;
 
373
        GHashTable *needle;
 
374
        gboolean ret;
 
375
 
 
376
        haystack = gkm_secret_fields_new ();
 
377
        gkm_secret_fields_add (haystack, "test", "value");
 
378
 
 
379
        needle = gkm_secret_fields_new ();
 
380
        gkm_secret_fields_add (needle, "field", "value");
 
381
 
 
382
        ret = gkm_secret_fields_match (haystack, needle);
 
383
        g_assert (ret == FALSE);
 
384
 
 
385
        g_hash_table_unref (haystack);
 
386
        g_hash_table_unref (needle);
 
387
}
 
388
 
 
389
static void
 
390
test_match_wrong_hashed (void)
 
391
{
 
392
        GHashTable *haystack;
 
393
        GHashTable *needle;
 
394
        gboolean ret;
 
395
 
 
396
        haystack = gkm_secret_fields_new ();
 
397
        gkm_secret_fields_add_compat_hashed_uint32 (haystack, "number", 0x1bc735a5); /* Hashed 992 */
 
398
 
 
399
        needle = gkm_secret_fields_new ();
 
400
        gkm_secret_fields_add (needle, "number", "1000");
 
401
 
 
402
        ret = gkm_secret_fields_match (haystack, needle);
 
403
        g_assert (ret == FALSE);
 
404
 
 
405
        g_hash_table_unref (haystack);
 
406
        g_hash_table_unref (needle);
 
407
}
 
408
 
 
409
int
 
410
main (int argc, char **argv)
 
411
{
 
412
        g_type_init ();
 
413
        g_test_init (&argc, &argv, NULL);
 
414
 
 
415
        g_test_add_func ("/secret-store/fields/new", test_new);
 
416
        g_test_add_func ("/secret-store/fields/boxed", test_boxed);
 
417
        g_test_add_func ("/secret-store/fields/add_get_values", test_add_get_values);
 
418
        g_test_add_func ("/secret-store/fields/parse", test_parse);
 
419
        g_test_add_func ("/secret-store/fields/parse_empty", test_parse_empty);
 
420
        g_test_add_func ("/secret-store/fields/parse_null_invalid", test_parse_null_invalid);
 
421
        g_test_add_func ("/secret-store/fields/parse_missing_value", test_parse_missing_value);
 
422
        g_test_add_func ("/secret-store/fields/parse_missing_terminator", test_parse_missing_terminator);
 
423
        g_test_add_func ("/secret-store/fields/parse_not_utf8", test_parse_not_utf8);
 
424
        g_test_add_func ("/secret-store/fields/serialize", test_serialize);
 
425
        g_test_add_func ("/secret-store/fields/serialize_length", test_serialize_length);
 
426
        g_test_add_func ("/secret-store/fields/add_get_compat_uint32", test_add_get_compat_uint32);
 
427
        g_test_add_func ("/secret-store/fields/get_compat_uint32_fail", test_get_compat_uint32_fail);
 
428
        g_test_add_func ("/secret-store/fields/get_compat_hashed_string", test_get_compat_hashed_string);
 
429
        g_test_add_func ("/secret-store/fields/get_compat_hashed_already", test_get_compat_hashed_already);
 
430
        g_test_add_func ("/secret-store/fields/get_compat_hashed_uint32", test_get_compat_hashed_uint32);
 
431
        g_test_add_func ("/secret-store/fields/get_compat_hashed_uint32_already", test_get_compat_hashed_uint32_already);
 
432
        g_test_add_func ("/secret-store/fields/get_names", test_get_names);
 
433
        g_test_add_func ("/secret-store/fields/match", test_match);
 
434
        g_test_add_func ("/secret-store/fields/match_mismatch_value", test_match_mismatch_value);
 
435
        g_test_add_func ("/secret-store/fields/match_mismatch_field", test_match_mismatch_field);
 
436
        g_test_add_func ("/secret-store/fields/match_wrong_hashed", test_match_wrong_hashed);
 
437
 
 
438
        return g_test_run ();
 
439
}