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
4 Copyright (C) 2011, Collabora Ltd.
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.
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.
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.
21 Author: Stef Walter <stefw@collabora.co.uk>
27 #include "gck/gck-private.h"
28 #include "gck/gck-test.h"
41 uri_data = gck_uri_parse ("pkcs11:", GCK_URI_FOR_MODULE, &error);
42 g_assert (uri_data != NULL);
43 g_assert_no_error (error);
45 g_assert (uri_data->attributes == NULL);
46 g_assert (uri_data->token_info == NULL);
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);
52 gck_uri_data_free (uri_data);
56 test_parse_bad_scheme (void)
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);
68 test_parse_with_label (void)
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);
78 if (!gck_attributes_find_string (uri_data->attributes, CKA_LABEL, &value))
79 g_assert_not_reached ();
81 g_assert_cmpstr (value, ==, "Test Label");
84 gck_uri_data_free (uri_data);
88 test_parse_with_label_and_klass (void)
95 uri_data = gck_uri_parse ("pkcs11:object=Test%20Label;objecttype=cert", GCK_URI_FOR_ANY, &error);
97 g_assert (uri_data->attributes);
99 if (!gck_attributes_find_string (uri_data->attributes, CKA_LABEL, &value))
100 g_assert_not_reached ();
102 if (!gck_attributes_find_ulong (uri_data->attributes, CKA_CLASS, &klass))
103 g_assert_not_reached ();
105 g_assert_cmpstr (value, ==, "Test Label");
106 g_assert (klass == CKO_CERTIFICATE);
109 gck_uri_data_free (uri_data);
113 test_parse_with_id (void)
115 GError *error = NULL;
116 const GckAttribute *attr;
117 GckUriData *uri_data;
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);
123 attr = gck_attributes_find (uri_data->attributes, CKA_ID);
125 g_assert (attr->value);
126 g_assert (attr->length == 5);
127 g_assert (memcmp (attr->value, "TEST", 5) == 0);
129 gck_uri_data_free (uri_data);
133 test_parse_with_bad_string_encoding (void)
135 GError *error = NULL;
136 GckUriData *uri_data;
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);
145 test_parse_with_bad_binary_encoding (void)
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);
156 test_parse_with_token (void)
158 GError *error = NULL;
159 GckUriData *uri_data = NULL;
161 uri_data = gck_uri_parse ("pkcs11:token=Token%20Label;serial=3333;model=Deluxe;manufacturer=Me",
162 GCK_URI_FOR_TOKEN, &error);
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);
174 test_parse_with_token_bad_encoding (void)
176 GError *error = NULL;
177 GckUriData *uri_data;
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);
186 test_parse_with_bad_syntax (void)
188 GError *error = NULL;
189 GckUriData *uri_data;
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);
198 test_parse_with_library (void)
200 GError *error = NULL;
201 GckUriData *uri_data = NULL;
203 uri_data = gck_uri_parse ("pkcs11:library-description=The%20Library;library-manufacturer=Me",
204 GCK_URI_FOR_MODULE, &error);
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);
214 test_parse_with_library_bad_encoding (void)
216 GError *error = NULL;
217 GckUriData *uri_data;
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);
226 test_build_empty (void)
231 memset (&uri_data, 0, sizeof (uri_data));
232 uri = gck_uri_build (&uri_data, 0);
233 g_assert_cmpstr (uri, ==, "pkcs11:");
238 test_build_with_token_info (void)
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");
251 uri = gck_uri_build (&uri_data, GCK_URI_FOR_TOKEN);
254 check = gck_uri_parse (uri, GCK_URI_FOR_TOKEN, NULL);
256 g_assert (check->token_info);
258 g_assert (_gck_token_info_match (uri_data.token_info, check->token_info));
260 gck_token_info_free (uri_data.token_info);
261 gck_uri_data_free (check);
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"));
273 test_build_with_token_null_info (void)
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");
282 uri = gck_uri_build (&uri_data, GCK_URI_FOR_TOKEN);
285 g_assert (g_str_has_prefix (uri, "pkcs11:"));
286 g_assert (strstr (uri, "token=The%20Label"));
287 g_assert (!strstr (uri, "serial="));
289 gck_token_info_free (uri_data.token_info);
294 test_build_with_token_empty_info (void)
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 ("");
304 uri = gck_uri_build (&uri_data, GCK_URI_FOR_TOKEN);
307 g_assert (g_str_has_prefix (uri, "pkcs11:"));
308 g_assert (strstr (uri, "token=The%20Label"));
309 g_assert (strstr (uri, "serial="));
311 gck_token_info_free (uri_data.token_info);
316 test_build_with_attributes (void)
318 GckBuilder builder = GCK_BUILDER_INIT;
324 const GckAttribute *attr;
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));
332 uri = gck_uri_build (&uri_data, GCK_URI_FOR_OBJECT);
335 gck_attributes_unref (uri_data.attributes);
337 check = gck_uri_parse (uri, GCK_URI_FOR_ANY, NULL);
339 g_assert (check->attributes);
341 if (!gck_attributes_find_string (check->attributes, CKA_LABEL, &string))
342 g_assert_not_reached ();
343 g_assert_cmpstr (string, ==, "The Label");
346 if (!gck_attributes_find_ulong (check->attributes, CKA_CLASS, &value))
347 g_assert_not_reached ();
348 g_assert (value == CKO_DATA);
350 attr = gck_attributes_find (check->attributes, CKA_ID);
352 g_assert (attr->length == 5);
353 g_assert (memcmp (attr->value, "TEST", 5) == 0);
355 gck_uri_data_free (check);
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=TEST%00"));
366 test_parse_private_key (void)
368 GckUriData *uri_data;
369 GError *error = NULL;
372 uri_data = gck_uri_parse ("pkcs11:objecttype=private", GCK_URI_FOR_OBJECT, &error);
374 g_assert_no_error (error);
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);
381 gck_uri_data_free (uri_data);
385 test_parse_secret_key (void)
387 GckUriData *uri_data;
388 GError *error = NULL;
391 uri_data = gck_uri_parse ("pkcs11:objecttype=secretkey", GCK_URI_FOR_OBJECT, &error);
393 g_assert_no_error (error);
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);
400 gck_uri_data_free (uri_data);
405 test_parse_unknown_objecttype (void)
407 GckUriData *uri_data;
408 GError *error = NULL;
411 uri_data = gck_uri_parse ("pkcs11:objecttype=unknown", GCK_URI_FOR_OBJECT, &error);
413 g_assert_no_error (error);
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 ();
420 gck_uri_data_free (uri_data);
424 test_build_objecttype_cert (void)
426 GckBuilder builder = GCK_BUILDER_INIT;
427 GckUriData *uri_data;
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));
434 uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT);
436 g_assert (strstr (uri, "object-type=cert"));
438 gck_uri_data_free (uri_data);
443 test_build_objecttype_private (void)
445 GckBuilder builder = GCK_BUILDER_INIT;
446 GckUriData *uri_data;
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));
453 uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT);
455 g_assert (strstr (uri, "object-type=private"));
457 gck_uri_data_free (uri_data);
462 test_build_objecttype_public (void)
464 GckBuilder builder = GCK_BUILDER_INIT;
465 GckUriData *uri_data;
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));
472 uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT);
474 g_assert (strstr (uri, "object-type=public"));
476 gck_uri_data_free (uri_data);
481 test_build_objecttype_secret (void)
483 GckBuilder builder = GCK_BUILDER_INIT;
484 GckUriData *uri_data;
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));
491 uri = gck_uri_build (uri_data, GCK_URI_FOR_OBJECT);
493 g_assert (strstr (uri, "object-type=secret-key"));
495 gck_uri_data_free (uri_data);
500 test_build_with_library (void)
502 GckUriData *uri_data;
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");
509 uri = gck_uri_build (uri_data, GCK_URI_FOR_MODULE);
511 g_assert (strstr (uri, "library-description=The%20Description"));
513 gck_uri_data_free (uri_data);
519 null_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
520 const gchar *message, gpointer user_data)
526 main (int argc, char **argv)
529 g_test_init (&argc, &argv, NULL);
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);
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);
561 return g_test_run ();