forked from jacquesf/COBS-Consistent-Overhead-Byte-Stuffing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcobs_test.c
353 lines (312 loc) · 9.29 KB
/
cobs_test.c
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
/* Copyright 2011, Jacques Fortier. All rights reserved.
*
* Redistribution and use in source and binary forms are permitted, with or without modification.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "cobs.h"
#define MARKER_BYTE 0xAB
#define MAX_TEST_SIZE 260
#define ASSERT_EQUAL_LUINT(value, expected) \
do {\
if( (value) != (expected) ) { \
printf( "%30s: Failed, %s != %s. Expected %lu, got %lu\n", __func__, #value, #expected, (unsigned long)(expected), (unsigned long)(value) ); \
return false; \
} \
} while(0)
#define ASSERT_EQUAL_MEM(path, value, expected, length) \
do {\
for( size_t i = 0; i < (length); i++ ) { \
if( (value)[i] != (expected)[i] ) { \
printf( "%30s: Failed, %s != %s. Expected %s[%lu] = 0x%02X, got 0x%02X (%s)\n", __func__, #value, #expected, #value, i, (expected)[i], (value)[i], path ); \
return false; \
} \
} \
} while(0)
#define SETUP_TEST \
do { \
test_count++; \
memset(working_buffer, MARKER_BYTE, sizeof(working_buffer)); \
} while(0)
#ifdef COBS_ENCODE_ADD_TERMINATOR
#define FWD_TEST \
size_t encoded_length = cobs_encode( test_data, sizeof(test_data), working_buffer ); \
ASSERT_EQUAL_LUINT( encoded_length, sizeof(expected) + 1); \
ASSERT_EQUAL_MEM( "FWD", working_buffer, expected, sizeof(expected) ); \
ASSERT_EQUAL_LUINT( working_buffer[encoded_length - 1], 0 ); \
for (uint16_t i = sizeof(expected) + 1; i < MAX_TEST_SIZE; i++) \
{ \
if (working_buffer[i] != MARKER_BYTE) \
{ \
printf("Failed: encoding overwrote buffer at pos %d in %s\n", i, __func__); \
return false; \
} \
}
#else
#define FWD_TEST \
size_t encoded_length = cobs_encode( test_data, sizeof(test_data), working_buffer ); \
ASSERT_EQUAL_LUINT( encoded_length, sizeof(expected) ); \
ASSERT_EQUAL_MEM( "FWD", working_buffer, expected, sizeof(expected) ); \
for (uint16_t i = sizeof(expected); i < MAX_TEST_SIZE; i++) \
{ \
if (working_buffer[i] != MARKER_BYTE) \
{ \
printf("Failed: encoding overwrote buffer at pos %d in %s\n", i, __func__); \
return false; \
} \
}
#endif
#define REV_TEST \
memset(working_buffer, MARKER_BYTE, sizeof(working_buffer)); \
size_t decoded_length = cobs_decode( expected, sizeof(expected), working_buffer ); \
ASSERT_EQUAL_LUINT( decoded_length, sizeof(test_data)); \
ASSERT_EQUAL_MEM( "REV", working_buffer, test_data, sizeof(test_data) ); \
for (uint16_t i = sizeof(test_data); i < MAX_TEST_SIZE; i++) \
{ \
if (working_buffer[i] != MARKER_BYTE) \
{ \
printf("Failed: decoding overwrote buffer at pos %d in %s\n", i, __func__); \
return false; \
} \
}
static unsigned int test_count = 0;
static uint8_t working_buffer[MAX_TEST_SIZE];
// Wikipedia Example 1
bool test_single_null( void )
{
SETUP_TEST;
uint8_t test_data[] = { 0 };
uint8_t expected[] = { 1, 1 };
FWD_TEST;
REV_TEST;
return true;
}
// Wikipedia Example 2
bool test_double_null( void )
{
SETUP_TEST;
uint8_t test_data[] = { 0, 0 };
uint8_t expected[] = { 1, 1, 1 };
FWD_TEST;
REV_TEST;
return true;
}
// not in wikipedia
bool test_hex1( void )
{
SETUP_TEST;
uint8_t test_data[] = { 1 };
uint8_t expected[] = { 2, 1 };
FWD_TEST;
REV_TEST;
return true;
}
bool test_cobs_encode_three_bytes(void) // Wikipedia Example 3
{
SETUP_TEST;
uint8_t test_data[] = { 0x00, 0x11, 0x00 };
uint8_t expected[] = { 0x01, 0x02, 0x11, 0x01 };
FWD_TEST;
REV_TEST;
return true;
}
bool test_cobs_encode_short_with_null(void) // Wikipedia Example 4
{
SETUP_TEST;
uint8_t test_data[] = { 0x11, 0x22, 0x00, 0x33 };
uint8_t expected[] = { 0x03, 0x11, 0x22, 0x02, 0x33 };
FWD_TEST;
REV_TEST;
return true;
}
bool test_cobs_encode_short_no_null(void) // Wikipedia Example 5
{
SETUP_TEST;
uint8_t test_data[] = { 0x11, 0x22, 0x33, 0x44 };
uint8_t expected[] = { 0x05, 0x11, 0x22, 0x33, 0x44 };
FWD_TEST;
REV_TEST;
return true;
}
bool test_cobs_encode_short_successive_nulls(void) // Wikipedia Example 6
{
SETUP_TEST;
uint8_t test_data[] = { 0x11, 0x00, 0x00, 0x00 };
uint8_t expected[] = { 0x02, 0x11, 0x01, 0x01, 0x01 };
FWD_TEST;
REV_TEST;
return true;
}
bool test_cobs_encode_254_bytes_no_null(void) // Wikipedia Example 7
{
SETUP_TEST;
uint8_t test_data[254];
for( uint8_t i = 0; i < 254; i++ )
{
test_data[i] = i + 1; // 1 to 0xFE (254)
}
uint8_t expected[255];
expected[0] = 0xFF;
for( int i = 1; i <= 254; i++ )
{
expected[i] = i;
}
FWD_TEST;
REV_TEST;
return true;
}
bool test_cobs_encode_255_bytes_leading_null(void) // Wikipedia Example 8
{
SETUP_TEST;
uint8_t test_data[255];
for( uint8_t i = 0; i < 255; i++ )
{
test_data[i] = i; // 0x00 to 0xFE (254)
}
uint8_t expected[256];
expected[0] = 0x01;
expected[1] = 0xFF;
for( uint8_t i = 1; i <= 254; i++ )
{
expected[i + 1] = i; // write 1 to 254 from index 2 to 255
}
FWD_TEST;
REV_TEST;
return true;
}
bool test_cobs_encode_255_bytes_no_null(void) // Wikipedia Example 9
{
SETUP_TEST;
uint8_t test_data[255];
for( uint8_t i = 0; i < 255; i++ )
{
test_data[i] = i + 1; // 0x01 to 0xFF(255)
}
uint8_t expected[257];
expected[0] = 0xFF;
for( int i = 1; i <= 254; i++ )
{
expected[i] = i; // write 0x01 to 0xFE from index 1 to 254
}
expected[255] = 0x02;
expected[256] = 0xFF;
FWD_TEST;
REV_TEST;
return true;
}
bool test_cobs_encode_254_bytes_trailing_null(void) // Wikipedia Example 10
{
SETUP_TEST;
uint8_t test_data[255];
for( uint8_t i = 0; i <= 253; i++ )
{
test_data[i] = i + 2; // 0x02 to 0xFF to indexes 0 to 253
}
test_data[254] = 0x00; // trailing null
uint8_t expected[257];
expected[0] = 0xFF;
for( int i = 1; i <= 254; i++ )
{
expected[i] = i + 1; // write 0x02 to 0xFF from index 1 to 254
}
expected[255] = 0x01;
expected[256] = 0x01;
FWD_TEST;
REV_TEST;
return true;
}
bool test_cobs_encode_254_bytes_trailing_null_one(void) // Wikipedia Example 11
{
SETUP_TEST;
uint8_t test_data[255];
for( uint8_t i = 0; i <= 252; i++ )
{
test_data[i] = i + 3; // 0x03 to 0xFF to indexes 0 to 252
}
test_data[253] = 0x00;
test_data[254] = 0x01;
uint8_t expected[256];
expected[0] = 0xFE;
for( int i = 1; i <= 253; i++ )
{
expected[i] = i + 2; // write 0x03 to 0xFF from index 1 to 254
}
expected[254] = 0x02;
expected[255] = 0x01;
FWD_TEST;
REV_TEST;
return true;
}
// We're done testing the correctness of encode/decode. WHat remains now is to check that the decoder
// returns zero when passed an invalid COBS packet - i.e. when the header (and/or the bytes it links
// to) extend beyond the length of the input.
bool test_utils_cobs_decode_header_too_large_1(void)
{
SETUP_TEST;
// header byte 0x2d = 45 => overrun
uint8_t input[] = {0x2d,0x51,0x32,0x30,0x32,0x31,0x30,0x31, //8
0x38,0x20,0x31,0x38,0x3a,0x32,0x39,0x3a, //16
0x32,0x39,0x20,0x2e,0x20,0x53,0x74,0x61, //24
0x72,0x74,0x20,0x6d,0x65,0x61,0x73,0x75, //32
0x72,0x65,0x6d,0x65,0x6e,0x74,0x20,0x5b, //40
0x39,0x5d,0x0a,0x12}; //44 bytes
uint8_t output[sizeof(input)];
uint16_t res = cobs_decode(input, sizeof(input), output);
ASSERT_EQUAL_LUINT(0, res); // signals that decoding failed
return true;
}
bool test_utils_cobs_decode_header_too_large_2(void)
{
SETUP_TEST;
// header byte 0x0f (15), byte 15 = 0xff (255) => overrun
uint8_t input[] = {0x0f,0x51,0x32,0x30,0x32,0x31,0x30,0x31, //8
0x38,0x20,0x31,0x38,0x3a,0x32,0x39,0xff, //16
0x32,0x39,0x20,0x2e,0x20,0x53,0x74,0x61, //24
0x72,0x74,0x20,0x6d,0x65,0x61,0x73,0x75, //32
0x72,0x65,0x6d,0x65,0x6e,0x74,0x20,0x5b, //40
0x39,0x5d,0x0a,0x12}; //44 bytes
uint16_t res = cobs_decode(input, sizeof(input), working_buffer);
ASSERT_EQUAL_LUINT(0, res); // signals that decoding failed
return true;
}
bool test_utils_cobs_fail_on_null(void)
{
SETUP_TEST;
// decoding should fail if a NULL byte is encountered anywhere in the encoded COBS
// we start with a small frame that decodes OK.
uint8_t good_input[] = { 0x03, 0x11, 0x22, 0x03, 0x33, 0x44};
uint16_t res = cobs_decode(good_input, sizeof(good_input), working_buffer);
ASSERT_EQUAL_LUINT(5, res); // decoded OK
// now we check that setting any byte to 0x00 fails
uint8_t bad_input[sizeof(good_input)];
for (uint8_t i = 0; i < sizeof(good_input); i++)
{
memcpy(bad_input, good_input, sizeof(good_input));
bad_input[i] = 0;
res = cobs_decode(bad_input,sizeof(good_input),working_buffer);
ASSERT_EQUAL_LUINT(0, res); // fail
}
return true;
}
int main(int argc, char*argv[])
{
test_single_null();
test_double_null();
test_hex1();
test_cobs_encode_three_bytes();
test_cobs_encode_short_with_null();
test_cobs_encode_short_no_null();
test_cobs_encode_short_successive_nulls();
test_cobs_encode_254_bytes_no_null();
test_cobs_encode_255_bytes_leading_null();
test_cobs_encode_255_bytes_no_null();
test_cobs_encode_254_bytes_trailing_null();
test_cobs_encode_254_bytes_trailing_null_one();
test_utils_cobs_decode_header_too_large_1();
test_utils_cobs_decode_header_too_large_2();
test_utils_cobs_fail_on_null();
printf("ran %d COBS unit tests\n", test_count);
return 0;
}