Fix ossl_shim SNI handling
[openssl.git] / test / pemtest.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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
8  */
9
10 #include <string.h>
11 #include <openssl/bio.h>
12 #include <openssl/pem.h>
13
14 #include "testutil.h"
15
16 static const char raw[] = "hello world";
17 static const char encoded[] = "aGVsbG8gd29ybGQ=";
18 static const char pemtype[] = "PEMTESTDATA";
19
20 static int test_b64(void)
21 {
22     BIO *b = BIO_new(BIO_s_mem());
23     char *name = NULL, *header = NULL;
24     unsigned char *data = NULL;
25     long len;
26     int ret = 0;
27
28     if (!TEST_ptr(b)
29         || !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype))
30         || !TEST_true(BIO_printf(b, "%s\n", encoded))
31         || !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype))
32         || !TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len,
33                                       PEM_FLAG_ONLY_B64)))
34         goto err;
35     if (!TEST_int_eq(memcmp(pemtype, name, sizeof(pemtype) - 1), 0)
36         || !TEST_int_eq(len,sizeof(raw) - 1)
37         || !TEST_int_eq(memcmp(data, raw, sizeof(raw) - 1), 0))
38         goto err;
39     ret = 1;
40  err:
41     BIO_free(b);
42     OPENSSL_free(name);
43     OPENSSL_free(header);
44     OPENSSL_free(data);
45     return ret;
46 }
47
48 static int test_invalid(void)
49 {
50     BIO *b = BIO_new(BIO_s_mem());
51     char *name = NULL, *header = NULL;
52     unsigned char *data = NULL;
53     long len;
54
55     if (!TEST_ptr(b)
56         || !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype))
57         || !TEST_true(BIO_printf(b, "%c%s\n", '\t', encoded))
58         || !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype))
59         /* Expected to fail due to non-base64 character */
60         || TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len,
61                                      PEM_FLAG_ONLY_B64))) {
62         BIO_free(b);
63         return 0;
64     }
65     BIO_free(b);
66     OPENSSL_free(name);
67     OPENSSL_free(header);
68     OPENSSL_free(data);
69     return 1;
70 }
71
72 int setup_tests(void)
73 {
74     ADD_TEST(test_b64);
75     ADD_TEST(test_invalid);
76     return 1;
77 }