1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-gck-attributes.c - the GObject PKCS#11 wrapper library
3
4 Copyright (C) 2011 Collabora Ltd.
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 see <http://www.gnu.org/licenses/>.
19
20 Author: Stef Walter <stefw@collabora.co.uk>
21 */
22
23 #include "config.h"
24
25 #include <glib.h>
26 #include <string.h>
27
28 #include "egg/egg-secure-memory.h"
29
30 #include "gck/gck.h"
31 #include "gck/gck-test.h"
32
33 EGG_SECURE_DECLARE (test_gck_attributes);
34
35 #define ATTR_TYPE 55
36 #define ATTR_DATA (const guchar *)"TEST DATA"
37 #define N_ATTR_DATA ((gsize)9)
38
39 static void
test_init_memory(void)40 test_init_memory (void)
41 {
42 GckAttribute attr;
43
44 g_assert_cmpuint (sizeof (attr), ==, sizeof (CK_ATTRIBUTE));
45
46 gck_attribute_init (&attr, ATTR_TYPE, (const guchar *)ATTR_DATA, N_ATTR_DATA);
47 g_assert_cmpuint (attr.type, ==, ATTR_TYPE);
48 g_assert_cmpmem (attr.value, attr.length, ATTR_DATA, N_ATTR_DATA);
49
50 gck_attribute_clear (&attr);
51 }
52
53 static void
test_init_boolean(void)54 test_init_boolean (void)
55 {
56 GckAttribute attr;
57 CK_BBOOL ck_value = CK_FALSE;
58
59 gck_attribute_init_boolean (&attr, ATTR_TYPE, TRUE);
60 g_assert_cmpuint (attr.type, ==, ATTR_TYPE);
61 g_assert_cmpuint (attr.length, ==, sizeof (CK_BBOOL));
62 memcpy(&ck_value, attr.value, sizeof (CK_BBOOL));
63 g_assert_cmpint (ck_value, ==, CK_TRUE);
64
65 gck_attribute_clear (&attr);
66 }
67
68 static void
test_init_date(void)69 test_init_date (void)
70 {
71 GckAttribute attr;
72 CK_DATE ck_date;
73 GDate *date;
74
75 date = g_date_new_dmy(05, 06, 1960);
76 memcpy (ck_date.year, "1960", 4);
77 memcpy (ck_date.month, "06", 2);
78 memcpy (ck_date.day, "05", 2);
79 gck_attribute_init_date (&attr, ATTR_TYPE, date);
80 g_date_free (date);
81 g_assert_cmpuint (attr.type, ==, ATTR_TYPE);
82 g_assert_cmpmem (attr.value, attr.length, &ck_date, sizeof (CK_DATE));
83
84 gck_attribute_clear (&attr);
85 }
86
87 static void
test_init_ulong(void)88 test_init_ulong (void)
89 {
90 GckAttribute attr;
91 CK_ULONG ck_value = 0;
92
93 gck_attribute_init_ulong (&attr, ATTR_TYPE, 88);
94 g_assert_cmpuint (attr.type, ==, ATTR_TYPE);
95 g_assert_cmpuint (attr.length, ==, sizeof (CK_ULONG));
96 memcpy(&ck_value, attr.value, sizeof (CK_ULONG));
97 g_assert_cmpuint (ck_value, ==, 88);
98
99 gck_attribute_clear (&attr);
100 }
101
102 static void
test_init_string(void)103 test_init_string (void)
104 {
105 GckAttribute attr;
106
107 gck_attribute_init_string (&attr, ATTR_TYPE, "a test string");
108 g_assert_cmpuint (attr.type, ==, ATTR_TYPE);
109 g_assert_cmpmem (attr.value, attr.length, "a test string", strlen ("a test string"));
110
111 gck_attribute_clear (&attr);
112 }
113
114 static void
test_init_invalid(void)115 test_init_invalid (void)
116 {
117 GckAttribute attr;
118
119 gck_attribute_init_invalid (&attr, ATTR_TYPE);
120 g_assert_cmpuint (attr.type, ==, ATTR_TYPE);
121 g_assert_cmpuint (attr.length, ==, (gulong)-1);
122 g_assert_null (attr.value);
123
124 g_assert_true (gck_attribute_is_invalid (&attr));
125 gck_attribute_clear (&attr);
126 }
127
128 static void
test_init_empty(void)129 test_init_empty (void)
130 {
131 GckAttribute attr;
132
133 gck_attribute_init_empty (&attr, ATTR_TYPE);
134 g_assert_cmpuint (attr.type, ==, ATTR_TYPE);
135 g_assert_cmpuint (attr.length, ==, 0);
136 g_assert_null (attr.value);
137
138 gck_attribute_clear (&attr);
139 }
140
141 static void
test_new_memory(void)142 test_new_memory (void)
143 {
144 GckAttribute *attr;
145
146 attr = gck_attribute_new (ATTR_TYPE, ATTR_DATA, N_ATTR_DATA);
147 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
148 g_assert_cmpmem (attr->value, attr->length, ATTR_DATA, N_ATTR_DATA);
149
150 gck_attribute_free (attr);
151 }
152
153 static void
test_new_boolean(void)154 test_new_boolean (void)
155 {
156 GckAttribute *attr;
157 CK_BBOOL ck_value = CK_FALSE;
158
159 attr = gck_attribute_new_boolean (ATTR_TYPE, TRUE);
160 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
161 g_assert_cmpuint (attr->length, ==, sizeof (CK_BBOOL));
162 memcpy(&ck_value, attr->value, sizeof (CK_BBOOL));
163 g_assert_cmpuint (ck_value, ==, CK_TRUE);
164
165 gck_attribute_free (attr);
166 }
167
168 static void
test_new_date(void)169 test_new_date (void)
170 {
171 GckAttribute *attr;
172 CK_DATE ck_date;
173 GDate *date;
174
175 date = g_date_new_dmy(05, 06, 1800);
176 memcpy (ck_date.year, "1800", 4);
177 memcpy (ck_date.month, "06", 2);
178 memcpy (ck_date.day, "05", 2);
179 attr = gck_attribute_new_date (ATTR_TYPE, date);
180 g_date_free (date);
181 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
182 g_assert_cmpmem (attr->value, attr->length, &ck_date, sizeof (CK_DATE));
183
184 gck_attribute_free (attr);
185 }
186
187 static void
test_new_ulong(void)188 test_new_ulong (void)
189 {
190 GckAttribute *attr;
191 CK_ULONG ck_value = 0;
192
193 attr = gck_attribute_new_ulong (ATTR_TYPE, 88);
194 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
195 g_assert_cmpuint (attr->length, ==, sizeof (CK_ULONG));
196 memcpy(&ck_value, attr->value, sizeof (CK_ULONG));
197 g_assert_cmpuint (ck_value, ==, 88);
198
199 gck_attribute_free (attr);
200 }
201
202
203 static void
test_new_string(void)204 test_new_string (void)
205 {
206 GckAttribute *attr;
207
208 attr = gck_attribute_new_string (ATTR_TYPE, "a test string");
209 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
210 g_assert_cmpmem (attr->value, attr->length, "a test string", strlen ("a test string"));
211
212 gck_attribute_free (attr);
213 }
214
215 static void
test_new_invalid(void)216 test_new_invalid (void)
217 {
218 GckAttribute *attr;
219
220 attr = gck_attribute_new_invalid (ATTR_TYPE);
221 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
222 g_assert_cmpuint (attr->length, ==, (gulong)-1);
223 g_assert_null (attr->value);
224
225 g_assert_true (gck_attribute_is_invalid (attr));
226
227 gck_attribute_free (attr);
228 }
229
230 static void
test_new_empty(void)231 test_new_empty (void)
232 {
233 GckAttribute *attr;
234
235 attr = gck_attribute_new_empty (ATTR_TYPE);
236 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
237 g_assert_cmpuint (attr->length, ==, 0);
238 g_assert_null (attr->value);
239
240 gck_attribute_free (attr);
241 }
242
243 static void
test_get_boolean(void)244 test_get_boolean (void)
245 {
246 GckAttribute *attr;
247
248 attr = gck_attribute_new_boolean (ATTR_TYPE, TRUE);
249 g_assert_true (gck_attribute_get_boolean (attr));
250 gck_attribute_free (attr);
251 }
252
253 static void
test_get_date(void)254 test_get_date (void)
255 {
256 GckAttribute *attr;
257 CK_DATE ck_date;
258 GDate date, date2;
259
260 g_date_set_dmy(&date, 05, 06, 1800);
261 memcpy (ck_date.year, "1800", 4);
262 memcpy (ck_date.month, "06", 2);
263 memcpy (ck_date.day, "05", 2);
264 attr = gck_attribute_new_date (ATTR_TYPE, &date);
265 gck_attribute_get_date (attr, &date2);
266 g_assert_true (g_date_compare (&date, &date2) == 0);
267 gck_attribute_free (attr);
268 }
269
270 static void
test_get_ulong(void)271 test_get_ulong (void)
272 {
273 GckAttribute *attr;
274
275 attr = gck_attribute_new_ulong (ATTR_TYPE, 88);
276 g_assert_cmpuint (gck_attribute_get_ulong (attr), ==, 88);
277 gck_attribute_free (attr);
278 }
279
280 static void
test_get_string(void)281 test_get_string (void)
282 {
283 GckAttribute *attr;
284 gchar *value;
285
286 attr = gck_attribute_new_string (ATTR_TYPE, "a test string");
287 value = gck_attribute_get_string (attr);
288 g_assert_cmpstr ("a test string", ==, value);
289 g_free (value);
290 gck_attribute_free (attr);
291
292 /* Should be able to store null strings */
293 attr = gck_attribute_new_string (ATTR_TYPE, NULL);
294 value = gck_attribute_get_string (attr);
295 g_assert_null (value);
296 gck_attribute_free (attr);
297 }
298
299 static void
test_dup_attribute(void)300 test_dup_attribute (void)
301 {
302 GckAttribute attr, *dup;
303
304 gck_attribute_init_ulong (&attr, ATTR_TYPE, 88);
305 dup = gck_attribute_dup (&attr);
306 gck_attribute_clear (&attr);
307 g_assert_cmpuint (gck_attribute_get_ulong (dup), ==, 88);
308 g_assert_cmpuint (dup->type, ==, ATTR_TYPE);
309 gck_attribute_free (dup);
310
311 /* Should be able to dup null */
312 dup = gck_attribute_dup (NULL);
313 g_assert_null (dup);
314 }
315
316 static void
test_copy_attribute(void)317 test_copy_attribute (void)
318 {
319 GckAttribute attr, copy;
320
321 gck_attribute_init_ulong (&attr, ATTR_TYPE, 88);
322 gck_attribute_init_copy (©, &attr);
323 gck_attribute_clear (&attr);
324 g_assert_cmpuint (gck_attribute_get_ulong (©), ==, 88);
325 g_assert_cmpuint (copy.type, ==, ATTR_TYPE);
326 gck_attribute_clear (©);
327 }
328
329 static void
builder_add_fixtures(GckBuilder * builder,guint seed)330 builder_add_fixtures (GckBuilder *builder,
331 guint seed)
332 {
333 GDate *date = g_date_new_dmy (11 + seed, 12, 2008);
334 gck_builder_add_boolean (builder, 0UL, (TRUE + seed) % 2);
335 gck_builder_add_ulong (builder, 101UL, 888 + seed);
336 gck_builder_add_string (builder, 202UL, "string");
337 gck_builder_add_date (builder, 303UL, date);
338 g_date_free (date);
339 gck_builder_add_data (builder, 404UL, (const guchar *)ATTR_DATA, N_ATTR_DATA);
340 gck_builder_add_invalid (builder, 505UL);
341 gck_builder_add_empty (builder, 606UL);
342 }
343
344 static void
test_builder_blank(void)345 test_builder_blank (void)
346 {
347 GckBuilder builder;
348
349 gck_builder_init (&builder);
350 g_assert_null (gck_builder_find (&builder, 88));
351 gck_builder_clear (&builder);
352 }
353
354 static void
test_build_data(void)355 test_build_data (void)
356 {
357 GckBuilder builder = GCK_BUILDER_INIT;
358 GckAttributes *attrs;
359 const GckAttribute *attr;
360
361 gck_builder_add_data (&builder, ATTR_TYPE, (const guchar *)"Hello", 5);
362 attr = gck_builder_find (&builder, ATTR_TYPE);
363 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
364 g_assert_cmpmem (attr->value, attr->length, "Hello", 5);
365
366 gck_builder_set_data (&builder, ATTR_TYPE, (const guchar *)ATTR_DATA, N_ATTR_DATA);
367 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
368 g_assert_cmpmem (attr->value, attr->length, ATTR_DATA, N_ATTR_DATA);
369
370 attrs = gck_builder_end (&builder);
371 attr = gck_attributes_at (attrs, 0);
372 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
373 g_assert_cmpmem (attr->value, attr->length, ATTR_DATA, N_ATTR_DATA);
374
375 gck_attributes_unref (attrs);
376 }
377
378 static void
test_build_data_invalid(void)379 test_build_data_invalid (void)
380 {
381 GckBuilder builder = GCK_BUILDER_INIT;
382 GckAttributes *attrs;
383 const GckAttribute *attr;
384
385 gck_builder_add_data (&builder, ATTR_TYPE, NULL, GCK_INVALID);
386 attrs = gck_builder_end (&builder);
387 attr = gck_attributes_at (attrs, 0);
388
389 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
390 g_assert_true (gck_attribute_is_invalid (attr));
391
392 gck_attributes_unref (attrs);
393 }
394
395 static void
test_build_data_secure(void)396 test_build_data_secure (void)
397 {
398 GckBuilder builder = GCK_BUILDER_INIT;
399 GckAttributes *attrs;
400 const GckAttribute *attr;
401 guchar *memory;
402
403 memory = egg_secure_strdup ("password");
404 gck_builder_add_data (&builder, ATTR_TYPE, memory, 8);
405 attrs = gck_builder_end (&builder);
406 attr = gck_attributes_at (attrs, 0);
407
408 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
409 g_assert_cmpmem (attr->value, attr->length, "password", 8);
410 g_assert_true (egg_secure_check (attr->value));
411
412 egg_secure_free (memory);
413 gck_attributes_unref (attrs);
414 }
415
416 static void
test_build_take(void)417 test_build_take (void)
418 {
419 GckBuilder builder = GCK_BUILDER_INIT;
420 GckAttributes *attrs;
421 const GckAttribute *attr;
422 guchar *memory;
423
424 memory = g_memdup (ATTR_DATA, N_ATTR_DATA);
425 gck_builder_take_data (&builder, ATTR_TYPE, memory, N_ATTR_DATA);
426 attrs = gck_builder_end (&builder);
427
428 attr = gck_attributes_at (attrs, 0);
429
430 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
431 g_assert_cmpmem (attr->value, attr->length, ATTR_DATA, N_ATTR_DATA);
432
433 gck_attributes_unref (attrs);
434 }
435
436 static void
test_build_take_invalid(void)437 test_build_take_invalid (void)
438 {
439 GckBuilder builder = GCK_BUILDER_INIT;
440 GckAttributes *attrs;
441 const GckAttribute *attr;
442 gpointer memory;
443
444 /* This memory should be freed */
445 memory = g_strdup ("BLAH");
446 gck_builder_take_data (&builder, ATTR_TYPE, memory, GCK_INVALID);
447
448 /* This memory should be freed */
449 memory = egg_secure_strdup ("BLAH");
450 gck_builder_take_data (&builder, ATTR_TYPE, memory, GCK_INVALID);
451
452 attrs = gck_builder_end (&builder);
453 attr = gck_attributes_at (attrs, 0);
454
455 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
456 g_assert_true (gck_attribute_is_invalid (attr));
457
458 gck_attributes_unref (attrs);
459 }
460
461 static void
test_build_take_secure(void)462 test_build_take_secure (void)
463 {
464 GckBuilder builder = GCK_BUILDER_INIT;
465 GckAttributes *attrs;
466 const GckAttribute *attr;
467 guchar *memory;
468
469 memory = egg_secure_strdup ("password");
470 gck_builder_take_data (&builder, ATTR_TYPE, memory, 8);
471 attrs = gck_builder_end (&builder);
472 attr = gck_attributes_at (attrs, 0);
473
474 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
475 g_assert_cmpmem (attr->value, attr->length, "password", 8);
476 g_assert_true (egg_secure_check (attr->value));
477
478 gck_attributes_unref (attrs);
479 }
480
481
482 static void
test_value_to_boolean(void)483 test_value_to_boolean (void)
484 {
485 CK_BBOOL data = CK_TRUE;
486 gboolean result = FALSE;
487
488 if (!gck_value_to_boolean (&data, sizeof (data), &result))
489 g_assert_not_reached ();
490
491 g_assert_true (result);
492
493 if (!gck_value_to_boolean (&data, sizeof (data), NULL))
494 g_assert_not_reached ();
495
496 /* Should fail */
497 if (gck_value_to_boolean (&data, 0, NULL))
498 g_assert_not_reached ();
499 if (gck_value_to_boolean (&data, 2, NULL))
500 g_assert_not_reached ();
501 if (gck_value_to_boolean (&data, (CK_ULONG)-1, NULL))
502 g_assert_not_reached ();
503 }
504
505 static void
test_value_to_ulong(void)506 test_value_to_ulong (void)
507 {
508 CK_ULONG data = 34343;
509 gulong result = 0;
510
511 if (!gck_value_to_ulong ((const guchar *)&data, sizeof (data), &result))
512 g_assert_not_reached ();
513
514 g_assert_cmpuint (result, ==, 34343);
515
516 if (!gck_value_to_ulong ((const guchar *)&data, sizeof (data), NULL))
517 g_assert_not_reached ();
518
519 /* Should fail */
520 if (gck_value_to_ulong ((const guchar *)&data, 0, NULL))
521 g_assert_not_reached ();
522 if (gck_value_to_ulong ((const guchar *)&data, 2, NULL))
523 g_assert_not_reached ();
524 if (gck_value_to_ulong ((const guchar *)&data, (CK_ULONG)-1, NULL))
525 g_assert_not_reached ();
526 }
527
528 static void
test_build_boolean(void)529 test_build_boolean (void)
530 {
531 GckBuilder builder = GCK_BUILDER_INIT;
532 GckAttributes *attrs;
533 const GckAttribute *attr;
534 gboolean value;
535 CK_BBOOL ck_value = CK_FALSE;
536
537 g_assert_false (gck_builder_find_boolean (&builder, 5, &value));
538
539 gck_builder_add_boolean (&builder, ATTR_TYPE, FALSE);
540
541 gck_builder_set_invalid (&builder, 5);
542 g_assert_false (gck_builder_find_boolean (&builder, 5, &value));
543 gck_builder_set_boolean (&builder, 5, TRUE);
544
545 attr = gck_builder_find (&builder, ATTR_TYPE);
546 g_assert_nonnull (attr);
547 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
548 g_assert_cmpuint (attr->length, ==, sizeof (CK_BBOOL));
549 memcpy(&ck_value, attr->value, sizeof (CK_BBOOL));
550 g_assert_cmpuint (ck_value, ==, CK_FALSE);
551 if (!gck_builder_find_boolean (&builder, ATTR_TYPE, &value))
552 g_assert_not_reached ();
553 g_assert_false (value);
554
555 gck_builder_set_boolean (&builder, ATTR_TYPE, TRUE);
556 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
557 g_assert_cmpuint (attr->length, ==, sizeof (CK_BBOOL));
558 memcpy(&ck_value, attr->value, sizeof (CK_BBOOL));
559 g_assert_cmpuint (ck_value, ==, CK_TRUE);
560 if (!gck_builder_find_boolean (&builder, ATTR_TYPE, &value))
561 g_assert_not_reached ();
562 g_assert_true (value);
563
564 if (!gck_builder_find_boolean (&builder, 5, &value))
565 g_assert_not_reached ();
566 g_assert_true (value);
567
568 attrs = gck_builder_end (&builder);
569 attr = gck_attributes_at (attrs, 0);
570 g_assert_nonnull (attr);
571
572 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
573 g_assert_cmpuint (attr->length, ==, sizeof (CK_BBOOL));
574 memcpy(&ck_value, attr->value, sizeof (CK_BBOOL));
575 g_assert_cmpuint (ck_value, ==, CK_TRUE);
576
577 if (!gck_attributes_find_boolean (attrs, ATTR_TYPE, &value))
578 g_assert_not_reached ();
579 g_assert_true (value);
580
581 g_assert_true (gck_attribute_get_boolean (attr));
582
583 g_assert_cmpuint (gck_attributes_count (attrs), ==, 2);
584 gck_attributes_unref (attrs);
585 }
586
587 static void
test_build_date(void)588 test_build_date (void)
589 {
590 GckBuilder builder = GCK_BUILDER_INIT;
591 GckAttributes *attrs;
592 const GckAttribute *attr;
593 CK_DATE ck_date;
594 GDate *date, date2;
595
596 g_assert_false (gck_builder_find_date (&builder, 5, &date2));
597
598 date = g_date_new_dmy(8, 8, 1960);
599 memcpy (ck_date.year, "1960", 4);
600 memcpy (ck_date.month, "08", 2);
601 memcpy (ck_date.day, "08", 2);
602
603 gck_builder_add_date (&builder, ATTR_TYPE, date);
604
605 gck_builder_set_invalid (&builder, 5);
606 g_assert_false (gck_builder_find_date (&builder, 5, &date2));
607 attr = gck_builder_find (&builder, 5);
608 gck_attribute_get_date (attr, &date2);
609 g_assert_cmpint (date2.day, ==, 0);
610 g_assert_cmpint (date2.month, ==, 0);
611 g_assert_cmpint (date2.year, ==, 0);
612
613 gck_builder_set_date (&builder, 5, date);
614
615 attr = gck_builder_find (&builder, ATTR_TYPE);
616 g_assert_nonnull (attr);
617 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
618 g_assert_cmpmem (attr->value, attr->length, &ck_date, sizeof (CK_DATE));
619 if (!gck_builder_find_date (&builder, ATTR_TYPE, &date2))
620 g_assert_not_reached ();
621 g_assert_true (g_date_compare (date, &date2) == 0);
622
623 if (!gck_builder_find_date (&builder, 5, &date2))
624 g_assert_not_reached ();
625 g_assert_true (g_date_compare (date, &date2) == 0);
626
627 g_date_free (date);
628
629 date = g_date_new_dmy(05, 06, 1960);
630 memcpy (ck_date.year, "1960", 4);
631 memcpy (ck_date.month, "06", 2);
632 memcpy (ck_date.day, "05", 2);
633 gck_builder_set_date (&builder, ATTR_TYPE, date);
634 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
635 g_assert_cmpmem (attr->value, attr->length, &ck_date, sizeof (CK_DATE));
636 if (!gck_builder_find_date (&builder, ATTR_TYPE, &date2))
637 g_assert_not_reached ();
638 g_assert_true (g_date_compare (date, &date2) == 0);
639
640 attrs = gck_builder_end (&builder);
641 attr = gck_attributes_at (attrs, 0);
642 g_assert_nonnull (attr);
643 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
644 g_assert_cmpmem (attr->value, attr->length, &ck_date, sizeof (CK_DATE));
645
646 gck_attribute_get_date (attr, &date2);
647 g_assert_true (g_date_compare (date, &date2) == 0);
648
649 g_date_free (date);
650
651 g_assert_cmpuint (gck_attributes_count (attrs), ==, 2);
652 gck_attributes_unref (attrs);
653 }
654
655 static void
test_build_ulong(void)656 test_build_ulong (void)
657 {
658 GckBuilder builder = GCK_BUILDER_INIT;
659 GckAttributes *attrs;
660 const GckAttribute *attr;
661 gulong value;
662 CK_ULONG ck_value = 0;
663
664 g_assert_false (gck_builder_find_ulong (&builder, 5, &value));
665
666 gck_builder_add_ulong (&builder, ATTR_TYPE, 99);
667
668 gck_builder_set_invalid (&builder, 5);
669 g_assert_false (gck_builder_find_ulong (&builder, 5, &value));
670 gck_builder_set_ulong (&builder, 5, 292);
671
672 attr = gck_builder_find (&builder, ATTR_TYPE);
673 g_assert_nonnull (attr);
674 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
675 g_assert_cmpuint (attr->length, ==, sizeof (CK_ULONG));
676 memcpy(&ck_value, attr->value, sizeof (CK_ULONG));
677 g_assert_cmpuint (ck_value, ==, 99);
678 if (!gck_builder_find_ulong (&builder, ATTR_TYPE, &value))
679 g_assert_not_reached ();
680 g_assert_cmpuint (value, ==, 99);
681
682 gck_builder_set_ulong (&builder, ATTR_TYPE, 88);
683 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
684 g_assert_cmpuint (attr->length, ==, sizeof (CK_ULONG));
685 memcpy(&ck_value, attr->value, sizeof (CK_ULONG));
686 g_assert_cmpuint (ck_value, ==, 88);
687 if (!gck_builder_find_ulong (&builder, ATTR_TYPE, &value))
688 g_assert_not_reached ();
689 g_assert_cmpuint (value, ==, 88);
690
691 if (!gck_builder_find_ulong (&builder, 5, &value))
692 g_assert_not_reached ();
693 g_assert_cmpuint (value, ==, 292);
694
695 attrs = gck_builder_end (&builder);
696 attr = gck_attributes_at (attrs, 0);
697 g_assert_nonnull (attr);
698 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
699 g_assert_cmpuint (attr->length, ==, sizeof (CK_ULONG));
700 memcpy(&ck_value, attr->value, sizeof (CK_ULONG));
701 g_assert_cmpuint (ck_value, ==, 88);
702
703 if (!gck_attributes_find_ulong (attrs, ATTR_TYPE, &value))
704 g_assert_not_reached ();
705 g_assert_cmpuint (value, ==, 88);
706 g_assert_cmpuint (gck_attribute_get_ulong (attr), ==, 88);
707
708 g_assert_cmpuint (gck_attributes_count (attrs), ==, 2);
709 gck_attributes_unref (attrs);
710 }
711
712 static void
test_build_string(void)713 test_build_string (void)
714 {
715 GckBuilder builder = GCK_BUILDER_INIT;
716 GckAttributes *attrs;
717 const GckAttribute *attr;
718 gchar *value;
719
720 g_assert_false (gck_builder_find_string (&builder, 5, &value));
721
722 gck_builder_add_string (&builder, ATTR_TYPE, "My my");
723
724 gck_builder_set_invalid (&builder, 5);
725 g_assert_false (gck_builder_find_string (&builder, 5, &value));
726 gck_builder_set_string (&builder, 5, "Hello");
727
728 attr = gck_builder_find (&builder, ATTR_TYPE);
729 g_assert_nonnull (attr);
730 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
731 g_assert_cmpmem (attr->value, attr->length, "My my", strlen ("My my"));
732
733 if (!gck_builder_find_string (&builder, 5, &value))
734 g_assert_not_reached ();
735 g_assert_cmpstr (value, ==, "Hello");
736 g_free (value);
737
738 gck_builder_set_string (&builder, ATTR_TYPE, "a test string");
739 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
740 g_assert_cmpmem (attr->value, attr->length,
741 "a test string", strlen ("a test string"));
742
743 attrs = gck_builder_end (&builder);
744 attr = gck_attributes_at (attrs, 0);
745 g_assert_nonnull (attr);
746
747 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
748 g_assert_cmpmem (attr->value, attr->length,
749 "a test string", strlen ("a test string"));
750
751 if (!gck_attributes_find_string (attrs, ATTR_TYPE, &value))
752 g_assert_not_reached ();
753 g_assert_cmpstr ("a test string", ==, value);
754 g_free (value);
755
756 value = gck_attribute_get_string (attr);
757 g_assert_cmpstr ("a test string", ==, value);
758 g_free (value);
759
760 g_assert_cmpuint (gck_attributes_count (attrs), ==, 2);
761 gck_attributes_unref (attrs);
762 }
763
764 static void
test_build_string_null(void)765 test_build_string_null (void)
766 {
767 GckBuilder builder = GCK_BUILDER_INIT;
768 GckAttributes *attrs;
769 const GckAttribute *attr;
770 gchar *value;
771
772 gck_builder_add_string (&builder, ATTR_TYPE, NULL);
773
774 g_assert_false (gck_builder_find_string (&builder, ATTR_TYPE, &value));
775
776 attrs = gck_builder_end (&builder);
777 attr = gck_attributes_at (attrs, 0);
778 g_assert_null (attr->value);
779 g_assert_cmpuint (attr->length, ==, 0);
780
781 value = gck_attribute_get_string (attr);
782 g_assert_null (value);
783
784 gck_attributes_unref (attrs);
785 }
786
787 static void
test_build_invalid(void)788 test_build_invalid (void)
789 {
790 GckBuilder builder = GCK_BUILDER_INIT;
791 GckAttributes *attrs;
792 const GckAttribute *attr;
793
794 gck_builder_add_invalid (&builder, ATTR_TYPE);
795 gck_builder_set_invalid (&builder, ATTR_TYPE);
796 gck_builder_set_invalid (&builder, 5);
797
798 attrs = gck_builder_end (&builder);
799 attr = gck_attributes_at (attrs, 0);
800 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
801 g_assert_cmpuint (attr->length, ==, (gulong)-1);
802 g_assert_null (attr->value);
803
804 g_assert_true (gck_attribute_is_invalid (attr));
805
806 g_assert_cmpuint (gck_attributes_count (attrs), ==, 2);
807 gck_attributes_unref (attrs);
808 }
809
810 static void
test_build_empty(void)811 test_build_empty (void)
812 {
813 GckBuilder builder = GCK_BUILDER_INIT;
814 GckAttributes *attrs;
815 const GckAttribute *attr;
816
817 gck_builder_add_empty (&builder, ATTR_TYPE);
818 gck_builder_set_empty (&builder, ATTR_TYPE);
819 gck_builder_set_empty (&builder, 5);
820
821 attr = gck_builder_find (&builder, 5);
822 g_assert_cmpuint (attr->type, ==, 5);
823 g_assert_cmpuint (attr->length, ==, 0);
824 g_assert_null (attr->value);
825
826 attrs = gck_builder_end (&builder);
827 attr = gck_attributes_at (attrs, 0);
828 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
829 g_assert_cmpuint (attr->length, ==, 0);
830 g_assert_null (attr->value);
831
832 g_assert_cmpuint (gck_attributes_count (attrs), ==, 2);
833 gck_attributes_unref (attrs);
834 }
835
836 static void
test_builder_secure(void)837 test_builder_secure (void)
838 {
839 GckAttributes *attrs;
840 GckBuilder builder;
841 const GckAttribute *attr;
842
843 gck_builder_init_full (&builder, GCK_BUILDER_SECURE_MEMORY);
844
845 gck_builder_add_boolean (&builder, 88, TRUE);
846 attrs = gck_builder_end (&builder);
847 attr = gck_attributes_at (attrs, 0);
848
849 g_assert_true (egg_secure_check (attr->value));
850
851 gck_attributes_unref (attrs);
852 }
853
854 static void
test_builder_copy(void)855 test_builder_copy (void)
856 {
857 GckBuilder builder = GCK_BUILDER_INIT;
858 GckAttributes *attrs;
859 GckBuilder *copy;
860 const GckAttribute *attr;
861
862 gck_builder_add_ulong (&builder, ATTR_TYPE, 88);
863 copy = gck_builder_copy (&builder);
864 gck_builder_clear (&builder);
865
866 attrs = gck_builder_end (copy);
867 gck_builder_unref (copy);
868
869 attr = gck_attributes_at (attrs, 0);
870 g_assert_cmpuint (gck_attribute_get_ulong (attr), ==, 88);
871 g_assert_cmpuint (attr->type, ==, ATTR_TYPE);
872
873 /* Should be able to copy null */
874 copy = gck_builder_copy (NULL);
875 g_assert_null (copy);
876
877 gck_attributes_unref (attrs);
878 }
879
880 static void
test_builder_refs(void)881 test_builder_refs (void)
882 {
883 GckBuilder *builder, *two;
884 gulong check;
885
886 builder = gck_builder_new (GCK_BUILDER_NONE);
887 gck_builder_add_ulong (builder, 88, 99);
888
889 two = gck_builder_ref (builder);
890
891 g_assert_true (builder == two);
892
893 if (!gck_builder_find_ulong (builder, 88, &check))
894 g_assert_not_reached ();
895 g_assert_cmpuint (check, ==, 99);
896
897 gck_builder_unref (builder);
898
899 if (!gck_builder_find_ulong (two, 88, &check))
900 g_assert_not_reached ();
901 g_assert_cmpuint (check, ==, 99);
902
903 gck_builder_unref (two);
904 }
905
906 static void
test_builder_boxed(void)907 test_builder_boxed (void)
908 {
909 GckBuilder *builder, *two;
910 gulong check;
911
912 builder = gck_builder_new (GCK_BUILDER_NONE);
913 gck_builder_add_ulong (builder, 88, 99);
914
915 two = g_boxed_copy (GCK_TYPE_BUILDER, builder);
916
917 g_assert_true (builder == two);
918
919 if (!gck_builder_find_ulong (builder, 88, &check))
920 g_assert_not_reached ();
921 g_assert_cmpuint (check, ==, 99);
922
923 g_boxed_free (GCK_TYPE_BUILDER, builder);
924
925 if (!gck_builder_find_ulong (two, 88, &check))
926 g_assert_not_reached ();
927 g_assert_cmpuint (check, ==, 99);
928
929 gck_builder_unref (two);
930 }
931
932 static void
test_builder_add_attr(void)933 test_builder_add_attr (void)
934 {
935 GckBuilder bone = GCK_BUILDER_INIT;
936 GckBuilder btwo = GCK_BUILDER_INIT;
937 const GckAttribute *aone, *atwo;
938 GckAttributes *aones, *atwos;
939 gchar *value;
940
941 gck_builder_add_string (&bone, ATTR_TYPE, "blah");
942 aones = gck_builder_end (&bone);
943 aone = gck_attributes_at (aones, 0);
944
945 gck_builder_add_all (&btwo, aones);
946 atwos = gck_builder_end (&btwo);
947 atwo = gck_attributes_at (atwos, 0);
948
949 /* Should be equal, and also share the values */
950 gck_attribute_equal (aone, atwo);
951 g_assert_true (aone->value == atwo->value);
952
953 gck_attributes_unref (aones);
954
955 value = gck_attribute_get_string (atwo);
956 g_assert_cmpstr (value, ==, "blah");
957 g_free (value);
958
959 gck_attributes_unref (atwos);
960 }
961
962 static void
test_attribute_hash(void)963 test_attribute_hash (void)
964 {
965 guchar *data = (guchar *)"extra attribute";
966 GckAttribute one = { CKA_LABEL, (guchar *)"yay", 3 };
967 GckAttribute null = { CKA_LABEL, (guchar *)NULL, 3 };
968 GckAttribute zero = { CKA_LABEL, (guchar *)NULL, 0 };
969 GckAttribute two = { CKA_VALUE, (guchar *)"yay", 3 };
970 GckAttribute other = { CKA_VALUE, data, 5 };
971 GckAttribute overflow = { CKA_VALUE, data, 5 };
972 GckAttribute content = { CKA_VALUE, (guchar *)"conte", 5 };
973 guint hash;
974
975 hash = gck_attribute_hash (&one);
976 g_assert_cmpuint (hash, !=, 0);
977
978 g_assert_cmpuint (gck_attribute_hash (&one), ==, hash);
979 g_assert_cmpuint (gck_attribute_hash (&two), !=, hash);
980 g_assert_cmpuint (gck_attribute_hash (&other), !=, hash);
981 g_assert_cmpuint (gck_attribute_hash (&overflow), !=, hash);
982 g_assert_cmpuint (gck_attribute_hash (&null), !=, hash);
983 g_assert_cmpuint (gck_attribute_hash (&zero), !=, hash);
984 g_assert_cmpuint (gck_attribute_hash (&content), !=, hash);
985 }
986
987 static void
test_attributes_refs(void)988 test_attributes_refs (void)
989 {
990 GckBuilder builder = GCK_BUILDER_INIT;
991 GckAttributes *attrs;
992
993 attrs = gck_builder_end (&builder);
994 g_assert_nonnull (attrs);
995 g_assert_cmpuint (gck_attributes_count (attrs), ==, 0);
996
997 g_assert_true (gck_attributes_ref (attrs) == attrs);
998 gck_attributes_unref (attrs);
999
1000 gck_attributes_unref (attrs);
1001
1002 /* Can unref NULL */
1003 gck_attributes_unref (NULL);
1004 }
1005
1006 static void
test_attributes_contents(GckAttributes * attrs,gboolean extras,gint count)1007 test_attributes_contents (GckAttributes *attrs,
1008 gboolean extras,
1009 gint count)
1010 {
1011 const GckAttribute *attr;
1012 gchar *value;
1013 GDate date, *check;
1014
1015 g_assert_nonnull (attrs);
1016 if (count < 0)
1017 count = extras ? 7 : 5;
1018 g_assert_cmpuint (gck_attributes_count (attrs), ==, count);
1019
1020 attr = gck_attributes_at (attrs, 0);
1021 g_assert_cmpuint (attr->type, ==, 0);
1022 g_assert_true (gck_attribute_get_boolean (attr));
1023
1024 attr = gck_attributes_at (attrs, 1);
1025 g_assert_cmpuint (attr->type, ==, 101);
1026 gck_assert_cmpulong (gck_attribute_get_ulong (attr), ==, 888);
1027
1028 attr = gck_attributes_at (attrs, 2);
1029 g_assert_cmpuint (attr->type, ==, 202);
1030 value = gck_attribute_get_string (attr);
1031 g_assert_cmpstr (value, ==, "string");
1032 g_free (value);
1033
1034 attr = gck_attributes_at (attrs, 3);
1035 g_assert_cmpuint (attr->type, ==, 303);
1036 check = g_date_new_dmy (11, 12, 2008);
1037 gck_attribute_get_date (attr, &date);
1038 g_assert_true (g_date_compare (&date, check) == 0);
1039 g_date_free (check);
1040
1041 attr = gck_attributes_at (attrs, 4);
1042 g_assert_cmpuint (attr->type, ==, 404);
1043 g_assert_cmpmem (attr->value, attr->length, ATTR_DATA, N_ATTR_DATA);
1044
1045 if (!extras)
1046 return;
1047
1048 attr = gck_attributes_at (attrs, 5);
1049 g_assert_cmpuint (attr->type, ==, 505);
1050 g_assert_cmpuint (attr->length, ==, (gulong)-1);
1051 g_assert_null (attr->value);
1052 g_assert_true (gck_attribute_is_invalid (attr));
1053
1054 attr = gck_attributes_at (attrs, 6);
1055 g_assert_cmpuint (attr->type, ==, 606);
1056 g_assert_cmpuint (attr->length, ==, 0);
1057 g_assert_null (attr->value);
1058 }
1059
1060 static void
test_attributes_new_empty(void)1061 test_attributes_new_empty (void)
1062 {
1063 GckAttributes *attrs;
1064 const GckAttribute *attr;
1065
1066 attrs = gck_attributes_new_empty (GCK_INVALID);
1067 g_assert_cmpuint (gck_attributes_count (attrs), ==, 0);
1068 gck_attributes_unref (attrs);
1069
1070 attrs = gck_attributes_new_empty (CKA_ID, CKA_LABEL, GCK_INVALID);
1071 g_assert_cmpuint (gck_attributes_count (attrs), ==, 2);
1072 attr = gck_attributes_at (attrs, 0);
1073 g_assert_cmpuint (attr->type, ==, CKA_ID);
1074 g_assert_cmpuint (attr->length, ==, 0);
1075 g_assert_null (attr->value);
1076 attr = gck_attributes_at (attrs, 1);
1077 g_assert_cmpuint (attr->type, ==, CKA_LABEL);
1078 g_assert_cmpuint (attr->length, ==, 0);
1079 g_assert_null (attr->value);
1080 gck_attributes_unref (attrs);
1081 }
1082
1083 static void
test_attributes_empty(void)1084 test_attributes_empty (void)
1085 {
1086 GckBuilder builder = GCK_BUILDER_INIT;
1087 GckAttributes *attrs;
1088 const GckAttribute *attr;
1089 guint i;
1090
1091 gck_builder_add_empty (&builder, 101UL);
1092 gck_builder_add_empty (&builder, 202UL);
1093 gck_builder_add_empty (&builder, 303UL);
1094 gck_builder_add_empty (&builder, 404UL);
1095 attrs = gck_builder_end (&builder);
1096
1097 g_assert_cmpuint (gck_attributes_count (attrs), ==, 4);
1098 for (i = 0; i < gck_attributes_count (attrs); ++i) {
1099 attr = gck_attributes_at (attrs, i);
1100 g_assert_cmpuint (attr->type, ==, ((i + 1) * 100) + i + 1);
1101 g_assert_null (attr->value);
1102 g_assert_cmpuint (attr->length, ==, 0);
1103 }
1104
1105 gck_attributes_unref (attrs);
1106 }
1107
1108 static void
test_builder_add_from(void)1109 test_builder_add_from (void)
1110 {
1111 GckBuilder builder = GCK_BUILDER_INIT;
1112 GckBuilder two = GCK_BUILDER_INIT;
1113 GckAttributes *attrs;
1114 guint i;
1115
1116 builder_add_fixtures (&builder, 0);
1117 attrs = gck_builder_end (&builder);
1118
1119 for (i = 0; i < gck_attributes_count (attrs); i++)
1120 gck_builder_add_attribute (&two, gck_attributes_at (attrs, i));
1121
1122 gck_attributes_unref (attrs);
1123 attrs = gck_builder_end (&two);
1124
1125 test_attributes_contents (attrs, TRUE, -1);
1126 gck_attributes_unref (attrs);
1127 }
1128
1129
1130 static void
test_builder_add_all(void)1131 test_builder_add_all (void)
1132 {
1133 GckBuilder builder = GCK_BUILDER_INIT;
1134 GckBuilder two = GCK_BUILDER_INIT;
1135 GckAttributes *attrs;
1136
1137 builder_add_fixtures (&builder, 0);
1138 attrs = gck_builder_end (&builder);
1139
1140 gck_builder_add_all (&two, attrs);
1141 gck_attributes_unref (attrs);
1142 attrs = gck_builder_end (&two);
1143
1144 test_attributes_contents (attrs, TRUE, -1);
1145 gck_attributes_unref (attrs);
1146 }
1147
1148 static void
test_builder_set_all(void)1149 test_builder_set_all (void)
1150 {
1151 GckBuilder builder = GCK_BUILDER_INIT;
1152 GckBuilder two = GCK_BUILDER_INIT;
1153 GckAttributes *attrs;
1154
1155 builder_add_fixtures (&builder, 5);
1156 builder_add_fixtures (&two, 0);
1157 attrs = gck_builder_end (&two);
1158 gck_builder_set_all (&builder, attrs);
1159 gck_attributes_unref (attrs);
1160 attrs = gck_builder_end (&builder);
1161
1162 test_attributes_contents (attrs, TRUE, -1);
1163 gck_attributes_unref (attrs);
1164 }
1165
1166
1167 static void
test_builder_set_blank(void)1168 test_builder_set_blank (void)
1169 {
1170 GckBuilder builder;
1171 gboolean value;
1172
1173 gck_builder_init (&builder);
1174 gck_builder_set_boolean (&builder, 5, TRUE);
1175 if (!gck_builder_find_boolean (&builder, 5, &value))
1176 g_assert_not_reached ();
1177 g_assert_true (value);
1178 gck_builder_clear (&builder);
1179 }
1180
1181 static void
test_builder_add_only(void)1182 test_builder_add_only (void)
1183 {
1184 GckBuilder builder = GCK_BUILDER_INIT;
1185 GckBuilder two = GCK_BUILDER_INIT;
1186 GckAttributes *attrs;
1187
1188 builder_add_fixtures (&builder, 0);
1189 attrs = gck_builder_end (&builder);
1190
1191 gck_builder_add_only (&two, attrs, 0UL, 202UL, 404UL, 606UL, GCK_INVALID);
1192 gck_attributes_unref (attrs);
1193 attrs = gck_builder_end (&two);
1194
1195 g_assert_nonnull (gck_attributes_find (attrs, 0UL));
1196 g_assert_nonnull (gck_attributes_find (attrs, 202UL));
1197 g_assert_nonnull (gck_attributes_find (attrs, 404UL));
1198 g_assert_nonnull (gck_attributes_find (attrs, 606UL));
1199
1200 g_assert_null (gck_attributes_find (attrs, 101UL));
1201 g_assert_null (gck_attributes_find (attrs, 303UL));
1202 g_assert_null (gck_attributes_find (attrs, 505UL));
1203
1204 gck_attributes_unref (attrs);
1205 }
1206
1207 static void
test_builder_add_except(void)1208 test_builder_add_except (void)
1209 {
1210 GckBuilder builder = GCK_BUILDER_INIT;
1211 GckBuilder two = GCK_BUILDER_INIT;
1212 GckAttributes *attrs;
1213
1214 builder_add_fixtures (&builder, 0);
1215 attrs = gck_builder_end (&builder);
1216
1217 gck_builder_add_except (&two, attrs, 0UL, 202UL, 404UL, 606UL, GCK_INVALID);
1218 gck_attributes_unref (attrs);
1219 attrs = gck_builder_end (&two);
1220
1221 g_assert_null (gck_attributes_find (attrs, 0UL));
1222 g_assert_null (gck_attributes_find (attrs, 202UL));
1223 g_assert_null (gck_attributes_find (attrs, 404UL));
1224 g_assert_null (gck_attributes_find (attrs, 606UL));
1225
1226 g_assert_nonnull (gck_attributes_find (attrs, 101UL));
1227 g_assert_nonnull (gck_attributes_find (attrs, 303UL));
1228 g_assert_nonnull (gck_attributes_find (attrs, 505UL));
1229
1230 gck_attributes_unref (attrs);
1231 }
1232
1233 static void
test_builder_add_only_and_except(void)1234 test_builder_add_only_and_except (void)
1235 {
1236 GckBuilder builder = GCK_BUILDER_INIT;
1237 GckBuilder two = GCK_BUILDER_INIT;
1238 GckAttributes *attrs;
1239
1240 builder_add_fixtures (&builder, 0);
1241 attrs = gck_builder_end (&builder);
1242
1243 gck_builder_add_only (&two, attrs, 0UL, 101UL, 202UL, 303UL, GCK_INVALID);
1244 gck_builder_add_except (&two, attrs, 0UL, 101UL, 202UL, 303UL, GCK_INVALID);
1245 gck_attributes_unref (attrs);
1246 attrs = gck_builder_end (&two);
1247
1248 test_attributes_contents (attrs, TRUE, -1);
1249 gck_attributes_unref (attrs);
1250 }
1251
1252 static void
test_find_attributes(void)1253 test_find_attributes (void)
1254 {
1255 GckBuilder builder = GCK_BUILDER_INIT;
1256 GDate check, *date = g_date_new_dmy (13, 12, 2008);
1257 GckAttributes *attrs;
1258 const GckAttribute *attr;
1259 gboolean bvalue, ret;
1260 gulong uvalue;
1261 gchar *svalue;
1262
1263 gck_builder_add_boolean (&builder, 0UL, TRUE);
1264 gck_builder_add_ulong (&builder, 101UL, 888UL);
1265 gck_builder_add_string (&builder, 202UL, "string");
1266 gck_builder_add_date (&builder, 303UL, date);
1267 gck_builder_add_data (&builder, 404UL, (const guchar *)ATTR_DATA, N_ATTR_DATA);
1268 attrs = gck_builder_end (&builder);
1269
1270 attr = gck_attributes_find (attrs, 404);
1271 g_assert_nonnull (attr);
1272 g_assert_cmpmem (attr->value, attr->length, ATTR_DATA, N_ATTR_DATA);
1273
1274 ret = gck_attributes_find_boolean (attrs, 0UL, &bvalue);
1275 g_assert_true (ret);
1276 g_assert_true (bvalue);
1277
1278 ret = gck_attributes_find_ulong (attrs, 101UL, &uvalue);
1279 g_assert_true (ret);
1280 g_assert_cmpuint (uvalue, ==, 888);
1281
1282 ret = gck_attributes_find_string (attrs, 202UL, &svalue);
1283 g_assert_true (ret);
1284 g_assert_nonnull (svalue);
1285 g_assert_cmpstr (svalue, ==, "string");
1286 g_free (svalue);
1287
1288 ret = gck_attributes_find_date (attrs, 303UL, &check);
1289 g_assert_true (ret);
1290 g_assert_true (g_date_compare (date, &check) == 0);
1291
1292 gck_attributes_unref (attrs);
1293 g_date_free (date);
1294 }
1295
1296 int
main(int argc,char ** argv)1297 main (int argc, char **argv)
1298 {
1299 g_test_init (&argc, &argv, NULL);
1300
1301 g_test_add_func ("/gck/value/to_boolean", test_value_to_boolean);
1302 g_test_add_func ("/gck/value/to_ulong", test_value_to_ulong);
1303 g_test_add_func ("/gck/attribute/init_memory", test_init_memory);
1304 g_test_add_func ("/gck/attribute/init_boolean", test_init_boolean);
1305 g_test_add_func ("/gck/attribute/init_date", test_init_date);
1306 g_test_add_func ("/gck/attribute/init_ulong", test_init_ulong);
1307 g_test_add_func ("/gck/attribute/init_string", test_init_string);
1308 g_test_add_func ("/gck/attribute/init_invalid", test_init_invalid);
1309 g_test_add_func ("/gck/attribute/init_empty", test_init_empty);
1310 g_test_add_func ("/gck/attribute/new_memory", test_new_memory);
1311 g_test_add_func ("/gck/attribute/new_boolean", test_new_boolean);
1312 g_test_add_func ("/gck/attribute/new_date", test_new_date);
1313 g_test_add_func ("/gck/attribute/new_ulong", test_new_ulong);
1314 g_test_add_func ("/gck/attribute/new_string", test_new_string);
1315 g_test_add_func ("/gck/attribute/new_invalid", test_new_invalid);
1316 g_test_add_func ("/gck/attribute/new_empty", test_new_empty);
1317 g_test_add_func ("/gck/attribute/get_boolean", test_get_boolean);
1318 g_test_add_func ("/gck/attribute/get_date", test_get_date);
1319 g_test_add_func ("/gck/attribute/get_ulong", test_get_ulong);
1320 g_test_add_func ("/gck/attribute/get_string", test_get_string);
1321 g_test_add_func ("/gck/attribute/dup_attribute", test_dup_attribute);
1322 g_test_add_func ("/gck/attribute/copy_attribute", test_copy_attribute);
1323 g_test_add_func ("/gck/attribute/hash", test_attribute_hash);
1324 g_test_add_func ("/gck/builder/blank", test_builder_blank);
1325 g_test_add_func ("/gck/builder/data", test_build_data);
1326 g_test_add_func ("/gck/builder/data-invalid", test_build_data_invalid);
1327 g_test_add_func ("/gck/builder/data-secure", test_build_data_secure);
1328 g_test_add_func ("/gck/builder/take", test_build_take);
1329 g_test_add_func ("/gck/builder/take-invalid", test_build_take_invalid);
1330 g_test_add_func ("/gck/builder/take-secure", test_build_take_secure);
1331 g_test_add_func ("/gck/builder/boolean", test_build_boolean);
1332 g_test_add_func ("/gck/builder/date", test_build_date);
1333 g_test_add_func ("/gck/builder/ulong", test_build_ulong);
1334 g_test_add_func ("/gck/builder/string", test_build_string);
1335 g_test_add_func ("/gck/builder/string-null", test_build_string_null);
1336 g_test_add_func ("/gck/builder/invalid", test_build_invalid);
1337 g_test_add_func ("/gck/builder/empty", test_build_empty);
1338 g_test_add_func ("/gck/builder/secure", test_builder_secure);
1339 g_test_add_func ("/gck/builder/copy", test_builder_copy);
1340 g_test_add_func ("/gck/builder/refs", test_builder_refs);
1341 g_test_add_func ("/gck/builder/boxed", test_builder_boxed);
1342 g_test_add_func ("/gck/builder/add-attr", test_builder_add_attr);
1343 g_test_add_func ("/gck/builder/add-all", test_builder_add_all);
1344 g_test_add_func ("/gck/builder/add-from", test_builder_add_from);
1345 g_test_add_func ("/gck/builder/add-only", test_builder_add_only);
1346 g_test_add_func ("/gck/builder/add-except", test_builder_add_except);
1347 g_test_add_func ("/gck/builder/add-only-and-except", test_builder_add_only_and_except);
1348 g_test_add_func ("/gck/builder/set-all", test_builder_set_all);
1349 g_test_add_func ("/gck/builder/set-blank", test_builder_set_blank);
1350 g_test_add_func ("/gck/attributes/refs", test_attributes_refs);
1351 g_test_add_func ("/gck/attributes/new-empty", test_attributes_new_empty);
1352 g_test_add_func ("/gck/attributes/empty", test_attributes_empty);
1353 g_test_add_func ("/gck/attributes/find_attributes", test_find_attributes);
1354
1355 return g_test_run ();
1356 }
1357