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
@@ -25,10 +25,10 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
AM_CFLAGS = $(WARN_CFLAGS)
AM_CFLAGS = $(WARN_CFLAGS) @CHECK_CFLAGS@
AM_CPPFLAGS = -I$(top_srcdir)/lib -I$(top_builddir)/lib
AM_LDFLAGS = -no-install
AM_LDFLAGS = -no-install @CHECK_LIBS@
LDADD = ../libykpiv.la
check_PROGRAMS = basic parse_key
+57 -34
View File
@@ -35,41 +35,64 @@
#include <stdlib.h>
#include <string.h>
#include <check.h>
START_TEST(test_version_string) {
if (strcmp(YKPIV_VERSION_STRING, ykpiv_check_version(NULL)) != 0) {
ck_abort_msg("version mismatch %s != %s\n", YKPIV_VERSION_STRING,
ykpiv_check_version(NULL));
}
if (ykpiv_check_version(YKPIV_VERSION_STRING) == NULL) {
ck_abort_msg("version NULL?\n");
}
if (ykpiv_check_version("99.99.99") != NULL) {
ck_abort_msg("version not NULL?\n");
}
fprintf(stderr, "ykpiv version: header %s library %s\n",
YKPIV_VERSION_STRING, ykpiv_check_version (NULL));
}
END_TEST
START_TEST(test_strerror) {
const char *s;
if (ykpiv_strerror(YKPIV_OK) == NULL) {
ck_abort_msg("ykpiv_strerror NULL\n");
}
s = ykpiv_strerror_name(YKPIV_OK);
if (s == NULL || strcmp(s, "YKPIV_OK") != 0) {
ck_abort_msg("ykpiv_strerror_name %s\n", s);
}
}
END_TEST
Suite *basic_suite(void) {
Suite *s;
TCase *tc;
s = suite_create("libykpiv basic");
tc = tcase_create("basic");
tcase_add_test(tc, test_version_string);
tcase_add_test(tc, test_strerror);
suite_add_tcase(s, tc);
return s;
}
int main(void)
{
if(strcmp(YKPIV_VERSION_STRING, ykpiv_check_version (NULL)) != 0) {
printf("version mismatch %s != %s\n", YKPIV_VERSION_STRING,
ykpiv_check_version(NULL));
return EXIT_FAILURE;
}
int number_failed;
Suite *s;
SRunner *sr;
if(ykpiv_check_version(YKPIV_VERSION_STRING) == NULL) {
printf("version NULL?\n");
return EXIT_FAILURE;
}
if(ykpiv_check_version("99.99.99") != NULL) {
printf ("version not NULL?\n");
return EXIT_FAILURE;
}
printf ("ykpiv version: header %s library %s\n",
YKPIV_VERSION_STRING, ykpiv_check_version (NULL));
if(ykpiv_strerror(YKPIV_OK) == NULL) {
printf ("ykpiv_strerror NULL\n");
return EXIT_FAILURE;
}
{
const char *s;
s = ykpiv_strerror_name(YKPIV_OK);
if(s == NULL || strcmp(s, "YKPIV_OK") != 0) {
printf("ykpiv_strerror_name %s\n", s);
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
s = basic_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;
}
+38 -23
View File
@@ -32,6 +32,8 @@
#include <stdlib.h>
#include <string.h>
#include <check.h>
#include "ykpiv.h"
struct key {
@@ -57,30 +59,43 @@ static int parse_key(const char *text, const unsigned char *expected, int valid)
unsigned char key[24];
size_t len = sizeof(key);
ykpiv_rc res = ykpiv_hex_decode(text, strlen(text), key, &len);
if(res != YKPIV_OK && valid == 1) {
printf("key check failed for %s!\n", text);
return EXIT_FAILURE;
} else if(res != YKPIV_OK && valid == 0) {
return EXIT_SUCCESS;
}
if(memcmp(expected, key, 24) != 0) {
printf("keys not matching for %s!\n", text);
return EXIT_FAILURE;
}
if (valid) {
ck_assert(res == YKPIV_OK);
ck_assert(memcmp(expected, key, 24) == 0);
} else {
ck_assert(res != YKPIV_OK);
}
return EXIT_SUCCESS;
}
int main(void) {
size_t i;
for(i = 0; i < sizeof(keys) / sizeof(struct key); i++) {
int res = parse_key(keys[i].text, keys[i].formatted, keys[i].valid);
if(res != EXIT_SUCCESS) {
return res;
}
}
return EXIT_SUCCESS;
START_TEST(test_parse_key) {
int res = parse_key(keys[_i].text, keys[_i].formatted, keys[_i].valid);
ck_assert(res == EXIT_SUCCESS);
}
END_TEST
Suite *parsekey_suite(void) {
Suite *s;
TCase *tc;
s = suite_create("libykpiv parsekey");
tc = tcase_create("parsekey");
tcase_add_loop_test(tc, test_parse_key, 0, sizeof(keys) / sizeof(struct key));
suite_add_tcase(s, tc);
return s;
}
int main(void)
{
int number_failed;
Suite *s;
SRunner *sr;
s = parsekey_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;
}