Skip to content

Commit e8f56f9

Browse files
defining testing suite for cbor encoders/decoders
1 parent 068424a commit e8f56f9

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

extras/test/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,26 @@ 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/tinycbor
40+
../../src/cbor/tinycbor/src/cborencoder.c
41+
../../src/cbor/tinycbor/src/cborencoder_close_container_checked.c
42+
../../src/cbor/tinycbor/src/cborerrorstrings.c
43+
../../src/cbor/tinycbor/src/cborparser.c
44+
../../src/cbor/tinycbor/src/cborparser_dup_string.c
45+
../../src/cbor/tinycbor/src/cborpretty.c
46+
../../src/cbor/tinycbor/src/cborpretty_stdio.c
47+
../../src/cbor/tinycbor/src/cbortojson.c
48+
../../src/cbor/tinycbor/src/cborvalidation.c
49+
../../src/cbor/tinycbor/src/open_memstream.c
3750
)
3851

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

0 commit comments

Comments
 (0)