Skip to content

Commit 4d93c68

Browse files
committed
Added unit test for wolfBoot_find_header
1 parent 944f816 commit 4d93c68

2 files changed

Lines changed: 189 additions & 0 deletions

File tree

tools/unit-tests/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
LDFLAGS=-lcheck -lm -pthread -lrt -lsubunit
2+
CFLAGS=-I../../src -I../../include
3+
4+
all: unit-parser
5+
6+
7+
unit-parser: unit-parser.o
8+
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
9+
10+
%.o:%.c
11+
gcc -c -o $@ $^ $(CFLAGS)
12+
13+
clean:
14+
rm -f unit-parser unit-parser.o

tools/unit-tests/unit-parser.c

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
#define WOLFBOOT_HASH_SHA256
2+
#define IMAGE_HEADER_SIZE 256
3+
#define UNIT_TEST
4+
#include <stdio.h>
5+
#include "libwolfboot.c"
6+
#include <check.h>
7+
static int locked = 0;
8+
9+
/* Mocks */
10+
void hal_init(void)
11+
{
12+
}
13+
int hal_flash_write(uint32_t address, const uint8_t *data, int len)
14+
{
15+
return 0;
16+
}
17+
int hal_flash_erase(uint32_t address, int len)
18+
{
19+
return 0;
20+
}
21+
void hal_flash_unlock(void)
22+
{
23+
fail_unless(locked, "Double unlock detected\n");
24+
locked--;
25+
}
26+
void hal_flash_lock(void)
27+
{
28+
fail_if(locked, "Double lock detected\n");
29+
locked++;
30+
}
31+
32+
void hal_prepare_boot(void)
33+
{
34+
}
35+
/* End Mocks */
36+
37+
Suite *wolfboot_suite(void);
38+
39+
static uint8_t test_buffer[512] = {
40+
'W', 'O', 'L', 'F', 0x00, 0x00, 0x01, 0x00,
41+
0x01, 0x00, 0x04, 0x00, 0x0d, 0x0c, 0x0b, 0x0a,
42+
0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0x00, 0x08, 0x00,
43+
0x07, 0x06, 0x05, 0x04, 0x03, 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F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
65+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
66+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
67+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
68+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
69+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
70+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
71+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
72+
/* End HDR */
73+
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
74+
};
75+
76+
START_TEST (test_parser_sunny)
77+
{
78+
uint8_t *p;
79+
int i;
80+
81+
/* Check version */
82+
fail_if(wolfBoot_find_header(test_buffer + 8, HDR_VERSION, &p) != 4, "Parser error: cannot locate version");
83+
fail_if((p[0] != 0x0d) || (p[1] != 0x0c) || (p[2] != 0x0b) || (p[3] != 0x0a), "Parser error: version doesn't match");
84+
85+
/* Check timestamp */
86+
fail_if(wolfBoot_find_header(test_buffer + 8, HDR_TIMESTAMP, &p) != 8, "Parser error: cannot locate timestamp");
87+
fail_if((p[0] != 0x07) || (p[1] != 0x06) || (p[2] != 0x05) || (p[3] != 0x04), "Parser error: timestamp doesn't match");
88+
fail_if((p[4] != 0x03) || (p[5] != 0x02) || (p[6] != 0x01) || (p[7] != 0x00), "Parser error: timestamp doesn't match");
89+
90+
/* Check sha256 field */
91+
fail_if(wolfBoot_find_header(test_buffer + 8, HDR_SHA256, &p) != 32, "Parser error: cannot locate hash");
92+
for (i = 0; i < 32; i++)
93+
fail_unless(p[i] == i, "Parser error: hash does not match");
94+
95+
/* Check non-existing field */
96+
fail_if(wolfBoot_find_header(test_buffer + 8, HDR_SHA3_384, &p) != 0, "Parser error: found a non-existing field");
97+
}
98+
END_TEST
99+
100+
START_TEST (test_parser_borders)
101+
{
102+
uint8_t *p;
103+
int i;
104+
uint8_t bad_buff[512];
105+
memset(bad_buff, 0xFF, 256);
106+
107+
/* Field out of bounds */
108+
bad_buff[256] = 0x02;
109+
bad_buff[257] = 0x00;
110+
bad_buff[258] = 0x04;
111+
bad_buff[259] = 0x00;
112+
fail_if(wolfBoot_find_header(bad_buff + 8, HDR_VERSION, &p) != 0, "Parser error: accessing version field out of bounds");
113+
114+
/* Single field too large */
115+
bad_buff[8] = 0x02;
116+
bad_buff[9] = 0x00;
117+
bad_buff[10] = 0xF8;
118+
bad_buff[11] = 0x00;
119+
fail_if(wolfBoot_find_header(bad_buff + 8, HDR_VERSION, &p) != 0, "Parser error: accessing version field out of bounds");
120+
121+
/* Second field too large */
122+
bad_buff[8] = 0x01;
123+
bad_buff[9] = 0x00;
124+
bad_buff[10] = 0x04;
125+
bad_buff[11] = 0x00;
126+
bad_buff[12] = 0x05;
127+
bad_buff[13] = 0x05;
128+
bad_buff[14] = 0x05;
129+
bad_buff[15] = 0x05;
130+
bad_buff[16] = 0x02;
131+
bad_buff[17] = 0x00;
132+
bad_buff[18] = 0xf0; /** Timestamp field too large **/
133+
bad_buff[19] = 0x00;
134+
fail_if(wolfBoot_find_header(bad_buff + 8, HDR_TIMESTAMP, &p) != 0, "Parser error: accessing version field out of bounds");
135+
136+
/* High memory access */
137+
fail_if(wolfBoot_find_header(((void *)(0 - 0xF8)), HDR_VERSION, &p) != 0);
138+
fail_if(wolfBoot_find_header(((void *)(0 - 0x10)), HDR_VERSION, &p) != 0);
139+
140+
}
141+
END_TEST
142+
143+
Suite *wolfboot_suite(void)
144+
{
145+
146+
/* Suite initialization */
147+
Suite *s = suite_create("wolfBoot");
148+
149+
/* Test cases */
150+
TCase *parser_sunny = tcase_create("Parser Sunny-day case");
151+
TCase *parser_borders = tcase_create("Parser test buffer borders");
152+
153+
/* Test function <-> Test case */
154+
tcase_add_test(parser_sunny, test_parser_sunny);
155+
tcase_add_test(parser_borders, test_parser_borders);
156+
157+
/* Set parameters + add to suite */
158+
tcase_set_timeout(parser_sunny, 20);
159+
suite_add_tcase(s, parser_sunny);
160+
suite_add_tcase(s, parser_borders);
161+
162+
return s;
163+
}
164+
165+
166+
int main(void)
167+
{
168+
int fails;
169+
Suite *s = wolfboot_suite();
170+
SRunner *sr = srunner_create(s);
171+
srunner_run_all(sr, CK_NORMAL);
172+
fails = srunner_ntests_failed(sr);
173+
srunner_free(sr);
174+
return fails;
175+
}

0 commit comments

Comments
 (0)