From 31242f8570c870f60feecb6a5039d19863ecdc55 Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Sun, 28 Nov 2021 14:45:11 +0000 Subject: x1000-installer: add test for xf_map_parseline Change-Id: I35db43c83dc3964607b95f6b5c300c2fef455ac8 --- lib/x1000-installer/Makefile | 1 + lib/x1000-installer/test/main.c | 2 + lib/x1000-installer/test/test_flashmap.c | 140 +++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 lib/x1000-installer/test/test_flashmap.c (limited to 'lib/x1000-installer') diff --git a/lib/x1000-installer/Makefile b/lib/x1000-installer/Makefile index b85fef31da..54c590dce4 100644 --- a/lib/x1000-installer/Makefile +++ b/lib/x1000-installer/Makefile @@ -13,6 +13,7 @@ TOBJ = test_lib/core_alloc.o \ test_lib/md5.o \ test_lib/strlcpy.o \ test/main.o \ + test/test_flashmap.o \ test/test_stream.o TBIN = xf_test diff --git a/lib/x1000-installer/test/main.c b/lib/x1000-installer/test/main.c index 9964e22716..f9bff1b704 100644 --- a/lib/x1000-installer/test/main.c +++ b/lib/x1000-installer/test/main.c @@ -41,11 +41,13 @@ struct test_info { extern void test_stream_read_lines(void); extern void test_stream_read_line_too_long(void); +extern void test_flashmap_parseline(void); #define TEST(x) {#x, x} static const struct test_info all_tests[] = { TEST(test_stream_read_lines), TEST(test_stream_read_line_too_long), + TEST(test_flashmap_parseline), }; #undef TEST diff --git a/lib/x1000-installer/test/test_flashmap.c b/lib/x1000-installer/test/test_flashmap.c new file mode 100644 index 0000000000..5e7aa39d78 --- /dev/null +++ b/lib/x1000-installer/test/test_flashmap.c @@ -0,0 +1,140 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2021 Aidan MacDonald + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#include "test.h" +#include "xf_stream.h" +#include "xf_flashmap.h" +#include "xf_error.h" +#include "file.h" +#include +#include +#include + +#define MD5SUM \ + "abcdef012345ABCDEF012345abcdef01" +#define MD5SUM_L \ + "abcdef012345abcdef012345abcdef01" +#define LONG_NAME \ + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +#define OVERLONG_NAME \ + LONG_NAME "a" + +static const struct parseline_test { + const char* line; + int rc; + const char* name; + const char* md5; + uint32_t file_length; + uint32_t offset; + uint32_t length; + int flags; +} parseline_tests[] = { + {"test1 " MD5SUM " 1234 0x9876 42", + XF_E_SUCCESS, "test1", MD5SUM_L, 1234, 0x9876, 42, + XF_MAP_HAS_FILE_LENGTH|XF_MAP_HAS_MD5}, + {" test1 " MD5SUM " 1234 0x9876 42", + XF_E_SUCCESS, "test1", MD5SUM_L, 1234, 0x9876, 42, + XF_MAP_HAS_FILE_LENGTH|XF_MAP_HAS_MD5}, + {"test1 " MD5SUM " 1234 0x9876 42 \r", + XF_E_SUCCESS, "test1", MD5SUM_L, 1234, 0x9876, 42, + XF_MAP_HAS_FILE_LENGTH|XF_MAP_HAS_MD5}, + {"test1 " MD5SUM " 1234 0x9876 42 # junk", + XF_E_SYNTAX_ERROR, NULL, NULL, 0, 0, 0, 0}, + {OVERLONG_NAME, + XF_E_FILENAME_TOO_LONG, NULL, NULL, 0, 0, 0, 0}, + {LONG_NAME " " MD5SUM " 1234 0x9876 42", + XF_E_SUCCESS, LONG_NAME, MD5SUM_L, 1234, 0x9876, 42, + XF_MAP_HAS_FILE_LENGTH|XF_MAP_HAS_MD5}, + {"test1 " MD5SUM " 4294967295 0 0xffffffff", + XF_E_SUCCESS, "test1", MD5SUM_L, 4294967295, 0, 0xfffffffful, + XF_MAP_HAS_FILE_LENGTH|XF_MAP_HAS_MD5}, + {"test1 " MD5SUM " 4294967295 1 0xffffffff", + XF_E_INT_OVERFLOW, NULL, NULL, 0, 0, 0, 0}, + {"test1 " MD5SUM " 4294967296 0 0xffffffff", + XF_E_INT_OVERFLOW, NULL, NULL, 0, 0, 0, 0}, + {"test1 " MD5SUM " 100294967295 0 0xffffffff", + XF_E_INT_OVERFLOW, NULL, NULL, 0, 0, 0, 0}, + {"test1 " MD5SUM " 0 0 0xffffffff0", + XF_E_INT_OVERFLOW, NULL, NULL, 0, 0, 0, 0}, + {"test1 " MD5SUM " 0 0 0x100000000", + XF_E_INT_OVERFLOW, NULL, NULL, 0, 0, 0, 0}, + {"test1 " MD5SUM " 0 0 0x100000001", + XF_E_INT_OVERFLOW, NULL, NULL, 0, 0, 0, 0}, + {"<", + XF_E_SYNTAX_ERROR, NULL, NULL, 0, 0, 0, 0}, + {"test1 abcXdef", + XF_E_SYNTAX_ERROR, NULL, NULL, 0, 0, 0, 0}, + {"test1 " MD5SUM "0", + XF_E_SYNTAX_ERROR, NULL, NULL, 0, 0, 0, 0}, + {"test1 - 4567 0xabcd", + XF_E_SUCCESS, "test1", NULL, 0, 4567, 0xabcd, 0}, + {"test1 - 0 0x123456789", + XF_E_INT_OVERFLOW, NULL, NULL, 0, 0, 0, 0}, + {"test1 - 4f", + XF_E_SYNTAX_ERROR, NULL, NULL, 0, 0, 0, 0}, +}; + +static void md5_to_string(char* buf, const uint8_t* md5) +{ + const char* hex = "0123456789abcdef"; + for(int i = 0; i < 16; ++i) { + uint8_t byte = md5[i]; + buf[2*i] = hex[(byte >> 4) & 0xf]; + buf[2*i+1] = hex[byte & 0xf]; + } + + buf[32] = '\0'; +} + +int test_flashmap_parseline(void) +{ + int rc; + struct xf_map map; + char md5string[33]; + + size_t num_cases = sizeof(parseline_tests) / sizeof(struct parseline_test); + const struct parseline_test* test = &parseline_tests[0]; + + for(; num_cases > 0; --num_cases, ++test) { + rc = xf_map_parseline(test->line, &map); + + T_ASSERT(rc == test->rc); + if(rc != XF_E_SUCCESS) + continue; + + T_ASSERT(strcmp(map.name, test->name) == 0); + T_ASSERT(map.offset == test->offset); + T_ASSERT(map.length == test->length); + T_ASSERT(map.flags == test->flags); + + if(test->flags & XF_MAP_HAS_MD5) { + md5_to_string(md5string, map.md5); + T_ASSERT(strcmp(md5string, test->md5) == 0); + } + + if(test->flags & XF_MAP_HAS_FILE_LENGTH) { + T_ASSERT(map.file_length == test->file_length); + } + } + + cleanup: + return rc; +} -- cgit v1.2.3