Skip to content

Commit 1e3b90d

Browse files
committed
Add command encoder tests
1 parent e802b62 commit 1e3b90d

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
Copyright (c) 2019 Arduino. All rights reserved.
3+
*/
4+
5+
/**************************************************************************************
6+
INCLUDE
7+
**************************************************************************************/
8+
9+
#include <catch.hpp>
10+
11+
#include <memory>
12+
13+
#include <util/CBORTestUtil.h>
14+
#include <MessageEncoder.h>
15+
16+
/**************************************************************************************
17+
TEST CODE
18+
**************************************************************************************/
19+
20+
SCENARIO("Test the encoding of command messages") {
21+
/************************************************************************************/
22+
23+
WHEN("Encode the ThingGetIdCmdUp message")
24+
{
25+
ThingGetIdCmdUp * command = new ThingGetIdCmdUp();
26+
String s = "thing_id";
27+
strcpy(command->fields.params.thing_id, s.c_str());
28+
29+
Message * message = new Message();
30+
message->id = CommandID::ThingGetIdCmdUpId;
31+
32+
uint8_t buffer[256];
33+
int bytes_encoded = 0;
34+
35+
CborError err = MessageEncoder::encode(message, buffer, sizeof(buffer), bytes_encoded);
36+
37+
// Test the encoding is
38+
// DA 00010300 # tag(66304)
39+
// 9F # array(*)
40+
// 68 # text(8)
41+
// 7468696E675F6964 # "thing_id"
42+
// FF # primitive(*)
43+
44+
THEN("The encoding is successful") {
45+
REQUIRE(err == CborNoError);
46+
REQUIRE(bytes_encoded == 11);
47+
REQUIRE(buffer[0] == 0xDA);
48+
REQUIRE(buffer[1] == 0x00);
49+
REQUIRE(buffer[2] == 0x01);
50+
REQUIRE(buffer[3] == 0x03);
51+
REQUIRE(buffer[4] == 0x00);
52+
REQUIRE(buffer[5] == 0x68);
53+
REQUIRE(buffer[6] == 0x74);
54+
REQUIRE(buffer[7] == 0x68);
55+
REQUIRE(buffer[8] == 0x69);
56+
REQUIRE(buffer[9] == 0x6E);
57+
REQUIRE(buffer[10] == 0x67);
58+
REQUIRE(buffer[11] == 0x5F);
59+
REQUIRE(buffer[12] == 0x69);
60+
REQUIRE(buffer[13] == 0x64);
61+
REQUIRE(buffer[14] == 0xFF);
62+
}
63+
delete message;
64+
}
65+
66+
/************************************************************************************/
67+
68+
WHEN("Encode the ThingGetLastValueCmdUpId message")
69+
{
70+
Message * message = new Message();
71+
message->id = CommandID::ThingGetLastValueCmdUpId;
72+
73+
uint8_t buffer[256];
74+
int bytes_encoded = 0;
75+
76+
CborError err = MessageEncoder::encode(message, buffer, sizeof(buffer), bytes_encoded);
77+
78+
// Test the encoding is
79+
// DA 00010500 # tag(66816)
80+
// 80 # array(0)
81+
THEN("The encoding is successful") {
82+
REQUIRE(err == CborNoError);
83+
REQUIRE(bytes_encoded == 4);
84+
REQUIRE(buffer[0] == 0xDA);
85+
REQUIRE(buffer[1] == 0x00);
86+
REQUIRE(buffer[2] == 0x01);
87+
REQUIRE(buffer[3] == 0x05);
88+
REQUIRE(buffer[4] == 0x80);
89+
}
90+
delete message;
91+
}
92+
93+
/************************************************************************************/
94+
95+
WHEN("Encode the DeviceBeginCmdUp message")
96+
{
97+
DeviceBeginCmdUp * command = new DeviceBeginCmdUp();
98+
uint8_t sha[SHA256_SIZE] = {0x01, 0x02, 0x03, 0x04};
99+
memcpy(command->fields.params.sha, sha, SHA256_SIZE);
100+
101+
Message * message = new Message();
102+
message->id = CommandID::DeviceBeginCmdUpId;
103+
104+
uint8_t buffer[256];
105+
int bytes_encoded = 0;
106+
107+
CborError err = MessageEncoder::encode(message, buffer, sizeof(buffer), bytes_encoded);
108+
109+
// Test the encoding is
110+
// DA 00010700 # tag(67328)
111+
// 81 # array(1)
112+
// 44 # bytes(4)
113+
// 01020304 # "\u0001\u0002\u0003\u0004"
114+
THEN("The encoding is successful") {
115+
REQUIRE(err == CborNoError);
116+
REQUIRE(bytes_encoded == 7);
117+
REQUIRE(buffer[0] == 0xDA);
118+
REQUIRE(buffer[1] == 0x00);
119+
REQUIRE(buffer[2] == 0x01);
120+
REQUIRE(buffer[3] == 0x07);
121+
REQUIRE(buffer[4] == 0x81);
122+
REQUIRE(buffer[5] == 0x44);
123+
REQUIRE(buffer[6] == 0x01);
124+
REQUIRE(buffer[7] == 0x02);
125+
REQUIRE(buffer[8] == 0x03);
126+
REQUIRE(buffer[9] == 0x04);
127+
}
128+
129+
delete message;
130+
}
131+
132+
/************************************************************************************/
133+
134+
WHEN("Encode the OtaProgressCmdUp message")
135+
{
136+
OtaProgressCmdUp * command = new OtaProgressCmdUp();
137+
command->fields.params.count = 1;
138+
command->fields.params.time = 2;
139+
String s = "id";
140+
strcpy(command->fields.params.id, s.c_str());
141+
s = "state";
142+
strcpy(command->fields.params.state, s.c_str());
143+
144+
Message * message = new Message();
145+
message->id = CommandID::OtaProgressCmdUpId;
146+
147+
uint8_t buffer[256];
148+
int bytes_encoded = 0;
149+
150+
CborError err = MessageEncoder::encode(message, buffer, sizeof(buffer), bytes_encoded);
151+
152+
// Test the encoding is
153+
// DA 00010200 # tag(66048)
154+
// 84 # array(4)
155+
// 01 # unsigned(1)
156+
// 02 # unsigned(2)
157+
// 62 # text(2)
158+
// 6964 # "id"
159+
// 65 # text(5)
160+
// 7374617465 # "state"
161+
THEN("The encoding is successful") {
162+
REQUIRE(err == CborNoError);
163+
REQUIRE(bytes_encoded == 13);
164+
REQUIRE(buffer[0] == 0xDA);
165+
REQUIRE(buffer[1] == 0x00);
166+
REQUIRE(buffer[2] == 0x01);
167+
REQUIRE(buffer[3] == 0x02);
168+
REQUIRE(buffer[4] == 0x00);
169+
REQUIRE(buffer[5] == 0x84);
170+
REQUIRE(buffer[6] == 0x01);
171+
REQUIRE(buffer[7] == 0x02);
172+
REQUIRE(buffer[8] == 0x62);
173+
REQUIRE(buffer[9] == 0x69);
174+
REQUIRE(buffer[10] == 0x64);
175+
REQUIRE(buffer[11] == 0x65);
176+
REQUIRE(buffer[12] == 0x73);
177+
REQUIRE(buffer[13] == 0x74);
178+
REQUIRE(buffer[14] == 0x61);
179+
REQUIRE(buffer[15] == 0x74);
180+
REQUIRE(buffer[16] == 0x65);
181+
}
182+
183+
delete message;
184+
}
185+
186+
}

0 commit comments

Comments
 (0)