Debian Codegraph @

pkcs11/gkm/test-attributes.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-attributes.c: Test attributes functionality
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 <http://www.gnu.org/licenses/>.
19
20 Author: Stef Walter <[email protected]>
21 */
22
23 #include "config.h"
24
25 #include "gkm/gkm-attributes.h"
26
27 #include <glib-object.h>
28
29 /* Some test data */
30 static CK_OBJECT_CLASS attr_template_klass = CKO_DATA;
31 static CK_BBOOL attr_template_token = CK_TRUE;
32 static CK_ATTRIBUTE attr_template[] = {
33 { CKA_LABEL, "funny", 5 },
34 { CKA_CLASS, &attr_template_klass, sizeof (CK_OBJECT_CLASS) },
35 { CKA_ID, "my-identifier", 13 },
36 { CKA_TOKEN, &attr_template_token, sizeof (CK_BBOOL) },
37 { CKA_VALUE, "\a[\315\025", 4 }
38 };
39
40 static void
41 test_attribute_equal_zero_len_null_ptr (void)
42 {
43 CK_ATTRIBUTE attr1 = { CKA_LABEL, "", 0 };
44 CK_ATTRIBUTE attr2 = { CKA_LABEL, NULL, 0 };
45 g_assert (gkm_attribute_equal (&attr1, &attr2));
46 }
47
48 static void
49 test_attribute_consume (void)
50 {
51 CK_ATTRIBUTE attr;
52 attr.type = CKA_LABEL;
53
54 gkm_attribute_consume (&attr);
55 g_assert (attr.type == (gulong)-1);
56 }
57
58 static void
59 test_attribute_consumed (void)
60 {
61 CK_ATTRIBUTE attr;
62 gboolean ret;
63
64 attr.type = CKA_LABEL;
65
66 ret = gkm_attribute_consumed (&attr);
67 g_assert (ret == FALSE);
68
69 gkm_attribute_consume (&attr);
70
71 ret = gkm_attribute_consumed (&attr);
72 g_assert (ret == TRUE);
73 }
74
75 static void
76 test_attribute_set_data (void)
77 {
78 guchar buffer[32];
79 CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) };
80 CK_RV rv;
81
82 rv = gkm_attribute_set_data (&attr, "mytest", 6);
83 g_assert (rv == CKR_OK);
84 g_assert (attr.ulValueLen == 6);
85 g_assert (memcmp (buffer, "mytest", 6) == 0);
86 }
87
88 static void
89 test_attribute_set_data_short (void)
90 {
91 guchar buffer[32];
92 CK_ATTRIBUTE attr = { 0, buffer, 4 };
93 CK_RV rv;
94
95 rv = gkm_attribute_set_data (&attr, "mytest", 6);
96 g_assert (rv == CKR_BUFFER_TOO_SMALL);
97 g_assert (attr.ulValueLen == (CK_ULONG)-1);
98 }
99
100 static void
101 test_attribute_set_data_length (void)
102 {
103 CK_ATTRIBUTE attr = { 0, NULL, 0 };
104 CK_RV rv;
105
106 rv = gkm_attribute_set_data (&attr, "mytest", 6);
107 g_assert (rv == CKR_OK);
108 g_assert (attr.ulValueLen == 6);
109 }
110
111 static void
112 test_attribute_set_empty (void)
113 {
114 CK_ATTRIBUTE attr;
115 gchar buf[30];
116 CK_RV rv;
117
118 attr.ulValueLen = 30;
119 attr.pValue = buf;
120 rv = gkm_attribute_set_empty (&attr);
121 g_assert (rv == CKR_OK);
122 g_assert (attr.ulValueLen == 0);
123 }
124
125 static void
126 test_attribute_get_bool (void)
127 {
128 CK_ATTRIBUTE attr;
129 CK_BBOOL val = CK_TRUE;
130 gboolean value;
131 CK_RV rv;
132
133 attr.ulValueLen = sizeof (CK_BBOOL);
134 attr.pValue = &val;
135 rv = gkm_attribute_get_bool (&attr, &value);
136 g_assert (rv == CKR_OK);
137 g_assert (value == TRUE);
138 }
139
140 static void
141 test_attribute_get_bool_invalid (void)
142 {
143 CK_ATTRIBUTE attr;
144 CK_ULONG val = 4;
145 gboolean value;
146 CK_RV rv;
147
148 attr.ulValueLen = sizeof (CK_ULONG);
149 attr.pValue = &val;
150 rv = gkm_attribute_get_bool (&attr, &value);
151 g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID);
152 }
153
154 static void
155 test_attribute_set_time (void)
156 {
157 CK_ATTRIBUTE attr;
158 gchar buf[30];
159 CK_RV rv;
160
161 attr.ulValueLen = 30;
162 attr.pValue = buf;
163 rv = gkm_attribute_set_time (&attr, 1247930171);
164 g_assert (rv == CKR_OK);
165 g_assert (attr.ulValueLen == 16);
166 g_assert (memcmp (attr.pValue, "2009071815161100", 16) == 0);
167 }
168
169 static void
170 test_attribute_set_time_empty (void)
171 {
172 CK_ATTRIBUTE attr;
173 gchar buf[30];
174 CK_RV rv;
175
176 attr.ulValueLen = 30;
177 attr.pValue = buf;
178 rv = gkm_attribute_set_time (&attr, -1);
179 g_assert (rv == CKR_OK);
180 g_assert (attr.ulValueLen == 0);
181 }
182
183 static void
184 test_attribute_set_time_length (void)
185 {
186 CK_ATTRIBUTE attr;
187 CK_RV rv;
188
189 attr.pValue = NULL;
190 attr.ulValueLen = 0;
191 rv = gkm_attribute_set_time (&attr, 1247930171);
192 g_assert (rv == CKR_OK);
193 g_assert (attr.ulValueLen == 16);
194 g_assert (attr.pValue == NULL);
195 }
196
197 static void
198 test_attribute_get_time (void)
199 {
200 CK_ATTRIBUTE attr;
201 glong when;
202 CK_RV rv;
203
204 attr.ulValueLen = 16;
205 attr.pValue = "2009071815161100";
206 rv = gkm_attribute_get_time (&attr, &when);
207 g_assert (rv == CKR_OK);
208 g_assert (when == 1247930171);
209 }
210
211 static void
212 test_attribute_get_time_empty (void)
213 {
214 CK_ATTRIBUTE attr;
215 glong when;
216 CK_RV rv;
217
218 attr.ulValueLen = 0;
219 attr.pValue = "";
220 rv = gkm_attribute_get_time (&attr, &when);
221 g_assert (rv == CKR_OK);
222 g_assert (when == -1);
223 }
224
225 static void
226 test_attribute_get_time_invalid (void)
227 {
228 CK_ATTRIBUTE attr;
229 glong when;
230 CK_RV rv;
231
232 attr.ulValueLen = 16;
233 attr.pValue = "aaaaaaaaaaaaaaaa";
234 rv = gkm_attribute_get_time (&attr, &when);
235 g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID);
236 }
237
238 static void
239 test_attribute_get_time_invalid_length (void)
240 {
241 CK_ATTRIBUTE attr;
242 glong when;
243 CK_RV rv;
244
245 attr.ulValueLen = 8;
246 attr.pValue = "2009071815161100";
247 rv = gkm_attribute_get_time (&attr, &when);
248 g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID);
249 }
250
251 static void
252 test_attribute_get_string (void)
253 {
254 CK_ATTRIBUTE attr;
255 gchar *value;
256 CK_RV rv;
257
258 attr.ulValueLen = 4;
259 attr.pValue = "blah";
260
261 rv = gkm_attribute_get_string (&attr, &value);
262 g_assert (rv == CKR_OK);
263 g_assert_cmpstr (value, ==, "blah");
264
265 g_free (value);
266 }
267
268 static void
269 test_attribute_get_string_null (void)
270 {
271 CK_ATTRIBUTE attr;
272 gchar *value;
273 CK_RV rv;
274
275 attr.ulValueLen = 0;
276 attr.pValue = NULL;
277
278 rv = gkm_attribute_get_string (&attr, &value);
279 g_assert (rv == CKR_OK);
280 g_assert (value == NULL);
281 }
282
283 static void
284 test_attribute_get_string_not_utf8 (void)
285 {
286 CK_ATTRIBUTE attr;
287 gchar *value;
288 CK_RV rv;
289
290 /* No embedded nulls, or non-UTF8 */
291 attr.ulValueLen = 5;
292 attr.pValue = "\0test";
293
294 rv = gkm_attribute_get_string (&attr, &value);
295 g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID);
296 }
297
298 static void
299 test_attribute_get_string_bad_pointer (void)
300 {
301 CK_ATTRIBUTE attr;
302 gchar *value;
303 CK_RV rv;
304
305 /* No embedded nulls, or non-UTF8 */
306 attr.ulValueLen = 5;
307 attr.pValue = NULL;
308
309 rv = gkm_attribute_get_string (&attr, &value);
310 g_assert (rv == CKR_ATTRIBUTE_VALUE_INVALID);
311 }
312
313 static void
314 test_attribute_set_bool (void)
315 {
316 guchar buffer[32];
317 CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) };
318 CK_RV rv;
319
320 rv = gkm_attribute_set_bool (&attr, CK_TRUE);
321 g_assert (rv == CKR_OK);
322 g_assert (attr.ulValueLen == 1);
323 g_assert (memcmp (buffer, "\1", 1) == 0);
324
325 rv = gkm_attribute_set_bool (&attr, CK_FALSE);
326 g_assert (rv == CKR_OK);
327 g_assert (attr.ulValueLen == 1);
328 g_assert (memcmp (buffer, "\0", 1) == 0);
329 }
330
331 static void
332 test_attribute_set_bool_short (void)
333 {
334 guchar buffer[32];
335 CK_ATTRIBUTE attr = { 0, buffer, 0 };
336 CK_RV rv;
337
338 rv = gkm_attribute_set_bool (&attr, CK_TRUE);
339 g_assert (rv == CKR_BUFFER_TOO_SMALL);
340 g_assert (attr.ulValueLen == (CK_ULONG)-1);
341 }
342
343 static void
344 test_attribute_set_bool_length (void)
345 {
346 CK_ATTRIBUTE attr = { 0, NULL, 0 };
347 CK_RV rv;
348
349 rv = gkm_attribute_set_bool (&attr, CK_TRUE);
350 g_assert (rv == CKR_OK);
351 g_assert (attr.ulValueLen == 1);
352 }
353
354 static void
355 test_attribute_set_ulong (void)
356 {
357 guchar buffer[32];
358 CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) };
359 CK_ULONG value = 55;
360 CK_RV rv;
361
362 rv = gkm_attribute_set_ulong (&attr, value);
363 g_assert (rv == CKR_OK);
364 g_assert (attr.ulValueLen == sizeof (CK_ULONG));
365 g_assert (memcmp (buffer, &value, sizeof (CK_ULONG)) == 0);
366 }
367
368 static void
369 test_attribute_set_ulong_short (void)
370 {
371 guchar buffer[32];
372 CK_ATTRIBUTE attr = { 0, buffer, 0 };
373 CK_RV rv;
374
375 rv = gkm_attribute_set_ulong (&attr, 1);
376 g_assert (rv == CKR_BUFFER_TOO_SMALL);
377 g_assert (attr.ulValueLen == (CK_ULONG)-1);
378 }
379
380 static void
381 test_attribute_set_ulong_length (void)
382 {
383 CK_ATTRIBUTE attr = { 0, NULL, 0 };
384 CK_RV rv;
385
386 rv = gkm_attribute_set_ulong (&attr, 98889);
387 g_assert (rv == CKR_OK);
388 g_assert (attr.ulValueLen == sizeof (CK_ULONG));
389 }
390
391 static void
392 test_attribute_set_string (void)
393 {
394 guchar buffer[32];
395 CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) };
396 CK_RV rv;
397
398 rv = gkm_attribute_set_string (&attr, "hello");
399 g_assert (rv == CKR_OK);
400 g_assert (attr.ulValueLen == 5);
401 g_assert (memcmp (buffer, "hello", 5) == 0);
402 }
403
404 static void
405 test_attribute_set_string_null (void)
406 {
407 guchar buffer[32];
408 CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) };
409 CK_RV rv;
410
411 rv = gkm_attribute_set_string (&attr, NULL);
412 g_assert (rv == CKR_OK);
413 g_assert (attr.ulValueLen == 0);
414 }
415
416 static void
417 test_attribute_set_string_short (void)
418 {
419 guchar buffer[32];
420 CK_ATTRIBUTE attr = { 0, buffer, 3 };
421 CK_RV rv;
422
423 rv = gkm_attribute_set_string (&attr, "hello");
424 g_assert (rv == CKR_BUFFER_TOO_SMALL);
425 g_assert (attr.ulValueLen == (CK_ULONG)-1);
426 }
427
428 static void
429 test_attribute_set_string_length (void)
430 {
431 CK_ATTRIBUTE attr = { 0, NULL, 0 };
432 CK_RV rv;
433
434 rv = gkm_attribute_set_string (&attr, "hello");
435 g_assert (rv == CKR_OK);
436 g_assert (attr.ulValueLen == 5);
437 }
438
439 static void
440 test_attribute_set_date (void)
441 {
442 guchar buffer[32];
443 CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) };
444 CK_DATE *date;
445 CK_RV rv;
446
447 rv = gkm_attribute_set_date (&attr, 1249845741);
448 g_assert (rv == CKR_OK);
449 g_assert (attr.ulValueLen == sizeof (CK_DATE));
450 date = (CK_DATE*)buffer;
451 g_assert (memcmp (date->day, "09", 2) == 0);
452 g_assert (memcmp (date->month, "08", 2) == 0);
453 g_assert (memcmp (date->year, "2009", 4) == 0);
454 }
455
456 static void
457 test_attribute_set_date_none (void)
458 {
459 guchar buffer[32];
460 CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) };
461 CK_RV rv;
462
463 rv = gkm_attribute_set_date (&attr, (time_t)-1);
464 g_assert (rv == CKR_OK);
465 g_assert (attr.ulValueLen == 0);
466 }
467
468 static void
469 test_attribute_set_date_short (void)
470 {
471 guchar buffer[32];
472 CK_ATTRIBUTE attr = { 0, buffer, 5 };
473 CK_RV rv;
474
475 rv = gkm_attribute_set_date (&attr, 1249845741);
476 g_assert (rv == CKR_BUFFER_TOO_SMALL);
477 g_assert (attr.ulValueLen == (CK_ULONG)-1);
478 }
479
480 static void
481 test_attribute_set_date_length (void)
482 {
483 CK_ATTRIBUTE attr = { 0, NULL, 0 };
484 CK_RV rv;
485
486 rv = gkm_attribute_set_date (&attr, 1249845741);
487 g_assert (rv == CKR_OK);
488 g_assert (attr.ulValueLen == sizeof (CK_DATE));
489 }
490
491 static void
492 test_attribute_set_mpi (void)
493 {
494 guchar buffer[32];
495 CK_ATTRIBUTE attr = { 0, buffer, sizeof (buffer) };
496 gcry_mpi_t mpi;
497 CK_RV rv;
498
499 mpi = gcry_mpi_new (32);
500 gcry_mpi_set_ui (mpi, 123456789);
501
502 rv = gkm_attribute_set_mpi (&attr, mpi);
503 g_assert (rv == CKR_OK);
504 g_assert (attr.ulValueLen == 4);
505 g_assert (memcmp (buffer, "\a[\315\025", 4) == 0);
506
507 gcry_mpi_release (mpi);
508 }
509
510 static void
511 test_attribute_set_mpi_short (void)
512 {
513 guchar buffer[32];
514 CK_ATTRIBUTE attr = { 0, buffer, 2 };
515 gcry_mpi_t mpi;
516 CK_RV rv;
517
518 mpi = gcry_mpi_new (32);
519 gcry_mpi_set_ui (mpi, 123456789);
520
521 rv = gkm_attribute_set_mpi (&attr, mpi);
522 g_assert (rv == CKR_BUFFER_TOO_SMALL);
523 g_assert (attr.ulValueLen == (CK_ULONG)-1);
524
525 gcry_mpi_release (mpi);
526 }
527
528 static void
529 test_attribute_set_mpi_length (void)
530 {
531 CK_ATTRIBUTE attr = { 0, NULL, 0 };
532 gcry_mpi_t mpi;
533 CK_RV rv;
534
535 mpi = gcry_mpi_new (32);
536 gcry_mpi_set_ui (mpi, 123456789);
537
538 rv = gkm_attribute_set_mpi (&attr, mpi);
539 g_assert (rv == CKR_OK);
540 g_assert (attr.ulValueLen == 4);
541
542 gcry_mpi_release (mpi);
543 }
544
545 static void
546 test_attribute_equal (void)
547 {
548 /* Make sure we actually have two different strings */
549 gchar *val1 = g_strdup ("my-identifier");
550 gchar *val2 = g_strdup ("my-identifier");
551 CK_ATTRIBUTE attr1 = { CKA_ID, val1, 13 };
552 CK_ATTRIBUTE attr2 = { CKA_ID, val2, 13 };
553 gboolean ret;
554
555 ret = gkm_attribute_equal (&attr1, &attr2);
556 g_assert (ret == TRUE);
557
558 g_free (val1);
559 g_free (val2);
560 }
561
562 static void
563 test_attribute_equal_same (void)
564 {
565 CK_ATTRIBUTE attr = { CKA_ID, "my-identifier", 13 };
566 gboolean ret;
567
568 ret = gkm_attribute_equal (&attr, &attr);
569 g_assert (ret == TRUE);
570 }
571
572 static void
573 test_attribute_equal_same_pointer (void)
574 {
575 gchar *val = "my-identifier";
576 CK_ATTRIBUTE attr1 = { CKA_ID, val, 13 };
577 CK_ATTRIBUTE attr2 = { CKA_ID, val, 13 };
578 gboolean ret;
579
580 ret = gkm_attribute_equal (&attr1, &attr2);
581 g_assert (ret == TRUE);
582 }
583
584 static void
585 test_attribute_equal_diff_types (void)
586 {
587 gchar *val = "my-identifier";
588 CK_ATTRIBUTE attr1 = { CKA_ID, val, 13 };
589 CK_ATTRIBUTE attr2 = { CKA_VALUE, val, 13 };
590 gboolean ret;
591
592 ret = gkm_attribute_equal (&attr1, &attr2);
593 g_assert (ret == FALSE);
594 }
595
596 static void
597 test_attribute_equal_diff_length (void)
598 {
599 CK_ATTRIBUTE attr1 = { CKA_ID, "my-identifier", 13 };
600 CK_ATTRIBUTE attr2 = { CKA_ID, "my-identifier", 2 };
601 gboolean ret;
602
603 ret = gkm_attribute_equal (&attr1, &attr2);
604 g_assert (ret == FALSE);
605 }
606
607 static void
608 test_attribute_equal_diff_value (void)
609 {
610 CK_ATTRIBUTE attr1 = { CKA_ID, "my-identifier", 13 };
611 CK_ATTRIBUTE attr2 = { CKA_ID, "xy-identifier", 13 };
612 gboolean ret;
613
614 ret = gkm_attribute_equal (&attr1, &attr2);
615 g_assert (ret == FALSE);
616 }
617
618 static void
619 test_attribute_hash (void)
620 {
621 CK_ATTRIBUTE attr = { CKA_VALUE, "value", 5 };
622 guint hash;
623
624 /* The hash value below could change as code changes */
625 hash = gkm_attribute_hash (&attr);
626 g_assert_cmpuint (hash, !=, 0U);
627 }
628
629 static void
630 test_attribute_contains (void)
631 {
632 CK_ATTRIBUTE attr = { CKA_ID, "my-identifier", 13 };
633 gboolean ret;
634
635 ret = gkm_attributes_contains (attr_template, G_N_ELEMENTS (attr_template), &attr);
636 g_assert (ret == TRUE);
637 }
638
639 static void
640 test_attribute_contains_no_value (void)
641 {
642 CK_ATTRIBUTE attr = { CKA_ID, "other-identifier", 16 };
643 gboolean ret;
644
645 ret = gkm_attributes_contains (attr_template, G_N_ELEMENTS (attr_template), &attr);
646 g_assert (ret == FALSE);
647 }
648
649 static void
650 test_attribute_contains_no_type (void)
651 {
652 CK_ATTRIBUTE attr = { CKA_VALUE, "value", 5 };
653 gboolean ret;
654
655 ret = gkm_attributes_contains (attr_template, G_N_ELEMENTS (attr_template), &attr);
656 g_assert (ret == FALSE);
657 }
658
659 static void
660 test_attributes_find (void)
661 {
662 CK_ATTRIBUTE_PTR attr;
663
664 attr = gkm_attributes_find (attr_template, G_N_ELEMENTS (attr_template), CKA_LABEL);
665 g_assert (attr != NULL);
666 g_assert (attr->type == CKA_LABEL);
667 }
668
669 static void
670 test_attributes_find_not_found (void)
671 {
672 CK_ATTRIBUTE_PTR attr;
673
674 attr = gkm_attributes_find (attr_template, G_N_ELEMENTS (attr_template), CKA_SENSITIVE);
675 g_assert (attr == NULL);
676 }
677
678 static void
679 test_attribute_find_boolean (void)
680 {
681 gboolean value;
682 gboolean ret;
683
684 ret = gkm_attributes_find_boolean (attr_template, G_N_ELEMENTS (attr_template), CKA_TOKEN, &value);
685 g_assert (ret == TRUE);
686 g_assert (value == TRUE);
687 }
688
689 static void
690 test_attribute_find_boolean_no_type (void)
691 {
692 gboolean value;
693 gboolean ret;
694
695 ret = gkm_attributes_find_boolean (attr_template, G_N_ELEMENTS (attr_template), CKA_SENSITIVE, &value);
696 g_assert (ret == FALSE);
697 }
698
699 static void
700 test_attribute_find_boolean_not_bbool (void)
701 {
702 gboolean value;
703 gboolean ret;
704
705 ret = gkm_attributes_find_boolean (attr_template, G_N_ELEMENTS (attr_template), CKA_CLASS, &value);
706 g_assert (ret == FALSE);
707 }
708
709 static void
710 test_attribute_find_ulong (void)
711 {
712 gulong value;
713 gboolean ret;
714
715 ret = gkm_attributes_find_ulong (attr_template, G_N_ELEMENTS (attr_template), CKA_CLASS, &value);
716 g_assert (ret == TRUE);
717 g_assert (value == CKO_DATA);
718 }
719
720 static void
721 test_attribute_find_ulong_no_type (void)
722 {
723 gulong value;
724 gboolean ret;
725
726 ret = gkm_attributes_find_ulong (attr_template, G_N_ELEMENTS (attr_template), CKA_KEY_TYPE, &value);
727 g_assert (ret == FALSE);
728 }
729
730 static void
731 test_attribute_find_ulong_not_ulong (void)
732 {
733 gulong value;
734 gboolean ret;
735
736 ret = gkm_attributes_find_ulong (attr_template, G_N_ELEMENTS (attr_template), CKA_ID, &value);
737 g_assert (ret == FALSE);
738 }
739
740 static void
741 test_attribute_find_mpi (void)
742 {
743 gcry_mpi_t mpi = NULL;
744 gboolean ret;
745
746 ret = gkm_attributes_find_mpi (attr_template, G_N_ELEMENTS (attr_template), CKA_VALUE, &mpi);
747 g_assert (ret == TRUE);
748 g_assert (mpi != NULL);
749
750 g_assert (gcry_mpi_cmp_ui (mpi, 123456789) == 0);
751 gcry_mpi_release (mpi);
752 }
753
754 static void
755 test_attribute_find_mpi_no_type (void)
756 {
757 gcry_mpi_t mpi = NULL;
758 gboolean ret;
759
760 ret = gkm_attributes_find_mpi (attr_template, G_N_ELEMENTS (attr_template), CKA_MODULUS, &mpi);
761 g_assert (ret == FALSE);
762 g_assert (mpi == NULL);
763 }
764
765 static void
766 test_attributes_consume (void)
767 {
768 CK_ATTRIBUTE_PTR attrs;
769 CK_ULONG n_attrs;
770
771 /* Dup because we're writing to this */
772 attrs = g_memdup2 (attr_template, sizeof (attr_template));
773 n_attrs = G_N_ELEMENTS (attr_template);
774
775 /* All these attributes are there */
776 g_assert (gkm_attributes_find (attrs, n_attrs, CKA_LABEL) != NULL);
777 g_assert (gkm_attributes_find (attrs, n_attrs, CKA_ID) != NULL);
778 g_assert (gkm_attributes_find (attrs, n_attrs, CKA_CLASS) != NULL);
779
780 /* Consume some of them */
781 gkm_attributes_consume (attrs, n_attrs, CKA_LABEL, CKA_ID, G_MAXULONG);
782
783 /* Two should be gone */
784 g_assert (gkm_attributes_find (attrs, n_attrs, CKA_LABEL) == NULL);
785 g_assert (gkm_attributes_find (attrs, n_attrs, CKA_ID) == NULL);
786 g_assert (gkm_attributes_find (attrs, n_attrs, CKA_CLASS) != NULL);
787
788 g_free (attrs);
789 }
790
791 static void
792 test_template_new_free (void)
793 {
794 GArray *template = gkm_template_new (attr_template, G_N_ELEMENTS (attr_template));
795 g_assert (template);
796 gkm_template_free (template);
797 }
798
799 static void
800 test_template_find (void)
801 {
802 GArray *template = gkm_template_new (attr_template, G_N_ELEMENTS (attr_template));
803 gulong uvalue;
804 gboolean ret, bvalue;
805
806 ret = gkm_template_find_ulong (template, CKA_CLASS, &uvalue);
807 g_assert (ret);
808 g_assert (uvalue == attr_template_klass);
809
810 ret = gkm_template_find_boolean (template, CKA_TOKEN, &bvalue);
811 g_assert (ret);
812 g_assert (bvalue == attr_template_token);
813
814 /* An invalid attribute */
815 ret = gkm_template_find_boolean (template, CKA_AC_ISSUER, &bvalue);
816 g_assert (!ret);
817
818 gkm_template_free (template);
819 }
820
821 static void
822 test_template_set_replace (void)
823 {
824 GArray *template = gkm_template_new (attr_template, G_N_ELEMENTS (attr_template));
825 CK_OBJECT_CLASS klass = CKO_HW_FEATURE;
826 CK_ATTRIBUTE attr = { CKA_CLASS, &klass, sizeof (klass) };
827 gulong uvalue;
828
829 if (!gkm_template_find_ulong (template, CKA_CLASS, &uvalue))
830 g_assert_not_reached ();
831 g_assert (uvalue == attr_template_klass);
832
833 /* Replace a previous attribute */
834 gkm_template_set (template, &attr);
835
836 if (!gkm_template_find_ulong (template, CKA_CLASS, &uvalue))
837 g_assert_not_reached ();
838 g_assert (uvalue == CKO_HW_FEATURE);
839
840 gkm_template_free (template);
841 }
842
843 int
844 main (int argc, char **argv)
845 {
846 #if !GLIB_CHECK_VERSION(2,35,0)
847 g_type_init ();
848 #endif
849 g_test_init (&argc, &argv, NULL);
850
851 g_test_add_func ("/gkm/attributes/attribute_equal_zero_len_null_ptr", test_attribute_equal_zero_len_null_ptr);
852 g_test_add_func ("/gkm/attributes/attribute_consume", test_attribute_consume);
853 g_test_add_func ("/gkm/attributes/attribute_consumed", test_attribute_consumed);
854 g_test_add_func ("/gkm/attributes/attribute_set_data", test_attribute_set_data);
855 g_test_add_func ("/gkm/attributes/attribute_set_data_short", test_attribute_set_data_short);
856 g_test_add_func ("/gkm/attributes/attribute_set_data_length", test_attribute_set_data_length);
857 g_test_add_func ("/gkm/attributes/attribute_set_empty", test_attribute_set_empty);
858 g_test_add_func ("/gkm/attributes/attribute_get_bool", test_attribute_get_bool);
859 g_test_add_func ("/gkm/attributes/attribute_get_bool_invalid", test_attribute_get_bool_invalid);
860 g_test_add_func ("/gkm/attributes/attribute_set_time", test_attribute_set_time);
861 g_test_add_func ("/gkm/attributes/attribute_set_time_empty", test_attribute_set_time_empty);
862 g_test_add_func ("/gkm/attributes/attribute_set_time_length", test_attribute_set_time_length);
863 g_test_add_func ("/gkm/attributes/attribute_get_time", test_attribute_get_time);
864 g_test_add_func ("/gkm/attributes/attribute_get_time_empty", test_attribute_get_time_empty);
865 g_test_add_func ("/gkm/attributes/attribute_get_time_invalid", test_attribute_get_time_invalid);
866 g_test_add_func ("/gkm/attributes/attribute_get_time_invalid_length", test_attribute_get_time_invalid_length);
867 g_test_add_func ("/gkm/attributes/attribute_get_string", test_attribute_get_string);
868 g_test_add_func ("/gkm/attributes/attribute_get_string_null", test_attribute_get_string_null);
869 g_test_add_func ("/gkm/attributes/attribute_get_string_not_utf8", test_attribute_get_string_not_utf8);
870 g_test_add_func ("/gkm/attributes/attribute_get_string_bad_pointer", test_attribute_get_string_bad_pointer);
871 g_test_add_func ("/gkm/attributes/attribute_set_bool", test_attribute_set_bool);
872 g_test_add_func ("/gkm/attributes/attribute_set_bool_short", test_attribute_set_bool_short);
873 g_test_add_func ("/gkm/attributes/attribute_set_bool_length", test_attribute_set_bool_length);
874 g_test_add_func ("/gkm/attributes/attribute_set_ulong", test_attribute_set_ulong);
875 g_test_add_func ("/gkm/attributes/attribute_set_ulong_short", test_attribute_set_ulong_short);
876 g_test_add_func ("/gkm/attributes/attribute_set_ulong_length", test_attribute_set_ulong_length);
877 g_test_add_func ("/gkm/attributes/attribute_set_string", test_attribute_set_string);
878 g_test_add_func ("/gkm/attributes/attribute_set_string_null", test_attribute_set_string_null);
879 g_test_add_func ("/gkm/attributes/attribute_set_string_short", test_attribute_set_string_short);
880 g_test_add_func ("/gkm/attributes/attribute_set_string_length", test_attribute_set_string_length);
881 g_test_add_func ("/gkm/attributes/attribute_set_date", test_attribute_set_date);
882 g_test_add_func ("/gkm/attributes/attribute_set_date_none", test_attribute_set_date_none);
883 g_test_add_func ("/gkm/attributes/attribute_set_date_short", test_attribute_set_date_short);
884 g_test_add_func ("/gkm/attributes/attribute_set_date_length", test_attribute_set_date_length);
885 g_test_add_func ("/gkm/attributes/attribute_set_mpi", test_attribute_set_mpi);
886 g_test_add_func ("/gkm/attributes/attribute_set_mpi_short", test_attribute_set_mpi_short);
887 g_test_add_func ("/gkm/attributes/attribute_set_mpi_length", test_attribute_set_mpi_length);
888 g_test_add_func ("/gkm/attributes/attribute_equal", test_attribute_equal);
889 g_test_add_func ("/gkm/attributes/attribute_equal_same", test_attribute_equal_same);
890 g_test_add_func ("/gkm/attributes/attribute_equal_same_pointer", test_attribute_equal_same_pointer);
891 g_test_add_func ("/gkm/attributes/attribute_equal_diff_types", test_attribute_equal_diff_types);
892 g_test_add_func ("/gkm/attributes/attribute_equal_diff_length", test_attribute_equal_diff_length);
893 g_test_add_func ("/gkm/attributes/attribute_equal_diff_value", test_attribute_equal_diff_value);
894 g_test_add_func ("/gkm/attributes/attribute_hash", test_attribute_hash);
895 g_test_add_func ("/gkm/attributes/attribute_contains", test_attribute_contains);
896 g_test_add_func ("/gkm/attributes/attribute_contains_no_value", test_attribute_contains_no_value);
897 g_test_add_func ("/gkm/attributes/attribute_contains_no_type", test_attribute_contains_no_type);
898 g_test_add_func ("/gkm/attributes/attributes_find", test_attributes_find);
899 g_test_add_func ("/gkm/attributes/attributes_find_not_found", test_attributes_find_not_found);
900 g_test_add_func ("/gkm/attributes/attribute_find_boolean", test_attribute_find_boolean);
901 g_test_add_func ("/gkm/attributes/attribute_find_boolean_no_type", test_attribute_find_boolean_no_type);
902 g_test_add_func ("/gkm/attributes/attribute_find_boolean_not_bbool", test_attribute_find_boolean_not_bbool);
903 g_test_add_func ("/gkm/attributes/attribute_find_ulong", test_attribute_find_ulong);
904 g_test_add_func ("/gkm/attributes/attribute_find_ulong_no_type", test_attribute_find_ulong_no_type);
905 g_test_add_func ("/gkm/attributes/attribute_find_ulong_not_ulong", test_attribute_find_ulong_not_ulong);
906 g_test_add_func ("/gkm/attributes/attribute_find_mpi", test_attribute_find_mpi);
907 g_test_add_func ("/gkm/attributes/attribute_find_mpi_no_type", test_attribute_find_mpi_no_type);
908 g_test_add_func ("/gkm/attributes/attributes_consume", test_attributes_consume);
909 g_test_add_func ("/gkm/attributes/template_new_free", test_template_new_free);
910 g_test_add_func ("/gkm/attributes/template_find", test_template_find);
911 g_test_add_func ("/gkm/attributes/template_set_replace", test_template_set_replace);
912
913 return g_test_run ();
914 }