5
#include "test-suite.h"
9
#define ATTR_DATA "TEST DATA"
10
#define N_ATTR_DATA ((gsize)9)
12
TESTING_TEST(init_memory)
16
g_assert (sizeof (attr) == sizeof (CK_ATTRIBUTE));
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);
23
gck_attribute_clear (&attr);
26
TESTING_TEST(value_to_boolean)
28
CK_BBOOL data = CK_TRUE;
29
gboolean result = FALSE;
31
if (!gck_value_to_boolean (&data, sizeof (data), &result))
32
g_assert_not_reached ();
34
g_assert (result == TRUE);
36
if (!gck_value_to_boolean (&data, sizeof (data), NULL))
37
g_assert_not_reached ();
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 ();
48
TESTING_TEST(value_to_ulong)
50
CK_ULONG data = 34343;
53
if (!gck_value_to_ulong (&data, sizeof (data), &result))
54
g_assert_not_reached ();
56
g_assert (result == 34343);
58
if (!gck_value_to_ulong (&data, sizeof (data), NULL))
59
g_assert_not_reached ();
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 ();
70
TESTING_TEST(init_boolean)
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);
79
gck_attribute_clear (&attr);
82
TESTING_TEST(init_date)
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);
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);
98
gck_attribute_clear (&attr);
101
TESTING_TEST(init_ulong)
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);
110
gck_attribute_clear (&attr);
113
TESTING_TEST(init_string)
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);
122
gck_attribute_clear (&attr);
125
TESTING_TEST(init_invalid)
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);
134
g_assert (gck_attribute_is_invalid (&attr));
135
gck_attribute_clear (&attr);
138
TESTING_TEST(init_empty)
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);
147
gck_attribute_clear (&attr);
150
TESTING_TEST(new_memory)
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);
159
gck_attribute_free (attr);
162
TESTING_TEST(new_boolean)
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);
171
gck_attribute_free (attr);
174
TESTING_TEST(new_date)
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);
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);
190
gck_attribute_free (attr);
193
TESTING_TEST(new_ulong)
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);
202
gck_attribute_free (attr);
205
TESTING_TEST(new_string)
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);
214
gck_attribute_free (attr);
217
TESTING_TEST(new_invalid)
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);
226
g_assert (gck_attribute_is_invalid (attr));
228
gck_attribute_free (attr);
231
TESTING_TEST(new_empty)
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);
240
gck_attribute_free (attr);
243
TESTING_TEST(get_boolean)
247
attr = gck_attribute_new_boolean (ATTR_TYPE, TRUE);
248
g_assert (gck_attribute_get_boolean (attr) == TRUE);
249
gck_attribute_free (attr);
252
TESTING_TEST(get_date)
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);
268
TESTING_TEST(get_ulong)
272
attr = gck_attribute_new_ulong (ATTR_TYPE, 88);
273
g_assert (gck_attribute_get_ulong (attr) == 88);
274
gck_attribute_free (attr);
277
TESTING_TEST(get_string)
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);
286
gck_attribute_free (attr);
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);
295
TESTING_TEST(dup_attribute)
297
GckAttribute attr, *dup;
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);
306
/* Should be able to dup null */
307
dup = gck_attribute_dup (NULL);
308
g_assert (dup == NULL);
311
TESTING_TEST(copy_attribute)
313
GckAttribute attr, copy;
315
gck_attribute_init_ulong (&attr, ATTR_TYPE, 88);
316
gck_attribute_init_copy (©, &attr);
317
gck_attribute_clear (&attr);
318
g_assert (gck_attribute_get_ulong (©) == 88);
319
g_assert (copy.type == ATTR_TYPE);
320
gck_attribute_clear (©);
323
TESTING_TEST(new_attributes)
325
GckAttributes *attrs;
327
attrs = gck_attributes_new ();
328
g_assert (attrs != NULL);
329
g_assert (gck_attributes_count (attrs) == 0);
331
gck_attributes_ref (attrs);
332
gck_attributes_unref (attrs);
334
gck_attributes_unref (attrs);
337
gck_attributes_unref (NULL);
341
test_attributes_contents (GckAttributes *attrs, gboolean extras)
348
g_assert (attrs != NULL);
349
count = extras ? 7 : 5;
350
g_assert_cmpuint (gck_attributes_count (attrs), ==, count);
352
attr = gck_attributes_at (attrs, 0);
353
g_assert (attr->type == 0);
354
g_assert (gck_attribute_get_boolean (attr) == TRUE);
356
attr = gck_attributes_at (attrs, 1);
357
g_assert (attr->type == 101);
358
g_assert (gck_attribute_get_ulong (attr) == 888);
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);
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);
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);
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));
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);
393
TESTING_TEST(new_empty_attributes)
395
GckAttributes *attrs = gck_attributes_new_empty (101UL, 202UL, 303UL, 404UL, GCK_INVALID);
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);
408
TESTING_TEST(add_data_attributes)
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);
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);
425
TESTING_TEST(add_attributes)
427
GckAttributes *attrs;
430
GDate *date = g_date_new_dmy (11, 12, 2008);
431
attrs = gck_attributes_new ();
433
gck_attribute_init_boolean (&attr, 0UL, TRUE);
434
gck_attributes_add (attrs, &attr);
435
gck_attribute_clear (&attr);
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);
444
gck_attribute_init_date (&attr, 303UL, date);
445
gck_attributes_add (attrs, &attr);
446
gck_attribute_clear (&attr);
449
gck_attribute_init (&attr, 404UL, ATTR_DATA, N_ATTR_DATA);
450
gck_attributes_add (attrs, &attr);
451
gck_attribute_clear (&attr);
453
gck_attribute_init_invalid (&attr, 505UL);
454
gck_attributes_add (attrs, &attr);
455
gck_attribute_clear (&attr);
457
gck_attribute_init_empty (&attr, 606UL);
458
gck_attributes_add (attrs, &attr);
459
gck_attribute_clear (&attr);
461
test_attributes_contents (attrs, TRUE);
462
gck_attributes_unref (attrs);
465
TESTING_TEST(add_all_attributes)
467
GckAttributes *attrs;
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);
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);
480
copy = gck_attributes_new ();
481
gck_attributes_add_all (copy, attrs);
482
test_attributes_contents (copy, TRUE);
484
gck_attributes_unref (attrs);
485
gck_attributes_unref (copy);
489
TESTING_TEST(find_attributes)
492
GDate check, *date = g_date_new_dmy (13, 12, 2008);
493
gboolean bvalue, ret;
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);
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);
509
ret = gck_attributes_find_boolean (attrs, 0UL, &bvalue);
510
g_assert (ret == TRUE);
511
g_assert (bvalue == TRUE);
513
ret = gck_attributes_find_ulong (attrs, 101UL, &uvalue);
514
g_assert (ret == TRUE);
515
g_assert (uvalue == 888);
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);
523
ret = gck_attributes_find_date (attrs, 303UL, &check);
524
g_assert (ret == TRUE);
525
g_assert (g_date_compare (date, &check) == 0);
527
gck_attributes_unref (attrs);