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

« back to all changes in this revision

Viewing changes to gck/tests/test-gck-attributes.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
 
 
2
 
#include <glib.h>
3
 
#include <string.h>
4
 
 
5
 
#include "test-suite.h"
6
 
#include "gck-test.h"
7
 
 
8
 
#define ATTR_TYPE 55
9
 
#define ATTR_DATA "TEST DATA"
10
 
#define N_ATTR_DATA ((gsize)9)
11
 
 
12
 
TESTING_TEST(init_memory)
13
 
{
14
 
        GckAttribute attr;
15
 
 
16
 
        g_assert (sizeof (attr) == sizeof (CK_ATTRIBUTE));
17
 
 
18
 
        gck_attribute_init (&attr, ATTR_TYPE, ATTR_DATA, N_ATTR_DATA);
19
 
        g_assert (attr.type == ATTR_TYPE);
20
 
        g_assert (attr.length == N_ATTR_DATA);
21
 
        g_assert (memcmp (attr.value, ATTR_DATA, attr.length) == 0);
22
 
 
23
 
        gck_attribute_clear (&attr);
24
 
}
25
 
 
26
 
TESTING_TEST(value_to_boolean)
27
 
{
28
 
        CK_BBOOL data = CK_TRUE;
29
 
        gboolean result = FALSE;
30
 
 
31
 
        if (!gck_value_to_boolean (&data, sizeof (data), &result))
32
 
                g_assert_not_reached ();
33
 
 
34
 
        g_assert (result == TRUE);
35
 
 
36
 
        if (!gck_value_to_boolean (&data, sizeof (data), NULL))
37
 
                g_assert_not_reached ();
38
 
 
39
 
        /* Should fail */
40
 
        if (gck_value_to_boolean (&data, 0, NULL))
41
 
                g_assert_not_reached ();
42
 
        if (gck_value_to_boolean (&data, 2, NULL))
43
 
                g_assert_not_reached ();
44
 
        if (gck_value_to_boolean (&data, (CK_ULONG)-1, NULL))
45
 
                g_assert_not_reached ();
46
 
}
47
 
 
48
 
TESTING_TEST(value_to_ulong)
49
 
{
50
 
        CK_ULONG data = 34343;
51
 
        gulong result = 0;
52
 
 
53
 
        if (!gck_value_to_ulong (&data, sizeof (data), &result))
54
 
                g_assert_not_reached ();
55
 
 
56
 
        g_assert (result == 34343);
57
 
 
58
 
        if (!gck_value_to_ulong (&data, sizeof (data), NULL))
59
 
                g_assert_not_reached ();
60
 
 
61
 
        /* Should fail */
62
 
        if (gck_value_to_ulong (&data, 0, NULL))
63
 
                g_assert_not_reached ();
64
 
        if (gck_value_to_ulong (&data, 2, NULL))
65
 
                g_assert_not_reached ();
66
 
        if (gck_value_to_ulong (&data, (CK_ULONG)-1, NULL))
67
 
                g_assert_not_reached ();
68
 
}
69
 
 
70
 
TESTING_TEST(init_boolean)
71
 
{
72
 
        GckAttribute attr;
73
 
 
74
 
        gck_attribute_init_boolean (&attr, ATTR_TYPE, TRUE);
75
 
        g_assert (attr.type == ATTR_TYPE);
76
 
        g_assert (attr.length == sizeof (CK_BBOOL));
77
 
        g_assert (*((CK_BBOOL*)attr.value) == CK_TRUE);
78
 
 
79
 
        gck_attribute_clear (&attr);
80
 
}
81
 
 
82
 
TESTING_TEST(init_date)
83
 
{
84
 
        GckAttribute attr;
85
 
        CK_DATE ck_date;
86
 
        GDate *date;
87
 
 
88
 
        date = g_date_new_dmy(05, 06, 1960);
89
 
        memcpy (ck_date.year, "1960", 4);
90
 
        memcpy (ck_date.month, "06", 2);
91
 
        memcpy (ck_date.day, "05", 2);
92
 
        gck_attribute_init_date (&attr, ATTR_TYPE, date);
93
 
        g_date_free (date);
94
 
        g_assert (attr.type == ATTR_TYPE);
95
 
        g_assert (attr.length == sizeof (CK_DATE));
96
 
        g_assert (memcmp (attr.value, &ck_date, attr.length) == 0);
97
 
 
98
 
        gck_attribute_clear (&attr);
99
 
}
100
 
 
101
 
TESTING_TEST(init_ulong)
102
 
{
103
 
        GckAttribute attr;
104
 
 
105
 
        gck_attribute_init_ulong (&attr, ATTR_TYPE, 88);
106
 
        g_assert (attr.type == ATTR_TYPE);
107
 
        g_assert (attr.length == sizeof (CK_ULONG));
108
 
        g_assert (*((CK_ULONG*)attr.value) == 88);
109
 
 
110
 
        gck_attribute_clear (&attr);
111
 
}
112
 
 
113
 
TESTING_TEST(init_string)
114
 
{
115
 
        GckAttribute attr;
116
 
 
117
 
        gck_attribute_init_string (&attr, ATTR_TYPE, "a test string");
118
 
        g_assert (attr.type == ATTR_TYPE);
119
 
        g_assert (attr.length == strlen ("a test string"));
120
 
        g_assert (memcmp (attr.value, "a test string", attr.length) == 0);
121
 
 
122
 
        gck_attribute_clear (&attr);
123
 
}
124
 
 
125
 
TESTING_TEST(init_invalid)
126
 
{
127
 
        GckAttribute attr;
128
 
 
129
 
        gck_attribute_init_invalid (&attr, ATTR_TYPE);
130
 
        g_assert (attr.type == ATTR_TYPE);
131
 
        g_assert (attr.length == (gulong)-1);
132
 
        g_assert (attr.value == NULL);
133
 
 
134
 
        g_assert (gck_attribute_is_invalid (&attr));
135
 
        gck_attribute_clear (&attr);
136
 
}
137
 
 
138
 
TESTING_TEST(init_empty)
139
 
{
140
 
        GckAttribute attr;
141
 
 
142
 
        gck_attribute_init_empty (&attr, ATTR_TYPE);
143
 
        g_assert (attr.type == ATTR_TYPE);
144
 
        g_assert (attr.length == 0);
145
 
        g_assert (attr.value == NULL);
146
 
 
147
 
        gck_attribute_clear (&attr);
148
 
}
149
 
 
150
 
TESTING_TEST(new_memory)
151
 
{
152
 
        GckAttribute *attr;
153
 
 
154
 
        attr = gck_attribute_new (ATTR_TYPE, ATTR_DATA, N_ATTR_DATA);
155
 
        g_assert (attr->type == ATTR_TYPE);
156
 
        g_assert (attr->length == N_ATTR_DATA);
157
 
        g_assert (memcmp (attr->value, ATTR_DATA, attr->length) == 0);
158
 
 
159
 
        gck_attribute_free (attr);
160
 
}
161
 
 
162
 
TESTING_TEST(new_boolean)
163
 
{
164
 
        GckAttribute *attr;
165
 
 
166
 
        attr = gck_attribute_new_boolean (ATTR_TYPE, TRUE);
167
 
        g_assert (attr->type == ATTR_TYPE);
168
 
        g_assert (attr->length == sizeof (CK_BBOOL));
169
 
        g_assert (*((CK_BBOOL*)attr->value) == CK_TRUE);
170
 
 
171
 
        gck_attribute_free (attr);
172
 
}
173
 
 
174
 
TESTING_TEST(new_date)
175
 
{
176
 
        GckAttribute *attr;
177
 
        CK_DATE ck_date;
178
 
        GDate *date;
179
 
 
180
 
        date = g_date_new_dmy(05, 06, 1800);
181
 
        memcpy (ck_date.year, "1800", 4);
182
 
        memcpy (ck_date.month, "06", 2);
183
 
        memcpy (ck_date.day, "05", 2);
184
 
        attr = gck_attribute_new_date (ATTR_TYPE, date);
185
 
        g_date_free (date);
186
 
        g_assert (attr->type == ATTR_TYPE);
187
 
        g_assert (attr->length == sizeof (CK_DATE));
188
 
        g_assert (memcmp (attr->value, &ck_date, attr->length) == 0);
189
 
 
190
 
        gck_attribute_free (attr);
191
 
}
192
 
 
193
 
TESTING_TEST(new_ulong)
194
 
{
195
 
        GckAttribute *attr;
196
 
 
197
 
        attr = gck_attribute_new_ulong (ATTR_TYPE, 88);
198
 
        g_assert (attr->type == ATTR_TYPE);
199
 
        g_assert (attr->length == sizeof (CK_ULONG));
200
 
        g_assert (*((CK_ULONG*)attr->value) == 88);
201
 
 
202
 
        gck_attribute_free (attr);
203
 
}
204
 
 
205
 
TESTING_TEST(new_string)
206
 
{
207
 
        GckAttribute *attr;
208
 
 
209
 
        attr = gck_attribute_new_string (ATTR_TYPE, "a test string");
210
 
        g_assert (attr->type == ATTR_TYPE);
211
 
        g_assert (attr->length == strlen ("a test string"));
212
 
        g_assert (memcmp (attr->value, "a test string", attr->length) == 0);
213
 
 
214
 
        gck_attribute_free (attr);
215
 
}
216
 
 
217
 
TESTING_TEST(new_invalid)
218
 
{
219
 
        GckAttribute *attr;
220
 
 
221
 
        attr = gck_attribute_new_invalid (ATTR_TYPE);
222
 
        g_assert (attr->type == ATTR_TYPE);
223
 
        g_assert (attr->length == (gulong)-1);
224
 
        g_assert (attr->value == NULL);
225
 
 
226
 
        g_assert (gck_attribute_is_invalid (attr));
227
 
 
228
 
        gck_attribute_free (attr);
229
 
}
230
 
 
231
 
TESTING_TEST(new_empty)
232
 
{
233
 
        GckAttribute *attr;
234
 
 
235
 
        attr = gck_attribute_new_empty (ATTR_TYPE);
236
 
        g_assert (attr->type == ATTR_TYPE);
237
 
        g_assert (attr->length == 0);
238
 
        g_assert (attr->value == NULL);
239
 
 
240
 
        gck_attribute_free (attr);
241
 
}
242
 
 
243
 
TESTING_TEST(get_boolean)
244
 
{
245
 
        GckAttribute *attr;
246
 
 
247
 
        attr = gck_attribute_new_boolean (ATTR_TYPE, TRUE);
248
 
        g_assert (gck_attribute_get_boolean (attr) == TRUE);
249
 
        gck_attribute_free (attr);
250
 
}
251
 
 
252
 
TESTING_TEST(get_date)
253
 
{
254
 
        GckAttribute *attr;
255
 
        CK_DATE ck_date;
256
 
        GDate date, date2;
257
 
 
258
 
        g_date_set_dmy(&date, 05, 06, 1800);
259
 
        memcpy (ck_date.year, "1800", 4);
260
 
        memcpy (ck_date.month, "06", 2);
261
 
        memcpy (ck_date.day, "05", 2);
262
 
        attr = gck_attribute_new_date (ATTR_TYPE, &date);
263
 
        gck_attribute_get_date (attr, &date2);
264
 
        g_assert (g_date_compare (&date, &date2) == 0);
265
 
        gck_attribute_free (attr);
266
 
}
267
 
 
268
 
TESTING_TEST(get_ulong)
269
 
{
270
 
        GckAttribute *attr;
271
 
 
272
 
        attr = gck_attribute_new_ulong (ATTR_TYPE, 88);
273
 
        g_assert (gck_attribute_get_ulong (attr) == 88);
274
 
        gck_attribute_free (attr);
275
 
}
276
 
 
277
 
TESTING_TEST(get_string)
278
 
{
279
 
        GckAttribute *attr;
280
 
        gchar *value;
281
 
 
282
 
        attr = gck_attribute_new_string (ATTR_TYPE, "a test string");
283
 
        value = gck_attribute_get_string (attr);
284
 
        g_assert (strcmp ("a test string", value) == 0);
285
 
        g_free (value);
286
 
        gck_attribute_free (attr);
287
 
 
288
 
        /* Should be able to store null strings */
289
 
        attr = gck_attribute_new_string (ATTR_TYPE, NULL);
290
 
        value = gck_attribute_get_string (attr);
291
 
        g_assert (value == NULL);
292
 
        gck_attribute_free (attr);
293
 
}
294
 
 
295
 
TESTING_TEST(dup_attribute)
296
 
{
297
 
        GckAttribute attr, *dup;
298
 
 
299
 
        gck_attribute_init_ulong (&attr, ATTR_TYPE, 88);
300
 
        dup = gck_attribute_dup (&attr);
301
 
        gck_attribute_clear (&attr);
302
 
        g_assert (gck_attribute_get_ulong (dup) == 88);
303
 
        g_assert (dup->type == ATTR_TYPE);
304
 
        gck_attribute_free (dup);
305
 
 
306
 
        /* Should be able to dup null */
307
 
        dup = gck_attribute_dup (NULL);
308
 
        g_assert (dup == NULL);
309
 
}
310
 
 
311
 
TESTING_TEST(copy_attribute)
312
 
{
313
 
        GckAttribute attr, copy;
314
 
 
315
 
        gck_attribute_init_ulong (&attr, ATTR_TYPE, 88);
316
 
        gck_attribute_init_copy (&copy, &attr);
317
 
        gck_attribute_clear (&attr);
318
 
        g_assert (gck_attribute_get_ulong (&copy) == 88);
319
 
        g_assert (copy.type == ATTR_TYPE);
320
 
        gck_attribute_clear (&copy);
321
 
}
322
 
 
323
 
TESTING_TEST(new_attributes)
324
 
{
325
 
        GckAttributes *attrs;
326
 
 
327
 
        attrs = gck_attributes_new ();
328
 
        g_assert (attrs != NULL);
329
 
        g_assert (gck_attributes_count (attrs) == 0);
330
 
 
331
 
        gck_attributes_ref (attrs);
332
 
        gck_attributes_unref (attrs);
333
 
 
334
 
        gck_attributes_unref (attrs);
335
 
 
336
 
        /* Can unref NULL */
337
 
        gck_attributes_unref (NULL);
338
 
}
339
 
 
340
 
static void
341
 
test_attributes_contents (GckAttributes *attrs, gboolean extras)
342
 
{
343
 
        GckAttribute *attr;
344
 
        gchar *value;
345
 
        GDate date, *check;
346
 
        guint count;
347
 
 
348
 
        g_assert (attrs != NULL);
349
 
        count = extras ? 7 : 5;
350
 
        g_assert_cmpuint (gck_attributes_count (attrs), ==, count);
351
 
 
352
 
        attr = gck_attributes_at (attrs, 0);
353
 
        g_assert (attr->type == 0);
354
 
        g_assert (gck_attribute_get_boolean (attr) == TRUE);
355
 
 
356
 
        attr = gck_attributes_at (attrs, 1);
357
 
        g_assert (attr->type == 101);
358
 
        g_assert (gck_attribute_get_ulong (attr) == 888);
359
 
 
360
 
        attr = gck_attributes_at (attrs, 2);
361
 
        g_assert (attr->type == 202);
362
 
        value = gck_attribute_get_string (attr);
363
 
        g_assert (strcmp (value, "string") == 0);
364
 
        g_free (value);
365
 
 
366
 
        attr = gck_attributes_at (attrs, 3);
367
 
        g_assert (attr->type == 303);
368
 
        check = g_date_new_dmy (11, 12, 2008);
369
 
        gck_attribute_get_date (attr, &date);
370
 
        g_assert (g_date_compare (&date, check) == 0);
371
 
        g_date_free (check);
372
 
 
373
 
        attr = gck_attributes_at (attrs, 4);
374
 
        g_assert (attr->type == 404);
375
 
        g_assert (attr->length == N_ATTR_DATA);
376
 
        g_assert (memcmp (attr->value, ATTR_DATA, N_ATTR_DATA) == 0);
377
 
 
378
 
        if (!extras)
379
 
                return;
380
 
 
381
 
        attr = gck_attributes_at (attrs, 5);
382
 
        g_assert (attr->type == 505);
383
 
        g_assert (attr->length == (gulong)-1);
384
 
        g_assert (attr->value == NULL);
385
 
        g_assert (gck_attribute_is_invalid (attr));
386
 
 
387
 
        attr = gck_attributes_at (attrs, 6);
388
 
        g_assert (attr->type == 606);
389
 
        g_assert (attr->length == 0);
390
 
        g_assert (attr->value == NULL);
391
 
}
392
 
 
393
 
TESTING_TEST(new_empty_attributes)
394
 
{
395
 
        GckAttributes *attrs = gck_attributes_new_empty (101UL, 202UL, 303UL, 404UL, GCK_INVALID);
396
 
        GckAttribute *attr;
397
 
        guint i;
398
 
 
399
 
        g_assert_cmpuint (gck_attributes_count (attrs), ==, 4);
400
 
        for (i = 0; i < gck_attributes_count (attrs); ++i) {
401
 
                attr = gck_attributes_at (attrs, i);
402
 
                g_assert (attr->type == ((i + 1) * 100) + i + 1);
403
 
                g_assert (attr->value == NULL);
404
 
                g_assert (attr->length == 0);
405
 
        }
406
 
}
407
 
 
408
 
TESTING_TEST(add_data_attributes)
409
 
{
410
 
        GckAttributes *attrs;
411
 
        GDate *date = g_date_new_dmy (11, 12, 2008);
412
 
        attrs = gck_attributes_new ();
413
 
        gck_attributes_add_boolean (attrs, 0UL, TRUE);
414
 
        gck_attributes_add_ulong (attrs, 101UL, 888);
415
 
        gck_attributes_add_string (attrs, 202UL, "string");
416
 
        gck_attributes_add_date (attrs, 303UL, date);
417
 
        g_date_free (date);
418
 
        gck_attributes_add_data (attrs, 404UL, ATTR_DATA, N_ATTR_DATA);
419
 
        gck_attributes_add_invalid (attrs, 505UL);
420
 
        gck_attributes_add_empty (attrs, 606UL);
421
 
        test_attributes_contents (attrs, TRUE);
422
 
        gck_attributes_unref (attrs);
423
 
}
424
 
 
425
 
TESTING_TEST(add_attributes)
426
 
{
427
 
        GckAttributes *attrs;
428
 
        GckAttribute attr;
429
 
 
430
 
        GDate *date = g_date_new_dmy (11, 12, 2008);
431
 
        attrs = gck_attributes_new ();
432
 
 
433
 
        gck_attribute_init_boolean (&attr, 0UL, TRUE);
434
 
        gck_attributes_add (attrs, &attr);
435
 
        gck_attribute_clear (&attr);
436
 
 
437
 
        gck_attribute_init_ulong (&attr, 101UL, 888);
438
 
        gck_attributes_add (attrs, &attr);
439
 
        gck_attribute_clear (&attr);
440
 
        gck_attribute_init_string (&attr, 202UL, "string");
441
 
        gck_attributes_add (attrs, &attr);
442
 
        gck_attribute_clear (&attr);
443
 
 
444
 
        gck_attribute_init_date (&attr, 303UL, date);
445
 
        gck_attributes_add (attrs, &attr);
446
 
        gck_attribute_clear (&attr);
447
 
        g_date_free (date);
448
 
 
449
 
        gck_attribute_init (&attr, 404UL, ATTR_DATA, N_ATTR_DATA);
450
 
        gck_attributes_add (attrs, &attr);
451
 
        gck_attribute_clear (&attr);
452
 
 
453
 
        gck_attribute_init_invalid (&attr, 505UL);
454
 
        gck_attributes_add (attrs, &attr);
455
 
        gck_attribute_clear (&attr);
456
 
 
457
 
        gck_attribute_init_empty (&attr, 606UL);
458
 
        gck_attributes_add (attrs, &attr);
459
 
        gck_attribute_clear (&attr);
460
 
 
461
 
        test_attributes_contents (attrs, TRUE);
462
 
        gck_attributes_unref (attrs);
463
 
}
464
 
 
465
 
TESTING_TEST(add_all_attributes)
466
 
{
467
 
        GckAttributes *attrs;
468
 
        GckAttributes *copy;
469
 
        GDate *date = g_date_new_dmy (11, 12, 2008);
470
 
        attrs = gck_attributes_new ();
471
 
        gck_attributes_add_boolean (attrs, 0UL, TRUE);
472
 
        gck_attributes_add_ulong (attrs, 101UL, 888);
473
 
        gck_attributes_add_string (attrs, 202UL, "string");
474
 
        gck_attributes_add_date (attrs, 303UL, date);
475
 
        g_date_free (date);
476
 
        gck_attributes_add_data (attrs, 404UL, ATTR_DATA, N_ATTR_DATA);
477
 
        gck_attributes_add_invalid (attrs, 505UL);
478
 
        gck_attributes_add_empty (attrs, 606UL);
479
 
 
480
 
        copy = gck_attributes_new ();
481
 
        gck_attributes_add_all (copy, attrs);
482
 
        test_attributes_contents (copy, TRUE);
483
 
 
484
 
        gck_attributes_unref (attrs);
485
 
        gck_attributes_unref (copy);
486
 
}
487
 
 
488
 
 
489
 
TESTING_TEST(find_attributes)
490
 
{
491
 
        GckAttribute *attr;
492
 
        GDate check, *date = g_date_new_dmy (13, 12, 2008);
493
 
        gboolean bvalue, ret;
494
 
        gulong uvalue;
495
 
        gchar *svalue;
496
 
 
497
 
        GckAttributes *attrs = gck_attributes_new ();
498
 
        gck_attributes_add_boolean (attrs, 0UL, TRUE);
499
 
        gck_attributes_add_ulong (attrs, 101UL, 888UL);
500
 
        gck_attributes_add_string (attrs, 202UL, "string");
501
 
        gck_attributes_add_date (attrs, 303UL, date);
502
 
        gck_attributes_add_data (attrs, 404UL, ATTR_DATA, N_ATTR_DATA);
503
 
 
504
 
        attr = gck_attributes_find (attrs, 404);
505
 
        g_assert (attr != NULL);
506
 
        g_assert (attr->length == N_ATTR_DATA);
507
 
        g_assert (memcmp (attr->value, ATTR_DATA, N_ATTR_DATA) == 0);
508
 
 
509
 
        ret = gck_attributes_find_boolean (attrs, 0UL, &bvalue);
510
 
        g_assert (ret == TRUE);
511
 
        g_assert (bvalue == TRUE);
512
 
 
513
 
        ret = gck_attributes_find_ulong (attrs, 101UL, &uvalue);
514
 
        g_assert (ret == TRUE);
515
 
        g_assert (uvalue == 888);
516
 
 
517
 
        ret = gck_attributes_find_string (attrs, 202UL, &svalue);
518
 
        g_assert (ret == TRUE);
519
 
        g_assert (svalue != NULL);
520
 
        g_assert (strcmp (svalue, "string") == 0);
521
 
        g_free (svalue);
522
 
 
523
 
        ret = gck_attributes_find_date (attrs, 303UL, &check);
524
 
        g_assert (ret == TRUE);
525
 
        g_assert (g_date_compare (date, &check) == 0);
526
 
 
527
 
        gck_attributes_unref (attrs);
528
 
        g_date_free (date);
529
 
}