1 /* GLib testing framework examples and tests
3 * Copyright (C) 2011 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
25 #include "gtesttlsbackend.h"
35 pem_parser (const Reference
*ref
)
37 GTlsCertificate
*cert
;
39 gchar
*parsed_cert_pem
= NULL
;
40 const gchar
*parsed_key_pem
= NULL
;
43 /* Check PEM parsing in certificate, private key order. */
44 g_file_get_contents (g_test_get_filename (G_TEST_DIST
, "cert-tests", "cert-key.pem", NULL
), &pem
, NULL
, &error
);
45 g_assert_no_error (error
);
48 cert
= g_tls_certificate_new_from_pem (pem
, -1, &error
);
49 g_assert_no_error (error
);
53 "certificate-pem", &parsed_cert_pem
,
55 parsed_key_pem
= g_test_tls_connection_get_private_key_pem (cert
);
56 g_assert_cmpstr (parsed_cert_pem
, ==, ref
->cert_pems
[0]);
57 g_free (parsed_cert_pem
);
58 parsed_cert_pem
= NULL
;
59 g_assert_cmpstr (parsed_key_pem
, ==, ref
->key_pem
);
60 parsed_key_pem
= NULL
;
62 g_object_unref (cert
);
64 /* Make sure length is respected and parser detect invalid (truncated) PEM. */
65 cert
= g_tls_certificate_new_from_pem (pem
, 10, &error
);
66 g_assert_error (error
, G_TLS_ERROR
, G_TLS_ERROR_BAD_CERTIFICATE
);
67 g_clear_error (&error
);
70 /* Check PEM parsing in private key, certificate order */
71 g_file_get_contents (g_test_get_filename (G_TEST_DIST
, "cert-tests", "key-cert.pem", NULL
), &pem
, NULL
, &error
);
72 g_assert_no_error (error
);
75 cert
= g_tls_certificate_new_from_pem (pem
, -1, &error
);
76 g_assert_no_error (error
);
80 "certificate-pem", &parsed_cert_pem
,
82 parsed_key_pem
= g_test_tls_connection_get_private_key_pem (cert
);
83 g_assert_cmpstr (parsed_cert_pem
, ==, ref
->cert_pems
[0]);
84 g_free (parsed_cert_pem
);
85 parsed_cert_pem
= NULL
;
86 g_assert_cmpstr (parsed_key_pem
, ==, ref
->key_pem
);
87 parsed_key_pem
= NULL
;
90 g_object_unref (cert
);
92 /* Check certificate only PEM */
93 g_file_get_contents (g_test_get_filename (G_TEST_DIST
, "cert-tests", "cert1.pem", NULL
), &pem
, NULL
, &error
);
94 g_assert_no_error (error
);
97 cert
= g_tls_certificate_new_from_pem (pem
, -1, &error
);
98 g_assert_no_error (error
);
102 "certificate-pem", &parsed_cert_pem
,
104 parsed_key_pem
= g_test_tls_connection_get_private_key_pem (cert
);
105 g_assert_cmpstr (parsed_cert_pem
, ==, ref
->cert_pems
[0]);
106 g_free (parsed_cert_pem
);
107 parsed_cert_pem
= NULL
;
108 g_assert (parsed_key_pem
== NULL
);
111 g_object_unref (cert
);
113 /* Check error with private key only PEM */
114 g_file_get_contents (g_test_get_filename (G_TEST_DIST
, "cert-tests", "key.pem", NULL
), &pem
, NULL
, &error
);
115 g_assert_no_error (error
);
118 cert
= g_tls_certificate_new_from_pem (pem
, -1, &error
);
119 g_assert_error (error
, G_TLS_ERROR
, G_TLS_ERROR_BAD_CERTIFICATE
);
120 g_clear_error (&error
);
121 g_assert (cert
== NULL
);
126 pem_parser_handles_chain (const Reference
*ref
)
128 GTlsCertificate
*cert
;
129 GTlsCertificate
*issuer
;
130 GTlsCertificate
*original_cert
;
132 gchar
*parsed_cert_pem
= NULL
;
133 const gchar
*parsed_key_pem
= NULL
;
134 GError
*error
= NULL
;
136 /* Check that a chain with exactly three certificates is returned */
137 g_file_get_contents (g_test_get_filename (G_TEST_DIST
, "cert-tests", "cert-list.pem", NULL
), &pem
, NULL
, &error
);
138 g_assert_no_error (error
);
141 cert
= original_cert
= g_tls_certificate_new_from_pem (pem
, -1, &error
);
143 g_assert_no_error (error
);
147 "certificate-pem", &parsed_cert_pem
,
149 g_assert_cmpstr (parsed_cert_pem
, ==, ref
->cert_pems
[0]);
150 g_clear_pointer (&parsed_cert_pem
, g_free
);
152 /* Make sure the private key was parsed */
153 parsed_key_pem
= g_test_tls_connection_get_private_key_pem (cert
);
154 g_assert_cmpstr (parsed_key_pem
, ==, ref
->key_pem
);
155 parsed_key_pem
= NULL
;
157 /* Now test the second cert */
158 issuer
= g_tls_certificate_get_issuer (cert
);
162 issuer
= g_tls_certificate_get_issuer (cert
);
166 "certificate-pem", &parsed_cert_pem
,
168 g_assert_cmpstr (parsed_cert_pem
, ==, ref
->cert_pems
[1]);
169 g_clear_pointer (&parsed_cert_pem
, g_free
);
171 /* Only the first cert should have a private key */
172 parsed_key_pem
= g_test_tls_connection_get_private_key_pem (cert
);
173 g_assert (!parsed_key_pem
);
175 /* Now test the final cert */
177 issuer
= g_tls_certificate_get_issuer (cert
);
181 "certificate-pem", &parsed_cert_pem
,
183 g_assert_cmpstr (parsed_cert_pem
, ==, ref
->cert_pems
[2]);
184 g_clear_pointer (&parsed_cert_pem
, g_free
);
186 parsed_key_pem
= g_test_tls_connection_get_private_key_pem (cert
);
187 g_assert (!parsed_key_pem
);
189 g_object_unref (original_cert
);
193 from_file (const Reference
*ref
)
195 GTlsCertificate
*cert
;
196 gchar
*parsed_cert_pem
= NULL
;
197 const gchar
*parsed_key_pem
= NULL
;
198 GError
*error
= NULL
;
200 cert
= g_tls_certificate_new_from_file (g_test_get_filename (G_TEST_DIST
, "cert-tests", "key-cert.pem", NULL
),
202 g_assert_no_error (error
);
206 "certificate-pem", &parsed_cert_pem
,
208 parsed_key_pem
= g_test_tls_connection_get_private_key_pem (cert
);
209 g_assert_cmpstr (parsed_cert_pem
, ==, ref
->cert_pems
[0]);
210 g_free (parsed_cert_pem
);
211 parsed_cert_pem
= NULL
;
212 g_assert_cmpstr (parsed_key_pem
, ==, ref
->key_pem
);
213 parsed_key_pem
= NULL
;
215 g_object_unref (cert
);
219 from_files (const Reference
*ref
)
221 GTlsCertificate
*cert
;
222 gchar
*parsed_cert_pem
= NULL
;
223 const gchar
*parsed_key_pem
= NULL
;
224 GError
*error
= NULL
;
226 cert
= g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST
, "cert-tests", "cert1.pem", NULL
),
227 g_test_get_filename (G_TEST_DIST
, "cert-tests", "key.pem", NULL
),
229 g_assert_no_error (error
);
233 "certificate-pem", &parsed_cert_pem
,
235 parsed_key_pem
= g_test_tls_connection_get_private_key_pem (cert
);
236 g_assert_cmpstr (parsed_cert_pem
, ==, ref
->cert_pems
[0]);
237 g_free (parsed_cert_pem
);
238 parsed_cert_pem
= NULL
;
239 g_assert_cmpstr (parsed_key_pem
, ==, ref
->key_pem
);
240 parsed_key_pem
= NULL
;
242 g_object_unref (cert
);
244 /* Missing private key */
245 cert
= g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST
, "cert-tests", "cert1.pem", NULL
),
246 g_test_get_filename (G_TEST_DIST
, "cert-tests", "cert2.pem", NULL
),
248 g_assert_error (error
, G_TLS_ERROR
, G_TLS_ERROR_BAD_CERTIFICATE
);
249 g_clear_error (&error
);
250 g_assert (cert
== NULL
);
252 /* Missing certificate */
253 cert
= g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST
, "cert-tests", "key.pem", NULL
),
254 g_test_get_filename (G_TEST_DIST
, "cert-tests", "key.pem", NULL
),
256 g_assert_error (error
, G_TLS_ERROR
, G_TLS_ERROR_BAD_CERTIFICATE
);
257 g_clear_error (&error
);
258 g_assert (cert
== NULL
);
260 /* Using this method twice with a file containing both private key and
261 * certificate as a way to inforce private key presence is a fair use
263 cert
= g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST
, "cert-tests", "key-cert.pem", NULL
),
264 g_test_get_filename (G_TEST_DIST
, "cert-tests", "key-cert.pem", NULL
),
266 g_assert_no_error (error
);
268 g_object_unref (cert
);
273 from_files_pkcs8 (const Reference
*ref
)
275 GTlsCertificate
*cert
;
276 gchar
*parsed_cert_pem
= NULL
;
277 const gchar
*parsed_key_pem
= NULL
;
278 GError
*error
= NULL
;
280 cert
= g_tls_certificate_new_from_files (g_test_get_filename (G_TEST_DIST
, "cert-tests", "cert1.pem", NULL
),
281 g_test_get_filename (G_TEST_DIST
, "cert-tests", "key8.pem", NULL
),
283 g_assert_no_error (error
);
287 "certificate-pem", &parsed_cert_pem
,
289 parsed_key_pem
= g_test_tls_connection_get_private_key_pem (cert
);
290 g_assert_cmpstr (parsed_cert_pem
, ==, ref
->cert_pems
[0]);
291 g_free (parsed_cert_pem
);
292 parsed_cert_pem
= NULL
;
293 g_assert_cmpstr (parsed_key_pem
, ==, ref
->key8_pem
);
294 parsed_key_pem
= NULL
;
296 g_object_unref (cert
);
300 list_from_file (const Reference
*ref
)
303 GError
*error
= NULL
;
306 list
= g_tls_certificate_list_new_from_file (g_test_get_filename (G_TEST_DIST
, "cert-tests", "cert-list.pem", NULL
),
308 g_assert_no_error (error
);
309 g_assert_cmpint (g_list_length (list
), ==, 3);
312 for (i
= 0; i
< 3; i
++)
314 GTlsCertificate
*cert
= l
->data
;
315 gchar
*parsed_cert_pem
= NULL
;
317 "certificate-pem", &parsed_cert_pem
,
319 g_assert_cmpstr (parsed_cert_pem
, ==, ref
->cert_pems
[i
]);
320 g_free (parsed_cert_pem
);
324 g_list_free_full (list
, g_object_unref
);
326 /* Empty list is not an error */
327 list
= g_tls_certificate_list_new_from_file (g_test_get_filename (G_TEST_DIST
, "cert-tests", "nothing.pem", NULL
),
329 g_assert_no_error (error
);
330 g_assert_cmpint (g_list_length (list
), ==, 0);
339 GError
*error
= NULL
;
342 g_test_init (&argc
, &argv
, NULL
);
344 _g_test_tls_backend_get_type ();
346 /* Load reference PEM */
347 path
= g_test_build_filename (G_TEST_DIST
, "cert-tests", "cert1.pem", NULL
);
348 g_file_get_contents (path
, &ref
.cert_pems
[0], NULL
, &error
);
349 g_assert_no_error (error
);
350 g_assert (ref
.cert_pems
[0]);
352 path
= g_test_build_filename (G_TEST_DIST
, "cert-tests", "cert2.pem", NULL
);
353 g_file_get_contents (path
, &ref
.cert_pems
[1], NULL
, &error
);
354 g_assert_no_error (error
);
355 g_assert (ref
.cert_pems
[1]);
357 path
= g_test_build_filename (G_TEST_DIST
, "cert-tests", "cert3.pem", NULL
);
358 g_file_get_contents (path
, &ref
.cert_pems
[2], NULL
, &error
);
359 g_assert_no_error (error
);
360 g_assert (ref
.cert_pems
[2]);
362 path
= g_test_build_filename (G_TEST_DIST
, "cert-tests", "key.pem", NULL
);
363 g_file_get_contents (path
, &ref
.key_pem
, NULL
, &error
);
364 g_assert_no_error (error
);
365 g_assert (ref
.key_pem
);
367 path
= g_test_build_filename (G_TEST_DIST
, "cert-tests", "key8.pem", NULL
);
368 g_file_get_contents (path
, &ref
.key8_pem
, NULL
, &error
);
369 g_assert_no_error (error
);
370 g_assert (ref
.key8_pem
);
373 g_test_add_data_func ("/tls-certificate/pem-parser",
374 &ref
, (GTestDataFunc
)pem_parser
);
375 g_test_add_data_func ("/tls-certificate/pem-parser-handles-chain",
376 &ref
, (GTestDataFunc
)pem_parser_handles_chain
);
377 g_test_add_data_func ("/tls-certificate/from_file",
378 &ref
, (GTestDataFunc
)from_file
);
379 g_test_add_data_func ("/tls-certificate/from_files",
380 &ref
, (GTestDataFunc
)from_files
);
381 g_test_add_data_func ("/tls-certificate/from_files_pkcs8",
382 &ref
, (GTestDataFunc
)from_files_pkcs8
);
383 g_test_add_data_func ("/tls-certificate/list_from_file",
384 &ref
, (GTestDataFunc
)list_from_file
);
388 g_free (ref
.cert_pems
[0]);
389 g_free (ref
.cert_pems
[1]);
390 g_free (ref
.cert_pems
[2]);
391 g_free (ref
.key_pem
);
392 g_free (ref
.key8_pem
);