Skip to content

Commit 3ac8d56

Browse files
defining testing suite for cbor encoders/decoders
1 parent 26a2c83 commit 3ac8d56

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

extras/test/CMakeLists.txt

+15
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,28 @@ set(TEST_SRCS
2727
src/crc16/test_crc16.cpp
2828
src/sha256/test_sha256.cpp
2929
src/hex/test_hex.cpp
30+
src/cbor/test_cbor_encoder.cpp
31+
src/cbor/test_cbor_decoder.cpp
3032
)
3133

3234
set(TEST_DUT_SRCS
3335
../../src/crc/crc32.cpp
3436
../../src/crc/crc16.cpp
3537
../../src/sha256/sha2.c
3638
../../src/hex/chex.h
39+
../../src/cbor/CborDecoder.cpp
40+
../../src/cbor/CborEncoder.cpp
41+
../../src/cbor/tinycbor
42+
../../src/cbor/tinycbor/src/cborencoder.c
43+
../../src/cbor/tinycbor/src/cborencoder_close_container_checked.c
44+
../../src/cbor/tinycbor/src/cborerrorstrings.c
45+
../../src/cbor/tinycbor/src/cborparser.c
46+
../../src/cbor/tinycbor/src/cborparser_dup_string.c
47+
../../src/cbor/tinycbor/src/cborpretty.c
48+
../../src/cbor/tinycbor/src/cborpretty_stdio.c
49+
../../src/cbor/tinycbor/src/cbortojson.c
50+
../../src/cbor/tinycbor/src/cborvalidation.c
51+
../../src/cbor/tinycbor/src/open_memstream.c
3752
)
3853

3954
##########################################################################
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
#include <catch2/catch_test_macros.hpp>
11+
#include <catch2/matchers/catch_matchers.hpp>
12+
#include <catch2/matchers/catch_matchers_string.hpp>
13+
#include <cbor/CborDecoder.h>
14+
15+
#include <stdio.h>
16+
17+
enum : MessageId {
18+
CBORTestMessageId = 0x0123,
19+
};
20+
21+
enum : CBORTag {
22+
CBORTestMessageTag = 0x0321,
23+
};
24+
25+
struct CBORTestMessage {
26+
Message m;
27+
char parameter[20];
28+
};
29+
30+
//TODO should this be a singleton?
31+
class CustomMessageDecoder: public CBORMessageDecoderInterface { // FIXME better names
32+
public:
33+
CustomMessageDecoder()
34+
: CBORMessageDecoderInterface(CBORTestMessageTag, CBORTestMessageId) {}
35+
36+
protected:
37+
Decoder::Status decode(CborValue* iter, Message *msg) override {
38+
CBORTestMessage* test = (CBORTestMessage*) msg;
39+
size_t dest_size = 20;
40+
41+
if(!cbor_value_is_text_string(iter)) {
42+
return Decoder::Status::Error;
43+
}
44+
45+
// NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string
46+
if(_cbor_value_copy_string(iter, test->parameter, &dest_size, NULL) != CborNoError) {
47+
return Decoder::Status::Error;
48+
}
49+
50+
return Decoder::Status::Complete;
51+
}
52+
} customMessageDecoder;
53+
54+
SCENARIO( "A custom decoder is defined", "[cbor][decode]" ) {
55+
CBORMessageDecoder decoder;
56+
GIVEN( "A buffer containing a cbor encoded message" ) {
57+
CBORTestMessage expected_result {
58+
CBORTestMessageId,
59+
"abcdef",
60+
};
61+
62+
uint8_t buffer[] {
63+
0xD9, 0x03, 0x21, 0x81, 0x66, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
64+
};
65+
size_t buffer_len = sizeof(buffer);
66+
67+
CBORTestMessage cmd_res;
68+
69+
Decoder::Status res = decoder.decode((Message*)&cmd_res, buffer, buffer_len);
70+
71+
THEN( "Message decode result is Complete" ) {
72+
REQUIRE(res == Decoder::Status::Complete);
73+
}
74+
75+
THEN( "the decode result matches the expectations" ) {
76+
REQUIRE(buffer_len == sizeof(buffer));
77+
78+
REQUIRE(expected_result.m.id == cmd_res.m.id);
79+
80+
std::string parameter_expected(expected_result.parameter);
81+
std::string parameter_result(cmd_res.parameter);
82+
83+
REQUIRE_THAT(parameter_result, Catch::Matchers::Equals(parameter_expected));
84+
}
85+
}
86+
}
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
#include <catch2/catch_test_macros.hpp>
11+
#include <catch2/matchers/catch_matchers.hpp>
12+
#include <catch2/matchers/catch_matchers_vector.hpp>
13+
#include <cbor/CborEncoder.h>
14+
15+
enum : MessageId {
16+
CBORTestMessageId = 0x0123,
17+
};
18+
19+
enum : CBORTag {
20+
CBORTestMessageTag = 0x0321,
21+
};
22+
23+
24+
struct CBORTestMessage {
25+
Message m;
26+
char parameter[20];
27+
};
28+
29+
//TODO should this be a singleton?
30+
class CustomMessageEncoder: public CBORMessageEncoderInterface { // FIXME better names
31+
public:
32+
CustomMessageEncoder()
33+
: CBORMessageEncoderInterface(CBORTestMessageTag, CBORTestMessageId) {}
34+
35+
protected:
36+
Encoder::Status encode(CborEncoder* encoder, Message *msg) override {
37+
CBORTestMessage * testMessage = (CBORTestMessage *) msg;
38+
CborEncoder array_encoder;
39+
40+
if(cbor_encoder_create_array(encoder, &array_encoder, 1) != CborNoError) {
41+
return Encoder::Status::Error;
42+
}
43+
44+
if(cbor_encode_text_stringz(&array_encoder, testMessage->parameter) != CborNoError) {
45+
return Encoder::Status::Error;
46+
}
47+
48+
if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) {
49+
return Encoder::Status::Error;
50+
}
51+
return Encoder::Status::Complete;
52+
}
53+
} customMessageEncoder;
54+
55+
SCENARIO( "A custom encoder is defined", "[cbor][encode]" ) {
56+
CBORMessageEncoder encoder;
57+
uint8_t buffer[100]; // shared buffer for encoding
58+
const size_t buf_len = sizeof(buffer);
59+
60+
GIVEN( "A Message with an id that the global encoder is able to encode" ) {
61+
CBORTestMessage cmd {
62+
CBORTestMessageId,
63+
"abcdef",
64+
};
65+
size_t res_len=buf_len;
66+
Encoder::Status res = encoder.encode((Message*)&cmd, buffer, res_len);
67+
68+
THEN( "Message encode result is Complete" ) {
69+
REQUIRE(res == Encoder::Status::Complete);
70+
}
71+
72+
THEN( "the encode result matches the expectations" ) {
73+
std::vector<uint8_t> res(buffer, buffer+res_len);
74+
75+
REQUIRE_THAT(res, Catch::Matchers::Equals(std::vector<uint8_t>{
76+
0xD9, 0x03, 0x21, 0x81, 0x66, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
77+
}));
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)