Skip to content

Commit 05c2048

Browse files
andreagilardonipennam
authored andcommitted
adding tests for encoder and decoder
1 parent 6fb4bee commit 05c2048

File tree

3 files changed

+479
-0
lines changed

3 files changed

+479
-0
lines changed

extras/test/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
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
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
/*
2+
Copyright (c) 2024 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+
WHEN("Decode the ThingUpdateCmdId message")
25+
{
26+
CommandDown command;
27+
/*
28+
DA 00010400 # tag(66560)
29+
81 # array(1)
30+
78 24 # text(36)
31+
65343439346435352D383732612D346664322D393634362D393266383739343933393463 # "e4494d55-872a-4fd2-9646-92f87949394c"
32+
*/
33+
uint8_t const payload[] = {0xDA, 0x00, 0x01, 0x04, 0x00, 0x81, 0x78, 0x24,
34+
0x65, 0x34, 0x34, 0x39, 0x34, 0x64, 0x35, 0x35,
35+
0x2D, 0x38, 0x37, 0x32, 0x61, 0x2D, 0x34, 0x66,
36+
0x64, 0x32, 0x2D, 0x39, 0x36, 0x34, 0x36, 0x2D,
37+
0x39, 0x32, 0x66, 0x38, 0x37, 0x39, 0x34, 0x39,
38+
0x33, 0x39, 0x34, 0x63};
39+
size_t payload_length = sizeof(payload) / sizeof(uint8_t);
40+
CBORMessageDecoder decoder;
41+
Decoder::Status err = decoder.decode((Message*)&command, payload, payload_length);
42+
const char *thingIdToMatch = "e4494d55-872a-4fd2-9646-92f87949394c";
43+
44+
THEN("The decode is successful") {
45+
REQUIRE(err == Decoder::Status::Complete);
46+
REQUIRE(strcmp(command.thingUpdateCmd.params.thing_id, thingIdToMatch) == 0);
47+
REQUIRE(command.c.id == ThingUpdateCmdId);
48+
}
49+
}
50+
51+
/****************************************************************************/
52+
53+
WHEN("Decode the SetTimezoneCommand message")
54+
{
55+
CommandDown command;
56+
57+
/*
58+
DA 00010764 # tag(67840)
59+
82 # array(2)
60+
1A 65DCB821 # unsigned(1708963873)
61+
1A 78ACA191 # unsigned(2024579473)
62+
*/
63+
64+
uint8_t const payload[] = {0xDA, 0x00, 0x01, 0x09, 0x00, 0x82, 0x1A, 0x65,
65+
0xDC, 0xB8, 0x21, 0x1A, 0x78, 0xAC, 0xA1, 0x91};
66+
67+
size_t payload_length = sizeof(payload) / sizeof(uint8_t);
68+
CBORMessageDecoder decoder;
69+
Decoder::Status err = decoder.decode((Message*)&command, payload, payload_length);
70+
71+
THEN("The decode is successful") {
72+
REQUIRE(err == Decoder::Status::Complete);
73+
REQUIRE(command.timezoneCommandDown.params.offset == (uint32_t)1708963873);
74+
REQUIRE(command.timezoneCommandDown.params.until == (uint32_t)2024579473);
75+
REQUIRE(command.c.id == TimezoneCommandDownId);
76+
}
77+
}
78+
79+
/****************************************************************************/
80+
81+
WHEN("Decode the LastValuesUpdateCmd message")
82+
{
83+
CommandDown command;
84+
85+
/*
86+
DA 00010600 # tag(67072)
87+
81 # array(1)
88+
4D # bytes(13)
89+
00010203040506070809101112 # "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\u0010\u0011\u0012"
90+
91+
*/
92+
93+
uint8_t const payload[] = {0xDA, 0x00, 0x01, 0x06, 0x00, 0x81, 0x4D, 0x00,
94+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
95+
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+
WHEN("Decode the OtaUpdateCmdDown message")
125+
{
126+
CommandDown command;
127+
128+
/*
129+
DA 00010100 # tag(65792)
130+
84 # array(4)
131+
50 # bytes(16)
132+
C73CB045F9C2434585AFFA36A307BFE7"\xC7<\xB0E\xF9\xC2CE\x85\xAF\xFA6\xA3\a\xBF\xE7"
133+
78 72 # text(141)
134+
68747470733A2F2F626F617264732D69
135+
6E742E6F6E69756472612E63632F7374
136+
6F726167652F6669726D776172652F76
137+
312F6466316561633963376264363334
138+
37336666666231313766393837333730
139+
33653465633935353933316532363766
140+
32363236326230393439626331366463
141+
3439 # "https://boards-int.oniudra.cc/storage/firmware/v1/df1eac9c7bd63473fffb117f9873703e4ec955931e267f26262b0949bc16dc49"
142+
58 20 # bytes(32)
143+
00000000000000000000000000000000
144+
00000000000000000000000000000000# "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000"
145+
58 20 # bytes(32)
146+
DF1EAC9C7BD63473FFFB117F9873703E
147+
4EC955931E267F26262B0949BC16DC49# "\xDF\u001E\xAC\x9C{\xD64s\xFF\xFB\u0011\u007F\x98sp>N\xC9U\x93\u001E&\u007F&&+\tI\xBC\u0016\xDCI"
148+
149+
*/
150+
uint8_t const payload[] = { 0xda, 0x00, 0x01, 0x01, 0x00, 0x84, 0x50, 0xc7,
151+
0x3c, 0xb0, 0x45, 0xf9, 0xc2, 0x43, 0x45, 0x85,
152+
0xaf, 0xfa, 0x36, 0xa3, 0x07, 0xbf, 0xe7, 0x78,
153+
0x72, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f,
154+
0x2f, 0x62, 0x6f, 0x61, 0x72, 0x64, 0x73, 0x2d,
155+
0x69, 0x6e, 0x74, 0x2e, 0x6f, 0x6e, 0x69, 0x75,
156+
0x64, 0x72, 0x61, 0x2e, 0x63, 0x63, 0x2f, 0x73,
157+
0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x2f, 0x66,
158+
0x69, 0x72, 0x6d, 0x77, 0x61, 0x72, 0x65, 0x2f,
159+
0x76, 0x31, 0x2f, 0x64, 0x66, 0x31, 0x65, 0x61,
160+
0x63, 0x39, 0x63, 0x37, 0x62, 0x64, 0x36, 0x33,
161+
0x34, 0x37, 0x33, 0x66, 0x66, 0x66, 0x62, 0x31,
162+
0x31, 0x37, 0x66, 0x39, 0x38, 0x37, 0x33, 0x37,
163+
0x30, 0x33, 0x65, 0x34, 0x65, 0x63, 0x39, 0x35,
164+
0x35, 0x39, 0x33, 0x31, 0x65, 0x32, 0x36, 0x37,
165+
0x66, 0x32, 0x36, 0x32, 0x36, 0x32, 0x62, 0x30,
166+
0x39, 0x34, 0x39, 0x62, 0x63, 0x31, 0x36, 0x64,
167+
0x63, 0x34, 0x39, 0x58, 0x20, 0x00, 0x00, 0x00,
168+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
169+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
170+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
171+
0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0x20, 0xdf,
172+
0x1e, 0xac, 0x9c, 0x7b, 0xd6, 0x34, 0x73, 0xff,
173+
0xfb, 0x11, 0x7f, 0x98, 0x73, 0x70, 0x3e, 0x4e,
174+
0xc9, 0x55, 0x93, 0x1e, 0x26, 0x7f, 0x26, 0x26,
175+
0x2b, 0x09, 0x49, 0xbc, 0x16, 0xdc, 0x49};
176+
size_t payload_length = sizeof(payload) / sizeof(uint8_t);
177+
CBORMessageDecoder decoder;
178+
Decoder::Status err = decoder.decode((Message*)&command, payload, payload_length);
179+
180+
uint8_t otaIdToMatch[ID_SIZE] = { 0xC7, 0x3C, 0xB0, 0x45, 0xF9, 0xC2, 0x43, 0x45,
181+
0x85, 0xAF, 0xFA, 0x36, 0xA3, 0x07, 0xBF, 0xE7};
182+
const char *urlToMatch = "https://boards-int.oniudra.cc/storage/firmware/v1/df1eac9c7bd63473fffb117f9873703e4ec955931e267f26262b0949bc16dc49";
183+
184+
THEN("The decode is successful") {
185+
REQUIRE(err == Decoder::Status::Complete);
186+
REQUIRE(memcmp(command.otaUpdateCmdDown.params.id, otaIdToMatch, ID_SIZE) == 0);
187+
REQUIRE(strcmp(command.otaUpdateCmdDown.params.url, urlToMatch) == 0);
188+
// Initial SHA256 check
189+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[0] == (uint8_t)0x00);
190+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[1] == (uint8_t)0x00);
191+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[2] == (uint8_t)0x00);
192+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[3] == (uint8_t)0x00);
193+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[4] == (uint8_t)0x00);
194+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[5] == (uint8_t)0x00);
195+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[6] == (uint8_t)0x00);
196+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[7] == (uint8_t)0x00);
197+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[8] == (uint8_t)0x00);
198+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[9] == (uint8_t)0x00);
199+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[10] == (uint8_t)0x00);
200+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[11] == (uint8_t)0x00);
201+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[12] == (uint8_t)0x00);
202+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[13] == (uint8_t)0x00);
203+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[14] == (uint8_t)0x00);
204+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[15] == (uint8_t)0x00);
205+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[16] == (uint8_t)0x00);
206+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[17] == (uint8_t)0x00);
207+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[18] == (uint8_t)0x00);
208+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[19] == (uint8_t)0x00);
209+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[20] == (uint8_t)0x00);
210+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[21] == (uint8_t)0x00);
211+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[22] == (uint8_t)0x00);
212+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[23] == (uint8_t)0x00);
213+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[24] == (uint8_t)0x00);
214+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[25] == (uint8_t)0x00);
215+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[26] == (uint8_t)0x00);
216+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[27] == (uint8_t)0x00);
217+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[28] == (uint8_t)0x00);
218+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[29] == (uint8_t)0x00);
219+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[30] == (uint8_t)0x00);
220+
REQUIRE(command.otaUpdateCmdDown.params.initialSha256[31] == (uint8_t)0x00);
221+
222+
// Final SHA256 check
223+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[0] == (uint8_t)0xdf);
224+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[1] == (uint8_t)0x1e);
225+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[2] == (uint8_t)0xac);
226+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[3] == (uint8_t)0x9c);
227+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[4] == (uint8_t)0x7b);
228+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[5] == (uint8_t)0xd6);
229+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[6] == (uint8_t)0x34);
230+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[7] == (uint8_t)0x73);
231+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[8] == (uint8_t)0xff);
232+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[9] == (uint8_t)0xfb);
233+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[10] == (uint8_t)0x11);
234+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[11] == (uint8_t)0x7f);
235+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[12] == (uint8_t)0x98);
236+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[13] == (uint8_t)0x73);
237+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[14] == (uint8_t)0x70);
238+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[15] == (uint8_t)0x3e);
239+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[16] == (uint8_t)0x4e);
240+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[17] == (uint8_t)0xc9);
241+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[18] == (uint8_t)0x55);
242+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[19] == (uint8_t)0x93);
243+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[20] == (uint8_t)0x1e);
244+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[21] == (uint8_t)0x26);
245+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[22] == (uint8_t)0x7f);
246+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[23] == (uint8_t)0x26);
247+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[24] == (uint8_t)0x26);
248+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[25] == (uint8_t)0x2b);
249+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[26] == (uint8_t)0x09);
250+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[27] == (uint8_t)0x49);
251+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[28] == (uint8_t)0xbc);
252+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[29] == (uint8_t)0x16);
253+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[30] == (uint8_t)0xdc);
254+
REQUIRE(command.otaUpdateCmdDown.params.finalSha256[31] == (uint8_t)0x49);
255+
256+
REQUIRE(command.c.id == OtaUpdateCmdDownId);
257+
}
258+
}
259+
}

0 commit comments

Comments
 (0)