|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
Packit Service |
084de1 |
* this file except in compliance with the License. You can obtain a copy
|
|
Packit Service |
084de1 |
* in the file LICENSE in the source distribution or at
|
|
Packit Service |
084de1 |
* https://www.openssl.org/source/license.html
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#include <string.h>
|
|
Packit Service |
084de1 |
#include <openssl/bio.h>
|
|
Packit Service |
084de1 |
#include <openssl/pem.h>
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#include "testutil.h"
|
|
Packit Service |
084de1 |
#include "internal/nelem.h"
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
typedef struct {
|
|
Packit Service |
084de1 |
const char *raw;
|
|
Packit Service |
084de1 |
const char *encoded;
|
|
Packit Service |
084de1 |
} TESTDATA;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static TESTDATA b64_pem_data[] = {
|
|
Packit Service |
084de1 |
{ "hello world",
|
|
Packit Service |
084de1 |
"aGVsbG8gd29ybGQ=" },
|
|
Packit Service |
084de1 |
{ "a very ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong input",
|
|
Packit Service |
084de1 |
"YSB2ZXJ5IG9vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29uZyBpbnB1dA==" }
|
|
Packit Service |
084de1 |
};
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static const char *pemtype = "PEMTESTDATA";
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int test_b64(int idx)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
BIO *b = BIO_new(BIO_s_mem());
|
|
Packit Service |
084de1 |
char *name = NULL, *header = NULL;
|
|
Packit Service |
084de1 |
unsigned char *data = NULL;
|
|
Packit Service |
084de1 |
long len;
|
|
Packit Service |
084de1 |
int ret = 0;
|
|
Packit Service |
084de1 |
const char *raw = b64_pem_data[idx].raw;
|
|
Packit Service |
084de1 |
const char *encoded = b64_pem_data[idx].encoded;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!TEST_ptr(b)
|
|
Packit Service |
084de1 |
|| !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype))
|
|
Packit Service |
084de1 |
|| !TEST_true(BIO_printf(b, "%s\n", encoded))
|
|
Packit Service |
084de1 |
|| !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype))
|
|
Packit Service |
084de1 |
|| !TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len,
|
|
Packit Service |
084de1 |
PEM_FLAG_ONLY_B64)))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
if (!TEST_int_eq(memcmp(pemtype, name, strlen(pemtype)), 0)
|
|
Packit Service |
084de1 |
|| !TEST_int_eq(len, strlen(raw))
|
|
Packit Service |
084de1 |
|| !TEST_int_eq(memcmp(data, raw, strlen(raw)), 0))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
ret = 1;
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
BIO_free(b);
|
|
Packit Service |
084de1 |
OPENSSL_free(name);
|
|
Packit Service |
084de1 |
OPENSSL_free(header);
|
|
Packit Service |
084de1 |
OPENSSL_free(data);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static int test_invalid(void)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
BIO *b = BIO_new(BIO_s_mem());
|
|
Packit Service |
084de1 |
char *name = NULL, *header = NULL;
|
|
Packit Service |
084de1 |
unsigned char *data = NULL;
|
|
Packit Service |
084de1 |
long len;
|
|
Packit Service |
084de1 |
const char *encoded = b64_pem_data[0].encoded;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!TEST_ptr(b)
|
|
Packit Service |
084de1 |
|| !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype))
|
|
Packit Service |
084de1 |
|| !TEST_true(BIO_printf(b, "%c%s\n", '\t', encoded))
|
|
Packit Service |
084de1 |
|| !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype))
|
|
Packit Service |
084de1 |
/* Expected to fail due to non-base64 character */
|
|
Packit Service |
084de1 |
|| TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len,
|
|
Packit Service |
084de1 |
PEM_FLAG_ONLY_B64))) {
|
|
Packit Service |
084de1 |
BIO_free(b);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
BIO_free(b);
|
|
Packit Service |
084de1 |
OPENSSL_free(name);
|
|
Packit Service |
084de1 |
OPENSSL_free(header);
|
|
Packit Service |
084de1 |
OPENSSL_free(data);
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int setup_tests(void)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
ADD_ALL_TESTS(test_b64, OSSL_NELEM(b64_pem_data));
|
|
Packit Service |
084de1 |
ADD_TEST(test_invalid);
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|