Skip to content

Commit d530c8b

Browse files
adding tests for encoder and decoder
1 parent 19806dc commit d530c8b

File tree

3 files changed

+447
-0
lines changed

3 files changed

+447
-0
lines changed

extras/test/CMakeLists.txt

+4
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,8 @@ 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
5862
../../src/cbor/lib/tinycbor/src/cborencoder.c
5963
../../src/cbor/lib/tinycbor/src/cborencoder_close_container_checked.c
6064
../../src/cbor/lib/tinycbor/src/cborerrorstrings.c
+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
WHEN("Decode the ThingGetIdCmdDown message")
25+
{
26+
ThingGetIdCmdDown * msg = new ThingGetIdCmdDown();
27+
Message * genericMessage = (Message*) msg;
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, 0x04, 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+
int payload_length = sizeof(payload) / sizeof(uint8_t);
41+
42+
MessageDecoder::DecoderState err = MessageDecoder::decode(genericMessage, payload, payload_length);
43+
44+
const char *thingIdToMatch = "e4494d55-872a-4fd2-9646-92f87949394c";
45+
46+
THEN("The decode is successful") {
47+
REQUIRE(err == MessageDecoder::DecoderState::Success);
48+
REQUIRE(strcmp(msg->fields.params.thing_id, thingIdToMatch) == 0);
49+
REQUIRE(genericMessage->id == (uint32_t)66560);
50+
}
51+
52+
delete msg;
53+
}
54+
55+
/************************************************************************************/
56+
57+
WHEN("Decode the SetTimezoneCommand message")
58+
{
59+
TimezoneCommandDown * msg = new TimezoneCommandDown();
60+
Message * genericMessage = (Message*) msg;
61+
62+
/*
63+
64+
DA 00010764 # tag(67840)
65+
82 # array(2)
66+
1A 65DCB821 # unsigned(1708963873)
67+
1A 78ACA191 # unsigned(2024579473)
68+
69+
*/
70+
71+
uint8_t const payload[] = {0xDA, 0x00, 0x01, 0x09, 0x00, 0x82, 0x1A, 0x65, 0xDC, 0xB8, 0x21, 0x1A, 0x78, 0xAC, 0xA1, 0x91};
72+
73+
int payload_length = sizeof(payload) / sizeof(uint8_t);
74+
MessageDecoder::DecoderState err = MessageDecoder::decode(genericMessage, payload, payload_length);
75+
76+
THEN("The decode is successful") {
77+
REQUIRE(err == MessageDecoder::DecoderState::Success);
78+
REQUIRE(msg->fields.params.offset == (uint32_t)1708963873);
79+
REQUIRE(msg->fields.params.until == (uint32_t)2024579473);
80+
REQUIRE(genericMessage->id == (uint32_t)67840);
81+
}
82+
83+
delete msg;
84+
}
85+
86+
/************************************************************************************/
87+
88+
WHEN("Decode the ThingGetLastValueCmdDown message")
89+
{
90+
ThingGetLastValueCmdDown * msg = new ThingGetLastValueCmdDown();
91+
Message * genericMessage = (Message*) msg;
92+
93+
/*
94+
95+
DA 00010600 # tag(67072)
96+
81 # array(1)
97+
4D # bytes(13)
98+
00010203040506070809101112 # "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\u0010\u0011\u0012"
99+
100+
*/
101+
102+
uint8_t const payload[] = {0xDA, 0x00, 0x01, 0x06, 0x00, 0x81, 0x4D, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12};
103+
104+
int payload_length = sizeof(payload) / sizeof(uint8_t);
105+
MessageDecoder::DecoderState err = MessageDecoder::decode(genericMessage, payload, payload_length);
106+
107+
THEN("The decode is successful") {
108+
REQUIRE(err == MessageDecoder::DecoderState::Success);
109+
REQUIRE(msg->fields.params.length == 13);
110+
REQUIRE(msg->fields.params.last_values[0] == (uint8_t)0x00);
111+
REQUIRE(msg->fields.params.last_values[1] == (uint8_t)0x01);
112+
REQUIRE(msg->fields.params.last_values[2] == (uint8_t)0x02);
113+
REQUIRE(msg->fields.params.last_values[3] == (uint8_t)0x03);
114+
REQUIRE(msg->fields.params.last_values[4] == (uint8_t)0x04);
115+
REQUIRE(msg->fields.params.last_values[5] == (uint8_t)0x05);
116+
REQUIRE(msg->fields.params.last_values[6] == (uint8_t)0x06);
117+
REQUIRE(msg->fields.params.last_values[7] == (uint8_t)0x07);
118+
REQUIRE(msg->fields.params.last_values[8] == (uint8_t)0x08);
119+
REQUIRE(msg->fields.params.last_values[9] == (uint8_t)0x09);
120+
REQUIRE(msg->fields.params.last_values[10] == (uint8_t)0x10);
121+
REQUIRE(msg->fields.params.last_values[11] == (uint8_t)0x11);
122+
REQUIRE(msg->fields.params.last_values[12] == (uint8_t)0x12);
123+
REQUIRE(genericMessage->id == (uint32_t)67072);
124+
}
125+
126+
free(msg->fields.params.last_values);
127+
delete msg;
128+
}
129+
130+
/************************************************************************************/
131+
132+
WHEN("Decode the OtaUpdateCmdDown message")
133+
{
134+
OtaUpdateCmdDown * msg = new OtaUpdateCmdDown();
135+
Message * genericMessage = (Message*) msg;
136+
137+
/*
138+
DA 00010100 # tag(65792)
139+
84 # array(4)
140+
6C # text(12)
141+
6F74612D69642D3132333435 # "ota-id-12345"
142+
75 # text(21)
143+
383736313233383736313233383736313233313233 # "876123876123876123123"
144+
53 # bytes(19)
145+
33303034616162323735313164623332313264 # "3004aab27511db3212d"
146+
50 # bytes(16)
147+
6A6B6173646B6A686173646B6A687868 # "jkasdkjhasdkjhxh"
148+
149+
*/
150+
151+
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};
152+
153+
int payload_length = sizeof(payload) / sizeof(uint8_t);
154+
MessageDecoder::DecoderState err = MessageDecoder::decode(genericMessage, payload, payload_length);
155+
156+
const char *otaIdToMatch = "ota-id-12345";
157+
const char *urlToMatch = "876123876123876123123";
158+
159+
THEN("The decode is successful") {
160+
REQUIRE(err == MessageDecoder::DecoderState::Success);
161+
REQUIRE(strcmp(msg->fields.params.id, otaIdToMatch) == 0);
162+
REQUIRE(strcmp(msg->fields.params.url, urlToMatch) == 0);
163+
// Initial SHA256 check
164+
REQUIRE(msg->fields.params.initialSha256[0] == (uint8_t)0x33);
165+
REQUIRE(msg->fields.params.initialSha256[1] == (uint8_t)0x30);
166+
REQUIRE(msg->fields.params.initialSha256[2] == (uint8_t)0x30);
167+
REQUIRE(msg->fields.params.initialSha256[3] == (uint8_t)0x34);
168+
REQUIRE(msg->fields.params.initialSha256[4] == (uint8_t)0x61);
169+
REQUIRE(msg->fields.params.initialSha256[5] == (uint8_t)0x61);
170+
REQUIRE(msg->fields.params.initialSha256[6] == (uint8_t)0x62);
171+
REQUIRE(msg->fields.params.initialSha256[7] == (uint8_t)0x32);
172+
173+
// Final SHA256 check
174+
REQUIRE(msg->fields.params.finalSha256[0] == (uint8_t)0x6A);
175+
REQUIRE(msg->fields.params.finalSha256[1] == (uint8_t)0x6B);
176+
REQUIRE(msg->fields.params.finalSha256[2] == (uint8_t)0x61);
177+
REQUIRE(msg->fields.params.finalSha256[3] == (uint8_t)0x73);
178+
REQUIRE(msg->fields.params.finalSha256[4] == (uint8_t)0x64);
179+
REQUIRE(msg->fields.params.finalSha256[5] == (uint8_t)0x6B);
180+
181+
REQUIRE(genericMessage->id == (uint32_t)65792);
182+
}
183+
184+
delete msg;
185+
}
186+
187+
}

0 commit comments

Comments
 (0)