aboutsummaryrefslogtreecommitdiff
path: root/quantum/serial_link/tests/transport_tests.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/serial_link/tests/transport_tests.cpp')
-rw-r--r--quantum/serial_link/tests/transport_tests.cpp184
1 files changed, 0 insertions, 184 deletions
diff --git a/quantum/serial_link/tests/transport_tests.cpp b/quantum/serial_link/tests/transport_tests.cpp
deleted file mode 100644
index cfd111046..000000000
--- a/quantum/serial_link/tests/transport_tests.cpp
+++ /dev/null
@@ -1,184 +0,0 @@
1/*
2The MIT License (MIT)
3
4Copyright (c) 2016 Fred Sundvik
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#include "gtest/gtest.h"
26#include "gmock/gmock.h"
27
28using testing::_;
29using testing::Args;
30using testing::ElementsAreArray;
31
32extern "C" {
33#include "serial_link/protocol/transport.h"
34}
35
36struct test_object1 {
37 uint32_t test;
38};
39
40struct test_object2 {
41 uint32_t test1;
42 uint32_t test2;
43};
44
45MASTER_TO_ALL_SLAVES_OBJECT(master_to_slave, test_object1);
46MASTER_TO_SINGLE_SLAVE_OBJECT(master_to_single_slave, test_object1);
47SLAVE_TO_MASTER_OBJECT(slave_to_master, test_object1);
48
49static remote_object_t* test_remote_objects[] = {
50 REMOTE_OBJECT(master_to_slave),
51 REMOTE_OBJECT(master_to_single_slave),
52 REMOTE_OBJECT(slave_to_master),
53};
54
55class Transport : public testing::Test {
56 public:
57 Transport() {
58 Instance = this;
59 add_remote_objects(test_remote_objects, sizeof(test_remote_objects) / sizeof(remote_object_t*));
60 }
61
62 ~Transport() {
63 Instance = nullptr;
64 reinitialize_serial_link_transport();
65 }
66
67 MOCK_METHOD0(signal_data_written, void());
68 MOCK_METHOD1(router_send_frame, void(uint8_t destination));
69
70 void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) {
71 router_send_frame(destination);
72 std::copy(data, data + size, std::back_inserter(sent_data));
73 }
74
75 static Transport* Instance;
76
77 std::vector<uint8_t> sent_data;
78};
79
80Transport* Transport::Instance = nullptr;
81
82extern "C" {
83void signal_data_written(void) { Transport::Instance->signal_data_written(); }
84
85void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { Transport::Instance->router_send_frame(destination, data, size); }
86}
87
88TEST_F(Transport, write_to_local_signals_an_event) {
89 begin_write_master_to_slave();
90 EXPECT_CALL(*this, signal_data_written());
91 end_write_master_to_slave();
92 begin_write_slave_to_master();
93 EXPECT_CALL(*this, signal_data_written());
94 end_write_slave_to_master();
95 begin_write_master_to_single_slave(1);
96 EXPECT_CALL(*this, signal_data_written());
97 end_write_master_to_single_slave(1);
98}
99
100TEST_F(Transport, writes_from_master_to_all_slaves) {
101 update_transport();
102 test_object1* obj = begin_write_master_to_slave();
103 obj->test = 5;
104 EXPECT_CALL(*this, signal_data_written());
105 end_write_master_to_slave();
106 EXPECT_CALL(*this, router_send_frame(0xFF));
107 update_transport();
108 transport_recv_frame(0, sent_data.data(), sent_data.size());
109 test_object1* obj2 = read_master_to_slave();
110 EXPECT_NE(obj2, nullptr);
111 EXPECT_EQ(obj2->test, 5);
112}
113
114TEST_F(Transport, writes_from_slave_to_master) {
115 update_transport();
116 test_object1* obj = begin_write_slave_to_master();
117 obj->test = 7;
118 EXPECT_CALL(*this, signal_data_written());
119 end_write_slave_to_master();
120 EXPECT_CALL(*this, router_send_frame(0));
121 update_transport();
122 transport_recv_frame(3, sent_data.data(), sent_data.size());
123 test_object1* obj2 = read_slave_to_master(2);
124 EXPECT_EQ(read_slave_to_master(0), nullptr);
125 EXPECT_NE(obj2, nullptr);
126 EXPECT_EQ(obj2->test, 7);
127}
128
129TEST_F(Transport, writes_from_master_to_single_slave) {
130 update_transport();
131 test_object1* obj = begin_write_master_to_single_slave(3);
132 obj->test = 7;
133 EXPECT_CALL(*this, signal_data_written());
134 end_write_master_to_single_slave(3);
135 EXPECT_CALL(*this, router_send_frame(4));
136 update_transport();
137 transport_recv_frame(0, sent_data.data(), sent_data.size());
138 test_object1* obj2 = read_master_to_single_slave();
139 EXPECT_NE(obj2, nullptr);
140 EXPECT_EQ(obj2->test, 7);
141}
142
143TEST_F(Transport, ignores_object_with_invalid_id) {
144 update_transport();
145 test_object1* obj = begin_write_master_to_single_slave(3);
146 obj->test = 7;
147 EXPECT_CALL(*this, signal_data_written());
148 end_write_master_to_single_slave(3);
149 EXPECT_CALL(*this, router_send_frame(4));
150 update_transport();
151 sent_data[sent_data.size() - 1] = 44;
152 transport_recv_frame(0, sent_data.data(), sent_data.size());
153 test_object1* obj2 = read_master_to_single_slave();
154 EXPECT_EQ(obj2, nullptr);
155}
156
157TEST_F(Transport, ignores_object_with_size_too_small) {
158 update_transport();
159 test_object1* obj = begin_write_master_to_slave();
160 obj->test = 7;
161 EXPECT_CALL(*this, signal_data_written());
162 end_write_master_to_slave();
163 EXPECT_CALL(*this, router_send_frame(_));
164 update_transport();
165 sent_data[sent_data.size() - 2] = 0;
166 transport_recv_frame(0, sent_data.data(), sent_data.size() - 1);
167 test_object1* obj2 = read_master_to_slave();
168 EXPECT_EQ(obj2, nullptr);
169}
170
171TEST_F(Transport, ignores_object_with_size_too_big) {
172 update_transport();
173 test_object1* obj = begin_write_master_to_slave();
174 obj->test = 7;
175 EXPECT_CALL(*this, signal_data_written());
176 end_write_master_to_slave();
177 EXPECT_CALL(*this, router_send_frame(_));
178 update_transport();
179 sent_data.resize(sent_data.size() + 22);
180 sent_data[sent_data.size() - 1] = 0;
181 transport_recv_frame(0, sent_data.data(), sent_data.size());
182 test_object1* obj2 = read_master_to_slave();
183 EXPECT_EQ(obj2, nullptr);
184}