Switch test cases to use libcheck framework

This keeps the test logic the same, but moves most of them into the libcheck
test suite framework.  It gives better control over grouping related tests,
running them in parallel, and reporting on multiple failures.

Running in parallel also brings problems, so libykcs11 tests are left
untouched.  Parallel access to a single hardware DUT does not make sense,
and pcsc-lite doesn't work after a fork() in OS X 10.11+, so it can't run
in libcheck's tests anyway.
This commit is contained in:
Trevor Bentley
2017-06-30 09:58:25 +02:00
parent ba7d256ecf
commit 0d2b85fcef
8 changed files with 194 additions and 88 deletions
+2 -2
View File
@@ -29,12 +29,12 @@ TESTS_ENVIRONMENT = export VERSION=$(PACKAGE_VERSION); export EXEEXT=$(EXEEXT);
LOG_COMPILER = $(VALGRIND)
AM_CFLAGS = $(WARN_CFLAGS)
AM_CFLAGS = $(WARN_CFLAGS) @CHECK_CFLAGS@
AM_CPPFLAGS = -I$(top_srcdir)/lib -I$(top_builddir)/lib
AM_CPPFLAGS += -I$(top_srcdir)/tool -I$(top_builddir)/tool
AM_CPPFLAGS += $(OPENSSL_CFLAGS)
AM_LDFLAGS = -no-install
AM_LDFLAGS = -no-install @CHECK_LIBS@
parse_name_LDADD = ../libpiv_util.la $(OPENSSL_LIBS)
test_inout_LDADD = ../libpiv_util.la
+49 -17
View File
@@ -28,6 +28,8 @@
*
*/
#include <check.h>
#include <string.h>
#include <stdbool.h>
@@ -39,18 +41,26 @@
#include "util.h"
static void test_name(const char *name, const char *expected, bool fail) {
struct name {
const char *name;
const char *parsed_name;
bool valid;
} names[] = {
{"/CN=test foo/", "CN = test foo", true},
{"/CN=test/OU=bar/O=EXAMPLE/", "CN = test, OU = bar, O = EXAMPLE", true},
{"/CN=test/OU=bar/O=EXAMPLE/", "CN = test, OU = wrong, O = EXAMPLE", false},
{"/foo/", "", false},
{"/CN=test/foobar/", "", false},
{"/CN=test/foo=bar/", "", false},
};
static bool test_name(const char *name, const char *expected) {
char buf[1024];
BIO *bio;
const char none[] = {0};
X509_NAME *parsed = parse_name(name);
if(parsed == NULL) {
if(fail) {
return;
} else {
printf("Failed parsing of '%s'!\n", name);
exit(EXIT_FAILURE);
}
return false;
}
bio = BIO_new(BIO_s_mem());
@@ -61,17 +71,39 @@ static void test_name(const char *name, const char *expected, bool fail) {
BIO_free(bio);
X509_NAME_free(parsed);
if(strcmp(buf, expected) != 0) {
printf("Names not matching: '%s' != '%s'\n", expected, buf);
exit(EXIT_FAILURE);
fprintf(stderr, "Names not matching: '%s' != '%s'\n", expected, buf);
return false;
}
return true;
}
int main(void) {
test_name("/CN=test foo/", "CN = test foo", false);
test_name("/CN=test/OU=bar/O=EXAMPLE/", "CN = test, OU = bar, O = EXAMPLE", false);
test_name("/foo/", "", true);
test_name("/CN=test/foobar/", "", true);
test_name("/CN=test/foo=bar/", "", true);
return EXIT_SUCCESS;
START_TEST(test_parse_name) {
ck_assert(test_name(names[_i].name, names[_i].parsed_name) == names[_i].valid);
}
END_TEST
Suite *test_suite(void) {
Suite *s;
TCase *tc;
s = suite_create("yubico-piv-tool parse_name");
tc = tcase_create("parse_name");
tcase_add_loop_test(tc, test_parse_name, 0, sizeof(names) / sizeof(struct name));
suite_add_tcase(s, tc);
return s;
}
int main(void)
{
int number_failed;
Suite *s;
SRunner *sr;
s = test_suite();
sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
+43 -8
View File
@@ -28,6 +28,8 @@
*
*/
#include <check.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
@@ -40,25 +42,58 @@
#define pipe(fds) _pipe(fds,4096, 0)
#endif
static void test_inout(enum enum_format format) {
enum enum_format formats[] = {
format_arg_base64,
format_arg_hex,
format_arg_binary,
};
static bool inout(enum enum_format format) {
const unsigned char buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
unsigned char buf2[sizeof(buf)];
int pipefd[2];
FILE *tmp1, *tmp2;
assert(pipe(pipefd) == 0);
if (pipe(pipefd) != 0)
return false;
tmp1 = fdopen(pipefd[1], "w");
dump_data(buf, sizeof(buf), tmp1, false, format);
fclose(tmp1);
tmp2 = fdopen(pipefd[0], "r");
read_data(buf2, sizeof(buf2), tmp2, format);
assert(memcmp(buf, buf2, sizeof(buf)) == 0);
if (memcmp(buf, buf2, sizeof(buf)) != 0)
return false;
fclose(tmp2);
return true;
}
int main(void) {
test_inout(format_arg_base64);
test_inout(format_arg_hex);
test_inout(format_arg_binary);
exit(0);
START_TEST(test_inout) {
ck_assert(inout(formats[_i]));
}
END_TEST
Suite *test_suite(void) {
Suite *s;
TCase *tc;
s = suite_create("yubico-piv-tool inout");
tc = tcase_create("inout");
tcase_add_loop_test(tc, test_inout, 0, sizeof(formats) / sizeof(*formats));
suite_add_tcase(s, tc);
return s;
}
int main(void)
{
int number_failed;
Suite *s;
SRunner *sr;
s = test_suite();
sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}