Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / device / serial / serial_io_handler_posix_unittest.cc
blobe4c15532f350ca111bfdb2704450154a53048d75
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"
8 namespace device {
10 class SerialIoHandlerPosixTest : public testing::Test {
11 public:
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,
30 int buffer_len,
31 int bytes_read,
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);
60 protected:
61 scoped_refptr<SerialIoHandlerPosix> serial_io_handler_posix_;
63 private:
64 DISALLOW_COPY_AND_ASSIGN(SerialIoHandlerPosixTest);
67 // 'a' 'b' 'c'
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'};
73 int bytes_read = 3;
74 TestHelper(buffer, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
75 0, "abc", 3, false, false);
79 // 'a' 'b'
80 // 'c'
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'};
86 int bytes_read = 2;
87 TestHelper(buffer_1, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
88 0, "ab", 2, false, false);
90 char buffer_2[30] = {'c'};
91 bytes_read = 1;
92 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
93 0, "c", 1, false, false);
97 // 'a'
98 // 'b' c'
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'};
104 int bytes_read = 1;
105 TestHelper(buffer_1, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
106 0, "a", 1, false, false);
108 char buffer_2[30] = {'b', 'c'};
109 bytes_read = 2;
110 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
111 0, "bc", 2, false, false);
115 // 'a'
116 // 'b'
117 // 'c'
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'};
123 int bytes_read = 1;
124 TestHelper(buffer_1, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
125 0, "a", 1, false, false);
127 char buffer_2[30] = {'b'};
128 bytes_read = 1;
129 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
130 0, "b", 1, false, false);
132 char buffer_3[30] = {'c'};
133 bytes_read = 1;
134 TestHelper(buffer_3, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
135 0, "c", 1, false, false);
139 // '\377' '\0' '\0'
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'};
145 int bytes_read = 3;
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'};
157 int bytes_read = 8;
158 TestHelper(buffer, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
159 0, "abcde", 5, true, false);
163 // '\377' '\0'
164 // '\0'
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'};
170 int bytes_read = 2;
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'};
175 bytes_read = 1;
176 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
177 0, "", 0, true, false);
181 // 'a' 'b' 'c' '\377' '\0'
182 // '\0' 'd' 'e'
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'};
189 int bytes_read = 5;
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'};
194 bytes_read = 3;
195 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
196 0, "de", 2, true, false);
200 // '\377'
201 // '\0' '\0'
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'};
207 int bytes_read = 1;
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'};
212 bytes_read = 2;
213 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
214 0, "", 0, true, false);
218 // 'a' 'b' '\377'
219 // '\0' '\0' 'c'
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'};
226 int bytes_read = 3;
227 TestHelper(buffer_1, buffer_len, bytes_read,
228 ErrorDetectState::MARK_377_SEEN, "\377", 1, "ab", 2, false,
229 false);
231 char buffer_2[30] = {'\0', '\0', 'c'};
232 bytes_read = 3;
233 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
234 0, "c", 1, true, false);
238 // '\377'
239 // '\0'
240 // '\0'
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'};
246 int bytes_read = 1;
247 TestHelper(buffer_1, buffer_len, bytes_read,
248 ErrorDetectState::MARK_377_SEEN, "\377", 1, "", 0, false, false);
250 char buffer_2[30] = {'\0'};
251 bytes_read = 1;
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'};
256 bytes_read = 1;
257 TestHelper(buffer_3, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
258 0, "", 0, true, false);
262 // 'a' '\377'
263 // '\0'
264 // '\0' 'b'
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'};
271 int bytes_read = 2;
272 TestHelper(buffer_1, buffer_len, bytes_read,
273 ErrorDetectState::MARK_377_SEEN, "\377", 1, "a", 1, false,
274 false);
276 char buffer_2[30] = {'\0'};
277 bytes_read = 1;
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'};
282 bytes_read = 2;
283 TestHelper(buffer_3, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
284 0, "b", 1, true, false);
288 // '\377' '\0' 'a'
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'};
294 int bytes_read = 3;
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'};
307 int bytes_read = 6;
308 TestHelper(buffer, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
309 0, "bcd", 3, false, true);
313 // '\377' '\0'
314 // 'a'
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'};
320 int bytes_read = 2;
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'};
325 bytes_read = 1;
326 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
327 0, "", 0, false, true);
331 // 'b' '\377' '\0'
332 // 'a' 'c' 'd'
333 TEST_F(
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'};
340 int bytes_read = 3;
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'};
345 bytes_read = 3;
346 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
347 0, "cd", 2, false, true);
351 // '\377'
352 // '\0' 'a'
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'};
358 int bytes_read = 1;
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'};
363 bytes_read = 2;
364 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
365 0, "", 0, false, true);
369 // 'b' 'c' '\377'
370 // '\0' 'a' 'd'
371 TEST_F(
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'};
378 int bytes_read = 3;
379 TestHelper(buffer_1, buffer_len, bytes_read,
380 ErrorDetectState::MARK_377_SEEN, "\377", 1, "bc", 2, false,
381 false);
383 char buffer_2[30] = {'\0', 'a', 'd'};
384 bytes_read = 3;
385 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
386 0, "d", 1, false, true);
390 // '\377'
391 // '\0'
392 // 'a'
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'};
398 int bytes_read = 1;
399 TestHelper(buffer_1, buffer_len, bytes_read,
400 ErrorDetectState::MARK_377_SEEN, "\377", 1, "", 0, false, false);
402 char buffer_2[30] = {'\0'};
403 bytes_read = 1;
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'};
408 bytes_read = 1;
409 TestHelper(buffer_3, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
410 0, "", 0, false, true);
414 // 'b' '\377'
415 // '\0'
416 // 'a' 'c'
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'};
423 int bytes_read = 2;
424 TestHelper(buffer_1, buffer_len, bytes_read,
425 ErrorDetectState::MARK_377_SEEN, "\377", 1, "b", 1, false,
426 false);
428 char buffer_2[30] = {'\0'};
429 bytes_read = 1;
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'};
434 bytes_read = 2;
435 TestHelper(buffer_3, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
436 0, "c", 1, false, true);
440 // '\377' '\377'
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'};
446 int bytes_read = 2;
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'};
458 int bytes_read = 5;
459 TestHelper(buffer, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
460 0, "a\377bc", 4, false, false);
464 // '\377'
465 // '\377'
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'};
471 int bytes_read = 1;
472 TestHelper(buffer_1, buffer_len, bytes_read,
473 ErrorDetectState::MARK_377_SEEN, "\377", 1, "", 0, false, false);
475 char buffer_2[30] = {'\377'};
476 bytes_read = 1;
477 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
478 0, "\377", 1, false, false);
482 // 'a' '\377'
483 // '\377' 'b'
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'};
489 int bytes_read = 2;
490 TestHelper(buffer_1, buffer_len, bytes_read,
491 ErrorDetectState::MARK_377_SEEN, "\377", 1, "a", 1, false,
492 false);
494 char buffer_2[30] = {'\377', 'b'};
495 bytes_read = 2;
496 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
497 0, "\377b", 2, false, false);
501 // '\377' '\0' 'a'
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'};
507 int bytes_read = 3;
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'};
520 int bytes_read = 5;
521 TestHelper(buffer, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
522 0, "b\377\0ac", 5, false, false);
526 // '\377' '\0'
527 // 'a'
528 TEST_F(SerialIoHandlerPosixTest,
529 ParityCheckDisabledReadTwiceBytesReadTwoAndOne) {
530 int buffer_len = 2;
531 Initialize(false, "", 0);
533 char buffer_1[30] = {'\377', '\0'};
534 int bytes_read = 2;
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'};
539 bytes_read = 1;
540 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "a",
541 1, "\377\0", 2, false, false);
544 // '\377' '\0'
545 // 'a' 'b'
546 TEST_F(SerialIoHandlerPosixTest,
547 ParityCheckDisabledReadTwiceBytesReadTwoAndOneHasBytesAfter) {
548 int buffer_len = 2;
549 Initialize(false, "", 0);
551 char buffer_1[30] = {'\377', '\0'};
552 int bytes_read = 2;
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'};
557 bytes_read = 2;
558 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "ab",
559 2, "\377\0", 2, false, false);
562 // '\377' '\0'
563 // 'a'
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'};
570 int bytes_read = 2;
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'};
575 bytes_read = 1;
576 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
577 0, "\377\0a", 3, false, false);
581 // 'b' '\377' '\0'
582 // 'a' 'c'
583 TEST_F(
584 SerialIoHandlerPosixTest,
585 ParityCheckDisabledReadTwiceBytesReadTwoAndOneBufferLenThreeHasBytesBeforeAndAfter) {
586 int buffer_len = 3;
587 Initialize(false, "", 0);
589 char buffer_1[30] = {'b', '\377', '\0'};
590 int bytes_read = 3;
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'};
595 bytes_read = 2;
596 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "c",
597 1, "\377\0a", 3, false, false);
600 // 'b' '\377' '\0'
601 // 'a' 'c'
602 TEST_F(
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'};
609 int bytes_read = 3;
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'};
614 bytes_read = 2;
615 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
616 0, "\377\0ac", 4, false, false);
620 // '\377'
621 // '\0' 'a'
622 TEST_F(SerialIoHandlerPosixTest,
623 ParityCheckDisabledReadTwiceBytesReadOneAndTwo) {
624 int buffer_len = 2;
625 Initialize(false, "", 0);
627 char buffer_1[30] = {'\377'};
628 int bytes_read = 1;
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'};
633 bytes_read = 2;
634 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "a",
635 1, "\377\0", 2, false, false);
637 char buffer_3[30];
638 bytes_read = 0;
639 TestHelper(buffer_3, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
640 0, "a", 1, false, false);
643 // 'b' '\377'
644 // '\0' 'a'
645 TEST_F(SerialIoHandlerPosixTest,
646 ParityCheckDisabledReadTwiceBytesReadOneAndTwoHasBytesBefore) {
647 int buffer_len = 2;
648 Initialize(false, "", 0);
650 char buffer_1[30] = {'b', '\377'};
651 int bytes_read = 2;
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'};
656 bytes_read = 2;
657 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "a",
658 1, "\377\0", 2, false, false);
660 char buffer_3[30];
661 bytes_read = 0;
662 TestHelper(buffer_3, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
663 0, "a", 1, false, false);
666 // '\377'
667 // '\0' 'a'
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'};
674 int bytes_read = 1;
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'};
679 bytes_read = 2;
680 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
681 0, "\377\0a", 3, false, false);
685 // 'b' '\377'
686 // '\0' 'a' 'c'
687 TEST_F(
688 SerialIoHandlerPosixTest,
689 ParityCheckDisabledReadTwiceBytesReadOneAndTwoBufferLenThreeHasBytesBeforeAndAfter) {
690 int buffer_len = 3;
691 Initialize(false, "", 0);
693 char buffer_1[30] = {'b', '\377'};
694 int bytes_read = 2;
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'};
699 bytes_read = 3;
700 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "c",
701 1, "\377\0a", 3, false, false);
704 // 'b' '\377'
705 // '\0' 'a' 'c'
706 TEST_F(
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'};
713 int bytes_read = 2;
714 TestHelper(buffer_1, buffer_len, bytes_read,
715 ErrorDetectState::MARK_377_SEEN, "\377", 1, "b", 1, false,
716 false);
718 char buffer_2[30] = {'\0', 'a', 'c'};
719 bytes_read = 3;
720 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
721 0, "\377\0ac", 4, false, false);
725 // '\377'
726 // '\0'
727 // 'a'
728 TEST_F(SerialIoHandlerPosixTest,
729 ParityCheckDisabledReadThreeTimesBufferLenOne) {
730 int buffer_len = 1;
731 Initialize(false, "", 0);
733 char buffer_1[30] = {'\377'};
734 int bytes_read = 1;
735 TestHelper(buffer_1, buffer_len, bytes_read, ErrorDetectState::MARK_377_SEEN,
736 "\377", 1, "", 0, false, false);
738 char buffer_2[30] = {'\0'};
739 bytes_read = 1;
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'};
744 bytes_read = 1;
745 TestHelper(buffer_3, buffer_len, bytes_read, ErrorDetectState::NO_ERROR,
746 "\0a", 2, "\377", 1, false, false);
749 // '\377'
750 // '\0'
751 // 'a'
752 TEST_F(SerialIoHandlerPosixTest,
753 ParityCheckDisabledReadThreeTimesBufferLenTwo) {
754 int buffer_len = 2;
755 Initialize(false, "", 0);
757 char buffer_1[30] = {'\377'};
758 int bytes_read = 1;
759 TestHelper(buffer_1, buffer_len, bytes_read, ErrorDetectState::MARK_377_SEEN,
760 "\377", 1, "", 0, false, false);
762 char buffer_2[30] = {'\0'};
763 bytes_read = 1;
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'};
768 bytes_read = 1;
769 TestHelper(buffer_3, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "a",
770 1, "\377\0", 2, false, false);
773 // '\377'
774 // '\0'
775 // 'a'
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'};
782 int bytes_read = 1;
783 TestHelper(buffer_1, buffer_len, bytes_read,
784 ErrorDetectState::MARK_377_SEEN, "\377", 1, "", 0, false, false);
786 char buffer_2[30] = {'\0'};
787 bytes_read = 1;
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'};
792 bytes_read = 1;
793 TestHelper(buffer_3, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
794 0, "\377\0a", 3, false, false);
798 // 'b' '\377'
799 // '\0'
800 // 'a' 'c'
801 TEST_F(SerialIoHandlerPosixTest,
802 ParityCheckDisabledReadThreeTimesBufferLenThreeByteBeforeAndAfter) {
803 int buffer_len = 3;
804 Initialize(false, "", 0);
806 char buffer_1[30] = {'b', '\377'};
807 int bytes_read = 2;
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'};
812 bytes_read = 1;
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'};
817 bytes_read = 2;
818 TestHelper(buffer_3, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "c",
819 1, "\377\0a", 3, false, false);
822 // 'b' '\377'
823 // '\0'
824 // 'a' 'c'
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'};
831 int bytes_read = 2;
832 TestHelper(buffer_1, buffer_len, bytes_read,
833 ErrorDetectState::MARK_377_SEEN, "\377", 1, "b", 1, false,
834 false);
836 char buffer_2[30] = {'\0'};
837 bytes_read = 1;
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'};
842 bytes_read = 2;
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);
852 char buffer[30];
853 int bytes_read = 0;
854 TestHelper(buffer, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
855 0, "", 0, false, false);
859 // '\377' 'a' 'b'
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'};
865 int bytes_read = 3;
866 TestHelper(buffer, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
867 0, "\377ab", 3, false, false);
871 // '\377' 'a'
872 // 'b'
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'};
878 int bytes_read = 2;
879 TestHelper(buffer_1, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
880 0, "\377a", 2, false, false);
882 char buffer_2[30] = {'b'};
883 bytes_read = 1;
884 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
885 0, "b", 1, false, false);
889 // '\377'
890 // 'a' 'b'
891 TEST_F(SerialIoHandlerPosixTest, InvalidSequenceReadTwiceBytesReadOneAndTwo) {
892 int buffer_len = 2;
893 Initialize(true, "", 0);
895 char buffer_1[30] = {'\377'};
896 int bytes_read = 1;
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'};
901 bytes_read = 2;
902 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "b",
903 1, "\377a", 2, false, false);
906 // '\377'
907 // 'a' 'b'
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'};
914 int bytes_read = 1;
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'};
919 bytes_read = 2;
920 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
921 0, "\377ab", 3, false, false);
925 // '\377'
926 // 'a'
927 // 'b'
928 TEST_F(SerialIoHandlerPosixTest, InvalidSequenceReadThreeTimes) {
929 int buffer_len = 1;
930 Initialize(true, "", 0);
932 char buffer_1[30] = {'\377'};
933 int bytes_read = 1;
934 TestHelper(buffer_1, buffer_len, bytes_read, ErrorDetectState::MARK_377_SEEN,
935 "\377", 1, "", 0, false, false);
937 char buffer_2[30] = {'a'};
938 bytes_read = 1;
939 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "a",
940 1, "\377", 1, false, false);
942 char buffer_3[30] = {'b'};
943 bytes_read = 1;
944 TestHelper(buffer_3, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "b",
945 1, "a", 1, false, false);
948 // '\377'
949 // 'a'
950 // 'b'
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'};
956 int bytes_read = 1;
957 TestHelper(buffer_1, buffer_len, bytes_read,
958 ErrorDetectState::MARK_377_SEEN, "\377", 1, "", 0, false, false);
960 char buffer_2[30] = {'a'};
961 bytes_read = 1;
962 TestHelper(buffer_2, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
963 0, "\377a", 2, false, false);
965 char buffer_3[30] = {'b'};
966 bytes_read = 1;
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) {
974 int buffer_len = 2;
975 Initialize(true, "ab", 2);
977 char buffer[30] = {'c', '\377'};
978 int bytes_read = 2;
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) {
985 int buffer_len = 12;
986 Initialize(true, "", 0);
988 char buffer[30] = {'b', 'c', '\377', '\0', '\0', '\377',
989 '\377', '\377', '\0', 'a', 'd', 'e'};
990 int bytes_read = 12;
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'
997 // 'a' 'd' 'e'
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'};
1003 int bytes_read = 6;
1004 TestHelper(buffer_1, buffer_len, bytes_read,
1005 ErrorDetectState::MARK_377_SEEN, "\377", 1, "bc", 2, true,
1006 false);
1008 char buffer_2[30] = {'\377', '\377', '\0'};
1009 bytes_read = 3;
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'};
1014 bytes_read = 3;
1015 TestHelper(buffer_3, buffer_len, bytes_read, ErrorDetectState::NO_ERROR, "",
1016 0, "de", 2, false, true);
1020 } // namespace device