mirror of
https://github.com/openssl/openssl.git
synced 2024-11-21 01:15:20 +08:00
ad887416f1
that needed test_main now works using the same infrastructure as tests that used register_tests. This meant: * renaming register_tests to setup_tests and giving it a success/failure return. * renaming the init_test function to setup_test_framework. * renaming the finish_test function to pulldown_test_framework. * adding a user provided global_init function that runs before the test frame work is initialised. It returns a failure indication that stops the stest. * adding helper functions that permit tests to access their command line args. * spliting the BIO initialisation and finalisation out from the test setup and teardown. * hiding some of the now test internal functions. * fix the comments in testutil.h Reviewed-by: Richard Levitte <levitte@openssl.org> (Merged from https://github.com/openssl/openssl/pull/3953)
78 lines
2.2 KiB
C
78 lines
2.2 KiB
C
/*
|
|
* 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 <string.h>
|
|
#include <openssl/bio.h>
|
|
#include <openssl/pem.h>
|
|
|
|
#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;
|
|
}
|
|
|
|
int setup_tests(void)
|
|
{
|
|
ADD_TEST(test_b64);
|
|
ADD_TEST(test_invalid);
|
|
return 1;
|
|
}
|