Skip to content

Commit 6f8e7fe

Browse files
committed
Add encode test to increase coverage
1 parent cce7ff6 commit 6f8e7fe

File tree

1 file changed

+113
-6
lines changed

1 file changed

+113
-6
lines changed

extras/test/src/test_command_encode.cpp

+113-6
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ SCENARIO("Test the encoding of command messages") {
3535
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
3636

3737
uint8_t expected_result[] = {
38-
0xda, 0x00, 0x01, 0x00, 0x00, 0x81, 0x58, 0x20, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00,
39-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38+
0xda, 0x00, 0x01, 0x00, 0x00, 0x81, 0x58, 0x20,
39+
0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00,
40+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4042
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
4143
};
4244

@@ -70,7 +72,8 @@ SCENARIO("Test the encoding of command messages") {
7072
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
7173

7274
uint8_t expected_result[] = {
73-
0xda, 0x00, 0x01, 0x03, 0x00, 0x81, 0x68, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x5f, 0x69, 0x64
75+
0xda, 0x00, 0x01, 0x03, 0x00, 0x81, 0x68, 0x74,
76+
0x68, 0x69, 0x6e, 0x67, 0x5f, 0x69, 0x64
7477
};
7578

7679
// Test the encoding is
@@ -130,7 +133,8 @@ SCENARIO("Test the encoding of command messages") {
130133
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
131134

132135
uint8_t expected_result[] = {
133-
0xda, 0x00, 0x01, 0x07, 0x00, 0x81, 0x65, 0x32, 0x2e, 0x30, 0x2e, 0x30
136+
0xda, 0x00, 0x01, 0x07, 0x00, 0x81, 0x65, 0x32,
137+
0x2e, 0x30, 0x2e, 0x30
134138
};
135139

136140
// Test the encoding is
@@ -167,8 +171,10 @@ SCENARIO("Test the encoding of command messages") {
167171
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
168172

169173
uint8_t expected_result[] = {
170-
0xda, 0x00, 0x01, 0x02, 0x00, 0x84, 0x50, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
171-
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xe1, 0x20, 0x18, 0x64
174+
0xda, 0x00, 0x01, 0x02, 0x00, 0x84, 0x50, 0x00,
175+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
176+
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xe1,
177+
0x20, 0x18, 0x64
172178
};
173179

174180
// Test the encoding is
@@ -212,4 +218,105 @@ SCENARIO("Test the encoding of command messages") {
212218
REQUIRE(memcmp(buffer, expected_result, sizeof(expected_result)) == 0);
213219
}
214220
}
221+
222+
/****************************************************************************/
223+
224+
WHEN("Encode the ThingUpdateCmdId message")
225+
{
226+
ThingUpdateCmd command;
227+
command.c.id = CommandId::ThingUpdateCmdId;
228+
229+
String thing_id = "e4494d55-872a-4fd2-9646-92f87949394c";
230+
strcpy(command.params.thing_id, thing_id.c_str());
231+
232+
uint8_t buffer[512];
233+
size_t bytes_encoded = sizeof(buffer);
234+
235+
CBORMessageEncoder encoder;
236+
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
237+
238+
THEN("The encoding is unsuccessful - ThingUpdateCmdId is not supported") {
239+
REQUIRE(err == Encoder::Status::Error);
240+
}
241+
}
242+
243+
/****************************************************************************/
244+
245+
WHEN("Encode the SetTimezoneCommand message")
246+
{
247+
TimezoneCommandDown command;
248+
command.c.id = CommandId::TimezoneCommandDownId;
249+
250+
command.params.offset = 1708963873;
251+
command.params.until = 2024579473;
252+
253+
uint8_t buffer[512];
254+
size_t bytes_encoded = sizeof(buffer);
255+
256+
CBORMessageEncoder encoder;
257+
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
258+
259+
THEN("The encoding is unsuccessful - SetTimezoneCommand is not supported") {
260+
REQUIRE(err == Encoder::Status::Error);
261+
}
262+
}
263+
264+
/****************************************************************************/
265+
266+
WHEN("Encode the LastValuesUpdateCmd message")
267+
{
268+
LastValuesUpdateCmd command;
269+
command.c.id = CommandId::LastValuesUpdateCmdId;
270+
271+
command.params.length = 13;
272+
uint8_t last_values[13] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
273+
0x08, 0x09, 0x10, 0x11, 0x12};
274+
command.params.last_values = last_values;
275+
276+
uint8_t buffer[512];
277+
size_t bytes_encoded = sizeof(buffer);
278+
279+
CBORMessageEncoder encoder;
280+
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
281+
282+
THEN("The encoding is unsuccessful - LastValuesUpdateCmd is not supported") {
283+
REQUIRE(err == Encoder::Status::Error);
284+
}
285+
}
286+
287+
/****************************************************************************/
288+
289+
WHEN("Encode the OtaUpdateCmdDown message")
290+
{
291+
OtaUpdateCmdDown command;
292+
command.c.id = CommandId::OtaUpdateCmdDownId;
293+
294+
uint8_t buffer[512];
295+
size_t bytes_encoded = sizeof(buffer);
296+
297+
CBORMessageEncoder encoder;
298+
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
299+
300+
THEN("The encoding is unsuccessful - OtaUpdateCmdDown is not supported") {
301+
REQUIRE(err == Encoder::Status::Error);
302+
}
303+
}
304+
305+
/****************************************************************************/
306+
307+
WHEN("Encode a message with unknown command Id")
308+
{
309+
OtaUpdateCmdDown command;
310+
command.c.id = CommandId::UnknownCmdId;
311+
312+
uint8_t buffer[512];
313+
size_t bytes_encoded = sizeof(buffer);
314+
315+
CBORMessageEncoder encoder;
316+
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
317+
318+
THEN("The encoding is unsuccessful - OtaUpdateCmdDown is not supported") {
319+
REQUIRE(err == Encoder::Status::Error);
320+
}
321+
}
215322
}

0 commit comments

Comments
 (0)