From d946b5cbd51db033fef3a81373ecce07cc61cba9 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Mon, 20 Jun 2022 21:07:46 +0200 Subject: tests: Cover few remaining branches with unit test for ACA handling Signed-off-by: Jakub Jelen --- Makefile.am | 8 ++++++ tests/meson.build | 13 +++++++++ tests/unit_cac.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 tests/unit_cac.c diff --git a/Makefile.am b/Makefile.am index 3e81def..e2620cb 100644 --- a/Makefile.am +++ b/Makefile.am @@ -86,12 +86,20 @@ AM_TESTS_ENVIRONMENT += \ SOFTHSM2_CONF=tests/softhsm2.conf test_programs = \ + tests/unit_cac \ tests/libcacard \ tests/simpletlv \ tests/hwtests \ tests/initialize \ $(NULL) +tests_unit_cac_LDADD = \ + $(GLIB2_LIBS) \ + libcacard.la \ + src/common.lo \ + src/simpletlv.lo \ + $(NULL) + tests_libcacard_SOURCES = \ tests/common.c \ tests/common.h \ diff --git a/tests/meson.build b/tests/meson.build index 6ce13e3..57b8d2d 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -11,6 +11,19 @@ modutil_dep = find_program('modutil', required: false) openssl_dep = find_program('openssl', required: false) softhsm_util_dep = find_program('softhsm2-util', required: false) +unit_cac_test = executable( + 'unit_cac', + ['unit_cac.c'], + objects: libcacard.extract_objects('src/simpletlv.c', 'src/common.c'), + dependencies: [libcacard_dep], +) + +test( + 'unit_cac', + unit_cac_test, + env: env, +) + libcacard_test = executable( 'libcacard', ['libcacard.c', 'common.c'], diff --git a/tests/unit_cac.c b/tests/unit_cac.c new file mode 100644 index 0000000..de91577 --- /dev/null +++ b/tests/unit_cac.c @@ -0,0 +1,81 @@ +/* + * Unit-test low-level CAC code + * + * Copyright 2022 Red Hat, Inc. + * + * Authors: + * Jakub Jelen + * + * This code is licensed under the GNU LGPL, version 2.1 or later. + * See the COPYING file in the top-level directory. + */ +#include +#include "cac-aca.c" + +struct acr_applet a = { + .id = 0x42, .num_objects = 1, .objects = { + {.id = "\xab\xcd", .num_ins = 1, .ins = { + {.code = 0x01, .acrid = 0x02, .config = ACR_INS_CONFIG_P2, .p1 = 0x03, .p2 = 0x04, .data1 = 0x05} + } + } + } +}; + +static void test_acr_applet_object_encode(void) +{ + unsigned int buflen, objlen; + unsigned char *buf = NULL, *p = NULL; + + /* Too short */ + buflen = 0; + p = acr_applet_object_encode(a.objects, buf, buflen, &objlen); + g_assert_null(p); + + buflen = 16; + buf = g_malloc(buflen); + + /* Still too short */ + buflen = 2; + p = acr_applet_object_encode(a.objects, buf, buflen, &objlen); + g_assert_null(p); + + /* Still too short */ + buflen = 4; + p = acr_applet_object_encode(a.objects, buf, buflen, &objlen); + g_assert_null(p); + + /* This should do */ + buflen = 16; + p = acr_applet_object_encode(a.objects, buf, buflen, &objlen); + g_assert_cmpmem(buf, objlen, "\xab\xcd\x01\x02\x04\x02", 6); + g_free(buf); +} + +static void test_acr_applet_encode(void) +{ + unsigned int outlen; + unsigned char *p = NULL; + + /* Wrong arguments */ + p = acr_applet_encode(&a, NULL); + g_assert_null(p); + + p = acr_applet_encode(&a, &outlen); + g_assert_cmpmem(p, outlen, "\x42\x01\x82\x06\xab\xcd\x01\x02\x04\x02", 10); + g_free(p); +} + +int main(int argc, char *argv[]) +{ + int ret; + + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/unittests/cac/acr_applet_object_encode", test_acr_applet_object_encode); + g_test_add_func("/unittests/cac/acr_applet_encode", test_acr_applet_encode); + ret = g_test_run(); + + return ret; +} + +/* vim: set ts=4 sw=4 tw=0 noet expandtab: */ -- cgit v1.2.3