From: Klas Lindfors Date: Thu, 26 Apr 2012 11:46:22 +0000 (+0200) Subject: add basic tests for the ndef construction functions. X-Git-Tag: v1.7.0~1^2~16 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a32c297d8ee794a86e9fea754db197f2c8f29fa8;p=yubikey-personalization add basic tests for the ndef construction functions. --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 06d890a..677cd47 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -31,7 +31,7 @@ AM_LDFLAGS = -no-install -static AM_CFLAGS=-I$(srcdir)/.. -I$(srcdir)/../ykcore LDADD = ../libykpers-1.la -ctests = selftest$(EXEEXT) test_args_to_config$(EXEEXT) test_key_generation$(EXEEXT) +ctests = selftest$(EXEEXT) test_args_to_config$(EXEEXT) test_key_generation$(EXEEXT) test_ndef_construction$(EXEEXT) check_PROGRAMS = $(ctests) TESTS = $(ctests) diff --git a/tests/test_ndef_construction.c b/tests/test_ndef_construction.c new file mode 100644 index 0000000..f591018 --- /dev/null +++ b/tests/test_ndef_construction.c @@ -0,0 +1,95 @@ +/* -*- mode:C; c-file-style: "bsd" -*- */ +/* + * Copyright (c) 2011-2012 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 + +void _test_https_uri() +{ + YKNDEF ndef; + memset(&ndef, 0, sizeof(YKNDEF)); + char uri[] = "https://example.com/foo"; + int rc = ykp_construct_ndef_uri(&ndef, uri); + assert(rc == 0); + assert(ndef.type == 'U'); + assert(ndef.data[0] == 0x04); + assert(strncmp(ndef.data + 1, "example.com/foo", 15) == 0); + assert(ndef.len == 16); +} + +void _test_to_long_uri() +{ + YKNDEF ndef; + memset(&ndef, 0, sizeof(YKNDEF)); + char uri[] = "https://example.example.example.example.com/foo/kaka/bar/blahonga"; + int rc = ykp_construct_ndef_uri(&ndef, uri); + assert(rc == 1); + assert(ndef.type == 0); + assert(ndef.len == 0); +} + +void _test_exact_uri() +{ + YKNDEF ndef; + memset(&ndef, 0, sizeof(YKNDEF)); + char uri[] = "https://www.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + int rc = ykp_construct_ndef_uri(&ndef, uri); + assert(rc == 0); + assert(ndef.type == 'U'); + assert(ndef.data[0] == 0x02); + assert(strncmp(ndef.data + 1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", NDEF_DATA_SIZE -1) == 0); + assert(ndef.len == NDEF_DATA_SIZE); +} + +void _test_exact_text() +{ + YKNDEF ndef; + memset(&ndef, 0, sizeof(YKNDEF)); + char text[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + int rc = ykp_construct_ndef_text(&ndef, text); + assert(rc == 0); + assert(ndef.type == 'T'); + assert(strncmp(ndef.data, text, NDEF_DATA_SIZE) == 0); + assert(ndef.len == NDEF_DATA_SIZE); +} + +int main (void) +{ + _test_https_uri(); + _test_to_long_uri(); + _test_exact_uri(); + _test_exact_text(); + + return 0; +}