-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_unit.c
More file actions
355 lines (298 loc) · 9.35 KB
/
Copy pathtest_unit.c
File metadata and controls
355 lines (298 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* Unit tests for hmcfgusb
*
* Tests hardware-independent modules: util.c, firmware.c, hm.h macros.
* Uses assert() — failure aborts with file/line info.
*
* Build and run: CC=gcc make test
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
/* Include firmware.c directly to access static crc16() */
#include "firmware.c"
/* hm.h for packet macros (SRC, DST, etc.) */
#include "hm.h"
static int tests_run = 0;
static int tests_passed = 0;
#define TEST(name) do { \
tests_run++; \
printf(" %s... ", #name); \
name(); \
tests_passed++; \
printf("OK\n"); \
} while(0)
/* ── util.c tests ───────────────────────────────────────────────── */
static void test_ascii_to_nibble_digits(void)
{
assert(ascii_to_nibble('0') == 0);
assert(ascii_to_nibble('1') == 1);
assert(ascii_to_nibble('5') == 5);
assert(ascii_to_nibble('9') == 9);
}
static void test_ascii_to_nibble_upper(void)
{
assert(ascii_to_nibble('A') == 10);
assert(ascii_to_nibble('B') == 11);
assert(ascii_to_nibble('F') == 15);
}
static void test_ascii_to_nibble_lower(void)
{
assert(ascii_to_nibble('a') == 10);
assert(ascii_to_nibble('c') == 12);
assert(ascii_to_nibble('f') == 15);
}
static void test_ascii_to_nibble_invalid(void)
{
assert(ascii_to_nibble('G') == 0);
assert(ascii_to_nibble('z') == 0);
assert(ascii_to_nibble(' ') == 0);
assert(ascii_to_nibble('/') == 0);
assert(ascii_to_nibble(':') == 0);
}
static void test_nibble_to_ascii_all(void)
{
const char *expected = "0123456789ABCDEF";
for (uint8_t i = 0; i < 16; i++) {
assert(nibble_to_ascii(i) == expected[i]);
}
}
static void test_nibble_to_ascii_masked(void)
{
/* Values > 15 should be masked with & 0xf */
assert(nibble_to_ascii(0x10) == '0');
assert(nibble_to_ascii(0x1A) == 'A');
assert(nibble_to_ascii(0xFF) == 'F');
}
static void test_validate_nibble_valid(void)
{
assert(validate_nibble('0') == 1);
assert(validate_nibble('9') == 1);
assert(validate_nibble('A') == 1);
assert(validate_nibble('F') == 1);
assert(validate_nibble('a') == 1);
assert(validate_nibble('f') == 1);
}
static void test_validate_nibble_invalid(void)
{
assert(validate_nibble('G') == 0);
assert(validate_nibble('g') == 0);
assert(validate_nibble(' ') == 0);
assert(validate_nibble('/') == 0);
assert(validate_nibble(':') == 0);
assert(validate_nibble('@') == 0);
assert(validate_nibble('`') == 0);
assert(validate_nibble(0x00) == 0);
assert(validate_nibble(0xFF) == 0);
}
static void test_validate_nibble_boundaries(void)
{
/* Just below/above valid ranges */
assert(validate_nibble('0' - 1) == 0);
assert(validate_nibble('9' + 1) == 0);
assert(validate_nibble('A' - 1) == 0);
assert(validate_nibble('F' + 1) == 0);
assert(validate_nibble('a' - 1) == 0);
assert(validate_nibble('f' + 1) == 0);
}
static void test_ascii_nibble_roundtrip(void)
{
/* Converting nibble→ascii→nibble should be identity for 0-15 */
for (uint8_t i = 0; i < 16; i++) {
char c = nibble_to_ascii(i);
assert(ascii_to_nibble((uint8_t)c) == i);
}
}
/* ── hm.h macro tests ──────────────────────────────────────────── */
static void test_src_dst_roundtrip(void)
{
uint8_t buf[16] = {0};
uint32_t src = 0x123456;
uint32_t dst = 0xABCDEF;
SET_SRC(buf, src);
SET_DST(buf, dst);
assert((uint32_t)SRC(buf) == src);
assert((uint32_t)DST(buf) == dst);
}
static void test_src_dst_zero(void)
{
uint8_t buf[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF};
SET_SRC(buf, 0x000000);
SET_DST(buf, 0x000000);
assert(SRC(buf) == 0x000000);
assert(DST(buf) == 0x000000);
}
static void test_src_dst_max(void)
{
uint8_t buf[16] = {0};
SET_SRC(buf, 0xFFFFFF);
SET_DST(buf, 0xFFFFFF);
assert(SRC(buf) == 0xFFFFFF);
assert(DST(buf) == 0xFFFFFF);
}
static void test_src_dst_independent(void)
{
uint8_t buf[16] = {0};
SET_SRC(buf, 0xAABBCC);
SET_DST(buf, 0x112233);
/* Changing DST should not affect SRC and vice versa */
assert(SRC(buf) == 0xAABBCC);
assert(DST(buf) == 0x112233);
SET_SRC(buf, 0x000000);
assert(DST(buf) == 0x112233);
}
static void test_payloadlen_roundtrip(void)
{
uint8_t buf[16] = {0};
SET_LEN_FROM_PAYLOADLEN(buf, 0);
assert(buf[LEN] == 0x09);
assert(PAYLOADLEN(buf) == 0);
SET_LEN_FROM_PAYLOADLEN(buf, 10);
assert(buf[LEN] == 19);
assert(PAYLOADLEN(buf) == 10);
SET_LEN_FROM_PAYLOADLEN(buf, 37);
assert(buf[LEN] == 46);
assert(PAYLOADLEN(buf) == 37);
}
static void test_hm_field_offsets(void)
{
uint8_t buf[16] = {0};
buf[LEN] = 0x15;
buf[MSGID] = 0x42;
buf[CTL] = 0xA4;
buf[TYPE] = 0x11;
assert(buf[0x00] == 0x15);
assert(buf[0x01] == 0x42);
assert(buf[0x02] == 0xA4);
assert(buf[0x03] == 0x11);
assert(PAYLOAD == 0x0a);
}
/* ── firmware.c tests (crc16) ───────────────────────────────────── */
static void test_crc16_empty(void)
{
uint8_t buf[1] = {0};
/* Zero-length input should return the initial CRC unchanged */
assert(crc16(buf, 0, CRC16_INIT) == CRC16_INIT);
assert(crc16(buf, 0, 0x0000) == 0x0000);
assert(crc16(buf, 0, 0x1234) == 0x1234);
}
static void test_crc16_deterministic(void)
{
uint8_t data[] = {0xDE, 0xAD, 0xBE, 0xEF};
uint16_t crc1 = crc16(data, sizeof(data), CRC16_INIT);
uint16_t crc2 = crc16(data, sizeof(data), CRC16_INIT);
assert(crc1 == crc2);
}
static void test_crc16_different_data(void)
{
uint8_t data1[] = {0x00};
uint8_t data2[] = {0x01};
uint16_t crc1 = crc16(data1, 1, CRC16_INIT);
uint16_t crc2 = crc16(data2, 1, CRC16_INIT);
assert(crc1 != crc2);
}
static void test_crc16_different_init(void)
{
uint8_t data[] = {0x41};
uint16_t crc1 = crc16(data, 1, 0x0000);
uint16_t crc2 = crc16(data, 1, 0xFFFF);
assert(crc1 != crc2);
}
static void test_crc16_not_mutate_input(void)
{
uint8_t data[] = {0xDE, 0xAD, 0xBE, 0xEF};
uint8_t copy[] = {0xDE, 0xAD, 0xBE, 0xEF};
crc16(data, sizeof(data), CRC16_INIT);
assert(memcmp(data, copy, sizeof(data)) == 0);
}
/* ── firmware.c tests (firmware_read_firmware) ──────────────────── */
static void test_firmware_read_eq3(void)
{
struct firmware *fw = firmware_read_firmware("testdata/test.eq3",
ATMEGA_UNKNOWN, 0);
assert(fw != NULL);
assert(fw->fw_blocks == 2);
/* Block 0: 4 bytes of DEADBEEF */
assert(fw->fw[0][0] == 0x00); /* block num high */
assert(fw->fw[0][1] == 0x00); /* block num low */
assert(fw->fw[0][2] == 0x00); /* len high */
assert(fw->fw[0][3] == 0x04); /* len low */
assert(fw->fw[0][4] == 0xDE);
assert(fw->fw[0][5] == 0xAD);
assert(fw->fw[0][6] == 0xBE);
assert(fw->fw[0][7] == 0xEF);
/* Block 1: 2 bytes of CAFE */
assert(fw->fw[1][0] == 0x00); /* block num high */
assert(fw->fw[1][1] == 0x01); /* block num low */
assert(fw->fw[1][2] == 0x00); /* len high */
assert(fw->fw[1][3] == 0x02); /* len low */
assert(fw->fw[1][4] == 0xCA);
assert(fw->fw[1][5] == 0xFE);
firmware_free(fw);
}
static void test_firmware_read_ihex(void)
{
struct firmware *fw = firmware_read_firmware("testdata/test.ihex",
ATMEGA_328P, 0);
assert(fw != NULL);
/* ATmega328P: image_size=0x7000, block_length=128 → 224 blocks */
assert(fw->fw_blocks == 224);
/* First block should contain our data at the start */
assert(fw->fw[0][0] == 0x00); /* block 0 high */
assert(fw->fw[0][1] == 0x00); /* block 0 low */
assert(fw->fw[0][2] == 0x00); /* len 128 high */
assert(fw->fw[0][3] == 0x80); /* len 128 low */
/* Data bytes from ihex record at address 0x0000 */
assert(fw->fw[0][4] == 0xDE);
assert(fw->fw[0][5] == 0xAD);
assert(fw->fw[0][6] == 0xBE);
assert(fw->fw[0][7] == 0xEF);
/* Rest of first block should be 0xFF (uninitialized image) */
assert(fw->fw[0][8] == 0xFF);
firmware_free(fw);
}
static void test_firmware_free_null_blocks(void)
{
/* firmware_free() with 0 blocks should not crash */
struct firmware *fw = malloc(sizeof(struct firmware));
assert(fw != NULL);
fw->fw = NULL;
fw->fw_blocks = 0;
firmware_free(fw);
}
/* ── main ───────────────────────────────────────────────────────── */
int main(void)
{
printf("Running unit tests...\n\n");
printf("util.c:\n");
TEST(test_ascii_to_nibble_digits);
TEST(test_ascii_to_nibble_upper);
TEST(test_ascii_to_nibble_lower);
TEST(test_ascii_to_nibble_invalid);
TEST(test_nibble_to_ascii_all);
TEST(test_nibble_to_ascii_masked);
TEST(test_validate_nibble_valid);
TEST(test_validate_nibble_invalid);
TEST(test_validate_nibble_boundaries);
TEST(test_ascii_nibble_roundtrip);
printf("\nhm.h macros:\n");
TEST(test_src_dst_roundtrip);
TEST(test_src_dst_zero);
TEST(test_src_dst_max);
TEST(test_src_dst_independent);
TEST(test_payloadlen_roundtrip);
TEST(test_hm_field_offsets);
printf("\nfirmware.c (crc16):\n");
TEST(test_crc16_empty);
TEST(test_crc16_deterministic);
TEST(test_crc16_different_data);
TEST(test_crc16_different_init);
TEST(test_crc16_not_mutate_input);
printf("\nfirmware.c (firmware_read):\n");
TEST(test_firmware_read_eq3);
TEST(test_firmware_read_ihex);
TEST(test_firmware_free_null_blocks);
printf("\n%d/%d tests passed.\n", tests_passed, tests_run);
return (tests_passed == tests_run) ? 0 : 1;
}