summaryrefslogtreecommitdiff
path: root/lib/x1000-installer/test
diff options
context:
space:
mode:
Diffstat (limited to 'lib/x1000-installer/test')
-rw-r--r--lib/x1000-installer/test/data/lines_shuffled.txt108
-rw-r--r--lib/x1000-installer/test/data/lines_sorted.txt36
-rw-r--r--lib/x1000-installer/test/main.c86
-rw-r--r--lib/x1000-installer/test/test.h39
-rw-r--r--lib/x1000-installer/test/test_flashmap.c140
-rw-r--r--lib/x1000-installer/test/test_stream.c108
6 files changed, 0 insertions, 517 deletions
diff --git a/lib/x1000-installer/test/data/lines_shuffled.txt b/lib/x1000-installer/test/data/lines_shuffled.txt
deleted file mode 100644
index c138fe7e7c..0000000000
--- a/lib/x1000-installer/test/data/lines_shuffled.txt
+++ /dev/null
@@ -1,108 +0,0 @@
11234567890abcdefghijklmnopqrstuvw
21234567890abcdefghijklmnopqrst
3123
41234567890abcdef
51234567890abcdefghijklm
61234567890abcdefghijklmn
71234567890abcdefghijk
81234567890abcdefgh
9123456
101234567
111234567890a
121234567890abcdefghijklm
131
141234567890abcdefghijklmnopq
151234567890abcdefghijkl
161234567890abcdefghijklmno
171234567890abcdefghijklmnopqrstuvwxyz
181234567890abcdefghijklmnopq
191234567890abcdefghijklmno
201234567890abcdefghijklmnopq
211234567890abcdefghijklmn
221234
231234567890abcdefghijklmnop
24123456789
251234
2612345
271234567890ab
281234567890abcdefghijklmnopqrstuvwxy
291234567890abcdefghijklmnopqrst
301234567890abcdefghijklmnopqrs
311234567890abcdefghijklmnopqrs
3212345678
331234567890ab
341234567890a
351234567890abcdefghijklmnopqr
36123456
371234567890abcdefghijkl
381234567890abcdefghijklm
391234567890abcdefghijklmnopqrstuvwxy
401234567890abcdefghijklmnopqrstuvwxy
411234567890abcdefghijklmnopqrstuv
421234567890abcdefghijklmnopqrstuv
431234567890abcdefghijklmnopqrstuvwx
441234567890abcde
451
461234567890abcdefgh
471234567890abcdef
481234567890abcdefghi
491234567890
501
5112345
52123456789
531234567
541234567890abcdefghi
551234567890abcdefghijklmnopqrstu
561234567890abcdefghijklmnopqrstuvwx
571234567890abcdefghijklmnopqrstuv
581234567890abcd
5912345
601234567890abcdefg
611234567890abcd
621234567890abcdefghijklmnopqrstuvw
631234567890abcdefghijklmnopqrstuvwxyz
6412345678
651234567890abcd
661234567890abcdefghijklmnop
67123
681234567890abcdefg
69123456
701234567890abcdefghij
711234567890abcdefg
721234567890abcdefghi
731234567890
741234567890abcdefghijklmno
751234567890abcdefghij
761234567890abcdefghijklmn
771234567890abcde
781234567890abc
79123456789
801234567890abcdefgh
811234567890abcdefghij
821234567890abcdefghijklmnopqrstuvwx
831234567890abcdefghijklmnopqr
841234567890abcdefghijklmnopqrst
851234567890abcdefghijklmnopqrs
861234567890abcdefghijklmnopqrstuvwxyz
871234567890abcdefghijkl
881234
8912
9012
9112345678
921234567
931234567890
94123
951234567890abc
961234567890abcdefghijklmnopqr
9712
981234567890abcde
991234567890abcdef
1001234567890a
1011234567890ab
1021234567890abcdefghijk
1031234567890abcdefghijklmnopqrstu
1041234567890abcdefghijklmnop
1051234567890abcdefghijk
1061234567890abcdefghijklmnopqrstu
1071234567890abcdefghijklmnopqrstuvw
1081234567890abc
diff --git a/lib/x1000-installer/test/data/lines_sorted.txt b/lib/x1000-installer/test/data/lines_sorted.txt
deleted file mode 100644
index 4944efd614..0000000000
--- a/lib/x1000-installer/test/data/lines_sorted.txt
+++ /dev/null
@@ -1,36 +0,0 @@
11
212
3123
41234
512345
6123456
71234567
812345678
9123456789
101234567890
111234567890a
121234567890ab
131234567890abc
141234567890abcd
151234567890abcde
161234567890abcdef
171234567890abcdefg
181234567890abcdefgh
191234567890abcdefghi
201234567890abcdefghij
211234567890abcdefghijk
221234567890abcdefghijkl
231234567890abcdefghijklm
241234567890abcdefghijklmn
251234567890abcdefghijklmno
261234567890abcdefghijklmnop
271234567890abcdefghijklmnopq
281234567890abcdefghijklmnopqr
291234567890abcdefghijklmnopqrs
301234567890abcdefghijklmnopqrst
311234567890abcdefghijklmnopqrstu
321234567890abcdefghijklmnopqrstuv
331234567890abcdefghijklmnopqrstuvw
341234567890abcdefghijklmnopqrstuvwx
351234567890abcdefghijklmnopqrstuvwxy
361234567890abcdefghijklmnopqrstuvwxyz
diff --git a/lib/x1000-installer/test/main.c b/lib/x1000-installer/test/main.c
deleted file mode 100644
index f9bff1b704..0000000000
--- a/lib/x1000-installer/test/main.c
+++ /dev/null
@@ -1,86 +0,0 @@
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 <stdio.h>
23
24static int test_num_executed = 0;
25static int test_num_failed = 0;
26int test_num_asserts_executed = 0;
27int test_num_asserts_failed = 0;
28
29void test_failure(const char* file, int line, const char* msg)
30{
31 fprintf(stderr, "%s:%d: ASSERTION FAILED: %s\n", file, line, msg);
32 ++test_num_asserts_failed;
33}
34
35typedef void(*test_t)(void);
36
37struct test_info {
38 const char* name;
39 test_t func;
40};
41
42extern void test_stream_read_lines(void);
43extern void test_stream_read_line_too_long(void);
44extern void test_flashmap_parseline(void);
45
46#define TEST(x) {#x, x}
47static const struct test_info all_tests[] = {
48 TEST(test_stream_read_lines),
49 TEST(test_stream_read_line_too_long),
50 TEST(test_flashmap_parseline),
51};
52#undef TEST
53
54void run_test(const struct test_info* tinfo)
55{
56 int asserts_now = test_num_asserts_failed;
57 ++test_num_executed;
58
59 fprintf(stderr, "RUN %s\n", tinfo->name);
60 tinfo->func();
61
62 if(test_num_asserts_failed > asserts_now) {
63 fprintf(stderr, " %s: FAILED!\n", tinfo->name);
64 ++test_num_failed;
65 }
66}
67
68int main(int argc, char* argv[])
69{
70 (void)argc;
71 (void)argv;
72
73 size_t num_tests = sizeof(all_tests) / sizeof(struct test_info);
74 for(size_t i = 0; i < num_tests; ++i)
75 run_test(&all_tests[i]);
76
77 fprintf(stderr, "------------------------------------------\n");
78 fprintf(stderr, "TEST COMPLETE\n");
79 fprintf(stderr, " Tests %d failed / %d executed\n",
80 test_num_failed, test_num_executed);
81 fprintf(stderr, " Assertions %d failed / %d executed\n",
82 test_num_asserts_failed, test_num_asserts_executed);
83
84 if(test_num_failed > 0)
85 return 1;
86}
diff --git a/lib/x1000-installer/test/test.h b/lib/x1000-installer/test/test.h
deleted file mode 100644
index 3d47a19d87..0000000000
--- a/lib/x1000-installer/test/test.h
+++ /dev/null
@@ -1,39 +0,0 @@
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#ifndef TEST_H
23#define TEST_H
24
25extern int test_num_asserts_executed;
26extern int test_num_asserts_failed;
27
28extern void test_failure(const char* file, int line, const char* msg);
29
30#define T_ASSERT(cond) \
31 do { \
32 ++test_num_asserts_executed; \
33 if(!(cond)) { \
34 test_failure(__FILE__, __LINE__, #cond); \
35 goto cleanup; \
36 } \
37 } while(0)
38
39#endif
diff --git a/lib/x1000-installer/test/test_flashmap.c b/lib/x1000-installer/test/test_flashmap.c
deleted file mode 100644
index 5e7aa39d78..0000000000
--- a/lib/x1000-installer/test/test_flashmap.c
+++ /dev/null
@@ -1,140 +0,0 @@
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
40static 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
95static 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
107int 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}
diff --git a/lib/x1000-installer/test/test_stream.c b/lib/x1000-installer/test/test_stream.c
deleted file mode 100644
index b4a5723b76..0000000000
--- a/lib/x1000-installer/test/test_stream.c
+++ /dev/null
@@ -1,108 +0,0 @@
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_error.h"
25#include "file.h"
26#include <string.h>
27#include <stdbool.h>
28#include <stdio.h>
29
30#define TESTDATADIR "test/data/"
31#define TESTFILE_SHUFFLED TESTDATADIR "lines_shuffled.txt"
32#define TESTFILE_SORTED TESTDATADIR "lines_sorted.txt"
33
34const char line_string[] = "1234567890abcdefghijklmnopqrstuvwxyz";
35
36static int read_line_cb(int n, char* buf, void* arg)
37{
38 int* max_n = (int*)arg;
39 *max_n = n;
40
41 T_ASSERT(strlen(buf) > 0);
42 T_ASSERT(strncmp(line_string, buf, strlen(buf)) == 0);
43
44 cleanup:
45 return 0;
46}
47
48void test_stream_read_lines(void)
49{
50 struct xf_stream s;
51 int rc;
52 char buf[100];
53 bool s_open = false;
54
55 for(int bufsz = 38; bufsz <= 100; bufsz += 1) {
56 rc = xf_open_file(TESTFILE_SHUFFLED, O_RDONLY, &s);
57 T_ASSERT(rc == 0);
58 s_open = true;
59
60 int max_n = 0;
61 rc = xf_stream_read_lines(&s, buf, bufsz, &read_line_cb, &max_n);
62 T_ASSERT(rc == 0);
63 T_ASSERT(max_n+1 == 108);
64
65 xf_stream_close(&s);
66 s_open = false;
67 }
68
69 cleanup:
70 if(s_open)
71 xf_stream_close(&s);
72 return;
73}
74
75void test_stream_read_line_too_long(void)
76{
77 struct xf_stream s;
78 int rc;
79 char buf[38];
80 bool s_open = false;
81
82 for(int bufsz = 0; bufsz <= 38; bufsz += 1) {
83 rc = xf_open_file(TESTFILE_SORTED, O_RDONLY, &s);
84 T_ASSERT(rc == 0);
85 s_open = true;
86
87 int max_n = -1;
88 rc = xf_stream_read_lines(&s, buf, bufsz, &read_line_cb, &max_n);
89 if(bufsz == 38) {
90 T_ASSERT(rc == 0);
91 T_ASSERT(max_n+1 == 36);
92 } else {
93 T_ASSERT(rc == XF_E_LINE_TOO_LONG);
94 if(bufsz <= 1)
95 T_ASSERT(max_n == -1);
96 else
97 T_ASSERT(max_n+1 == bufsz-2);
98 }
99
100 xf_stream_close(&s);
101 s_open = false;
102 }
103
104 cleanup:
105 if(s_open)
106 xf_stream_close(&s);
107 return;
108}