2 * Copyright (c) 2011, Collabora Ltd.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * * Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the
10 * following disclaimer.
11 * * Redistributions in binary form must reproduce the
12 * above copyright notice, this list of conditions and
13 * the following disclaimer in the documentation and/or
14 * other materials provided with the distribution.
15 * * The names of contributors to this software may not be
16 * used to endorse or promote products derived from this
17 * software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32 * Author: Stef Walter <stefw@collabora.co.uk>
58 CK_FUNCTION_LIST module;
69 memcpy (&test.module, &mock_module, sizeof (CK_FUNCTION_LIST));
70 rv = p11_kit_initialize_module (&test.module);
71 CuAssertIntEquals (tc, CKR_OK, rv);
73 mock_module_reset_objects (MOCK_SLOT_ONE_ID);
75 test.iter = p11_kit_iter_new (NULL);
77 p11_extract_info_init (&test.ex);
79 test.directory = p11_path_expand ("$TEMP/test-extract.XXXXXX");
80 if (!mkdtemp (test.directory))
81 CuFail (tc, "mkdtemp() failed");
89 if (rmdir (test.directory) < 0)
90 CuFail (tc, "rmdir() failed");
91 free (test.directory);
93 p11_extract_info_cleanup (&test.ex);
94 p11_kit_iter_free (test.iter);
96 rv = p11_kit_finalize_module (&test.module);
97 CuAssertIntEquals (tc, CKR_OK, rv);
100 static CK_OBJECT_CLASS certificate_class = CKO_CERTIFICATE;
101 static CK_CERTIFICATE_TYPE x509_type = CKC_X_509;
103 static CK_ATTRIBUTE cacert3_authority_attrs[] = {
104 { CKA_VALUE, (void *)test_cacert3_ca_der, sizeof (test_cacert3_ca_der) },
105 { CKA_CLASS, &certificate_class, sizeof (certificate_class) },
106 { CKA_CERTIFICATE_TYPE, &x509_type, sizeof (x509_type) },
107 { CKA_LABEL, "Cacert3 Here", 12 },
108 { CKA_SUBJECT, (void *)test_cacert3_ca_subject, sizeof (test_cacert3_ca_subject) },
109 { CKA_ID, "ID1", 3 },
113 static CK_ATTRIBUTE certificate_filter[] = {
114 { CKA_CLASS, &certificate_class, sizeof (certificate_class) },
119 test_file (CuTest *tc)
125 mock_module_add_object (MOCK_SLOT_ONE_ID, cacert3_authority_attrs);
127 p11_kit_iter_add_callback (test.iter, p11_extract_info_load_filter, &test.ex, NULL);
128 p11_kit_iter_add_filter (test.iter, certificate_filter, 1);
129 p11_kit_iter_begin_with (test.iter, &test.module, 0, 0);
131 if (asprintf (&test.ex.destination, "%s/%s", test.directory, "extract.cer") < 0)
132 assert_not_reached ();
134 ret = p11_extract_x509_file (test.iter, &test.ex);
135 CuAssertIntEquals (tc, true, ret);
137 test_check_file (tc, test.directory, "extract.cer", SRCDIR "/files/cacert3.der");
139 free (test.ex.destination);
144 test_file_multiple (CuTest *tc)
150 mock_module_add_object (MOCK_SLOT_ONE_ID, cacert3_authority_attrs);
151 mock_module_add_object (MOCK_SLOT_ONE_ID, cacert3_authority_attrs);
153 p11_kit_iter_add_callback (test.iter, p11_extract_info_load_filter, &test.ex, NULL);
154 p11_kit_iter_add_filter (test.iter, certificate_filter, 1);
155 p11_kit_iter_begin_with (test.iter, &test.module, 0, 0);
157 if (asprintf (&test.ex.destination, "%s/%s", test.directory, "extract.cer") < 0)
158 assert_not_reached ();
160 p11_message_quiet ();
162 ret = p11_extract_x509_file (test.iter, &test.ex);
163 CuAssertIntEquals (tc, true, ret);
165 CuAssertTrue (tc, strstr (p11_message_last (), "multiple certificates") != NULL);
169 test_check_file (tc, test.directory, "extract.cer", SRCDIR "/files/cacert3.der");
171 free (test.ex.destination);
176 test_file_without (CuTest *tc)
182 p11_kit_iter_add_callback (test.iter, p11_extract_info_load_filter, &test.ex, NULL);
183 p11_kit_iter_add_filter (test.iter, certificate_filter, 1);
184 p11_kit_iter_begin_with (test.iter, &test.module, 0, 0);
186 if (asprintf (&test.ex.destination, "%s/%s", test.directory, "extract.cer") < 0)
187 assert_not_reached ();
189 p11_message_quiet ();
191 ret = p11_extract_x509_file (test.iter, &test.ex);
192 CuAssertIntEquals (tc, false, ret);
194 CuAssertTrue (tc, strstr (p11_message_last (), "no certificate") != NULL);
198 free (test.ex.destination);
203 test_directory (CuTest *tc)
209 mock_module_add_object (MOCK_SLOT_ONE_ID, cacert3_authority_attrs);
210 mock_module_add_object (MOCK_SLOT_ONE_ID, cacert3_authority_attrs);
212 p11_kit_iter_add_callback (test.iter, p11_extract_info_load_filter, &test.ex, NULL);
213 p11_kit_iter_add_filter (test.iter, certificate_filter, 1);
214 p11_kit_iter_begin_with (test.iter, &test.module, 0, 0);
216 /* Yes, this is a race, and why you shouldn't build software as root */
217 if (rmdir (test.directory) < 0)
218 assert_not_reached ();
219 test.ex.destination = test.directory;
221 ret = p11_extract_x509_directory (test.iter, &test.ex);
222 CuAssertIntEquals (tc, true, ret);
224 test_check_directory (tc, test.directory, ("Cacert3_Here.cer", "Cacert3_Here.1.cer", NULL));
225 test_check_file (tc, test.directory, "Cacert3_Here.cer", SRCDIR "/files/cacert3.der");
226 test_check_file (tc, test.directory, "Cacert3_Here.1.cer", SRCDIR "/files/cacert3.der");
232 test_directory_empty (CuTest *tc)
238 p11_kit_iter_add_callback (test.iter, p11_extract_info_load_filter, &test.ex, NULL);
239 p11_kit_iter_add_filter (test.iter, certificate_filter, 1);
240 p11_kit_iter_begin_with (test.iter, &test.module, 0, 0);
242 /* Yes, this is a race, and why you shouldn't build software as root */
243 if (rmdir (test.directory) < 0)
244 assert_not_reached ();
245 test.ex.destination = test.directory;
247 ret = p11_extract_x509_directory (test.iter, &test.ex);
248 CuAssertIntEquals (tc, true, ret);
250 test_check_directory (tc, test.directory, (NULL, NULL));
258 CuString *output = CuStringNew ();
259 CuSuite* suite = CuSuiteNew ();
262 putenv ("P11_KIT_STRICT=1");
266 SUITE_ADD_TEST (suite, test_file);
267 SUITE_ADD_TEST (suite, test_file_multiple);
268 SUITE_ADD_TEST (suite, test_file_without);
269 SUITE_ADD_TEST (suite, test_directory);
270 SUITE_ADD_TEST (suite, test_directory_empty);
273 CuSuiteSummary (suite, output);
274 CuSuiteDetails (suite, output);
275 printf ("%s\n", output->buffer);
276 ret = suite->failCount;
277 CuSuiteDelete (suite);
278 CuStringDelete (output);