2016-02-14 11:45:12 +01:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) 2016 Fred Sundvik
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
extern "C" {
|
2016-02-21 22:17:59 +01:00
|
|
|
#include "serial_link/protocol/byte_stuffer.h"
|
|
|
|
#include "serial_link/protocol/frame_validator.h"
|
|
|
|
#include "serial_link/protocol/physical.h"
|
2016-08-24 21:23:17 +02:00
|
|
|
}
|
2016-02-14 11:45:12 +01:00
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
using testing::_;
|
|
|
|
using testing::Args;
|
2019-08-30 20:19:03 +02:00
|
|
|
using testing::ElementsAreArray;
|
2016-02-14 13:04:51 +01:00
|
|
|
|
2019-08-30 20:19:03 +02:00
|
|
|
class ByteStuffer : public ::testing::Test {
|
|
|
|
public:
|
2016-08-24 21:23:17 +02:00
|
|
|
ByteStuffer() {
|
|
|
|
Instance = this;
|
|
|
|
init_byte_stuffer();
|
|
|
|
}
|
2016-02-14 11:45:12 +01:00
|
|
|
|
2019-08-30 20:19:03 +02:00
|
|
|
~ByteStuffer() { Instance = nullptr; }
|
2016-08-24 21:23:17 +02:00
|
|
|
|
2019-08-30 20:19:03 +02:00
|
|
|
MOCK_METHOD3(validator_recv_frame, void(uint8_t link, uint8_t* data, uint16_t size));
|
2016-08-24 21:23:17 +02:00
|
|
|
|
2019-08-30 20:19:03 +02:00
|
|
|
void send_data(uint8_t link, const uint8_t* data, uint16_t size) { std::copy(data, data + size, std::back_inserter(sent_data)); }
|
2016-08-24 21:23:17 +02:00
|
|
|
std::vector<uint8_t> sent_data;
|
2016-02-14 11:45:12 +01:00
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
static ByteStuffer* Instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
ByteStuffer* ByteStuffer::Instance = nullptr;
|
|
|
|
|
|
|
|
extern "C" {
|
2019-08-30 20:19:03 +02:00
|
|
|
void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) { ByteStuffer::Instance->validator_recv_frame(link, data, size); }
|
2016-08-24 21:23:17 +02:00
|
|
|
|
2019-08-30 20:19:03 +02:00
|
|
|
void send_data(uint8_t link, const uint8_t* data, uint16_t size) { ByteStuffer::Instance->send_data(link, data, size); }
|
2016-02-14 16:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_no_frame_for_a_single_zero_byte) {
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).Times(0);
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 0);
|
2016-02-14 11:45:12 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_no_frame_for_a_single_FF_byte) {
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).Times(0);
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 0xFF);
|
2016-02-14 11:45:12 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_no_frame_for_a_single_random_byte) {
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).Times(0);
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 0x4A);
|
2016-02-14 13:04:51 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_no_frame_for_a_zero_length_frame) {
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).Times(0);
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 1);
|
|
|
|
byte_stuffer_recv_byte(0, 0);
|
2016-02-14 14:57:44 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_single_byte_valid_frame) {
|
2016-02-14 13:04:51 +01:00
|
|
|
uint8_t expected[] = {0x37};
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(expected)));
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 2);
|
|
|
|
byte_stuffer_recv_byte(0, 0x37);
|
|
|
|
byte_stuffer_recv_byte(0, 0);
|
2016-02-14 13:04:51 +01:00
|
|
|
}
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_three_bytes_valid_frame) {
|
2016-02-14 13:04:51 +01:00
|
|
|
uint8_t expected[] = {0x37, 0x99, 0xFF};
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(expected)));
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 4);
|
|
|
|
byte_stuffer_recv_byte(0, 0x37);
|
|
|
|
byte_stuffer_recv_byte(0, 0x99);
|
|
|
|
byte_stuffer_recv_byte(0, 0xFF);
|
|
|
|
byte_stuffer_recv_byte(0, 0);
|
2016-02-14 13:04:51 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_single_zero_valid_frame) {
|
2016-02-14 13:04:51 +01:00
|
|
|
uint8_t expected[] = {0};
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(expected)));
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 1);
|
|
|
|
byte_stuffer_recv_byte(0, 1);
|
|
|
|
byte_stuffer_recv_byte(0, 0);
|
2016-02-14 13:04:51 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_valid_frame_with_zeroes) {
|
2016-02-14 13:04:51 +01:00
|
|
|
uint8_t expected[] = {5, 0, 3, 0};
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(expected)));
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 2);
|
|
|
|
byte_stuffer_recv_byte(0, 5);
|
|
|
|
byte_stuffer_recv_byte(0, 2);
|
|
|
|
byte_stuffer_recv_byte(0, 3);
|
|
|
|
byte_stuffer_recv_byte(0, 1);
|
|
|
|
byte_stuffer_recv_byte(0, 0);
|
2016-02-14 11:45:12 +01:00
|
|
|
}
|
2016-02-14 13:25:54 +01:00
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_two_valid_frames) {
|
2016-02-14 13:25:54 +01:00
|
|
|
uint8_t expected1[] = {5, 0};
|
|
|
|
uint8_t expected2[] = {3};
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(expected1)));
|
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(expected2)));
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(1, 2);
|
|
|
|
byte_stuffer_recv_byte(1, 5);
|
|
|
|
byte_stuffer_recv_byte(1, 1);
|
|
|
|
byte_stuffer_recv_byte(1, 0);
|
|
|
|
byte_stuffer_recv_byte(1, 2);
|
|
|
|
byte_stuffer_recv_byte(1, 3);
|
|
|
|
byte_stuffer_recv_byte(1, 0);
|
2016-02-14 13:25:54 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_valid_frame_after_unexpected_zero) {
|
2016-02-14 13:25:54 +01:00
|
|
|
uint8_t expected[] = {5, 7};
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(expected)));
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(1, 3);
|
|
|
|
byte_stuffer_recv_byte(1, 1);
|
|
|
|
byte_stuffer_recv_byte(1, 0);
|
|
|
|
byte_stuffer_recv_byte(1, 3);
|
|
|
|
byte_stuffer_recv_byte(1, 5);
|
|
|
|
byte_stuffer_recv_byte(1, 7);
|
|
|
|
byte_stuffer_recv_byte(1, 0);
|
2016-02-14 13:25:54 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) {
|
2016-02-14 13:25:54 +01:00
|
|
|
uint8_t expected[] = {5, 7};
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(expected)));
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 2);
|
|
|
|
byte_stuffer_recv_byte(0, 9);
|
2019-08-30 20:19:03 +02:00
|
|
|
byte_stuffer_recv_byte(0, 4); // This should have been zero
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 0);
|
|
|
|
byte_stuffer_recv_byte(0, 3);
|
|
|
|
byte_stuffer_recv_byte(0, 5);
|
|
|
|
byte_stuffer_recv_byte(0, 7);
|
|
|
|
byte_stuffer_recv_byte(0, 0);
|
2016-02-14 13:25:54 +01:00
|
|
|
}
|
2016-02-14 14:28:57 +01:00
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_of_frame) {
|
2016-02-14 14:28:57 +01:00
|
|
|
uint8_t expected[254];
|
2019-08-30 20:19:03 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 254; i++) {
|
2016-02-14 14:28:57 +01:00
|
|
|
expected[i] = i + 1;
|
|
|
|
}
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(expected)));
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 0xFF);
|
2019-08-30 20:19:03 +02:00
|
|
|
for (i = 0; i < 254; i++) {
|
|
|
|
byte_stuffer_recv_byte(0, i + 1);
|
2016-02-14 14:28:57 +01:00
|
|
|
}
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 0);
|
2016-02-14 14:28:57 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_non_zero) {
|
2016-02-14 14:28:57 +01:00
|
|
|
uint8_t expected[255];
|
2019-08-30 20:19:03 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 254; i++) {
|
2016-02-14 14:28:57 +01:00
|
|
|
expected[i] = i + 1;
|
|
|
|
}
|
|
|
|
expected[254] = 7;
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(expected)));
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 0xFF);
|
2019-08-30 20:19:03 +02:00
|
|
|
for (i = 0; i < 254; i++) {
|
|
|
|
byte_stuffer_recv_byte(0, i + 1);
|
2016-02-14 14:28:57 +01:00
|
|
|
}
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 2);
|
|
|
|
byte_stuffer_recv_byte(0, 7);
|
|
|
|
byte_stuffer_recv_byte(0, 0);
|
2016-02-14 14:28:57 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_zero) {
|
2016-02-14 14:28:57 +01:00
|
|
|
uint8_t expected[255];
|
2019-08-30 20:19:03 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 254; i++) {
|
2016-02-14 14:28:57 +01:00
|
|
|
expected[i] = i + 1;
|
|
|
|
}
|
|
|
|
expected[254] = 0;
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(expected)));
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 0xFF);
|
2019-08-30 20:19:03 +02:00
|
|
|
for (i = 0; i < 254; i++) {
|
|
|
|
byte_stuffer_recv_byte(0, i + 1);
|
2016-02-14 14:28:57 +01:00
|
|
|
}
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 1);
|
|
|
|
byte_stuffer_recv_byte(0, 1);
|
|
|
|
byte_stuffer_recv_byte(0, 0);
|
2016-02-14 14:28:57 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_two_long_frames_and_some_more) {
|
2016-02-14 14:28:57 +01:00
|
|
|
uint8_t expected[515];
|
2019-08-30 20:19:03 +02:00
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
for (i = 0; i < 254; i++) {
|
|
|
|
expected[i + 254 * j] = i + 1;
|
2016-02-14 14:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
2019-08-30 20:19:03 +02:00
|
|
|
for (i = 0; i < 7; i++) {
|
|
|
|
expected[254 * 2 + i] = i + 1;
|
2016-02-14 14:28:57 +01:00
|
|
|
}
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(expected)));
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 0xFF);
|
2019-08-30 20:19:03 +02:00
|
|
|
for (i = 0; i < 254; i++) {
|
|
|
|
byte_stuffer_recv_byte(0, i + 1);
|
2016-02-14 14:28:57 +01:00
|
|
|
}
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 0xFF);
|
2019-08-30 20:19:03 +02:00
|
|
|
for (i = 0; i < 254; i++) {
|
|
|
|
byte_stuffer_recv_byte(0, i + 1);
|
2016-02-14 14:28:57 +01:00
|
|
|
}
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 8);
|
|
|
|
byte_stuffer_recv_byte(0, 1);
|
|
|
|
byte_stuffer_recv_byte(0, 2);
|
|
|
|
byte_stuffer_recv_byte(0, 3);
|
|
|
|
byte_stuffer_recv_byte(0, 4);
|
|
|
|
byte_stuffer_recv_byte(0, 5);
|
|
|
|
byte_stuffer_recv_byte(0, 6);
|
|
|
|
byte_stuffer_recv_byte(0, 7);
|
|
|
|
byte_stuffer_recv_byte(0, 0);
|
2016-02-14 14:28:57 +01:00
|
|
|
}
|
2016-02-14 14:57:44 +01:00
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) {
|
2016-02-14 14:57:44 +01:00
|
|
|
uint8_t expected[MAX_FRAME_SIZE] = {};
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(expected)));
|
2016-02-14 14:57:44 +01:00
|
|
|
int i;
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 1);
|
2019-08-30 20:19:03 +02:00
|
|
|
for (i = 0; i < MAX_FRAME_SIZE; i++) {
|
|
|
|
byte_stuffer_recv_byte(0, 1);
|
2016-02-14 14:57:44 +01:00
|
|
|
}
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 0);
|
2016-02-14 14:57:44 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, doesnt_recv_a_frame_thats_too_long_all_zeroes) {
|
2016-02-14 14:57:44 +01:00
|
|
|
uint8_t expected[1] = {0};
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).Times(0);
|
2016-02-14 14:57:44 +01:00
|
|
|
int i;
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 1);
|
2019-08-30 20:19:03 +02:00
|
|
|
for (i = 0; i < MAX_FRAME_SIZE; i++) {
|
|
|
|
byte_stuffer_recv_byte(0, 1);
|
2016-02-14 14:57:44 +01:00
|
|
|
}
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 1);
|
|
|
|
byte_stuffer_recv_byte(0, 0);
|
2016-02-14 14:57:44 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, received_frame_is_aborted_when_its_too_long) {
|
2016-02-14 14:57:44 +01:00
|
|
|
uint8_t expected[1] = {1};
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(expected)));
|
2016-02-14 14:57:44 +01:00
|
|
|
int i;
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 1);
|
2019-08-30 20:19:03 +02:00
|
|
|
for (i = 0; i < MAX_FRAME_SIZE; i++) {
|
|
|
|
byte_stuffer_recv_byte(0, 1);
|
2016-02-14 14:57:44 +01:00
|
|
|
}
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_recv_byte(0, 2);
|
|
|
|
byte_stuffer_recv_byte(0, 1);
|
|
|
|
byte_stuffer_recv_byte(0, 0);
|
2016-02-14 14:57:44 +01:00
|
|
|
}
|
2016-02-14 16:45:25 +01:00
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, does_nothing_when_sending_zero_size_frame) {
|
|
|
|
EXPECT_EQ(sent_data.size(), 0);
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(0, NULL, 0);
|
2016-02-14 16:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, send_one_byte_frame) {
|
2016-02-14 16:45:25 +01:00
|
|
|
uint8_t data[] = {5};
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(1, data, 1);
|
2016-02-14 16:45:25 +01:00
|
|
|
uint8_t expected[] = {2, 5, 0};
|
2016-08-24 21:23:17 +02:00
|
|
|
EXPECT_THAT(sent_data, ElementsAreArray(expected));
|
2016-02-14 16:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, sends_two_byte_frame) {
|
2016-02-14 16:45:25 +01:00
|
|
|
uint8_t data[] = {5, 0x77};
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(0, data, 2);
|
2016-02-14 16:45:25 +01:00
|
|
|
uint8_t expected[] = {3, 5, 0x77, 0};
|
2016-08-24 21:23:17 +02:00
|
|
|
EXPECT_THAT(sent_data, ElementsAreArray(expected));
|
2016-02-14 17:59:01 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, sends_one_byte_frame_with_zero) {
|
2016-02-14 17:59:01 +01:00
|
|
|
uint8_t data[] = {0};
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(0, data, 1);
|
2016-02-14 17:59:01 +01:00
|
|
|
uint8_t expected[] = {1, 1, 0};
|
2016-08-24 21:23:17 +02:00
|
|
|
EXPECT_THAT(sent_data, ElementsAreArray(expected));
|
2016-02-14 17:59:01 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, sends_two_byte_frame_starting_with_zero) {
|
2016-02-14 17:59:01 +01:00
|
|
|
uint8_t data[] = {0, 9};
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(1, data, 2);
|
2016-02-14 17:59:01 +01:00
|
|
|
uint8_t expected[] = {1, 2, 9, 0};
|
2016-08-24 21:23:17 +02:00
|
|
|
EXPECT_THAT(sent_data, ElementsAreArray(expected));
|
2016-02-14 17:59:01 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, sends_two_byte_frame_starting_with_non_zero) {
|
2016-02-14 17:59:01 +01:00
|
|
|
uint8_t data[] = {9, 0};
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(1, data, 2);
|
2016-02-14 17:59:01 +01:00
|
|
|
uint8_t expected[] = {2, 9, 1, 0};
|
2016-08-24 21:23:17 +02:00
|
|
|
EXPECT_THAT(sent_data, ElementsAreArray(expected));
|
2016-02-14 17:59:01 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, sends_three_byte_frame_zero_in_the_middle) {
|
2016-02-14 17:59:01 +01:00
|
|
|
uint8_t data[] = {9, 0, 0x68};
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(0, data, 3);
|
2016-02-14 17:59:01 +01:00
|
|
|
uint8_t expected[] = {2, 9, 2, 0x68, 0};
|
2016-08-24 21:23:17 +02:00
|
|
|
EXPECT_THAT(sent_data, ElementsAreArray(expected));
|
2016-02-14 17:59:01 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, sends_three_byte_frame_data_in_the_middle) {
|
2016-02-14 17:59:01 +01:00
|
|
|
uint8_t data[] = {0, 0x55, 0};
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(0, data, 3);
|
2016-02-14 17:59:01 +01:00
|
|
|
uint8_t expected[] = {1, 2, 0x55, 1, 0};
|
2016-08-24 21:23:17 +02:00
|
|
|
EXPECT_THAT(sent_data, ElementsAreArray(expected));
|
2016-02-14 17:59:01 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, sends_three_byte_frame_with_all_zeroes) {
|
2016-02-14 17:59:01 +01:00
|
|
|
uint8_t data[] = {0, 0, 0};
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(0, data, 3);
|
2016-02-14 17:59:01 +01:00
|
|
|
uint8_t expected[] = {1, 1, 1, 1, 0};
|
2016-08-24 21:23:17 +02:00
|
|
|
EXPECT_THAT(sent_data, ElementsAreArray(expected));
|
2016-02-14 16:45:25 +01:00
|
|
|
}
|
2016-02-14 18:56:22 +01:00
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, sends_frame_with_254_non_zeroes) {
|
2016-02-14 18:56:22 +01:00
|
|
|
uint8_t data[254];
|
2019-08-30 20:19:03 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 254; i++) {
|
2016-02-14 18:56:22 +01:00
|
|
|
data[i] = i + 1;
|
|
|
|
}
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(0, data, 254);
|
2016-02-14 18:56:22 +01:00
|
|
|
uint8_t expected[256];
|
|
|
|
expected[0] = 0xFF;
|
2019-08-30 20:19:03 +02:00
|
|
|
for (i = 1; i < 255; i++) {
|
2016-02-14 18:56:22 +01:00
|
|
|
expected[i] = i;
|
|
|
|
}
|
|
|
|
expected[255] = 0;
|
2016-08-24 21:23:17 +02:00
|
|
|
EXPECT_THAT(sent_data, ElementsAreArray(expected));
|
2016-02-14 18:56:22 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, sends_frame_with_255_non_zeroes) {
|
2016-02-14 18:56:22 +01:00
|
|
|
uint8_t data[255];
|
2019-08-30 20:19:03 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 255; i++) {
|
2016-02-14 18:56:22 +01:00
|
|
|
data[i] = i + 1;
|
|
|
|
}
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(0, data, 255);
|
2016-02-14 18:56:22 +01:00
|
|
|
uint8_t expected[258];
|
|
|
|
expected[0] = 0xFF;
|
2019-08-30 20:19:03 +02:00
|
|
|
for (i = 1; i < 255; i++) {
|
2016-02-14 18:56:22 +01:00
|
|
|
expected[i] = i;
|
|
|
|
}
|
|
|
|
expected[255] = 2;
|
|
|
|
expected[256] = 255;
|
|
|
|
expected[257] = 0;
|
2016-08-24 21:23:17 +02:00
|
|
|
EXPECT_THAT(sent_data, ElementsAreArray(expected));
|
2016-02-14 18:56:22 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, sends_frame_with_254_non_zeroes_followed_by_zero) {
|
2016-02-14 18:56:22 +01:00
|
|
|
uint8_t data[255];
|
2019-08-30 20:19:03 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 254; i++) {
|
2016-02-14 18:56:22 +01:00
|
|
|
data[i] = i + 1;
|
|
|
|
}
|
2016-08-24 21:23:17 +02:00
|
|
|
data[254] = 0;
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(0, data, 255);
|
2016-02-14 18:56:22 +01:00
|
|
|
uint8_t expected[258];
|
|
|
|
expected[0] = 0xFF;
|
2019-08-30 20:19:03 +02:00
|
|
|
for (i = 1; i < 255; i++) {
|
2016-02-14 18:56:22 +01:00
|
|
|
expected[i] = i;
|
|
|
|
}
|
|
|
|
expected[255] = 1;
|
|
|
|
expected[256] = 1;
|
|
|
|
expected[257] = 0;
|
2016-08-24 21:23:17 +02:00
|
|
|
EXPECT_THAT(sent_data, ElementsAreArray(expected));
|
2016-02-14 18:56:22 +01:00
|
|
|
}
|
2016-02-14 19:07:51 +01:00
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_small_packet) {
|
2019-08-30 20:19:03 +02:00
|
|
|
uint8_t original_data[] = {1, 2, 3};
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(0, original_data, sizeof(original_data));
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(original_data)));
|
2016-02-14 19:07:51 +01:00
|
|
|
int i;
|
2019-08-30 20:19:03 +02:00
|
|
|
for (auto& d : sent_data) {
|
|
|
|
byte_stuffer_recv_byte(1, d);
|
2016-02-14 19:07:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_small_packet_with_zeros) {
|
2019-08-30 20:19:03 +02:00
|
|
|
uint8_t original_data[] = {1, 0, 3, 0, 0, 9};
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(1, original_data, sizeof(original_data));
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(original_data)));
|
2016-02-14 19:07:51 +01:00
|
|
|
int i;
|
2019-08-30 20:19:03 +02:00
|
|
|
for (auto& d : sent_data) {
|
|
|
|
byte_stuffer_recv_byte(1, d);
|
2016-02-14 19:07:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_254_bytes) {
|
2016-02-14 19:07:51 +01:00
|
|
|
uint8_t original_data[254];
|
2019-08-30 20:19:03 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 254; i++) {
|
2016-02-14 19:07:51 +01:00
|
|
|
original_data[i] = i + 1;
|
|
|
|
}
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(0, original_data, sizeof(original_data));
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(original_data)));
|
|
|
|
for (auto& d : sent_data) {
|
|
|
|
byte_stuffer_recv_byte(1, d);
|
2016-02-14 19:07:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_256_bytes) {
|
2016-02-14 19:07:51 +01:00
|
|
|
uint8_t original_data[256];
|
2019-08-30 20:19:03 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 254; i++) {
|
2016-02-14 19:07:51 +01:00
|
|
|
original_data[i] = i + 1;
|
|
|
|
}
|
|
|
|
original_data[254] = 22;
|
|
|
|
original_data[255] = 23;
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(0, original_data, sizeof(original_data));
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(original_data)));
|
|
|
|
for (auto& d : sent_data) {
|
|
|
|
byte_stuffer_recv_byte(1, d);
|
2016-02-14 19:07:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 21:23:17 +02:00
|
|
|
TEST_F(ByteStuffer, sends_and_receives_full_roundtrip_254_bytes_and_then_zero) {
|
2016-02-14 19:07:51 +01:00
|
|
|
uint8_t original_data[255];
|
2019-08-30 20:19:03 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 254; i++) {
|
2016-02-14 19:07:51 +01:00
|
|
|
original_data[i] = i + 1;
|
|
|
|
}
|
|
|
|
original_data[254] = 0;
|
2016-02-20 12:59:48 +01:00
|
|
|
byte_stuffer_send_frame(0, original_data, sizeof(original_data));
|
2019-08-30 20:19:03 +02:00
|
|
|
EXPECT_CALL(*this, validator_recv_frame(_, _, _)).With(Args<1, 2>(ElementsAreArray(original_data)));
|
|
|
|
for (auto& d : sent_data) {
|
|
|
|
byte_stuffer_recv_byte(1, d);
|
2016-02-14 19:07:51 +01:00
|
|
|
}
|
|
|
|
}
|