diff options
author | Aidan MacDonald <amachronic@protonmail.com> | 2021-11-28 14:45:11 +0000 |
---|---|---|
committer | Aidan MacDonald <amachronic@protonmail.com> | 2021-11-28 15:46:02 +0000 |
commit | 31242f8570c870f60feecb6a5039d19863ecdc55 (patch) | |
tree | e3d596b8fd246ae2b5747125381adb7fb1889fa6 | |
parent | 4879891da3f5aad8ca525f97be56b780b7df7af9 (diff) | |
download | rockbox-31242f8570c870f60feecb6a5039d19863ecdc55.tar.gz rockbox-31242f8570c870f60feecb6a5039d19863ecdc55.zip |
x1000-installer: add test for xf_map_parseline
Change-Id: I35db43c83dc3964607b95f6b5c300c2fef455ac8
-rw-r--r-- | lib/x1000-installer/Makefile | 1 | ||||
-rw-r--r-- | lib/x1000-installer/test/main.c | 2 | ||||
-rw-r--r-- | lib/x1000-installer/test/test_flashmap.c | 140 |
3 files changed, 143 insertions, 0 deletions
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 \ | |||
13 | test_lib/md5.o \ | 13 | test_lib/md5.o \ |
14 | test_lib/strlcpy.o \ | 14 | test_lib/strlcpy.o \ |
15 | test/main.o \ | 15 | test/main.o \ |
16 | test/test_flashmap.o \ | ||
16 | test/test_stream.o | 17 | test/test_stream.o |
17 | TBIN = xf_test | 18 | TBIN = xf_test |
18 | 19 | ||
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 { | |||
41 | 41 | ||
42 | extern void test_stream_read_lines(void); | 42 | extern void test_stream_read_lines(void); |
43 | extern void test_stream_read_line_too_long(void); | 43 | extern void test_stream_read_line_too_long(void); |
44 | extern void test_flashmap_parseline(void); | ||
44 | 45 | ||
45 | #define TEST(x) {#x, x} | 46 | #define TEST(x) {#x, x} |
46 | static const struct test_info all_tests[] = { | 47 | static const struct test_info all_tests[] = { |
47 | TEST(test_stream_read_lines), | 48 | TEST(test_stream_read_lines), |
48 | TEST(test_stream_read_line_too_long), | 49 | TEST(test_stream_read_line_too_long), |
50 | TEST(test_flashmap_parseline), | ||
49 | }; | 51 | }; |
50 | #undef TEST | 52 | #undef TEST |
51 | 53 | ||
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 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2021 Aidan MacDonald | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "test.h" | ||
23 | #include "xf_stream.h" | ||
24 | #include "xf_flashmap.h" | ||
25 | #include "xf_error.h" | ||
26 | #include "file.h" | ||
27 | #include <string.h> | ||
28 | #include <stdbool.h> | ||
29 | #include <stdio.h> | ||
30 | |||
31 | #define MD5SUM \ | ||
32 | "abcdef012345ABCDEF012345abcdef01" | ||
33 | #define MD5SUM_L \ | ||
34 | "abcdef012345abcdef012345abcdef01" | ||
35 | #define LONG_NAME \ | ||
36 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | ||
37 | #define OVERLONG_NAME \ | ||
38 | LONG_NAME "a" | ||
39 | |||
40 | static const struct parseline_test { | ||
41 | const char* line; | ||
42 | int rc; | ||
43 | const char* name; | ||
44 | const char* md5; | ||
45 | uint32_t file_length; | ||
46 | uint32_t offset; | ||
47 | uint32_t length; | ||
48 | int flags; | ||
49 | } parseline_tests[] = { | ||
50 | {"test1 " MD5SUM " 1234 0x9876 42", | ||
51 | XF_E_SUCCESS, "test1", MD5SUM_L, 1234, 0x9876, 42, | ||
52 | XF_MAP_HAS_FILE_LENGTH|XF_MAP_HAS_MD5}, | ||
53 | {" test1 " MD5SUM " 1234 0x9876 42", | ||
54 | XF_E_SUCCESS, "test1", MD5SUM_L, 1234, 0x9876, 42, | ||
55 | XF_MAP_HAS_FILE_LENGTH|XF_MAP_HAS_MD5}, | ||
56 | {"test1 " MD5SUM " 1234 0x9876 42 \r", | ||
57 | XF_E_SUCCESS, "test1", MD5SUM_L, 1234, 0x9876, 42, | ||
58 | XF_MAP_HAS_FILE_LENGTH|XF_MAP_HAS_MD5}, | ||
59 | {"test1 " MD5SUM " 1234 0x9876 42 # junk", | ||
60 | XF_E_SYNTAX_ERROR, NULL, NULL, 0, 0, 0, 0}, | ||
61 | {OVERLONG_NAME, | ||
62 | XF_E_FILENAME_TOO_LONG, NULL, NULL, 0, 0, 0, 0}, | ||
63 | {LONG_NAME " " MD5SUM " 1234 0x9876 42", | ||
64 | XF_E_SUCCESS, LONG_NAME, MD5SUM_L, 1234, 0x9876, 42, | ||
65 | XF_MAP_HAS_FILE_LENGTH|XF_MAP_HAS_MD5}, | ||
66 | {"test1 " MD5SUM " 4294967295 0 0xffffffff", | ||
67 | XF_E_SUCCESS, "test1", MD5SUM_L, 4294967295, 0, 0xfffffffful, | ||
68 | XF_MAP_HAS_FILE_LENGTH|XF_MAP_HAS_MD5}, | ||
69 | {"test1 " MD5SUM " 4294967295 1 0xffffffff", | ||
70 | XF_E_INT_OVERFLOW, NULL, NULL, 0, 0, 0, 0}, | ||
71 | {"test1 " MD5SUM " 4294967296 0 0xffffffff", | ||
72 | XF_E_INT_OVERFLOW, NULL, NULL, 0, 0, 0, 0}, | ||
73 | {"test1 " MD5SUM " 100294967295 0 0xffffffff", | ||
74 | XF_E_INT_OVERFLOW, NULL, NULL, 0, 0, 0, 0}, | ||
75 | {"test1 " MD5SUM " 0 0 0xffffffff0", | ||
76 | XF_E_INT_OVERFLOW, NULL, NULL, 0, 0, 0, 0}, | ||
77 | {"test1 " MD5SUM " 0 0 0x100000000", | ||
78 | XF_E_INT_OVERFLOW, NULL, NULL, 0, 0, 0, 0}, | ||
79 | {"test1 " MD5SUM " 0 0 0x100000001", | ||
80 | XF_E_INT_OVERFLOW, NULL, NULL, 0, 0, 0, 0}, | ||
81 | {"<", | ||
82 | XF_E_SYNTAX_ERROR, NULL, NULL, 0, 0, 0, 0}, | ||
83 | {"test1 abcXdef", | ||
84 | XF_E_SYNTAX_ERROR, NULL, NULL, 0, 0, 0, 0}, | ||
85 | {"test1 " MD5SUM "0", | ||
86 | XF_E_SYNTAX_ERROR, NULL, NULL, 0, 0, 0, 0}, | ||
87 | {"test1 - 4567 0xabcd", | ||
88 | XF_E_SUCCESS, "test1", NULL, 0, 4567, 0xabcd, 0}, | ||
89 | {"test1 - 0 0x123456789", | ||
90 | XF_E_INT_OVERFLOW, NULL, NULL, 0, 0, 0, 0}, | ||
91 | {"test1 - 4f", | ||
92 | XF_E_SYNTAX_ERROR, NULL, NULL, 0, 0, 0, 0}, | ||
93 | }; | ||
94 | |||
95 | static void md5_to_string(char* buf, const uint8_t* md5) | ||
96 | { | ||
97 | const char* hex = "0123456789abcdef"; | ||
98 | for(int i = 0; i < 16; ++i) { | ||
99 | uint8_t byte = md5[i]; | ||
100 | buf[2*i] = hex[(byte >> 4) & 0xf]; | ||
101 | buf[2*i+1] = hex[byte & 0xf]; | ||
102 | } | ||
103 | |||
104 | buf[32] = '\0'; | ||
105 | } | ||
106 | |||
107 | int test_flashmap_parseline(void) | ||
108 | { | ||
109 | int rc; | ||
110 | struct xf_map map; | ||
111 | char md5string[33]; | ||
112 | |||
113 | size_t num_cases = sizeof(parseline_tests) / sizeof(struct parseline_test); | ||
114 | const struct parseline_test* test = &parseline_tests[0]; | ||
115 | |||
116 | for(; num_cases > 0; --num_cases, ++test) { | ||
117 | rc = xf_map_parseline(test->line, &map); | ||
118 | |||
119 | T_ASSERT(rc == test->rc); | ||
120 | if(rc != XF_E_SUCCESS) | ||
121 | continue; | ||
122 | |||
123 | T_ASSERT(strcmp(map.name, test->name) == 0); | ||
124 | T_ASSERT(map.offset == test->offset); | ||
125 | T_ASSERT(map.length == test->length); | ||
126 | T_ASSERT(map.flags == test->flags); | ||
127 | |||
128 | if(test->flags & XF_MAP_HAS_MD5) { | ||
129 | md5_to_string(md5string, map.md5); | ||
130 | T_ASSERT(strcmp(md5string, test->md5) == 0); | ||
131 | } | ||
132 | |||
133 | if(test->flags & XF_MAP_HAS_FILE_LENGTH) { | ||
134 | T_ASSERT(map.file_length == test->file_length); | ||
135 | } | ||
136 | } | ||
137 | |||
138 | cleanup: | ||
139 | return rc; | ||
140 | } | ||