From: Fredrik Thulin Date: Thu, 13 Jan 2011 22:07:57 +0000 (+0100) Subject: Add test cases for ykpersonalize argument parsing. X-Git-Tag: v1.4.0~19^2~2 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=764d7aab1fc888f9b5694f7f29e958557cfe2755;p=yubikey-personalization.old Add test cases for ykpersonalize argument parsing. --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 392a939..ece6e63 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -33,6 +33,9 @@ LDADD = ../libykpers-1.la ykpersonalize_LDADD = ../libykpers-1.la ../ykcore/libykcore.la -ctests = selftest$(EXEEXT) +ctests = selftest$(EXEEXT) test_args_to_config$(EXEEXT) check_PROGRAMS = $(ctests) TESTS = $(ctests) + +test_args_to_config_LDADD = ../ykpersonalize.o $(LDADD) $(ykpersonalize_LDADD) +test_args_to_config_LDFLAGS = -z muldefs diff --git a/tests/test_args_to_config.c b/tests/test_args_to_config.c new file mode 100644 index 0000000..8122704 --- /dev/null +++ b/tests/test_args_to_config.c @@ -0,0 +1,193 @@ +/* -*- mode:C; c-file-style: "bsd" -*- */ +/* + * Copyright (c) 2011, Yubico AB + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +#include +#include +#include + +#include +#include +/* +#include +#include +*/ + +/* duplicated from ykpers.c */ +struct ykp_config_t { + unsigned int yk_major_version; + unsigned int yk_minor_version; + unsigned int configuration_number; + + struct config_st *ykcore_config; +}; + +void _yktest_hexdump(char *prefix, void *buffer, int size, int break_on) +{ + unsigned char *p = buffer; + int i; + if (prefix != NULL) + fprintf(stderr, "%s", prefix); + for (i = 0; i < size; i++) { + fprintf(stderr, " %02x", *p); + if (! ((i + 1) % break_on)) + fprintf(stderr, "\n"); + p++; + } + fprintf(stderr, "\n"); + fflush(stderr); +} + +int _test_config (YKP_CONFIG *cfg, YK_STATUS *st, int argc, char **argv) +{ + FILE *inf = NULL; const char *infname = NULL; + FILE *outf = NULL; const char *outfname = NULL; + bool verbose = false; + bool aesviahash = false; + bool use_access_code = false; + unsigned char access_code[256]; + YK_KEY *yk = 0; + bool autocommit = false; + int exit_code = 0; + + /* Options */ + char *salt = NULL; + + int rc; + + /* call args_to_config from ykpersonalize.c with a fake set of program arguments */ + rc = args_to_config(argc, argv, cfg, + infname, outfname, + &autocommit, salt, + st, &verbose, + access_code, &use_access_code, + &aesviahash, + &exit_code); + + return rc; +} + +YK_STATUS * _test_init_st(int major, int minor, int build) +{ + YK_STATUS *st = ykds_alloc(); + struct status_st *t; + + t = (struct status_st *) st; + + /* connected key details */ + t->versionMajor = major; + t->versionMinor = minor; + t->versionBuild = build; + + return st; +} + +int _test_config_slot1() +{ + YKP_CONFIG *cfg = ykp_create_config(); + YK_STATUS *st = _test_init_st(1, 3, 0); + int rc = 0; + unsigned char *p; + struct config_st *ycfg; + + char *argv[] = { + "unittest", "-1", NULL + }; + int argc = sizeof argv/sizeof argv[0] - 1; + + rc = _test_config(cfg, st, argc, argv); + assert(rc == 1); + + /* verify required version for this config */ + assert(cfg->yk_major_version == 1); + assert(cfg->yk_minor_version == 3); + + /* verify some specific flags */ + ycfg = (struct config_st *) &cfg->ykcore_config; + assert(ycfg->tktFlags == TKTFLAG_APPEND_CR); + + /* then check CRC against a known value to bulk check the rest */ + ycfg->crc = ~yubikey_crc16 ((unsigned char *) ycfg, + offsetof(struct config_st, crc)); + + if (ycfg->crc != 0xc046) + _yktest_hexdump ("NO-MATCH :\n", ycfg, 64, 8); + + assert(ycfg->crc == 0xc046); + + ykp_free_config(cfg); + free(st); +} + +int _test_config_static_slot2() +{ + YKP_CONFIG *cfg = ykp_create_config(); + YK_STATUS *st = _test_init_st(1, 3, 0); + int rc = 0; + unsigned char *p; + struct config_st *ycfg; + + char *argv[] = { + "unittest", "-2", "-a303132333435363738393a3b3c3d3e3f", + NULL + }; + int argc = sizeof argv/sizeof argv[0] - 1; + + rc = _test_config(cfg, st, argc, argv); + assert(rc == 1); + + /* verify required version for this config */ + assert(cfg->yk_major_version == 1); + assert(cfg->yk_minor_version == 3); + + /* verify some specific flags */ + ycfg = (struct config_st *) &cfg->ykcore_config; + assert(ycfg->tktFlags == TKTFLAG_APPEND_CR); + + /* then check CRC against a known value to bulk check the rest */ + ycfg->crc = ~yubikey_crc16 ((unsigned char *) ycfg, + offsetof(struct config_st, crc)); + + if (ycfg->crc != 0x79dd) + _yktest_hexdump ("NO-MATCH :\n", ycfg, 64, 8); + + assert(ycfg->crc == 0x79dd); + + ykp_free_config(cfg); + free(st); +} + +int main (int argc, char **argv) +{ + _test_config_slot1(); + _test_config_static_slot2(); + + return 0; +}