Skip to content

Commit 6190d72

Browse files
improving test coverage for cbor encode
1 parent 56afa6b commit 6190d72

File tree

1 file changed

+315
-0
lines changed

1 file changed

+315
-0
lines changed

extras/test/src/test_command_encode.cpp

+315
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,49 @@ SCENARIO("Test the encoding of command messages") {
5555
}
5656
}
5757

58+
WHEN("Encode the OtaBeginUp message, but the buffer is not big enough to accommodate the encode the array open")
59+
{
60+
OtaBeginUp command;
61+
uint8_t sha[SHA256_SIZE] = {0x01, 0x02, 0x03, 0x04};
62+
memcpy(command.params.sha, sha, SHA256_SIZE);
63+
64+
command.c.id = CommandId::OtaBeginUpId;
65+
66+
uint8_t buffer[5];
67+
size_t bytes_encoded = sizeof(buffer);
68+
69+
CBORMessageEncoder encoder;
70+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
71+
72+
// Test the encoding is
73+
// DA 00010000 # tag(65536)
74+
// 81 # array(1)
75+
// 58 20 # bytes(32)
76+
// 01020304
77+
REQUIRE(err == MessageEncoder::Status::Error);
78+
}
79+
80+
WHEN("Encode the OtaBeginUp message, but the buffer is not big enough to accommodate the sha")
81+
{
82+
OtaBeginUp command;
83+
uint8_t sha[SHA256_SIZE] = {0x01, 0x02, 0x03, 0x04};
84+
memcpy(command.params.sha, sha, SHA256_SIZE);
85+
86+
command.c.id = CommandId::OtaBeginUpId;
87+
88+
uint8_t buffer[6];
89+
size_t bytes_encoded = sizeof(buffer);
90+
91+
CBORMessageEncoder encoder;
92+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
93+
94+
// Test the encoding is
95+
// DA 00010000 # tag(65536)
96+
// 81 # array(1)
97+
// 58 20 # bytes(32)
98+
// 01020304
99+
REQUIRE(err == MessageEncoder::Status::Error);
100+
}
58101

59102
/****************************************************************************/
60103

@@ -89,6 +132,50 @@ SCENARIO("Test the encoding of command messages") {
89132
}
90133
}
91134

135+
WHEN("Encode the ThingBeginCmd message, but the buffer is not big enough to accommodate the array open")
136+
{
137+
ThingBeginCmd command;
138+
String thing_id = "thing_id";
139+
strcpy(command.params.thing_id, thing_id.c_str());
140+
141+
command.c.id = CommandId::ThingBeginCmdId;
142+
143+
uint8_t buffer[5];
144+
size_t bytes_encoded = sizeof(buffer);
145+
146+
CBORMessageEncoder encoder;
147+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
148+
149+
// Test the encoding is
150+
// DA 00010300 # tag(66304)
151+
// 81 # array(1)
152+
// 68 # text(8)
153+
// 7468696E675F6964 # "thing_id"
154+
REQUIRE(err == MessageEncoder::Status::Error);
155+
}
156+
157+
WHEN("Encode the ThingBeginCmd message, but the buffer is not big enough to accommodate the thing id")
158+
{
159+
ThingBeginCmd command;
160+
String thing_id = "thing_id";
161+
strcpy(command.params.thing_id, thing_id.c_str());
162+
163+
command.c.id = CommandId::ThingBeginCmdId;
164+
165+
uint8_t buffer[6];
166+
size_t bytes_encoded = sizeof(buffer);
167+
168+
CBORMessageEncoder encoder;
169+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
170+
171+
// Test the encoding is
172+
// DA 00010300 # tag(66304)
173+
// 81 # array(1)
174+
// 68 # text(8)
175+
// 7468696E675F6964 # "thing_id"
176+
REQUIRE(err == MessageEncoder::Status::Error);
177+
}
178+
92179
/****************************************************************************/
93180

94181
WHEN("Encode the LastValuesBeginCmd message")
@@ -115,6 +202,23 @@ SCENARIO("Test the encoding of command messages") {
115202
}
116203
}
117204

205+
WHEN("Encode the LastValuesBeginCmd message, but the buffer is not big enough to accommodate the array open")
206+
{
207+
LastValuesBeginCmd command;
208+
command.c.id = CommandId::LastValuesBeginCmdId;
209+
210+
uint8_t buffer[5];
211+
size_t bytes_encoded = sizeof(buffer);
212+
213+
CBORMessageEncoder encoder;
214+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
215+
216+
// Test the encoding is
217+
// DA 00010500 # tag(66816)
218+
// 80 # array(0)
219+
REQUIRE(err == MessageEncoder::Status::Error);
220+
}
221+
118222
/**************************************************************************/
119223

120224
WHEN("Encode the DeviceBeginCmd message")
@@ -147,6 +251,50 @@ SCENARIO("Test the encoding of command messages") {
147251
}
148252
}
149253

254+
WHEN("Encode the DeviceBeginCmd message, but the buffer is not big enough to accommodate the array open")
255+
{
256+
DeviceBeginCmd command;
257+
String lib_version = "2.0.0";
258+
strcpy(command.params.lib_version, lib_version.c_str());
259+
260+
command.c.id = CommandId::DeviceBeginCmdId;
261+
262+
uint8_t buffer[5];
263+
size_t bytes_encoded = sizeof(buffer);
264+
265+
CBORMessageEncoder encoder;
266+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
267+
268+
// Test the encoding is
269+
// DA 00010700 # tag(67328)
270+
// 81 # array(1)
271+
// 65 # text(5)
272+
// 322E302E30 # "2.0.0"
273+
REQUIRE(err == MessageEncoder::Status::Error);
274+
}
275+
276+
WHEN("Encode the DeviceBeginCmd message, but the buffer is not big enough to accommodate the lib version")
277+
{
278+
DeviceBeginCmd command;
279+
String lib_version = "2.0.0";
280+
strcpy(command.params.lib_version, lib_version.c_str());
281+
282+
command.c.id = CommandId::DeviceBeginCmdId;
283+
284+
uint8_t buffer[6];
285+
size_t bytes_encoded = sizeof(buffer);
286+
287+
CBORMessageEncoder encoder;
288+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
289+
290+
// Test the encoding is
291+
// DA 00010700 # tag(67328)
292+
// 81 # array(1)
293+
// 65 # text(5)
294+
// 322E302E30 # "2.0.0"
295+
REQUIRE(err == MessageEncoder::Status::Error);
296+
}
297+
150298
/****************************************************************************/
151299

152300
WHEN("Encode the OtaProgressCmdUp message")
@@ -189,6 +337,156 @@ SCENARIO("Test the encoding of command messages") {
189337
}
190338
}
191339

340+
WHEN("Encode the OtaProgressCmdUp message, but the buffer is not big enough to accommodate the array open")
341+
{
342+
OtaProgressCmdUp command;
343+
command.params.time = 2;
344+
345+
uint8_t id[ID_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
346+
memcpy(command.params.id, id, ID_SIZE);
347+
command.params.state = 1;
348+
command.params.state_data = -1;
349+
command.params.time = 100;
350+
351+
command.c.id = CommandId::OtaProgressCmdUpId;
352+
353+
uint8_t buffer[5];
354+
size_t bytes_encoded = sizeof(buffer);
355+
356+
CBORMessageEncoder encoder;
357+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
358+
359+
// Test the encoding is
360+
// DA 00010200 # tag(66048)
361+
// 84 # array(4)
362+
// 50 # bytes(16)
363+
// 000102030405060708090A0B0C0D0E0F # "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f"
364+
// E1 # primitive(1)
365+
// 20 # negative(0)
366+
// 18 64 # unsigned(100)
367+
REQUIRE(err == MessageEncoder::Status::Error);
368+
}
369+
370+
WHEN("Encode the OtaProgressCmdUp message, but the buffer is not big enough to accommodate the id")
371+
{
372+
OtaProgressCmdUp command;
373+
command.params.time = 2;
374+
375+
uint8_t id[ID_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
376+
memcpy(command.params.id, id, ID_SIZE);
377+
command.params.state = 1;
378+
command.params.state_data = -1;
379+
command.params.time = 100;
380+
381+
command.c.id = CommandId::OtaProgressCmdUpId;
382+
383+
uint8_t buffer[6];
384+
size_t bytes_encoded = sizeof(buffer);
385+
386+
CBORMessageEncoder encoder;
387+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
388+
389+
// Test the encoding is
390+
// DA 00010200 # tag(66048)
391+
// 84 # array(4)
392+
// 50 # bytes(16)
393+
// 000102030405060708090A0B0C0D0E0F # "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f"
394+
// E1 # primitive(1)
395+
// 20 # negative(0)
396+
// 18 64 # unsigned(100)
397+
REQUIRE(err == MessageEncoder::Status::Error);
398+
}
399+
400+
WHEN("Encode the OtaProgressCmdUp message, but the buffer is not big enough to accommodate the state")
401+
{
402+
OtaProgressCmdUp command;
403+
command.params.time = 2;
404+
405+
uint8_t id[ID_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
406+
memcpy(command.params.id, id, ID_SIZE);
407+
command.params.state = 1;
408+
command.params.state_data = -1;
409+
command.params.time = 100;
410+
411+
command.c.id = CommandId::OtaProgressCmdUpId;
412+
413+
uint8_t buffer[23];
414+
size_t bytes_encoded = sizeof(buffer);
415+
416+
CBORMessageEncoder encoder;
417+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
418+
419+
// Test the encoding is
420+
// DA 00010200 # tag(66048)
421+
// 84 # array(4)
422+
// 50 # bytes(16)
423+
// 000102030405060708090A0B0C0D0E0F # "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f"
424+
// E1 # primitive(1)
425+
// 20 # negative(0)
426+
// 18 64 # unsigned(100)
427+
REQUIRE(err == MessageEncoder::Status::Error);
428+
}
429+
430+
WHEN("Encode the OtaProgressCmdUp message, but the buffer is not big enough to accommodate the state_data")
431+
{
432+
OtaProgressCmdUp command;
433+
command.params.time = 2;
434+
435+
uint8_t id[ID_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
436+
memcpy(command.params.id, id, ID_SIZE);
437+
command.params.state = 1;
438+
command.params.state_data = -1;
439+
command.params.time = 100;
440+
441+
command.c.id = CommandId::OtaProgressCmdUpId;
442+
443+
uint8_t buffer[24];
444+
size_t bytes_encoded = sizeof(buffer);
445+
446+
CBORMessageEncoder encoder;
447+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
448+
449+
// Test the encoding is
450+
// DA 00010200 # tag(66048)
451+
// 84 # array(4)
452+
// 50 # bytes(16)
453+
// 000102030405060708090A0B0C0D0E0F # "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f"
454+
// E1 # primitive(1)
455+
// 20 # negative(0)
456+
// 18 64 # unsigned(100)
457+
REQUIRE(err == MessageEncoder::Status::Error);
458+
}
459+
460+
WHEN("Encode the OtaProgressCmdUp message, but the buffer is not big enough to accommodate the time")
461+
{
462+
OtaProgressCmdUp command;
463+
command.params.time = 2;
464+
465+
uint8_t id[ID_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
466+
memcpy(command.params.id, id, ID_SIZE);
467+
command.params.state = 1;
468+
command.params.state_data = -1;
469+
command.params.time = 100;
470+
471+
command.c.id = CommandId::OtaProgressCmdUpId;
472+
473+
uint8_t buffer[25];
474+
size_t bytes_encoded = sizeof(buffer);
475+
476+
CBORMessageEncoder encoder;
477+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
478+
479+
// Test the encoding is
480+
// DA 00010200 # tag(66048)
481+
// 84 # array(4)
482+
// 50 # bytes(16)
483+
// 000102030405060708090A0B0C0D0E0F # "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f"
484+
// E1 # primitive(1)
485+
// 20 # negative(0)
486+
// 18 64 # unsigned(100)
487+
REQUIRE(err == MessageEncoder::Status::Error);
488+
}
489+
192490
/****************************************************************************/
193491

194492
WHEN("Encode the TimezoneCommandUp message")
@@ -215,6 +513,23 @@ SCENARIO("Test the encoding of command messages") {
215513
}
216514
}
217515

516+
WHEN("Encode the TimezoneCommandUp message, but the buffer is not big enough to accommodate the array open")
517+
{
518+
TimezoneCommandUp command;
519+
command.c.id = CommandId::TimezoneCommandUpId;
520+
521+
uint8_t buffer[5];
522+
size_t bytes_encoded = sizeof(buffer);
523+
524+
CBORMessageEncoder encoder;
525+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
526+
527+
// Test the encoding is
528+
// DA 00010800 # tag(67584)
529+
// 80 # array(0)
530+
REQUIRE(err == MessageEncoder::Status::Error);
531+
}
532+
218533
/****************************************************************************/
219534

220535
WHEN("Encode the ThingUpdateCmdId message")

0 commit comments

Comments
 (0)