gck: Fix tests with p11-kit 0.13 and later
[platform/upstream/gcr.git] / gck / tests / test-gck-uri.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-gck-uri.c: Test routines for PKCS#11 URIs
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    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.
20
21    Author: Stef Walter <stefw@collabora.co.uk>
22 */
23
24 #include "config.h"
25
26 #include "gck/gck.h"
27 #include "gck/gck-private.h"
28 #include "gck/gck-test.h"
29
30 #include <glib.h>
31
32 #include <errno.h>
33 #include <string.h>
34
35 static void
36 test_parse (void)
37 {
38         GError *error = NULL;
39         GckUriData *uri_data;
40
41         uri_data = gck_uri_parse ("pkcs11:", GCK_URI_FOR_MODULE, &error);
42         g_assert (uri_data != NULL);
43         g_assert_no_error (error);
44
45         g_assert (uri_data->attributes == NULL);
46         g_assert (uri_data->token_info == NULL);
47
48         g_assert (uri_data->module_info != NULL);
49         g_assert (uri_data->module_info->library_description == NULL);
50         g_assert (uri_data->module_info->manufacturer_id == NULL);
51
52         gck_uri_data_free (uri_data);
53 }
54
55 static void
56 test_parse_bad_scheme (void)
57 {
58         GError *error = NULL;
59         GckUriData *uri_data;
60
61         uri_data = gck_uri_parse ("http:\\example.com\test", GCK_URI_FOR_ANY, &error);
62         g_assert (uri_data == NULL);
63         g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_PREFIX);
64         g_error_free (error);
65 }
66
67 static void
68 test_parse_with_label (void)
69 {
70         GError *error = NULL;
71         GckUriData *uri_data;
72         gchar *value;
73
74         uri_data = gck_uri_parse ("pkcs11:object=Test%20Label", GCK_URI_FOR_ANY, &error);
75         g_assert (uri_data != NULL);
76         g_assert (uri_data->attributes != NULL);
77
78         if (!gck_attributes_find_string (uri_data->attributes, CKA_LABEL, &value))
79                 g_assert_not_reached ();
80
81         g_assert_cmpstr (value, ==, "Test Label");
82         g_free (value);
83
84         gck_uri_data_free (uri_data);
85 }
86
87 static void
88 test_parse_with_label_and_klass (void)
89 {
90         GError *error = NULL;
91         GckUriData *uri_data;
92         gchar *value;
93         gulong klass;
94
95         uri_data = gck_uri_parse ("pkcs11:object=Test%20Label;objecttype=cert", GCK_URI_FOR_ANY, &error);
96         g_assert (uri_data);
97         g_assert (uri_data->attributes);
98
99         if (!gck_attributes_find_string (uri_data->attributes, CKA_LABEL, &value))
100                 g_assert_not_reached ();
101
102         if (!gck_attributes_find_ulong (uri_data->attributes, CKA_CLASS, &klass))
103                 g_assert_not_reached ();
104
105         g_assert_cmpstr (value, ==, "Test Label");
106         g_assert (klass == CKO_CERTIFICATE);
107         g_free (value);
108
109         gck_uri_data_free (uri_data);
110 }
111
112 static void
113 test_parse_with_id (void)
114 {
115         GError *error = NULL;
116         const GckAttribute *attr;
117         GckUriData *uri_data;
118
119         uri_data = gck_uri_parse ("pkcs11:id=%54%45%53%54%00", GCK_URI_FOR_OBJECT, &error);
120         g_assert (uri_data != NULL);
121         g_assert (uri_data->attributes != NULL);
122
123         attr = gck_attributes_find (uri_data->attributes, CKA_ID);
124         g_assert (attr);
125         g_assert (attr->value);
126         g_assert (attr->length == 5);
127         g_assert (memcmp (attr->value, "TEST", 5) == 0);
128
129         gck_uri_data_free (uri_data);
130 }
131
132 static void
133 test_parse_with_bad_string_encoding (void)
134 {
135         GError *error = NULL;
136         GckUriData *uri_data;
137
138         uri_data = gck_uri_parse ("pkcs11:object=Test%", GCK_URI_FOR_OBJECT, &error);
139         g_assert (uri_data == NULL);
140         g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_ENCODING);
141         g_error_free (error);
142 }
143
144 static void
145 test_parse_with_bad_binary_encoding (void)
146 {
147         GError *error = NULL;
148         GckUriData *uri_data;
149         uri_data = gck_uri_parse ("pkcs11:id=%%", GCK_URI_FOR_ANY, &error);
150         g_assert (!uri_data);
151         g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_ENCODING);
152         g_error_free (error);
153 }
154
155 static void
156 test_parse_with_token (void)
157 {
158         GError *error = NULL;
159         GckUriData *uri_data = NULL;
160
161         uri_data = gck_uri_parse ("pkcs11:token=Token%20Label;serial=3333;model=Deluxe;manufacturer=Me",
162                                   GCK_URI_FOR_TOKEN, &error);
163
164         g_assert (uri_data);
165         g_assert (uri_data->token_info);
166         g_assert_cmpstr (uri_data->token_info->label, ==, "Token Label");
167         g_assert_cmpstr (uri_data->token_info->serial_number, ==, "3333");
168         g_assert_cmpstr (uri_data->token_info->model, ==, "Deluxe");
169         g_assert_cmpstr (uri_data->token_info->manufacturer_id, ==, "Me");
170         gck_uri_data_free (uri_data);
171 }
172
173 static void
174 test_parse_with_token_bad_encoding (void)
175 {
176         GError *error = NULL;
177         GckUriData *uri_data;
178
179         uri_data = gck_uri_parse ("pkcs11:token=Token%", GCK_URI_FOR_TOKEN, &error);
180         g_assert (!uri_data);
181         g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_ENCODING);
182         g_error_free (error);
183 }
184
185 static void
186 test_parse_with_bad_syntax (void)
187 {
188         GError *error = NULL;
189         GckUriData *uri_data;
190
191         uri_data = gck_uri_parse ("pkcs11:token", GCK_URI_FOR_ANY, &error);
192         g_assert (uri_data == NULL);
193         g_assert (g_error_matches (error, GCK_URI_ERROR, GCK_URI_BAD_SYNTAX));
194         g_error_free (error);
195 }
196
197 static void
198 test_parse_with_library (void)
199 {
200         GError *error = NULL;
201         GckUriData *uri_data = NULL;
202
203         uri_data = gck_uri_parse ("pkcs11:library-description=The%20Library;library-manufacturer=Me",
204                                   GCK_URI_FOR_MODULE, &error);
205
206         g_assert (uri_data);
207         g_assert (uri_data->module_info);
208         g_assert_cmpstr (uri_data->module_info->manufacturer_id, ==, "Me");
209         g_assert_cmpstr (uri_data->module_info->library_description, ==, "The Library");
210         gck_uri_data_free (uri_data);
211 }
212
213 static void
214 test_parse_with_library_bad_encoding (void)
215 {
216         GError *error = NULL;
217         GckUriData *uri_data;
218
219         uri_data = gck_uri_parse ("pkcs11:library-description=Library%", GCK_URI_FOR_MODULE, &error);
220         g_assert (!uri_data);
221         g_assert_error (error, GCK_URI_ERROR, GCK_URI_BAD_ENCODING);
222         g_error_free (error);
223 }
224
225 static void
226 test_build_empty (void)
227 {
228         GckUriData uri_data;
229         gchar *uri;
230
231         memset (&uri_data, 0, sizeof (uri_data));
232         uri = gck_uri_build (&uri_data, 0);
233         g_assert_cmpstr (uri, ==, "pkcs11:");
234         g_free (uri);
235 }
236
237 static void
238 test_build_with_token_info (void)
239 {
240         gchar *uri = NULL;
241         GckUriData uri_data;
242         GckUriData *check;
243
244         memset (&uri_data, 0, sizeof (uri_data));
245         uri_data.token_info = g_new0 (GckTokenInfo, 1);
246         uri_data.token_info->label = g_strdup ("The Label");
247         uri_data.token_info->serial_number = g_strdup ("44444");
248         uri_data.token_info->manufacturer_id = g_strdup ("Me");
249         uri_data.token_info->model = g_strdup ("Deluxe");
250
251         uri = gck_uri_build (&uri_data, GCK_URI_FOR_TOKEN);
252         g_assert (uri);
253
254         check = gck_uri_parse (uri, GCK_URI_FOR_TOKEN, NULL);
255         g_assert (check);
256         g_assert (check->token_info);
257
258         g_assert (_gck_token_info_match (uri_data.token_info, check->token_info));
259
260         gck_token_info_free (uri_data.token_info);
261         gck_uri_data_free (check);
262
263         g_assert (g_str_has_prefix (uri, "pkcs11:"));
264         g_assert (strstr (uri, "token=The%20Label"));
265         g_assert (strstr (uri, "serial=44444"));
266         g_assert (strstr (uri, "manufacturer=Me"));
267         g_assert (strstr (uri, "model=Deluxe"));
268
269         g_free (uri);
270 }
271
272 static void
273 test_build_with_token_null_info (void)
274 {
275         gchar *uri = NULL;
276         GckUriData uri_data;
277
278         memset (&uri_data, 0, sizeof (uri_data));
279         uri_data.token_info = g_new0 (GckTokenInfo, 1);
280         uri_data.token_info->label = g_strdup ("The Label");
281
282         uri = gck_uri_build (&uri_data, GCK_URI_FOR_TOKEN);
283         g_assert (uri);
284
285         g_assert (g_str_has_prefix (uri, "pkcs11:"));
286         g_assert (strstr (uri, "token=The%20Label"));
287         g_assert (!strstr (uri, "serial="));
288
289         gck_token_info_free (uri_data.token_info);
290         g_free (uri);
291 }
292
293 static void
294 test_build_with_token_empty_info (void)
295 {
296         gchar *uri = NULL;
297         GckUriData uri_data;
298
299         memset (&uri_data, 0, sizeof (uri_data));
300         uri_data.token_info = g_new0 (GckTokenInfo, 1);
301         uri_data.token_info->label = g_strdup ("The Label");
302         uri_data.token_info->serial_number = g_strdup ("");
303
304         uri = gck_uri_build (&uri_data, GCK_URI_FOR_TOKEN);
305         g_assert (uri);
306
307         g_assert (g_str_has_prefix (uri, "pkcs11:"));
308         g_assert (strstr (uri, "token=The%20Label"));
309         g_assert (strstr (uri, "serial="));
310
311         gck_token_info_free (uri_data.token_info);
312         g_free (uri);
313 }
314
315 static void
316 test_build_with_attributes (void)
317 {
318         GckBuilder builder = GCK_BUILDER_INIT;
319         gchar *uri = NULL;
320         GckUriData uri_data;
321         GckUriData *check;
322         gchar *string;
323         gulong value;
324         const GckAttribute *attr;
325
326         memset (&uri_data, 0, sizeof (uri_data));
327         gck_builder_add_string (&builder, CKA_LABEL, "The Label");
328         gck_builder_add_ulong (&builder, CKA_CLASS, CKO_DATA);
329         gck_builder_add_data (&builder, CKA_ID, (const guchar *)"TEST", 5);
330         uri_data.attributes = gck_attributes_ref_sink (gck_builder_end (&builder));
331
332         uri = gck_uri_build (&uri_data, GCK_URI_FOR_OBJECT);
333         g_assert (uri);
334
335         gck_attributes_unref (uri_data.attributes);
336
337         check = gck_uri_parse (uri, GCK_URI_FOR_ANY, NULL);
338         g_assert (check);
339         g_assert (check->attributes);
340
341         if (!gck_attributes_find_string (check->attributes, CKA_LABEL, &string))
342                 g_assert_not_reached ();
343         g_assert_cmpstr (string, ==, "The Label");
344         g_free (string);
345
346         if (!gck_attributes_find_ulong (check->attributes, CKA_CLASS, &value))
347                 g_assert_not_reached ();
348         g_assert (value == CKO_DATA);
349
350         attr = gck_attributes_find (check->attributes, CKA_ID);
351         g_assert (attr);
352         g_assert (attr->length == 5);
353         g_assert (memcmp (attr->value, "TEST", 5) == 0);
354
355         gck_uri_data_free (check);
356
357         g_assert (g_str_has_prefix (uri, "pkcs11:"));
358         g_assert (strstr (uri, "object=The%20Label"));
359         g_assert (strstr (uri, "object-type=data"));
360         g_assert (strstr (uri, "id=%54%45%53%54%00") || strstr (uri, "id=TEST%00"));
361
362         g_free (uri);
363 }
364
365 static void
366 test_parse_private_key (void)
367 {
368         GckUriData *uri_data;
369         GError *error = NULL;
370         gulong klass;
371
372         uri_data = gck_uri_parse ("pkcs11:objecttype=private", GCK_URI_FOR_OBJECT, &error);
373         g_assert (uri_data);
374         g_assert_no_error (error);
375
376         g_assert (uri_data->attributes);
377         if (!gck_attributes_find_ulong (uri_data->attributes, CKA_CLASS, &klass))
378                 g_assert_not_reached ();
379         gck_assert_cmpulong (klass, ==, CKO_PRIVATE_KEY);
380
381         gck_uri_data_free (uri_data);
382 }
383
384 static void
385 test_parse_secret_key (void)
386 {
387         GckUriData *uri_data;
388         GError *error = NULL;
389         gulong klass;
390
391         uri_data = gck_uri_parse ("pkcs11:objecttype=secretkey", GCK_URI_FOR_OBJECT, &error);
392         g_assert (uri_data);
393         g_assert_no_error (error);
394
395         g_assert (uri_data->attributes);
396         if (!gck_attributes_find_ulong (uri_data->attributes, CKA_CLASS, &klass))
397                 g_assert_not_reached ();
398         gck_assert_cmpulong (klass, ==, CKO_SECRET_KEY);
399
400         gck_uri_data_free (uri_data);
401 }
402
403
404 static void
405 test_parse_unknown_objecttype (void)
406 {
407         GckUriData *uri_data;
408         GError *error = NULL;
409         gulong klass;
410
411         uri_data = gck_uri_parse ("pkcs11:objecttype=unknown", GCK_URI_FOR_OBJECT, &error);
412         g_assert (uri_data);
413         g_assert_no_error (error);
414
415         g_assert (uri_data->attributes);
416         g_assert (uri_data->any_unrecognized == TRUE);
417         if (gck_attributes_find_ulong (uri_data->attributes, CKA_CLASS, &klass))
418                 g_assert_not_reached ();
419
420         gck_uri_data_free (uri_data);
421 }
422
423 static void
424 test_build_objecttype_cert (void)
425 {
426         GckBuilder builder = GCK_BUILDER_INIT;
427         GckUriData *uri_data;
428         gchar *uri;
429
430         uri_data = gck_uri_data_new ();
431         gck_builder_add_ulong (&builder, CKA_CLASS, CKO_CERTIFICATE);
432         uri_data->attributes = gck_attributes_ref_sink (gck_builder_end (&builder));
433
434         uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT);
435         g_assert (uri);
436         g_assert (strstr (uri, "object-type=cert"));
437
438         gck_uri_data_free (uri_data);
439         g_free (uri);
440 }
441
442 static void
443 test_build_objecttype_private (void)
444 {
445         GckBuilder builder = GCK_BUILDER_INIT;
446         GckUriData *uri_data;
447         gchar *uri;
448
449         uri_data = gck_uri_data_new ();
450         gck_builder_add_ulong (&builder, CKA_CLASS, CKO_PRIVATE_KEY);
451         uri_data->attributes = gck_attributes_ref_sink (gck_builder_end (&builder));
452
453         uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT);
454         g_assert (uri);
455         g_assert (strstr (uri, "object-type=private"));
456
457         gck_uri_data_free (uri_data);
458         g_free (uri);
459 }
460
461 static void
462 test_build_objecttype_public (void)
463 {
464         GckBuilder builder = GCK_BUILDER_INIT;
465         GckUriData *uri_data;
466         gchar *uri;
467
468         uri_data = gck_uri_data_new ();
469         gck_builder_add_ulong (&builder, CKA_CLASS, CKO_PUBLIC_KEY);
470         uri_data->attributes = gck_attributes_ref_sink (gck_builder_end (&builder));
471
472         uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT);
473         g_assert (uri);
474         g_assert (strstr (uri, "object-type=public"));
475
476         gck_uri_data_free (uri_data);
477         g_free (uri);
478 }
479
480 static void
481 test_build_objecttype_secret (void)
482 {
483         GckBuilder builder = GCK_BUILDER_INIT;
484         GckUriData *uri_data;
485         gchar *uri;
486
487         uri_data = gck_uri_data_new ();
488         gck_builder_add_ulong (&builder, CKA_CLASS, CKO_SECRET_KEY);
489         uri_data->attributes = gck_attributes_ref_sink (gck_builder_end (&builder));
490
491         uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT);
492         g_assert (uri);
493         g_assert (strstr (uri, "object-type=secret-key"));
494
495         gck_uri_data_free (uri_data);
496         g_free (uri);
497 }
498
499 static void
500 test_build_with_library (void)
501 {
502         GckUriData *uri_data;
503         gchar *uri;
504
505         uri_data = gck_uri_data_new ();
506         uri_data->module_info = g_new0 (GckModuleInfo, 1);
507         uri_data->module_info->library_description = g_strdup ("The Description");
508
509         uri = gck_uri_build (uri_data, GCK_URI_FOR_MODULE);
510         g_assert (uri);
511         g_assert (strstr (uri, "library-description=The%20Description"));
512
513         gck_uri_data_free (uri_data);
514         g_free (uri);
515 }
516
517
518 static void
519 null_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
520                   const gchar *message, gpointer user_data)
521 {
522
523 }
524
525 int
526 main (int argc, char **argv)
527 {
528         g_type_init ();
529         g_test_init (&argc, &argv, NULL);
530
531         /* Suppress these messages in tests */
532         g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG,
533                            null_log_handler, NULL);
534
535         g_test_add_func ("/gck/uri/parse", test_parse);
536         g_test_add_func ("/gck/uri/parse_bad_scheme", test_parse_bad_scheme);
537         g_test_add_func ("/gck/uri/parse_with_label", test_parse_with_label);
538         g_test_add_func ("/gck/uri/parse_with_label_and_klass", test_parse_with_label_and_klass);
539         g_test_add_func ("/gck/uri/parse_with_id", test_parse_with_id);
540         g_test_add_func ("/gck/uri/parse_with_bad_string_encoding", test_parse_with_bad_string_encoding);
541         g_test_add_func ("/gck/uri/parse_with_bad_binary_encoding", test_parse_with_bad_binary_encoding);
542         g_test_add_func ("/gck/uri/parse_with_token", test_parse_with_token);
543         g_test_add_func ("/gck/uri/parse_with_token_bad_encoding", test_parse_with_token_bad_encoding);
544         g_test_add_func ("/gck/uri/parse_with_bad_syntax", test_parse_with_bad_syntax);
545         g_test_add_func ("/gck/uri/parse_with_library", test_parse_with_library);
546         g_test_add_func ("/gck/uri/parse_with_library_bad_encoding", test_parse_with_library_bad_encoding);
547         g_test_add_func ("/gck/uri/build_empty", test_build_empty);
548         g_test_add_func ("/gck/uri/build_with_token_info", test_build_with_token_info);
549         g_test_add_func ("/gck/uri/build_with_token_null_info", test_build_with_token_null_info);
550         g_test_add_func ("/gck/uri/build_with_token_empty_info", test_build_with_token_empty_info);
551         g_test_add_func ("/gck/uri/build_with_attributes", test_build_with_attributes);
552         g_test_add_func ("/gck/uri/parse_private_key", test_parse_private_key);
553         g_test_add_func ("/gck/uri/parse_secret_key", test_parse_secret_key);
554         g_test_add_func ("/gck/uri/parse_unknown_objecttype", test_parse_unknown_objecttype);
555         g_test_add_func ("/gck/uri/build_objecttype_cert", test_build_objecttype_cert);
556         g_test_add_func ("/gck/uri/build_objecttype_private", test_build_objecttype_private);
557         g_test_add_func ("/gck/uri/build_objecttype_public", test_build_objecttype_public);
558         g_test_add_func ("/gck/uri/build_objecttype_secret", test_build_objecttype_secret);
559         g_test_add_func ("/gck/uri/build_with_library", test_build_with_library);
560
561         return g_test_run ();
562 }