CoLa2ProtocolHandlerTest.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2023 SICK AG, Waldkirch
3 //
4 // SPDX-License-Identifier: Unlicense
5 
6 #include "CoLa2ProtocolHandler.h"
7 #include "CoLaParameterReader.h"
8 #include "CoLaParameterWriter.h"
9 #include "MockTransport.h"
10 #include "VisionaryEndian.h"
11 #include "gtest/gtest.h"
12 
13 using namespace visionary_test;
14 using namespace visionary;
15 //---------------------------------------------------------------------------------------
16 
17 TEST(CoLa2ProtocolHandlerTest, OpenSession)
18 {
19  constexpr uint32_t SESSION_ID = 0x4e11ba11u;
20  MockCoLa2Transport transport;
21  CoLa2ProtocolHandler protocolHandler{transport};
22  transport.sessionId(SESSION_ID).cmdMode("OA");
23 
24  EXPECT_TRUE(protocolHandler.openSession(50u));
25 
26  EXPECT_TRUE(transport.cmdHeader().has_value());
27  EXPECT_EQ(transport.cmdHeader()->cmdMode, "Ox");
28  EXPECT_EQ(protocolHandler.getSessionId(), SESSION_ID);
29 }
30 
31 TEST(CoLa2ProtocolHandlerTest, ReadVariable)
32 {
33  MockCoLa2Transport transport;
34  const ByteBuffer results({0x01u, 0x02u, 0x03u, 0x04u});
35  transport.cmdMode("RA").name(" varname ").returnvals(results);
36 
37  CoLa2ProtocolHandler protocolHandler{transport};
38 
40 
41  auto response = protocolHandler.send(testCommand);
42 
43  EXPECT_TRUE(transport.cmdHeader().has_value());
44  EXPECT_EQ(transport.cmdHeader()->cmdMode, "RN");
46  EXPECT_EQ(CoLaError::OK, response.getError());
47 
48  EXPECT_EQ(CoLaParameterReader(response).readUDInt(), 0x01020304u);
49 }
50 
51 TEST(CoLa2ProtocolHandlerTest, WriteVariable)
52 {
53  MockCoLa2Transport transport;
54  constexpr int32_t VAR_VALUE = -0x12345678; // is hex 0xEDCBA988
55  transport.cmdMode("WA").name(" vname ");
56 
57  CoLa2ProtocolHandler protocolHandler{transport};
58 
59  CoLaCommand testCommand =
61  auto response = protocolHandler.send(testCommand);
62 
63  EXPECT_TRUE(transport.cmdHeader().has_value());
64  EXPECT_EQ(transport.cmdHeader()->cmdMode, "WN");
65 
66  const ByteBuffer expectedValue{' ', 'v', 'n', 'a', 'm', 'e', ' ', 0xedu, 0xcbu, 0xa9u, 0x88u}; // varname + parameter
67  EXPECT_EQ(transport.cmdpayload(), expectedValue);
68 
70  EXPECT_EQ(CoLaError::OK, response.getError());
71 }
72 
73 TEST(CoLa2ProtocolHandlerTest, MethodInvocation)
74 {
75  MockCoLa2Transport transport;
76  constexpr uint16_t PAR_VALUE = 0xfeed;
77  const ByteBuffer results({0x01u, 0x02u, 0x03u, 0x04u});
78  transport.cmdMode("AN").name(" mtd ").returnvals(results);
79 
80  CoLa2ProtocolHandler protocolHandler{transport};
81 
82  CoLaCommand testCommand =
84 
85  auto response = protocolHandler.send(testCommand);
86 
87  const ByteBuffer expectedValue({' ', 'm', 't', 'd', ' ', 0xfeu, 0xedu});
88  EXPECT_EQ(transport.cmdpayload(), expectedValue);
89 
90  EXPECT_TRUE(transport.cmdHeader().has_value());
91  EXPECT_EQ(transport.cmdHeader()->cmdMode, "MN");
92 
93  EXPECT_EQ(CoLaCommandType::METHOD_RETURN_VALUE, response.getType());
94  EXPECT_EQ(CoLaError::OK, response.getError());
95 
96  EXPECT_EQ(CoLaParameterReader(response).readUDInt(), 0x01020304u);
97 }
98 
99 TEST(CoLa2ProtocolHandlerTest, OpenSessionInvalidMagic)
100 {
101  MockTransport transport{0x02, 0x02, 0x02, 0x01};
102  CoLa2ProtocolHandler protocolHandler{transport};
103 
104  EXPECT_FALSE(protocolHandler.openSession(50u));
105 }
106 
107 TEST(CoLa2ProtocolHandlerTest, OpenSessionBrokenPacket)
108 {
109  MockTransport transport{0x02, 0x02, 0x02, 0x02, 0x0, 0x0, 0x0, 0x1, 0x1};
110  CoLa2ProtocolHandler protocolHandler{transport};
111 
112  EXPECT_FALSE(protocolHandler.openSession(50u));
113 }
114 
115 TEST(CoLa2ProtocolHandlerTest, OpenSessionEmptyPacket)
116 {
117  MockTransport transport;
118  CoLa2ProtocolHandler protocolHandler{transport};
119 
120  EXPECT_FALSE(protocolHandler.openSession(50u));
121 }
122 
123 TEST(CoLa2ProtocolHandlerTest, InvalidMagicBytes)
124 {
125  MockTransport transport{0x02, 0x02, 0x02, 0x01};
126  CoLa2ProtocolHandler protocolHandler{transport};
127 
129 
130  auto response = protocolHandler.send(testCommand);
131 
132  EXPECT_EQ(CoLaCommandType::NETWORK_ERROR, response.getType());
133  EXPECT_EQ(CoLaError::NETWORK_ERROR, response.getError());
134 }
135 
136 TEST(CoLa2ProtocolHandlerTest, tooFewMagicBytes)
137 {
138  MockTransport transport{0x02, 0x02, 0x02};
139  CoLa2ProtocolHandler protocolHandler{transport};
140 
142  auto response = protocolHandler.send(testCommand);
143 
144  EXPECT_EQ(CoLaCommandType::NETWORK_ERROR, response.getType());
145  EXPECT_EQ(CoLaError::NETWORK_ERROR, response.getError());
146 }
147 
148 TEST(CoLa2ProtocolHandlerTest, EmptyAnswer)
149 {
150  MockTransport transport{0x02, 0x02, 0x02, 0x02};
151  CoLa2ProtocolHandler protocolHandler{transport};
152 
154  auto response = protocolHandler.send(testCommand);
155 
156  EXPECT_EQ(CoLaCommandType::NETWORK_ERROR, response.getType());
157  EXPECT_EQ(CoLaError::NETWORK_ERROR, response.getError());
158 }
159 
160 TEST(CoLa2ProtocolHandlerTest, EmptyPackage)
161 {
162  MockTransport transport{0x02, 0x02, 0x02, 0x02};
163  CoLa2ProtocolHandler protocolHandler{transport};
164 
166  auto response = protocolHandler.send(testCommand);
167 
168  EXPECT_EQ(CoLaCommandType::NETWORK_ERROR, response.getType());
169  EXPECT_EQ(CoLaError::NETWORK_ERROR, response.getError());
170 }
171 
172 TEST(CoLa2ProtocolHandlerTest, InvalidSessionId)
173 {
174  MockCoLa2Transport transport;
175  const ByteBuffer results({0x01u, 0x02u, 0x03u, 0x04u});
176  transport.sessionId(0xbadfeed1u).cmdMode("RA").name(" varname ").returnvals(results);
177 
178  CoLa2ProtocolHandler protocolHandler{transport};
179 
181  auto response = protocolHandler.send(testCommand);
182 
183  EXPECT_EQ(CoLaCommandType::NETWORK_ERROR, response.getType());
184  EXPECT_EQ(CoLaError::NETWORK_ERROR, response.getError());
185 }
186 
187 TEST(CoLa2ProtocolHandlerTest, InvalidReqId)
188 {
189  MockCoLa2Transport transport;
190  constexpr int32_t VAR_VALUE = -0x12345678; // is hex 0xEDCBA988
191  transport.reqId(0xdeadu).cmdMode("WA").name(" vname ");
192 
193  CoLa2ProtocolHandler protocolHandler{transport};
194 
195  CoLaCommand testCommand =
197  auto response = protocolHandler.send(testCommand);
198 
199  EXPECT_EQ(CoLaCommandType::NETWORK_ERROR, response.getType());
200  EXPECT_EQ(CoLaError::NETWORK_ERROR, response.getError());
201 }
202 
203 TEST(CoLa2ProtocolHandlerTest, InvalidResponseCode)
204 {
205  MockCoLa2Transport transport;
206  transport.cmdMode("FB");
207  CoLa2ProtocolHandler protocolHandler{transport};
208 
210 
211  auto response = protocolHandler.send(testCommand);
212 
213  EXPECT_EQ(CoLaCommandType::UNKNOWN, response.getType());
214  EXPECT_EQ(CoLaError::UNKNOWN, response.getError());
215 }
216 
217 TEST(CoLa2ProtocolHandlerTest, ColaAnswerTooShort)
218 {
219  MockCoLa2Transport transport;
220  transport.cmdMode("RA");
221  CoLa2ProtocolHandler protocolHandler{transport};
222 
224 
225  auto response = protocolHandler.send(testCommand);
226 
227  EXPECT_EQ(CoLaCommandType::UNKNOWN, response.getType());
228  EXPECT_EQ(CoLaError::UNKNOWN, response.getError());
229 }
230 
231 TEST(CoLa2ProtocolHandlerTest, ColaErrorTooShort)
232 {
233  MockCoLa2Transport transport;
234  transport.cmdMode("F");
235  CoLa2ProtocolHandler protocolHandler{transport};
236 
238 
239  auto response = protocolHandler.send(testCommand);
240 
241  EXPECT_EQ(CoLaCommandType::UNKNOWN, response.getType());
242  EXPECT_EQ(CoLaError::UNKNOWN, response.getError());
243 }
244 
245 TEST(CoLa2ProtocolHandlerTest, ColaErrorErrornoMissing)
246 {
247  MockCoLa2Transport transport;
248  transport.cmdMode("FA").returnvals({0x01});
249  CoLa2ProtocolHandler protocolHandler{transport};
250 
252 
253  auto response = protocolHandler.send(testCommand);
254 
255  EXPECT_EQ(CoLaCommandType::UNKNOWN, response.getType());
256  EXPECT_EQ(CoLaError::UNKNOWN, response.getError());
257 }
258 
259 TEST(CoLa2ProtocolHandlerTest, ColaErrorErrorTooShort)
260 {
261  MockCoLa2Transport transport;
262  transport.cmdMode("FA");
263 
264  CoLa2ProtocolHandler protocolHandler{transport};
265 
267 
268  auto response = protocolHandler.send(testCommand);
269 
270  EXPECT_EQ(CoLaCommandType::UNKNOWN, response.getType());
271  EXPECT_EQ(CoLaError::UNKNOWN, response.getError());
272 }
273 
274 TEST(CoLa2ProtocolHandlerTest, ColaErrorValid)
275 {
276  MockCoLa2Transport transport;
277  transport.cmdMode("FA").returnvals({0x00u, 0x04u});
278 
279  CoLa2ProtocolHandler protocolHandler{transport};
280 
282 
283  auto response = protocolHandler.send(testCommand);
284 
285  EXPECT_EQ(CoLaCommandType::COLA_ERROR, response.getType());
286  EXPECT_EQ(CoLaError::LOCAL_CONDITION_FAILED, response.getError());
287 }
288 
289 TEST(CoLa2ProtocolHandlerTest, SendFailed)
290 {
291  MockCoLa2Transport transport;
292  transport.cmdMode("FA").returnvals({0x00u, 0x01u}).fakeSendReturn(-1);
293 
294  CoLa2ProtocolHandler protocolHandler{transport};
295 
297 
298  auto response = protocolHandler.send(testCommand);
299 
300  EXPECT_EQ(CoLaCommandType::NETWORK_ERROR, response.getType());
301  EXPECT_EQ(CoLaError::NETWORK_ERROR, response.getError());
302 }
response
const std::string response
VisionaryEndian.h
TEST
TEST(CoLa2ProtocolHandlerTest, OpenSession)
Definition: CoLa2ProtocolHandlerTest.cpp:17
visionary::CoLaParameterReader
Class for reading data from a CoLaCommand.
Definition: CoLaParameterReader.h:17
visionary
Definition: MD5.cpp:44
visionary::CoLaParameterWriter
Builder for constructing CoLaCommands.
Definition: CoLaParameterWriter.h:19
visionary::CoLaCommand
Definition: CoLaCommand.h:17
visionary_test::MockCoLa2Transport::reqId
MockCoLa2Transport & reqId(std::uint16_t reqId)
Definition: MockTransport.cpp:213
visionary::CoLaCommandType::WRITE_VARIABLE
@ WRITE_VARIABLE
Definition: CoLaCommandType.h:18
visionary_test::MockCoLa2Transport::cmdMode
MockCoLa2Transport & cmdMode(const std::string &cmdMode)
Definition: MockTransport.cpp:219
CoLaParameterWriter.h
CoLa2ProtocolHandler.h
visionary_test::MockCoLa2Transport::returnvals
MockCoLa2Transport & returnvals(const ByteBuffer &parameterBuf)
Definition: MockTransport.cpp:231
visionary::CoLaCommandType::READ_VARIABLE
@ READ_VARIABLE
Definition: CoLaCommandType.h:16
MockTransport.h
visionary::CoLaError::LOCAL_CONDITION_FAILED
@ LOCAL_CONDITION_FAILED
Local condition violated, e.g. giving a value that exceeds the minimum or maximum allowed value for t...
Definition: CoLaError.h:31
visionary_test::MockCoLa2Transport::cmdHeader
const Opt< CmdReqRespHeader > & cmdHeader() const
Definition: MockTransport.h:191
visionary::CoLaCommandType::NETWORK_ERROR
@ NETWORK_ERROR
Definition: CoLaCommandType.h:14
visionary::CoLaCommandType::WRITE_VARIABLE_RESPONSE
@ WRITE_VARIABLE_RESPONSE
Definition: CoLaCommandType.h:19
visionary_test::MockTransport
Definition: MockTransport.h:98
visionary::CoLaCommandType::METHOD_INVOCATION
@ METHOD_INVOCATION
Definition: CoLaCommandType.h:20
visionary::CoLaCommandType::READ_VARIABLE_RESPONSE
@ READ_VARIABLE_RESPONSE
Definition: CoLaCommandType.h:17
visionary_test::MockCoLa2Transport
Definition: MockTransport.h:168
visionary::CoLaParameterWriter::parameterUInt
CoLaParameterWriter & parameterUInt(const uint16_t uInt)
Add a unsigned int (16-bit, range [0, 65535]).
Definition: CoLaParameterWriter.cpp:45
visionary_test::MockCoLa2Transport::name
MockCoLa2Transport & name(const std::string &name)
Definition: MockTransport.cpp:225
visionary_test::MockCoLa2Transport::cmdpayload
const ByteBuffer & cmdpayload() const
Definition: MockTransport.h:195
visionary::CoLaCommandType::COLA_ERROR
@ COLA_ERROR
Definition: CoLaCommandType.h:22
CoLaParameterReader.h
visionary_test
Definition: MockTransport.cpp:14
visionary::CoLaCommandType::METHOD_RETURN_VALUE
@ METHOD_RETURN_VALUE
Definition: CoLaCommandType.h:21
visionary::UNKNOWN
@ UNKNOWN
Definition: AuthenticationSecure.h:15
visionary_test::ByteBuffer
std::vector< std::uint8_t > ByteBuffer
Definition: MockTransport.h:18
visionary::CoLaParameterWriter::parameterDInt
CoLaParameterWriter & parameterDInt(const int32_t dInt)
Add an signed double int (32-bit).
Definition: CoLaParameterWriter.cpp:54
visionary_test::MockCoLa2Transport::sessionId
MockCoLa2Transport & sessionId(std::uint32_t sessionId)
Definition: MockTransport.cpp:207
visionary::CoLa2ProtocolHandler
Definition: CoLa2ProtocolHandler.h:17
visionary::CoLaParameterWriter::build
const CoLaCommand build()
Definition: CoLaParameterWriter.cpp:181


sick_visionary_ros
Author(s): SICK AG TechSupport 3D Snapshot
autogenerated on Thu Feb 8 2024 03:38:05