From fa3ed5b2c2b508a6444124fdf12ecbb4898007ed Mon Sep 17 00:00:00 2001 From: Benjamin Kaduk Date: Wed, 12 Apr 2017 16:24:43 -0500 Subject: [PATCH] Add unit test for PEM_FLAG_ONLY_B64 Get some trivial test coverage that this flag does what it claims to. [extended tests] Reviewed-by: Rich Salz Reviewed-by: Richard Levitte (Merged from https://github.com/openssl/openssl/pull/1700) --- test/build.info | 6 ++- test/pemtest.c | 76 ++++++++++++++++++++++++++++++++++++++ test/recipes/04-test_pem.t | 4 +- 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 test/pemtest.c diff --git a/test/build.info b/test/build.info index 94a7402da1..bd80b18fee 100644 --- a/test/build.info +++ b/test/build.info @@ -43,7 +43,7 @@ INCLUDE_MAIN___test_libtestutil_OLB = /INCLUDE=MAIN bioprinttest sslapitest dtlstest sslcorrupttest bio_enc_test \ pkey_meth_test uitest cipherbytes_test asn1_encode_test \ x509_time_test x509_dup_cert_test recordlentest \ - time_offset_test + time_offset_test pemtest SOURCE[aborttest]=aborttest.c INCLUDE[aborttest]=../include @@ -314,6 +314,10 @@ INCLUDE_MAIN___test_libtestutil_OLB = /INCLUDE=MAIN INCLUDE[x509_dup_cert_test]=../include DEPEND[x509_dup_cert_test]=../libcrypto libtestutil.a + SOURCE[pemtest]=pemtest.c + INCLUDE[pemtest]=../include . + DEPEND[pemtest]=../libcrypto libtestutil.a + IF[{- !$disabled{psk} -}] PROGRAMS_NO_INST=dtls_mtu_test SOURCE[dtls_mtu_test]=dtls_mtu_test.c ssltestlib.c diff --git a/test/pemtest.c b/test/pemtest.c new file mode 100644 index 0000000000..c573278c7e --- /dev/null +++ b/test/pemtest.c @@ -0,0 +1,76 @@ +/* + * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include +#include +#include + +#include "testutil.h" + +static const char raw[] = "hello world"; +static const char encoded[] = "aGVsbG8gd29ybGQ="; +static const char pemtype[] = "PEMTESTDATA"; + +static int test_b64(void) +{ + BIO *b = BIO_new(BIO_s_mem()); + char *name = NULL, *header = NULL; + unsigned char *data = NULL; + long len; + int ret = 0; + + if (!TEST_ptr(b) + || !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype)) + || !TEST_true(BIO_printf(b, "%s\n", encoded)) + || !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype)) + || !TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len, + PEM_FLAG_ONLY_B64))) + goto err; + if (!TEST_int_eq(memcmp(pemtype, name, sizeof(pemtype) - 1), 0) + || !TEST_int_eq(len,sizeof(raw) - 1) + || !TEST_int_eq(memcmp(data, raw, sizeof(raw) - 1), 0)) + goto err; + ret = 1; + err: + BIO_free(b); + OPENSSL_free(name); + OPENSSL_free(header); + OPENSSL_free(data); + return ret; +} + +static int test_invalid(void) +{ + BIO *b = BIO_new(BIO_s_mem()); + char *name = NULL, *header = NULL; + unsigned char *data = NULL; + long len; + + if (!TEST_ptr(b) + || !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype)) + || !TEST_true(BIO_printf(b, "%c%s\n", '\t', encoded)) + || !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype)) + /* Expected to fail due to non-base64 character */ + || TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len, + PEM_FLAG_ONLY_B64))) { + BIO_free(b); + return 0; + } + BIO_free(b); + OPENSSL_free(name); + OPENSSL_free(header); + OPENSSL_free(data); + return 1; +} + +void register_tests(void) +{ + ADD_TEST(test_b64); + ADD_TEST(test_invalid); +} diff --git a/test/recipes/04-test_pem.t b/test/recipes/04-test_pem.t index 23d1ef8a93..c37c98738b 100644 --- a/test/recipes/04-test_pem.t +++ b/test/recipes/04-test_pem.t @@ -76,7 +76,7 @@ my %dsa_expected = ( "dsa.pem" => 1 ); -plan tests => scalar keys(%cert_expected) + scalar keys(%dsa_expected) + 1; +plan tests => scalar keys(%cert_expected) + scalar keys(%dsa_expected) + 2; foreach my $input (keys %cert_expected) { my @common = ($cmd, "x509", "-text", "-noout", "-inform", "PEM", "-in"); @@ -100,3 +100,5 @@ SKIP: { my @match = grep /00:a0:3a:21:14:5d:cd:b6:d5:a0:3e:49:23:c1:3a:/, @data; ok(scalar @match > 0 ? 1 : 0); } + +ok(run(test(["pemtest"])), "running pemtest"); -- 2.25.1