1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "device/serial/serial_io_handler_posix.h"
6 #include "testing/gtest/include/gtest/gtest.h"
10 class SerialIoHandlerPosixTest
: public testing::Test
{
12 SerialIoHandlerPosixTest() {}
14 void SetUp() override
{
15 serial_io_handler_posix_
= new SerialIoHandlerPosix(nullptr, nullptr);
18 void Initialize(bool parity_check_enabled
,
19 const char* chars_stashed
,
20 int num_chars_stashed
) {
21 serial_io_handler_posix_
->error_detect_state_
= ErrorDetectState::NO_ERROR
;
22 serial_io_handler_posix_
->parity_check_enabled_
= parity_check_enabled
;
23 serial_io_handler_posix_
->num_chars_stashed_
= num_chars_stashed
;
24 for (int i
= 0; i
< num_chars_stashed
; ++i
) {
25 serial_io_handler_posix_
->chars_stashed_
[i
] = chars_stashed
[i
];
29 void TestHelper(char* buffer
,
32 ErrorDetectState error_detect_state_expected
,
33 const char* chars_stashed_expected
,
34 int num_chars_stashed_expected
,
35 const char* buffer_expected
,
36 int new_bytes_read_expected
,
37 bool break_detected_expected
,
38 bool parity_error_detected_expected
) {
39 bool break_detected
= false;
40 bool parity_error_detected
= false;
41 int new_bytes_read
= serial_io_handler_posix_
->CheckReceiveError(
42 buffer
, buffer_len
, bytes_read
, break_detected
, parity_error_detected
);
44 EXPECT_EQ(error_detect_state_expected
,
45 serial_io_handler_posix_
->error_detect_state_
);
46 EXPECT_EQ(num_chars_stashed_expected
,
47 serial_io_handler_posix_
->num_chars_stashed_
);
48 for (int i
= 0; i
< num_chars_stashed_expected
; ++i
) {
49 EXPECT_EQ(chars_stashed_expected
[i
],
50 serial_io_handler_posix_
->chars_stashed_
[i
]);
52 EXPECT_EQ(new_bytes_read_expected
, new_bytes_read
);
53 for (int i
= 0; i
< new_bytes_read_expected
; ++i
) {
54 EXPECT_EQ(buffer_expected
[i
], buffer
[i
]);
56 EXPECT_EQ(break_detected_expected
, break_detected
);
57 EXPECT_EQ(parity_error_detected_expected
, parity_error_detected
);
61 scoped_refptr
<SerialIoHandlerPosix
> serial_io_handler_posix_
;
64 DISALLOW_COPY_AND_ASSIGN(SerialIoHandlerPosixTest
);
68 TEST_F(SerialIoHandlerPosixTest
, NoErrorReadOnce
) {
69 for (int buffer_len
= 3; buffer_len
<= 20; ++buffer_len
) {
70 Initialize(true, "", 0);
72 char buffer
[30] = {'a', 'b', 'c'};
74 TestHelper(buffer
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
75 0, "abc", 3, false, false);
81 TEST_F(SerialIoHandlerPosixTest
, NoErrorReadTwiceBytesReadTwoAndOne
) {
82 for (int buffer_len
= 2; buffer_len
<= 20; ++buffer_len
) {
83 Initialize(true, "", 0);
85 char buffer_1
[30] = {'a', 'b'};
87 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
88 0, "ab", 2, false, false);
90 char buffer_2
[30] = {'c'};
92 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
93 0, "c", 1, false, false);
99 TEST_F(SerialIoHandlerPosixTest
, NoErrorReadTwiceBytesReadOneAndTwo
) {
100 for (int buffer_len
= 2; buffer_len
<= 20; ++buffer_len
) {
101 Initialize(true, "", 0);
103 char buffer_1
[30] = {'a'};
105 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
106 0, "a", 1, false, false);
108 char buffer_2
[30] = {'b', 'c'};
110 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
111 0, "bc", 2, false, false);
118 TEST_F(SerialIoHandlerPosixTest
, NoErrorReadThreeTimes
) {
119 for (int buffer_len
= 1; buffer_len
<= 20; ++buffer_len
) {
120 Initialize(true, "", 0);
122 char buffer_1
[30] = {'a'};
124 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
125 0, "a", 1, false, false);
127 char buffer_2
[30] = {'b'};
129 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
130 0, "b", 1, false, false);
132 char buffer_3
[30] = {'c'};
134 TestHelper(buffer_3
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
135 0, "c", 1, false, false);
140 TEST_F(SerialIoHandlerPosixTest
, BreakReadOnce
) {
141 for (int buffer_len
= 3; buffer_len
<= 20; ++buffer_len
) {
142 Initialize(true, "", 0);
144 char buffer
[30] = {'\377', '\0', '\0'};
146 TestHelper(buffer
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
147 0, "", 0, true, false);
151 // 'a' 'b' '\377' '\0' '\0' 'c' 'd' 'e'
152 TEST_F(SerialIoHandlerPosixTest
, BreakReadOnceHasBytesBeforeAndAfterBreak
) {
153 for (int buffer_len
= 8; buffer_len
<= 20; ++buffer_len
) {
154 Initialize(true, "", 0);
156 char buffer
[30] = {'a', 'b', '\377', '\0', '\0', 'c', 'd', 'e'};
158 TestHelper(buffer
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
159 0, "abcde", 5, true, false);
165 TEST_F(SerialIoHandlerPosixTest
, BreakReadTwiceBytesReadTwoAndOne
) {
166 for (int buffer_len
= 2; buffer_len
<= 20; ++buffer_len
) {
167 Initialize(true, "", 0);
169 char buffer_1
[30] = {'\377', '\0'};
171 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
172 "\377\0", 2, "", 0, false, false);
174 char buffer_2
[30] = {'\0'};
176 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
177 0, "", 0, true, false);
181 // 'a' 'b' 'c' '\377' '\0'
183 TEST_F(SerialIoHandlerPosixTest
,
184 BreakReadTwiceBytesReadTwoAndOneHasBytesBeforeAndAfterBreak
) {
185 for (int buffer_len
= 5; buffer_len
<= 20; ++buffer_len
) {
186 Initialize(true, "", 0);
188 char buffer_1
[30] = {'a', 'b', 'c', '\377', '\0'};
190 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
191 "\377\0", 2, "abc", 3, false, false);
193 char buffer_2
[30] = {'\0', 'd', 'e'};
195 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
196 0, "de", 2, true, false);
202 TEST_F(SerialIoHandlerPosixTest
, BreakReadTwiceBytesReadOneAndTwo
) {
203 for (int buffer_len
= 2; buffer_len
<= 20; ++buffer_len
) {
204 Initialize(true, "", 0);
206 char buffer_1
[30] = {'\377'};
208 TestHelper(buffer_1
, buffer_len
, bytes_read
,
209 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "", 0, false, false);
211 char buffer_2
[30] = {'\0', '\0'};
213 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
214 0, "", 0, true, false);
220 TEST_F(SerialIoHandlerPosixTest
,
221 BreakReadTwiceBytesReadOneAndTwoHasBytesBeforeAndAfterBreak
) {
222 for (int buffer_len
= 3; buffer_len
<= 20; ++buffer_len
) {
223 Initialize(true, "", 0);
225 char buffer_1
[30] = {'a', 'b', '\377'};
227 TestHelper(buffer_1
, buffer_len
, bytes_read
,
228 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "ab", 2, false,
231 char buffer_2
[30] = {'\0', '\0', 'c'};
233 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
234 0, "c", 1, true, false);
241 TEST_F(SerialIoHandlerPosixTest
, BreakReadThreeTimes
) {
242 for (int buffer_len
= 1; buffer_len
<= 20; ++buffer_len
) {
243 Initialize(true, "", 0);
245 char buffer_1
[30] = {'\377'};
247 TestHelper(buffer_1
, buffer_len
, bytes_read
,
248 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "", 0, false, false);
250 char buffer_2
[30] = {'\0'};
252 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
253 "\377\0", 2, "", 0, false, false);
255 char buffer_3
[30] = {'\0'};
257 TestHelper(buffer_3
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
258 0, "", 0, true, false);
265 TEST_F(SerialIoHandlerPosixTest
,
266 BreakReadThreeTimesHasBytesBeforeAndAfterBreak
) {
267 for (int buffer_len
= 2; buffer_len
<= 20; ++buffer_len
) {
268 Initialize(true, "", 0);
270 char buffer_1
[30] = {'a', '\377'};
272 TestHelper(buffer_1
, buffer_len
, bytes_read
,
273 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "a", 1, false,
276 char buffer_2
[30] = {'\0'};
278 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
279 "\377\0", 2, "", 0, false, false);
281 char buffer_3
[30] = {'\0', 'b'};
283 TestHelper(buffer_3
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
284 0, "b", 1, true, false);
289 TEST_F(SerialIoHandlerPosixTest
, ParityErrorReadOnce
) {
290 for (int buffer_len
= 3; buffer_len
<= 20; ++buffer_len
) {
291 Initialize(true, "", 0);
293 char buffer
[30] = {'\377', '\0', 'a'};
295 TestHelper(buffer
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
296 0, "", 0, false, true);
300 // 'b' 'c' '\377' '\0' 'a' 'd'
301 TEST_F(SerialIoHandlerPosixTest
,
302 ParityErrorReadOnceHasBytesBeforeAndAfterParityError
) {
303 for (int buffer_len
= 6; buffer_len
<= 20; ++buffer_len
) {
304 Initialize(true, "", 0);
306 char buffer
[30] = {'b', 'c', '\377', '\0', 'a', 'd'};
308 TestHelper(buffer
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
309 0, "bcd", 3, false, true);
315 TEST_F(SerialIoHandlerPosixTest
, ParityErrorReadTwiceBytesReadTwoAndOne
) {
316 for (int buffer_len
= 2; buffer_len
<= 20; ++buffer_len
) {
317 Initialize(true, "", 0);
319 char buffer_1
[30] = {'\377', '\0'};
321 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
322 "\377\0", 2, "", 0, false, false);
324 char buffer_2
[30] = {'a'};
326 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
327 0, "", 0, false, true);
334 SerialIoHandlerPosixTest
,
335 ParityErrorReadTwiceBytesReadTwoAndOneHasBytesBeforeAndAfterParityError
) {
336 for (int buffer_len
= 3; buffer_len
<= 20; ++buffer_len
) {
337 Initialize(true, "", 0);
339 char buffer_1
[30] = {'b', '\377', '\0'};
341 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
342 "\377\0", 2, "b", 1, false, false);
344 char buffer_2
[30] = {'a', 'c', 'd'};
346 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
347 0, "cd", 2, false, true);
353 TEST_F(SerialIoHandlerPosixTest
, ParityErrorReadTwiceBytesReadOneAndTwo
) {
354 for (int buffer_len
= 2; buffer_len
<= 20; ++buffer_len
) {
355 Initialize(true, "", 0);
357 char buffer_1
[30] = {'\377'};
359 TestHelper(buffer_1
, buffer_len
, bytes_read
,
360 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "", 0, false, false);
362 char buffer_2
[30] = {'\0', 'a'};
364 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
365 0, "", 0, false, true);
372 SerialIoHandlerPosixTest
,
373 ParityErrorReadTwiceBytesReadOneAndTwoHasBytesBeforeAndAfterParityError
) {
374 for (int buffer_len
= 3; buffer_len
<= 20; ++buffer_len
) {
375 Initialize(true, "", 0);
377 char buffer_1
[30] = {'b', 'c', '\377'};
379 TestHelper(buffer_1
, buffer_len
, bytes_read
,
380 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "bc", 2, false,
383 char buffer_2
[30] = {'\0', 'a', 'd'};
385 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
386 0, "d", 1, false, true);
393 TEST_F(SerialIoHandlerPosixTest
, ParityErrorReadThreeTimes
) {
394 for (int buffer_len
= 1; buffer_len
<= 20; ++buffer_len
) {
395 Initialize(true, "", 0);
397 char buffer_1
[30] = {'\377'};
399 TestHelper(buffer_1
, buffer_len
, bytes_read
,
400 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "", 0, false, false);
402 char buffer_2
[30] = {'\0'};
404 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
405 "\377\0", 2, "", 0, false, false);
407 char buffer_3
[30] = {'a'};
409 TestHelper(buffer_3
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
410 0, "", 0, false, true);
417 TEST_F(SerialIoHandlerPosixTest
,
418 ParityErrorReadThreeTimesHasBytesBeforeAndAfterParityError
) {
419 for (int buffer_len
= 2; buffer_len
<= 20; ++buffer_len
) {
420 Initialize(true, "", 0);
422 char buffer_1
[30] = {'b', '\377'};
424 TestHelper(buffer_1
, buffer_len
, bytes_read
,
425 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "b", 1, false,
428 char buffer_2
[30] = {'\0'};
430 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
431 "\377\0", 2, "", 0, false, false);
433 char buffer_3
[30] = {'a', 'c'};
435 TestHelper(buffer_3
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
436 0, "c", 1, false, true);
441 TEST_F(SerialIoHandlerPosixTest
, TwoEOFsReadOnce
) {
442 for (int buffer_len
= 2; buffer_len
<= 20; ++buffer_len
) {
443 Initialize(true, "", 0);
445 char buffer
[30] = {'\377', '\377'};
447 TestHelper(buffer
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
448 0, "\377", 1, false, false);
452 // 'a' '\377' '\377' 'b' 'c'
453 TEST_F(SerialIoHandlerPosixTest
, TwoEOFsReadOnceHasBytesBeforeAndAfterEOF
) {
454 for (int buffer_len
= 5; buffer_len
<= 20; ++buffer_len
) {
455 Initialize(true, "", 0);
457 char buffer
[30] = {'a', '\377', '\377', 'b', 'c'};
459 TestHelper(buffer
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
460 0, "a\377bc", 4, false, false);
466 TEST_F(SerialIoHandlerPosixTest
, TwoEOFsReadTwice
) {
467 for (int buffer_len
= 1; buffer_len
<= 20; ++buffer_len
) {
468 Initialize(true, "", 0);
470 char buffer_1
[30] = {'\377'};
472 TestHelper(buffer_1
, buffer_len
, bytes_read
,
473 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "", 0, false, false);
475 char buffer_2
[30] = {'\377'};
477 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
478 0, "\377", 1, false, false);
484 TEST_F(SerialIoHandlerPosixTest
, TwoEOFsReadTwiceHasBytesBeforeAndAfterEOF
) {
485 for (int buffer_len
= 2; buffer_len
<= 20; ++buffer_len
) {
486 Initialize(true, "", 0);
488 char buffer_1
[30] = {'a', '\377'};
490 TestHelper(buffer_1
, buffer_len
, bytes_read
,
491 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "a", 1, false,
494 char buffer_2
[30] = {'\377', 'b'};
496 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
497 0, "\377b", 2, false, false);
502 TEST_F(SerialIoHandlerPosixTest
, ParityCheckDisabledReadOnce
) {
503 for (int buffer_len
= 3; buffer_len
<= 20; ++buffer_len
) {
504 Initialize(false, "", 0);
506 char buffer
[30] = {'\377', '\0', 'a'};
508 TestHelper(buffer
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
509 0, "\377\0a", 3, false, false);
513 // 'b' '\377' '\0' 'a' 'c'
514 TEST_F(SerialIoHandlerPosixTest
,
515 ParityCheckDisabledReadOnceHasBytesBeforeAndAfter
) {
516 for (int buffer_len
= 5; buffer_len
<= 20; ++buffer_len
) {
517 Initialize(false, "", 0);
519 char buffer
[30] = {'b', '\377', '\0', 'a', 'c'};
521 TestHelper(buffer
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
522 0, "b\377\0ac", 5, false, false);
528 TEST_F(SerialIoHandlerPosixTest
,
529 ParityCheckDisabledReadTwiceBytesReadTwoAndOne
) {
531 Initialize(false, "", 0);
533 char buffer_1
[30] = {'\377', '\0'};
535 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
536 "\377\0", 2, "", 0, false, false);
538 char buffer_2
[30] = {'a'};
540 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "a",
541 1, "\377\0", 2, false, false);
546 TEST_F(SerialIoHandlerPosixTest
,
547 ParityCheckDisabledReadTwiceBytesReadTwoAndOneHasBytesAfter
) {
549 Initialize(false, "", 0);
551 char buffer_1
[30] = {'\377', '\0'};
553 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
554 "\377\0", 2, "", 0, false, false);
556 char buffer_2
[30] = {'a', 'b'};
558 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "ab",
559 2, "\377\0", 2, false, false);
564 TEST_F(SerialIoHandlerPosixTest
,
565 ParityCheckDisabledReadTwiceBytesReadTwoAndOneLargerBufferLen
) {
566 for (int buffer_len
= 3; buffer_len
<= 20; ++buffer_len
) {
567 Initialize(false, "", 0);
569 char buffer_1
[30] = {'\377', '\0'};
571 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
572 "\377\0", 2, "", 0, false, false);
574 char buffer_2
[30] = {'a'};
576 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
577 0, "\377\0a", 3, false, false);
584 SerialIoHandlerPosixTest
,
585 ParityCheckDisabledReadTwiceBytesReadTwoAndOneBufferLenThreeHasBytesBeforeAndAfter
) {
587 Initialize(false, "", 0);
589 char buffer_1
[30] = {'b', '\377', '\0'};
591 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
592 "\377\0", 2, "b", 1, false, false);
594 char buffer_2
[30] = {'a', 'c'};
596 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "c",
597 1, "\377\0a", 3, false, false);
603 SerialIoHandlerPosixTest
,
604 ParityCheckDisabledReadTwiceBytesReadTwoAndOneLargerBufferLenHasBytesBeforeAndAfter
) {
605 for (int buffer_len
= 4; buffer_len
<= 20; ++buffer_len
) {
606 Initialize(false, "", 0);
608 char buffer_1
[30] = {'b', '\377', '\0'};
610 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
611 "\377\0", 2, "b", 1, false, false);
613 char buffer_2
[30] = {'a', 'c'};
615 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
616 0, "\377\0ac", 4, false, false);
622 TEST_F(SerialIoHandlerPosixTest
,
623 ParityCheckDisabledReadTwiceBytesReadOneAndTwo
) {
625 Initialize(false, "", 0);
627 char buffer_1
[30] = {'\377'};
629 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_377_SEEN
,
630 "\377", 1, "", 0, false, false);
632 char buffer_2
[30] = {'\0', 'a'};
634 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "a",
635 1, "\377\0", 2, false, false);
639 TestHelper(buffer_3
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
640 0, "a", 1, false, false);
645 TEST_F(SerialIoHandlerPosixTest
,
646 ParityCheckDisabledReadTwiceBytesReadOneAndTwoHasBytesBefore
) {
648 Initialize(false, "", 0);
650 char buffer_1
[30] = {'b', '\377'};
652 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_377_SEEN
,
653 "\377", 1, "b", 1, false, false);
655 char buffer_2
[30] = {'\0', 'a'};
657 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "a",
658 1, "\377\0", 2, false, false);
662 TestHelper(buffer_3
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
663 0, "a", 1, false, false);
668 TEST_F(SerialIoHandlerPosixTest
,
669 ParityCheckDisabledReadTwiceBytesReadOneAndTwoLargerBufferLen
) {
670 for (int buffer_len
= 3; buffer_len
<= 20; ++buffer_len
) {
671 Initialize(false, "", 0);
673 char buffer_1
[30] = {'\377'};
675 TestHelper(buffer_1
, buffer_len
, bytes_read
,
676 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "", 0, false, false);
678 char buffer_2
[30] = {'\0', 'a'};
680 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
681 0, "\377\0a", 3, false, false);
688 SerialIoHandlerPosixTest
,
689 ParityCheckDisabledReadTwiceBytesReadOneAndTwoBufferLenThreeHasBytesBeforeAndAfter
) {
691 Initialize(false, "", 0);
693 char buffer_1
[30] = {'b', '\377'};
695 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_377_SEEN
,
696 "\377", 1, "b", 1, false, false);
698 char buffer_2
[30] = {'\0', 'a', 'c'};
700 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "c",
701 1, "\377\0a", 3, false, false);
707 SerialIoHandlerPosixTest
,
708 ParityCheckDisabledReadTwiceBytesReadOneAndTwoLargerBufferLenHasBytesBeforeAndAfter
) {
709 for (int buffer_len
= 4; buffer_len
<= 20; ++buffer_len
) {
710 Initialize(false, "", 0);
712 char buffer_1
[30] = {'b', '\377'};
714 TestHelper(buffer_1
, buffer_len
, bytes_read
,
715 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "b", 1, false,
718 char buffer_2
[30] = {'\0', 'a', 'c'};
720 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
721 0, "\377\0ac", 4, false, false);
728 TEST_F(SerialIoHandlerPosixTest
,
729 ParityCheckDisabledReadThreeTimesBufferLenOne
) {
731 Initialize(false, "", 0);
733 char buffer_1
[30] = {'\377'};
735 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_377_SEEN
,
736 "\377", 1, "", 0, false, false);
738 char buffer_2
[30] = {'\0'};
740 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
741 "\377\0", 2, "", 0, false, false);
743 char buffer_3
[30] = {'a'};
745 TestHelper(buffer_3
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
,
746 "\0a", 2, "\377", 1, false, false);
752 TEST_F(SerialIoHandlerPosixTest
,
753 ParityCheckDisabledReadThreeTimesBufferLenTwo
) {
755 Initialize(false, "", 0);
757 char buffer_1
[30] = {'\377'};
759 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_377_SEEN
,
760 "\377", 1, "", 0, false, false);
762 char buffer_2
[30] = {'\0'};
764 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
765 "\377\0", 2, "", 0, false, false);
767 char buffer_3
[30] = {'a'};
769 TestHelper(buffer_3
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "a",
770 1, "\377\0", 2, false, false);
776 TEST_F(SerialIoHandlerPosixTest
,
777 ParityCheckDisabledReadThreeTimesLargerBufferLen
) {
778 for (int buffer_len
= 3; buffer_len
<= 20; ++buffer_len
) {
779 Initialize(false, "", 0);
781 char buffer_1
[30] = {'\377'};
783 TestHelper(buffer_1
, buffer_len
, bytes_read
,
784 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "", 0, false, false);
786 char buffer_2
[30] = {'\0'};
788 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
789 "\377\0", 2, "", 0, false, false);
791 char buffer_3
[30] = {'a'};
793 TestHelper(buffer_3
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
794 0, "\377\0a", 3, false, false);
801 TEST_F(SerialIoHandlerPosixTest
,
802 ParityCheckDisabledReadThreeTimesBufferLenThreeByteBeforeAndAfter
) {
804 Initialize(false, "", 0);
806 char buffer_1
[30] = {'b', '\377'};
808 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_377_SEEN
,
809 "\377", 1, "b", 1, false, false);
811 char buffer_2
[30] = {'\0'};
813 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
814 "\377\0", 2, "", 0, false, false);
816 char buffer_3
[30] = {'a', 'c'};
818 TestHelper(buffer_3
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "c",
819 1, "\377\0a", 3, false, false);
825 TEST_F(SerialIoHandlerPosixTest
,
826 ParityCheckDisabledReadThreeTimesLargerBufferLenHasBytesBeforeAndAfter
) {
827 for (int buffer_len
= 4; buffer_len
<= 20; ++buffer_len
) {
828 Initialize(false, "", 0);
830 char buffer_1
[30] = {'b', '\377'};
832 TestHelper(buffer_1
, buffer_len
, bytes_read
,
833 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "b", 1, false,
836 char buffer_2
[30] = {'\0'};
838 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
839 "\377\0", 2, "", 0, false, false);
841 char buffer_3
[30] = {'a', 'c'};
843 TestHelper(buffer_3
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
844 0, "\377\0ac", 4, false, false);
848 TEST_F(SerialIoHandlerPosixTest
, BytesReadZero
) {
849 for (int buffer_len
= 1; buffer_len
<= 20; ++buffer_len
) {
850 Initialize(true, "", 0);
854 TestHelper(buffer
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
855 0, "", 0, false, false);
860 TEST_F(SerialIoHandlerPosixTest
, InvalidSequenceReadOnce
) {
861 for (int buffer_len
= 3; buffer_len
<= 20; ++buffer_len
) {
862 Initialize(true, "", 0);
864 char buffer
[30] = {'\377', 'a', 'b'};
866 TestHelper(buffer
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
867 0, "\377ab", 3, false, false);
873 TEST_F(SerialIoHandlerPosixTest
, InvalidSequenceReadTwiceBytesReadTwoAndOne
) {
874 for (int buffer_len
= 2; buffer_len
<= 20; ++buffer_len
) {
875 Initialize(true, "", 0);
877 char buffer_1
[30] = {'\377', 'a'};
879 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
880 0, "\377a", 2, false, false);
882 char buffer_2
[30] = {'b'};
884 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
885 0, "b", 1, false, false);
891 TEST_F(SerialIoHandlerPosixTest
, InvalidSequenceReadTwiceBytesReadOneAndTwo
) {
893 Initialize(true, "", 0);
895 char buffer_1
[30] = {'\377'};
897 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_377_SEEN
,
898 "\377", 1, "", 0, false, false);
900 char buffer_2
[30] = {'a', 'b'};
902 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "b",
903 1, "\377a", 2, false, false);
908 TEST_F(SerialIoHandlerPosixTest
,
909 InvalidSequenceReadTwiceBytesReadOneAndTwoLargerBufferLen
) {
910 for (int buffer_len
= 3; buffer_len
<= 20; ++buffer_len
) {
911 Initialize(true, "", 0);
913 char buffer_1
[30] = {'\377'};
915 TestHelper(buffer_1
, buffer_len
, bytes_read
,
916 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "", 0, false, false);
918 char buffer_2
[30] = {'a', 'b'};
920 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
921 0, "\377ab", 3, false, false);
928 TEST_F(SerialIoHandlerPosixTest
, InvalidSequenceReadThreeTimes
) {
930 Initialize(true, "", 0);
932 char buffer_1
[30] = {'\377'};
934 TestHelper(buffer_1
, buffer_len
, bytes_read
, ErrorDetectState::MARK_377_SEEN
,
935 "\377", 1, "", 0, false, false);
937 char buffer_2
[30] = {'a'};
939 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "a",
940 1, "\377", 1, false, false);
942 char buffer_3
[30] = {'b'};
944 TestHelper(buffer_3
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "b",
945 1, "a", 1, false, false);
951 TEST_F(SerialIoHandlerPosixTest
, InvalidSequenceReadThreeTimesLargerBufferLen
) {
952 for (int buffer_len
= 2; buffer_len
<= 20; ++buffer_len
) {
953 Initialize(true, "", 0);
955 char buffer_1
[30] = {'\377'};
957 TestHelper(buffer_1
, buffer_len
, bytes_read
,
958 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "", 0, false, false);
960 char buffer_2
[30] = {'a'};
962 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
963 0, "\377a", 2, false, false);
965 char buffer_3
[30] = {'b'};
967 TestHelper(buffer_3
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
968 0, "b", 1, false, false);
972 // 'a' 'b' 'c' '\377'
973 TEST_F(SerialIoHandlerPosixTest
, CharsStashedPreset
) {
975 Initialize(true, "ab", 2);
977 char buffer
[30] = {'c', '\377'};
979 TestHelper(buffer
, buffer_len
, bytes_read
, ErrorDetectState::MARK_377_SEEN
,
980 "c\377", 2, "ab", 2, false, false);
983 // 'b' 'c' '\377' '\0' '\0' '\377' '\377' '\377' '\0' 'a' 'd' 'e'
984 TEST_F(SerialIoHandlerPosixTest
, BreakAndEOFAndParityError
) {
986 Initialize(true, "", 0);
988 char buffer
[30] = {'b', 'c', '\377', '\0', '\0', '\377',
989 '\377', '\377', '\0', 'a', 'd', 'e'};
991 TestHelper(buffer
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "", 0,
992 "bc\377de", 5, true, true);
995 // 'b' 'c' '\377' '\0' '\0' '\377'
996 // '\377' '\377' '\0'
998 TEST_F(SerialIoHandlerPosixTest
, BreakAndEOFAndParityErrorReadThreeTimes
) {
999 for (int buffer_len
= 6; buffer_len
<= 20; ++buffer_len
) {
1000 Initialize(true, "", 0);
1002 char buffer_1
[30] = {'b', 'c', '\377', '\0', '\0', '\377'};
1004 TestHelper(buffer_1
, buffer_len
, bytes_read
,
1005 ErrorDetectState::MARK_377_SEEN
, "\377", 1, "bc", 2, true,
1008 char buffer_2
[30] = {'\377', '\377', '\0'};
1010 TestHelper(buffer_2
, buffer_len
, bytes_read
, ErrorDetectState::MARK_0_SEEN
,
1011 "\377\0", 2, "\377", 1, false, false);
1013 char buffer_3
[30] = {'a', 'd', 'e'};
1015 TestHelper(buffer_3
, buffer_len
, bytes_read
, ErrorDetectState::NO_ERROR
, "",
1016 0, "de", 2, false, true);
1020 } // namespace device