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