Skip to content

Commit f5336c3

Browse files
adding tests for encoder and decoder
1 parent eaef553 commit f5336c3

File tree

3 files changed

+397
-0
lines changed

3 files changed

+397
-0
lines changed

extras/test/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ set(TEST_SRCS
3636
src/test_CloudSchedule.cpp
3737
src/test_decode.cpp
3838
src/test_encode.cpp
39+
src/test_command_decode.cpp
40+
src/test_command_encode.cpp
3941
src/test_publishEvery.cpp
4042
src/test_publishOnChange.cpp
4143
src/test_publishOnChangeRateLimit.cpp
@@ -55,6 +57,9 @@ set(TEST_DUT_SRCS
5557
../../src/property/PropertyContainer.cpp
5658
../../src/cbor/CBORDecoder.cpp
5759
../../src/cbor/CBOREncoder.cpp
60+
../../src/cbor/MessageDecoder.cpp
61+
../../src/cbor/MessageEncoder.cpp
62+
../../src/cbor/CBOR.cpp
5863
../../src/cbor/lib/tinycbor/src/cborencoder.c
5964
../../src/cbor/lib/tinycbor/src/cborencoder_close_container_checked.c
6065
../../src/cbor/lib/tinycbor/src/cborerrorstrings.c
+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
Copyright (c) 2019 Arduino. All rights reserved.
3+
*/
4+
5+
/**************************************************************************************
6+
INCLUDE
7+
**************************************************************************************/
8+
9+
#include <catch.hpp>
10+
#include <string.h>
11+
12+
#include <memory>
13+
14+
#include <util/CBORTestUtil.h>
15+
#include <MessageDecoder.h>
16+
17+
/**************************************************************************************
18+
TEST CODE
19+
**************************************************************************************/
20+
21+
SCENARIO("Test the decoding of command messages") {
22+
/************************************************************************************/
23+
24+
// FIXME
25+
// WHEN("Decode the ThingBeginCmd message")
26+
// {
27+
// CommandDown command;
28+
29+
// /*
30+
31+
// DA 00010300 # tag(66560)
32+
// 81 # array(1)
33+
// 78 24 # text(36)
34+
// 65343439346435352D383732612D346664322D393634362D393266383739343933393463 # "e4494d55-872a-4fd2-9646-92f87949394c"
35+
36+
// */
37+
38+
// uint8_t const payload[] = {0xDA, 0x00, 0x01, 0x03, 0x00, 0x81, 0x78, 0x24, 0x65, 0x34, 0x34, 0x39, 0x34, 0x64, 0x35, 0x35, 0x2D, 0x38, 0x37, 0x32, 0x61, 0x2D, 0x34, 0x66, 0x64, 0x32, 0x2D, 0x39, 0x36, 0x34, 0x36, 0x2D, 0x39, 0x32, 0x66, 0x38, 0x37, 0x39, 0x34, 0x39, 0x33, 0x39, 0x34, 0x63};
39+
40+
// size_t payload_length = sizeof(payload) / sizeof(uint8_t);
41+
42+
// CBORMessageDecoder decoder;
43+
// Decoder::Status err = decoder.decode((Message*)&command, payload, payload_length);
44+
45+
// const char *thingIdToMatch = "e4494d55-872a-4fd2-9646-92f87949394c";
46+
47+
// THEN("The decode is successful") {
48+
// REQUIRE(err == Decoder::Status::Complete);
49+
// REQUIRE(strcmp(command.thingBeginCmd.params.thing_id, thingIdToMatch) == 0);
50+
// REQUIRE(command.c.id == ThingBeginCmdId);
51+
// }
52+
// }
53+
54+
/************************************************************************************/
55+
56+
WHEN("Decode the SetTimezoneCommand message")
57+
{
58+
CommandDown command;
59+
60+
/*
61+
DA 00010764 # tag(67840)
62+
82 # array(2)
63+
1A 65DCB821 # unsigned(1708963873)
64+
1A 78ACA191 # unsigned(2024579473)
65+
*/
66+
67+
uint8_t const payload[] = {0xDA, 0x00, 0x01, 0x09, 0x00, 0x82, 0x1A, 0x65, 0xDC, 0xB8, 0x21, 0x1A, 0x78, 0xAC, 0xA1, 0x91};
68+
69+
size_t payload_length = sizeof(payload) / sizeof(uint8_t);
70+
CBORMessageDecoder decoder;
71+
Decoder::Status err = decoder.decode((Message*)&command, payload, payload_length);
72+
73+
THEN("The decode is successful") {
74+
REQUIRE(err == Decoder::Status::Complete);
75+
REQUIRE(command.timezoneCommandDown.params.offset == (uint32_t)1708963873);
76+
REQUIRE(command.timezoneCommandDown.params.until == (uint32_t)2024579473);
77+
REQUIRE(command.c.id == TimezoneCommandDownId);
78+
}
79+
}
80+
81+
/************************************************************************************/
82+
83+
WHEN("Decode the LastValuesUpdateCmd message")
84+
{
85+
CommandDown command;
86+
87+
/*
88+
DA 00010600 # tag(67072)
89+
81 # array(1)
90+
4D # bytes(13)
91+
00010203040506070809101112 # "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\u0010\u0011\u0012"
92+
93+
*/
94+
95+
uint8_t const payload[] = {0xDA, 0x00, 0x01, 0x06, 0x00, 0x81, 0x4D, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12};
96+
97+
size_t payload_length = sizeof(payload) / sizeof(uint8_t);
98+
CBORMessageDecoder decoder;
99+
Decoder::Status err = decoder.decode((Message*)&command, payload, payload_length);
100+
101+
THEN("The decode is successful") {
102+
REQUIRE(err == Decoder::Status::Complete);
103+
REQUIRE(command.lastValuesUpdateCmd.params.length == 13);
104+
REQUIRE(command.lastValuesUpdateCmd.params.last_values[0] == (uint8_t)0x00);
105+
REQUIRE(command.lastValuesUpdateCmd.params.last_values[1] == (uint8_t)0x01);
106+
REQUIRE(command.lastValuesUpdateCmd.params.last_values[2] == (uint8_t)0x02);
107+
REQUIRE(command.lastValuesUpdateCmd.params.last_values[3] == (uint8_t)0x03);
108+
REQUIRE(command.lastValuesUpdateCmd.params.last_values[4] == (uint8_t)0x04);
109+
REQUIRE(command.lastValuesUpdateCmd.params.last_values[5] == (uint8_t)0x05);
110+
REQUIRE(command.lastValuesUpdateCmd.params.last_values[6] == (uint8_t)0x06);
111+
REQUIRE(command.lastValuesUpdateCmd.params.last_values[7] == (uint8_t)0x07);
112+
REQUIRE(command.lastValuesUpdateCmd.params.last_values[8] == (uint8_t)0x08);
113+
REQUIRE(command.lastValuesUpdateCmd.params.last_values[9] == (uint8_t)0x09);
114+
REQUIRE(command.lastValuesUpdateCmd.params.last_values[10] == (uint8_t)0x10);
115+
REQUIRE(command.lastValuesUpdateCmd.params.last_values[11] == (uint8_t)0x11);
116+
REQUIRE(command.lastValuesUpdateCmd.params.last_values[12] == (uint8_t)0x12);
117+
REQUIRE(command.c.id == LastValuesUpdateCmdId);
118+
}
119+
free(command.lastValuesUpdateCmd.params.last_values);
120+
}
121+
122+
/************************************************************************************/
123+
124+
// FIXME
125+
// WHEN("Decode the OtaUpdateCmdDown message")
126+
// {
127+
// CommandDown command;
128+
129+
// /*
130+
// DA 00010100 # tag(65792)
131+
// 84 # array(4)
132+
// 50 # text(12)
133+
// 000102030405060708090A0B0C0D0E0F
134+
// 75 # text(21)
135+
// 383736313233383736313233383736313233313233 # "876123876123876123123"
136+
// 53 # bytes(19)
137+
// 33303034616162323735313164623332313264 # "3004aab27511db3212d"
138+
// 50 # bytes(16)
139+
// 6A6B6173646B6A686173646B6A687868 # "jkasdkjhasdkjhxh"
140+
141+
// */
142+
143+
// uint8_t const payload[] = {0xDA, 0x00, 0x01, 0x01, 0x00, 0x84, 0x6C, 0x6F, 0x74, 0x61, 0x2D, 0x69, 0x64, 0x2D, 0x31, 0x32, 0x33, 0x34, 0x35, 0x75, 0x38, 0x37, 0x36, 0x31, 0x32, 0x33, 0x38, 0x37, 0x36, 0x31, 0x32, 0x33, 0x38, 0x37, 0x36, 0x31, 0x32, 0x33, 0x31, 0x32, 0x33, 0x53, 0x33, 0x30, 0x30, 0x34, 0x61, 0x61, 0x62, 0x32, 0x37, 0x35, 0x31, 0x31, 0x64, 0x62, 0x33, 0x32, 0x31, 0x32, 0x64, 0x50, 0x6A, 0x6B, 0x61, 0x73, 0x64, 0x6B, 0x6A, 0x68, 0x61, 0x73, 0x64, 0x6B, 0x6A, 0x68, 0x78, 0x68};
144+
145+
// size_t payload_length = sizeof(payload) / sizeof(uint8_t);
146+
// CBORMessageDecoder decoder;
147+
// Decoder::Status err = decoder.decode((Message*)&command, payload, payload_length);
148+
149+
// uint8_t otaIdToMatch[ID_SIZE] = {};
150+
// const char *urlToMatch = "876123876123876123123";
151+
152+
// THEN("The decode is successful") {
153+
// REQUIRE(err == Decoder::Status::Complete);
154+
// REQUIRE(memcmp(command.otaUpdateCmdDown.params.id, otaIdToMatch, ID_SIZE) == 0);
155+
// REQUIRE(strcmp(command.otaUpdateCmdDown.params.url, urlToMatch) == 0);
156+
// // Initial SHA256 check
157+
// REQUIRE(command.otaUpdateCmdDown.params.initialSha256[0] == (uint8_t)0x33);
158+
// REQUIRE(command.otaUpdateCmdDown.params.initialSha256[1] == (uint8_t)0x30);
159+
// REQUIRE(command.otaUpdateCmdDown.params.initialSha256[2] == (uint8_t)0x30);
160+
// REQUIRE(command.otaUpdateCmdDown.params.initialSha256[3] == (uint8_t)0x34);
161+
// REQUIRE(command.otaUpdateCmdDown.params.initialSha256[4] == (uint8_t)0x61);
162+
// REQUIRE(command.otaUpdateCmdDown.params.initialSha256[5] == (uint8_t)0x61);
163+
// REQUIRE(command.otaUpdateCmdDown.params.initialSha256[6] == (uint8_t)0x62);
164+
// REQUIRE(command.otaUpdateCmdDown.params.initialSha256[7] == (uint8_t)0x32);
165+
166+
// // Final SHA256 check
167+
// REQUIRE(command.otaUpdateCmdDown.params.finalSha256[0] == (uint8_t)0x6A);
168+
// REQUIRE(command.otaUpdateCmdDown.params.finalSha256[1] == (uint8_t)0x6B);
169+
// REQUIRE(command.otaUpdateCmdDown.params.finalSha256[2] == (uint8_t)0x61);
170+
// REQUIRE(command.otaUpdateCmdDown.params.finalSha256[3] == (uint8_t)0x73);
171+
// REQUIRE(command.otaUpdateCmdDown.params.finalSha256[4] == (uint8_t)0x64);
172+
// REQUIRE(command.otaUpdateCmdDown.params.finalSha256[5] == (uint8_t)0x6B);
173+
174+
// REQUIRE(command.c.id == OtaUpdateCmdDownId);
175+
// }
176+
// }
177+
}

0 commit comments

Comments
 (0)