From 51bfd4921a18552b72ecc15f282e52116c136554 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Wed, 6 May 2015 22:37:41 -0500 Subject: [PATCH] add app tests from regress/usr.bin/openssl These are added directly rather than imported by update.sh since they require local modifications and its not worth breaking everyone's git forks yet to import them through cvs2git. --- .gitignore | 1 + tests/Makefile.am | 20 +++ tests/openssl.cnf | 29 ++++ tests/optionstest.c | 380 ++++++++++++++++++++++++++++++++++++++++++++ tests/testdsa.sh | 35 ++++ tests/testenc.sh | 66 ++++++++ tests/testrsa.sh | 33 ++++ 7 files changed, 564 insertions(+) create mode 100644 tests/openssl.cnf create mode 100644 tests/optionstest.c create mode 100755 tests/testdsa.sh create mode 100755 tests/testenc.sh create mode 100755 tests/testrsa.sh diff --git a/.gitignore b/.gitignore index 471ca3a..28cf002 100644 --- a/.gitignore +++ b/.gitignore @@ -58,6 +58,7 @@ tests/pbkdf2* tests/*.pem tests/testssl tests/*.txt +!tests/optionstest.c # ctags stuff TAGS diff --git a/tests/Makefile.am b/tests/Makefile.am index 2ed7a44..aed12ff 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -3,6 +3,7 @@ include $(top_srcdir)/Makefile.am.common AM_CPPFLAGS += -I $(top_srcdir)/crypto/modes AM_CPPFLAGS += -I $(top_srcdir)/crypto/asn1 AM_CPPFLAGS += -I $(top_srcdir)/ssl +AM_CPPFLAGS += -I $(top_srcdir)/apps LDADD = $(PLATFORM_LDADD) $(PROG_LDADD) LDADD += $(top_builddir)/ssl/libssl.la @@ -192,6 +193,13 @@ TESTS += mont check_PROGRAMS += mont mont_SOURCES = mont.c +# optionstest +TESTS += optionstest +check_PROGRAMS += optionstest +optionstest_SOURCES = optionstest.c +optionstest_SOURCES += $(top_srcdir)/apps/apps.c +optionstest_SOURCES += $(top_srcdir)/apps/strtonum.c + # pbkdf2 TESTS += pbkdf2 check_PROGRAMS += pbkdf2 @@ -270,6 +278,18 @@ ssltest_SOURCES = ssltest.c EXTRA_DIST += ssltest.sh EXTRA_DIST += testssl ca.pem server.pem +# testdsa +TESTS += testdsa.sh +EXTRA_DIST += testdsa.sh + +# testenc +TESTS += testenc.sh +EXTRA_DIST += testenc.sh + +# testrsa +TESTS += testrsa.sh +EXTRA_DIST += testrsa.sh + # timingsafe TESTS += timingsafe check_PROGRAMS += timingsafe diff --git a/tests/openssl.cnf b/tests/openssl.cnf new file mode 100644 index 0000000..8e1eeb7 --- /dev/null +++ b/tests/openssl.cnf @@ -0,0 +1,29 @@ +# $OpenBSD: openssl.cnf,v 1.1 2014/08/26 17:50:07 jsing Exp $ + +# +# SSLeay example configuration file. +# This is mostly being used for generation of certificate requests. +# +# hacked by iang to do DSA certs - Server + +RANDFILE = ./.rnd + +#################################################################### +[ req ] +distinguished_name = req_distinguished_name +encrypt_rsa_key = no + +[ req_distinguished_name ] +countryName = Country Name (2 letter code) +countryName_default = CA +countryName_value = CA + +organizationName = Organization Name (eg, company) +organizationName_value = Shake it Vera + +0.commonName = Common Name (eg, YOUR name) +0.commonName_value = Wastelandus + +1.commonName = Common Name (eg, YOUR name) +1.commonName_value = Maximus + diff --git a/tests/optionstest.c b/tests/optionstest.c new file mode 100644 index 0000000..0cedfe6 --- /dev/null +++ b/tests/optionstest.c @@ -0,0 +1,380 @@ +/* $OpenBSD: optionstest.c,v 1.8 2015/01/22 05:48:00 doug Exp $ */ +/* + * Copyright (c) 2014 Joel Sing + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include +#include + +#include + +/* Needed to keep apps.c happy... */ +BIO *bio_err; +CONF *config; + +static int argfunc(char *arg); +static int defaultarg(int argc, char **argv, int *argsused); +static int multiarg(int argc, char **argv, int *argsused); + +static struct { + char *arg; + int flag; +} test_config; + +static struct option test_options[] = { + { + .name = "arg", + .argname = "argname", + .type = OPTION_ARG, + .opt.arg = &test_config.arg, + }, + { + .name = "argfunc", + .argname = "argname", + .type = OPTION_ARG_FUNC, + .opt.argfunc = argfunc, + }, + { + .name = "flag", + .type = OPTION_FLAG, + .opt.flag = &test_config.flag, + }, + { + .name = "multiarg", + .type = OPTION_ARGV_FUNC, + .opt.argvfunc = multiarg, + }, + { + .name = NULL, + .type = OPTION_ARGV_FUNC, + .opt.argvfunc = defaultarg, + }, + { NULL }, +}; + +char *args1[] = { "opts" }; +char *args2[] = { "opts", "-arg", "arg", "-flag" }; +char *args3[] = { "opts", "-arg", "arg", "-flag", "unnamed" }; +char *args4[] = { "opts", "-arg", "arg", "unnamed", "-flag" }; +char *args5[] = { "opts", "unnamed1", "-arg", "arg", "-flag", "unnamed2" }; +char *args6[] = { "opts", "-argfunc", "arg", "-flag" }; +char *args7[] = { "opts", "-arg", "arg", "-flag", "-", "-unnamed" }; +char *args8[] = { "opts", "-arg", "arg", "-flag", "file1", "file2", "file3" }; +char *args9[] = { "opts", "-arg", "arg", "-flag", "file1", "-file2", "file3" }; +char *args10[] = { "opts", "-arg", "arg", "-flag", "-", "file1", "file2" }; +char *args11[] = { "opts", "-arg", "arg", "-flag", "-", "-file1", "-file2" }; +char *args12[] = { "opts", "-multiarg", "arg1", "arg2", "-flag", "unnamed" }; +char *args13[] = { "opts", "-multiargz", "arg1", "arg2", "-flagz", "unnamed" }; + +struct options_test { + int argc; + char **argv; + enum { + OPTIONS_TEST_NONE, + OPTIONS_TEST_UNNAMED, + OPTIONS_TEST_ARGSUSED, + } type; + char *unnamed; + int used; + int want; + char *wantarg; + int wantflag; +}; + +struct options_test options_tests[] = { + { + /* Test 1 - No arguments (only program name). */ + .argc = 1, + .argv = args1, + .type = OPTIONS_TEST_NONE, + .want = 0, + .wantarg = NULL, + .wantflag = 0, + }, + { + /* Test 2 - Named arguments (unnamed not permitted). */ + .argc = 4, + .argv = args2, + .type = OPTIONS_TEST_NONE, + .want = 0, + .wantarg = "arg", + .wantflag = 1, + }, + { + /* Test 3 - Named arguments (unnamed permitted). */ + .argc = 4, + .argv = args2, + .type = OPTIONS_TEST_UNNAMED, + .unnamed = NULL, + .want = 0, + .wantarg = "arg", + .wantflag = 1, + }, + { + /* Test 4 - Named and single unnamed (unnamed not permitted). */ + .argc = 5, + .argv = args3, + .type = OPTIONS_TEST_NONE, + .want = 1, + }, + { + /* Test 5 - Named and single unnamed (unnamed permitted). */ + .argc = 5, + .argv = args3, + .type = OPTIONS_TEST_UNNAMED, + .unnamed = "unnamed", + .want = 0, + .wantarg = "arg", + .wantflag = 1, + }, + { + /* Test 6 - Named and single unnamed (different sequence). */ + .argc = 5, + .argv = args4, + .type = OPTIONS_TEST_UNNAMED, + .unnamed = "unnamed", + .want = 0, + .wantarg = "arg", + .wantflag = 1, + }, + { + /* Test 7 - Multiple unnamed arguments (should fail). */ + .argc = 6, + .argv = args5, + .type = OPTIONS_TEST_UNNAMED, + .want = 1, + }, + { + /* Test 8 - Function. */ + .argc = 4, + .argv = args6, + .type = OPTIONS_TEST_NONE, + .want = 0, + .wantarg = "arg", + .wantflag = 1, + }, + { + /* Test 9 - Named and single unnamed (hyphen separated). */ + .argc = 6, + .argv = args7, + .type = OPTIONS_TEST_UNNAMED, + .unnamed = "-unnamed", + .want = 0, + .wantarg = "arg", + .wantflag = 1, + }, + { + /* Test 10 - Named and multiple unnamed. */ + .argc = 7, + .argv = args8, + .used = 4, + .type = OPTIONS_TEST_ARGSUSED, + .want = 0, + .wantarg = "arg", + .wantflag = 1, + }, + { + /* Test 11 - Named and multiple unnamed. */ + .argc = 7, + .argv = args9, + .used = 4, + .type = OPTIONS_TEST_ARGSUSED, + .want = 0, + .wantarg = "arg", + .wantflag = 1, + }, + { + /* Test 12 - Named and multiple unnamed. */ + .argc = 7, + .argv = args10, + .used = 5, + .type = OPTIONS_TEST_ARGSUSED, + .want = 0, + .wantarg = "arg", + .wantflag = 1, + }, + { + /* Test 13 - Named and multiple unnamed. */ + .argc = 7, + .argv = args11, + .used = 5, + .type = OPTIONS_TEST_ARGSUSED, + .want = 0, + .wantarg = "arg", + .wantflag = 1, + }, + { + /* Test 14 - Named only. */ + .argc = 4, + .argv = args2, + .used = 4, + .type = OPTIONS_TEST_ARGSUSED, + .want = 0, + .wantarg = "arg", + .wantflag = 1, + }, + { + /* Test 15 - Multiple argument callback. */ + .argc = 6, + .argv = args12, + .unnamed = "unnamed", + .type = OPTIONS_TEST_UNNAMED, + .want = 0, + .wantarg = NULL, + .wantflag = 1, + }, + { + /* Test 16 - Multiple argument callback. */ + .argc = 6, + .argv = args12, + .used = 5, + .type = OPTIONS_TEST_ARGSUSED, + .want = 0, + .wantarg = NULL, + .wantflag = 1, + }, + { + /* Test 17 - Default callback. */ + .argc = 6, + .argv = args13, + .unnamed = "unnamed", + .type = OPTIONS_TEST_UNNAMED, + .want = 0, + .wantarg = NULL, + .wantflag = 1, + }, + { + /* Test 18 - Default callback. */ + .argc = 6, + .argv = args13, + .used = 5, + .type = OPTIONS_TEST_ARGSUSED, + .want = 0, + .wantarg = NULL, + .wantflag = 1, + }, +}; + +#define N_OPTIONS_TESTS \ + (sizeof(options_tests) / sizeof(*options_tests)) + +static int +argfunc(char *arg) +{ + test_config.arg = arg; + return (0); +} + +static int +defaultarg(int argc, char **argv, int *argsused) +{ + if (argc < 1) + return (1); + + if (strcmp(argv[0], "-multiargz") == 0) { + if (argc < 3) + return (1); + *argsused = 3; + return (0); + } else if (strcmp(argv[0], "-flagz") == 0) { + test_config.flag = 1; + *argsused = 1; + return (0); + } + + return (1); +} + +static int +multiarg(int argc, char **argv, int *argsused) +{ + if (argc < 3) + return (1); + + *argsused = 3; + return (0); +} + +static int +do_options_test(int test_no, struct options_test *ot) +{ + int *argsused = NULL; + char *unnamed = NULL; + char **arg = NULL; + int used = 0; + int ret; + + if (ot->type == OPTIONS_TEST_UNNAMED) + arg = &unnamed; + else if (ot->type == OPTIONS_TEST_ARGSUSED) + argsused = &used; + + memset(&test_config, 0, sizeof(test_config)); + ret = options_parse(ot->argc, ot->argv, test_options, arg, argsused); + if (ret != ot->want) { + fprintf(stderr, "FAIL: test %i options_parse() returned %i, " + "want %i\n", test_no, ret, ot->want); + return (1); + } + if (ret != 0) + return (0); + + if ((test_config.arg != NULL || ot->wantarg != NULL) && + (test_config.arg == NULL || ot->wantarg == NULL || + strcmp(test_config.arg, ot->wantarg) != 0)) { + fprintf(stderr, "FAIL: test %i got arg '%s', want '%s'\n", + test_no, test_config.arg, ot->wantarg); + return (1); + } + if (test_config.flag != ot->wantflag) { + fprintf(stderr, "FAIL: test %i got flag %i, want %i\n", + test_no, test_config.flag, ot->wantflag); + return (1); + } + if (ot->type == OPTIONS_TEST_UNNAMED && + (unnamed != NULL || ot->unnamed != NULL) && + (unnamed == NULL || ot->unnamed == NULL || + strcmp(unnamed, ot->unnamed) != 0)) { + fprintf(stderr, "FAIL: test %i got unnamed '%s', want '%s'\n", + test_no, unnamed, ot->unnamed); + return (1); + } + if (ot->type == OPTIONS_TEST_ARGSUSED && used != ot->used) { + fprintf(stderr, "FAIL: test %i got used %i, want %i\n", + test_no, used, ot->used); + return (1); + } + + return (0); +} + +int +main(int argc, char **argv) +{ + int failed = 0; + size_t i; + + for (i = 0; i < N_OPTIONS_TESTS; i++) { + printf("Test %d%s\n", (int)(i + 1), options_tests[i].want == 0 ? + "" : " is expected to complain"); + failed += do_options_test(i + 1, &options_tests[i]); + } + + return (failed); +} diff --git a/tests/testdsa.sh b/tests/testdsa.sh new file mode 100755 index 0000000..413323e --- /dev/null +++ b/tests/testdsa.sh @@ -0,0 +1,35 @@ +#!/bin/sh +# $OpenBSD: testdsa.sh,v 1.1 2014/08/26 17:50:07 jsing Exp $ + + +#Test DSA certificate generation of openssl + +cmd=../apps/openssl + +if [ -z $srcdir ]; then + srcdir=. +fi + +# Generate DSA paramter set +$cmd dsaparam 512 -out dsa512.pem +if [ $? != 0 ]; then + exit 1; +fi + + +# Denerate a DSA certificate +$cmd req -config $srcdir/openssl.cnf -x509 -newkey dsa:dsa512.pem -out testdsa.pem -keyout testdsa.key +if [ $? != 0 ]; then + exit 1; +fi + + +# Now check the certificate +$cmd x509 -text -in testdsa.pem +if [ $? != 0 ]; then + exit 1; +fi + +rm testdsa.key + +exit 0 diff --git a/tests/testenc.sh b/tests/testenc.sh new file mode 100755 index 0000000..51af0ab --- /dev/null +++ b/tests/testenc.sh @@ -0,0 +1,66 @@ +#!/bin/sh +# $OpenBSD: testenc.sh,v 1.1 2014/08/26 17:50:07 jsing Exp $ + +test=p +cmd=../apps/openssl + +cat openssl.cnf >$test; + +echo cat +$cmd enc < $test > $test.cipher +$cmd enc < $test.cipher >$test.clear +cmp $test $test.clear +if [ $? != 0 ] +then + exit 1 +else + /bin/rm $test.cipher $test.clear +fi +echo base64 +$cmd enc -a -e < $test > $test.cipher +$cmd enc -a -d < $test.cipher >$test.clear +cmp $test $test.clear +if [ $? != 0 ] +then + exit 1 +else + /bin/rm $test.cipher $test.clear +fi + +for i in \ + aes-128-cbc aes-128-cfb aes-128-cfb1 aes-128-cfb8 \ + aes-128-ecb aes-128-ofb aes-192-cbc aes-192-cfb \ + aes-192-cfb1 aes-192-cfb8 aes-192-ecb aes-192-ofb \ + aes-256-cbc aes-256-cfb aes-256-cfb1 aes-256-cfb8 \ + aes-256-ecb aes-256-ofb \ + bf-cbc bf-cfb bf-ecb bf-ofb \ + cast-cbc cast5-cbc cast5-cfb cast5-ecb cast5-ofb \ + des-cbc des-cfb des-cfb8 des-ecb des-ede \ + des-ede-cbc des-ede-cfb des-ede-ofb des-ede3 \ + des-ede3-cbc des-ede3-cfb des-ede3-ofb des-ofb desx-cbc \ + rc2-40-cbc rc2-64-cbc rc2-cbc rc2-cfb rc2-ecb rc2-ofb \ + rc4 rc4-40 +do + echo $i + $cmd $i -e -k test < $test > $test.$i.cipher + $cmd $i -d -k test < $test.$i.cipher >$test.$i.clear + cmp $test $test.$i.clear + if [ $? != 0 ] + then + exit 1 + else + /bin/rm $test.$i.cipher $test.$i.clear + fi + + echo $i base64 + $cmd $i -a -e -k test < $test > $test.$i.cipher + $cmd $i -a -d -k test < $test.$i.cipher >$test.$i.clear + cmp $test $test.$i.clear + if [ $? != 0 ] + then + exit 1 + else + /bin/rm $test.$i.cipher $test.$i.clear + fi +done +rm -f $test diff --git a/tests/testrsa.sh b/tests/testrsa.sh new file mode 100755 index 0000000..cb4e28d --- /dev/null +++ b/tests/testrsa.sh @@ -0,0 +1,33 @@ +#!/bin/sh +# $OpenBSD: testrsa.sh,v 1.1 2014/08/26 17:50:07 jsing Exp $ + + +#Test RSA certificate generation of openssl + +cmd=../apps/openssl + +if [ -z $srcdir ]; then + srcdir=. +fi + +# Generate RSA private key +$cmd genrsa -out rsakey.pem +if [ $? != 0 ]; then + exit 1; +fi + + +# Generate an RSA certificate +$cmd req -config $srcdir/openssl.cnf -key rsakey.pem -new -x509 -days 365 -out rsacert.pem +if [ $? != 0 ]; then + exit 1; +fi + + +# Now check the certificate +$cmd x509 -text -in rsacert.pem +if [ $? != 0 ]; then + exit 1; +fi + +exit 0