]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/pemtest.c
2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/bio.h>
12 #include <openssl/pem.h>
15 #include "internal/nelem.h"
22 static TESTDATA b64_pem_data
[] = {
25 { "a very ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong input",
26 "YSB2ZXJ5IG9vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29uZyBpbnB1dA==" }
29 static const char *pemtype
= "PEMTESTDATA";
33 static int test_b64(int idx
)
35 BIO
*b
= BIO_new(BIO_s_mem());
36 char *name
= NULL
, *header
= NULL
;
37 unsigned char *data
= NULL
;
40 const char *raw
= b64_pem_data
[idx
].raw
;
41 const char *encoded
= b64_pem_data
[idx
].encoded
;
44 || !TEST_true(BIO_printf(b
, "-----BEGIN %s-----\n", pemtype
))
45 || !TEST_true(BIO_printf(b
, "%s\n", encoded
))
46 || !TEST_true(BIO_printf(b
, "-----END %s-----\n", pemtype
))
47 || !TEST_true(PEM_read_bio_ex(b
, &name
, &header
, &data
, &len
,
50 if (!TEST_int_eq(memcmp(pemtype
, name
, strlen(pemtype
)), 0)
51 || !TEST_int_eq(len
, strlen(raw
))
52 || !TEST_int_eq(memcmp(data
, raw
, strlen(raw
)), 0))
63 static int test_invalid(void)
65 BIO
*b
= BIO_new(BIO_s_mem());
66 char *name
= NULL
, *header
= NULL
;
67 unsigned char *data
= NULL
;
69 const char *encoded
= b64_pem_data
[0].encoded
;
72 || !TEST_true(BIO_printf(b
, "-----BEGIN %s-----\n", pemtype
))
73 || !TEST_true(BIO_printf(b
, "%c%s\n", '\t', encoded
))
74 || !TEST_true(BIO_printf(b
, "-----END %s-----\n", pemtype
))
75 /* Expected to fail due to non-base64 character */
76 || TEST_true(PEM_read_bio_ex(b
, &name
, &header
, &data
, &len
,
77 PEM_FLAG_ONLY_B64
))) {
88 static int test_cert_key_cert(void)
92 if (!TEST_ptr(key
= load_pkey_pem(pemfile
, NULL
)))
101 if (!TEST_ptr(pemfile
= test_get_argument(0)))
103 ADD_ALL_TESTS(test_b64
, OSSL_NELEM(b64_pem_data
));
104 ADD_TEST(test_invalid
);
105 ADD_TEST(test_cert_key_cert
);