4 Copyright (c) 2016 Fred Sundvik
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #include <cgreen/cgreen.h>
26 #include <cgreen/mocks.h>
27 #include "serial_link/protocol/byte_stuffer.h"
28 #include "serial_link/protocol/byte_stuffer.c"
29 #include "serial_link/protocol/frame_validator.h"
30 #include "serial_link/protocol/physical.h"
32 static uint8_t sent_data[MAX_FRAME_SIZE*2];
33 static uint16_t sent_data_size;
35 Describe(ByteStuffer);
36 BeforeEach(ByteStuffer) {
40 AfterEach(ByteStuffer) {}
42 void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) {
46 void send_data(uint8_t link, const uint8_t* data, uint16_t size) {
47 memcpy(sent_data + sent_data_size, data, size);
48 sent_data_size += size;
51 Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) {
52 never_expect(validator_recv_frame);
53 byte_stuffer_recv_byte(0, 0);
56 Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) {
57 never_expect(validator_recv_frame);
58 byte_stuffer_recv_byte(0, 0xFF);
61 Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) {
62 never_expect(validator_recv_frame);
63 byte_stuffer_recv_byte(0, 0x4A);
66 Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) {
67 never_expect(validator_recv_frame);
68 byte_stuffer_recv_byte(0, 1);
69 byte_stuffer_recv_byte(0, 0);
72 Ensure(ByteStuffer, receives_single_byte_valid_frame) {
73 uint8_t expected[] = {0x37};
74 expect(validator_recv_frame,
75 when(size, is_equal_to(1)),
76 when(data, is_equal_to_contents_of(expected, 1))
78 byte_stuffer_recv_byte(0, 2);
79 byte_stuffer_recv_byte(0, 0x37);
80 byte_stuffer_recv_byte(0, 0);
83 Ensure(ByteStuffer, receives_three_bytes_valid_frame) {
84 uint8_t expected[] = {0x37, 0x99, 0xFF};
85 expect(validator_recv_frame,
86 when(size, is_equal_to(3)),
87 when(data, is_equal_to_contents_of(expected, 3))
89 byte_stuffer_recv_byte(0, 4);
90 byte_stuffer_recv_byte(0, 0x37);
91 byte_stuffer_recv_byte(0, 0x99);
92 byte_stuffer_recv_byte(0, 0xFF);
93 byte_stuffer_recv_byte(0, 0);
96 Ensure(ByteStuffer, receives_single_zero_valid_frame) {
97 uint8_t expected[] = {0};
98 expect(validator_recv_frame,
99 when(size, is_equal_to(1)),
100 when(data, is_equal_to_contents_of(expected, 1))
102 byte_stuffer_recv_byte(0, 1);
103 byte_stuffer_recv_byte(0, 1);
104 byte_stuffer_recv_byte(0, 0);
107 Ensure(ByteStuffer, receives_valid_frame_with_zeroes) {
108 uint8_t expected[] = {5, 0, 3, 0};
109 expect(validator_recv_frame,
110 when(size, is_equal_to(4)),
111 when(data, is_equal_to_contents_of(expected, 4))
113 byte_stuffer_recv_byte(0, 2);
114 byte_stuffer_recv_byte(0, 5);
115 byte_stuffer_recv_byte(0, 2);
116 byte_stuffer_recv_byte(0, 3);
117 byte_stuffer_recv_byte(0, 1);
118 byte_stuffer_recv_byte(0, 0);
121 Ensure(ByteStuffer, receives_two_valid_frames) {
122 uint8_t expected1[] = {5, 0};
123 uint8_t expected2[] = {3};
124 expect(validator_recv_frame,
125 when(size, is_equal_to(2)),
126 when(data, is_equal_to_contents_of(expected1, 2))
128 expect(validator_recv_frame,
129 when(size, is_equal_to(1)),
130 when(data, is_equal_to_contents_of(expected2, 1))
132 byte_stuffer_recv_byte(1, 2);
133 byte_stuffer_recv_byte(1, 5);
134 byte_stuffer_recv_byte(1, 1);
135 byte_stuffer_recv_byte(1, 0);
136 byte_stuffer_recv_byte(1, 2);
137 byte_stuffer_recv_byte(1, 3);
138 byte_stuffer_recv_byte(1, 0);
141 Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) {
142 uint8_t expected[] = {5, 7};
143 expect(validator_recv_frame,
144 when(size, is_equal_to(2)),
145 when(data, is_equal_to_contents_of(expected, 2))
147 byte_stuffer_recv_byte(1, 3);
148 byte_stuffer_recv_byte(1, 1);
149 byte_stuffer_recv_byte(1, 0);
150 byte_stuffer_recv_byte(1, 3);
151 byte_stuffer_recv_byte(1, 5);
152 byte_stuffer_recv_byte(1, 7);
153 byte_stuffer_recv_byte(1, 0);
156 Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) {
157 uint8_t expected[] = {5, 7};
158 expect(validator_recv_frame,
159 when(size, is_equal_to(2)),
160 when(data, is_equal_to_contents_of(expected, 2))
162 byte_stuffer_recv_byte(0, 2);
163 byte_stuffer_recv_byte(0, 9);
164 byte_stuffer_recv_byte(0, 4); // This should have been zero
165 byte_stuffer_recv_byte(0, 0);
166 byte_stuffer_recv_byte(0, 3);
167 byte_stuffer_recv_byte(0, 5);
168 byte_stuffer_recv_byte(0, 7);
169 byte_stuffer_recv_byte(0, 0);
172 Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_of_frame) {
173 uint8_t expected[254];
175 for (i=0;i<254;i++) {
178 expect(validator_recv_frame,
179 when(size, is_equal_to(254)),
180 when(data, is_equal_to_contents_of(expected, 254))
182 byte_stuffer_recv_byte(0, 0xFF);
183 for (i=0;i<254;i++) {
184 byte_stuffer_recv_byte(0, i+1);
186 byte_stuffer_recv_byte(0, 0);
189 Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_non_zero) {
190 uint8_t expected[255];
192 for (i=0;i<254;i++) {
196 expect(validator_recv_frame,
197 when(size, is_equal_to(255)),
198 when(data, is_equal_to_contents_of(expected, 255))
200 byte_stuffer_recv_byte(0, 0xFF);
201 for (i=0;i<254;i++) {
202 byte_stuffer_recv_byte(0, i+1);
204 byte_stuffer_recv_byte(0, 2);
205 byte_stuffer_recv_byte(0, 7);
206 byte_stuffer_recv_byte(0, 0);
209 Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_zero) {
210 uint8_t expected[255];
212 for (i=0;i<254;i++) {
216 expect(validator_recv_frame,
217 when(size, is_equal_to(255)),
218 when(data, is_equal_to_contents_of(expected, 255))
220 byte_stuffer_recv_byte(0, 0xFF);
221 for (i=0;i<254;i++) {
222 byte_stuffer_recv_byte(0, i+1);
224 byte_stuffer_recv_byte(0, 1);
225 byte_stuffer_recv_byte(0, 1);
226 byte_stuffer_recv_byte(0, 0);
229 Ensure(ByteStuffer, receives_two_long_frames_and_some_more) {
230 uint8_t expected[515];
234 for (i=0;i<254;i++) {
235 expected[i+254*j] = i + 1;
239 expected[254*2+i] = i + 1;
241 expect(validator_recv_frame,
242 when(size, is_equal_to(515)),
243 when(data, is_equal_to_contents_of(expected, 510))
245 byte_stuffer_recv_byte(0, 0xFF);
246 for (i=0;i<254;i++) {
247 byte_stuffer_recv_byte(0, i+1);
249 byte_stuffer_recv_byte(0, 0xFF);
250 for (i=0;i<254;i++) {
251 byte_stuffer_recv_byte(0, i+1);
253 byte_stuffer_recv_byte(0, 8);
254 byte_stuffer_recv_byte(0, 1);
255 byte_stuffer_recv_byte(0, 2);
256 byte_stuffer_recv_byte(0, 3);
257 byte_stuffer_recv_byte(0, 4);
258 byte_stuffer_recv_byte(0, 5);
259 byte_stuffer_recv_byte(0, 6);
260 byte_stuffer_recv_byte(0, 7);
261 byte_stuffer_recv_byte(0, 0);
264 Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) {
265 uint8_t expected[MAX_FRAME_SIZE] = {};
266 expect(validator_recv_frame,
267 when(size, is_equal_to(MAX_FRAME_SIZE)),
268 when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE))
271 byte_stuffer_recv_byte(0, 1);
272 for(i=0;i<MAX_FRAME_SIZE;i++) {
273 byte_stuffer_recv_byte(0, 1);
275 byte_stuffer_recv_byte(0, 0);
278 Ensure(ByteStuffer, doesnt_recv_a_frame_thats_too_long_all_zeroes) {
279 uint8_t expected[1] = {0};
280 never_expect(validator_recv_frame);
282 byte_stuffer_recv_byte(0, 1);
283 for(i=0;i<MAX_FRAME_SIZE;i++) {
284 byte_stuffer_recv_byte(0, 1);
286 byte_stuffer_recv_byte(0, 1);
287 byte_stuffer_recv_byte(0, 0);
290 Ensure(ByteStuffer, received_frame_is_aborted_when_its_too_long) {
291 uint8_t expected[1] = {1};
292 expect(validator_recv_frame,
293 when(size, is_equal_to(1)),
294 when(data, is_equal_to_contents_of(expected, 1))
297 byte_stuffer_recv_byte(0, 1);
298 for(i=0;i<MAX_FRAME_SIZE;i++) {
299 byte_stuffer_recv_byte(0, 1);
301 byte_stuffer_recv_byte(0, 2);
302 byte_stuffer_recv_byte(0, 1);
303 byte_stuffer_recv_byte(0, 0);
306 Ensure(ByteStuffer, does_nothing_when_sending_zero_size_frame) {
307 assert_that(sent_data_size, is_equal_to(0));
308 byte_stuffer_send_frame(0, NULL, 0);
311 Ensure(ByteStuffer, send_one_byte_frame) {
312 uint8_t data[] = {5};
313 byte_stuffer_send_frame(1, data, 1);
314 uint8_t expected[] = {2, 5, 0};
315 assert_that(sent_data_size, is_equal_to(sizeof(expected)));
316 assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
319 Ensure(ByteStuffer, sends_two_byte_frame) {
320 uint8_t data[] = {5, 0x77};
321 byte_stuffer_send_frame(0, data, 2);
322 uint8_t expected[] = {3, 5, 0x77, 0};
323 assert_that(sent_data_size, is_equal_to(sizeof(expected)));
324 assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
327 Ensure(ByteStuffer, sends_one_byte_frame_with_zero) {
328 uint8_t data[] = {0};
329 byte_stuffer_send_frame(0, data, 1);
330 uint8_t expected[] = {1, 1, 0};
331 assert_that(sent_data_size, is_equal_to(sizeof(expected)));
332 assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
335 Ensure(ByteStuffer, sends_two_byte_frame_starting_with_zero) {
336 uint8_t data[] = {0, 9};
337 byte_stuffer_send_frame(1, data, 2);
338 uint8_t expected[] = {1, 2, 9, 0};
339 assert_that(sent_data_size, is_equal_to(sizeof(expected)));
340 assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
343 Ensure(ByteStuffer, sends_two_byte_frame_starting_with_non_zero) {
344 uint8_t data[] = {9, 0};
345 byte_stuffer_send_frame(1, data, 2);
346 uint8_t expected[] = {2, 9, 1, 0};
347 assert_that(sent_data_size, is_equal_to(sizeof(expected)));
348 assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
351 Ensure(ByteStuffer, sends_three_byte_frame_zero_in_the_middle) {
352 uint8_t data[] = {9, 0, 0x68};
353 byte_stuffer_send_frame(0, data, 3);
354 uint8_t expected[] = {2, 9, 2, 0x68, 0};
355 assert_that(sent_data_size, is_equal_to(sizeof(expected)));
356 assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
359 Ensure(ByteStuffer, sends_three_byte_frame_data_in_the_middle) {
360 uint8_t data[] = {0, 0x55, 0};
361 byte_stuffer_send_frame(0, data, 3);
362 uint8_t expected[] = {1, 2, 0x55, 1, 0};
363 assert_that(sent_data_size, is_equal_to(sizeof(expected)));
364 assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
367 Ensure(ByteStuffer, sends_three_byte_frame_with_all_zeroes) {
368 uint8_t data[] = {0, 0, 0};
369 byte_stuffer_send_frame(0, data, 3);
370 uint8_t expected[] = {1, 1, 1, 1, 0};
371 assert_that(sent_data_size, is_equal_to(sizeof(expected)));
372 assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
375 Ensure(ByteStuffer, sends_frame_with_254_non_zeroes) {
381 byte_stuffer_send_frame(0, data, 254);
382 uint8_t expected[256];
388 assert_that(sent_data_size, is_equal_to(sizeof(expected)));
389 assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
392 Ensure(ByteStuffer, sends_frame_with_255_non_zeroes) {
398 byte_stuffer_send_frame(0, data, 255);
399 uint8_t expected[258];
407 assert_that(sent_data_size, is_equal_to(sizeof(expected)));
408 assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
411 Ensure(ByteStuffer, sends_frame_with_254_non_zeroes_followed_by_zero) {
418 byte_stuffer_send_frame(0, data, 255);
419 uint8_t expected[258];
427 assert_that(sent_data_size, is_equal_to(sizeof(expected)));
428 assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected)));
431 Ensure(ByteStuffer, sends_and_receives_full_roundtrip_small_packet) {
432 uint8_t original_data[] = { 1, 2, 3};
433 byte_stuffer_send_frame(0, original_data, sizeof(original_data));
434 expect(validator_recv_frame,
435 when(size, is_equal_to(sizeof(original_data))),
436 when(data, is_equal_to_contents_of(original_data, sizeof(original_data)))
439 for(i=0;i<sent_data_size;i++) {
440 byte_stuffer_recv_byte(1, sent_data[i]);
444 Ensure(ByteStuffer, sends_and_receives_full_roundtrip_small_packet_with_zeros) {
445 uint8_t original_data[] = { 1, 0, 3, 0, 0, 9};
446 byte_stuffer_send_frame(1, original_data, sizeof(original_data));
447 expect(validator_recv_frame,
448 when(size, is_equal_to(sizeof(original_data))),
449 when(data, is_equal_to_contents_of(original_data, sizeof(original_data)))
452 for(i=0;i<sent_data_size;i++) {
453 byte_stuffer_recv_byte(0, sent_data[i]);
457 Ensure(ByteStuffer, sends_and_receives_full_roundtrip_254_bytes) {
458 uint8_t original_data[254];
461 original_data[i] = i + 1;
463 byte_stuffer_send_frame(0, original_data, sizeof(original_data));
464 expect(validator_recv_frame,
465 when(size, is_equal_to(sizeof(original_data))),
466 when(data, is_equal_to_contents_of(original_data, sizeof(original_data)))
468 for(i=0;i<sent_data_size;i++) {
469 byte_stuffer_recv_byte(1, sent_data[i]);
473 Ensure(ByteStuffer, sends_and_receives_full_roundtrip_256_bytes) {
474 uint8_t original_data[256];
477 original_data[i] = i + 1;
479 original_data[254] = 22;
480 original_data[255] = 23;
481 byte_stuffer_send_frame(0, original_data, sizeof(original_data));
482 expect(validator_recv_frame,
483 when(size, is_equal_to(sizeof(original_data))),
484 when(data, is_equal_to_contents_of(original_data, sizeof(original_data)))
486 for(i=0;i<sent_data_size;i++) {
487 byte_stuffer_recv_byte(1, sent_data[i]);
491 Ensure(ByteStuffer, sends_and_receives_full_roundtrip_254_bytes_and_then_zero) {
492 uint8_t original_data[255];
495 original_data[i] = i + 1;
497 original_data[254] = 0;
498 byte_stuffer_send_frame(0, original_data, sizeof(original_data));
499 expect(validator_recv_frame,
500 when(size, is_equal_to(sizeof(original_data))),
501 when(data, is_equal_to_contents_of(original_data, sizeof(original_data)))
503 for(i=0;i<sent_data_size;i++) {
504 byte_stuffer_recv_byte(1, sent_data[i]);