|
| 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/MessageDecoder.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 | +class CustomMessageDecoder: public CBORMessageDecoderInterface { |
| 31 | +public: |
| 32 | + CustomMessageDecoder() |
| 33 | + : CBORMessageDecoderInterface(CBORTestMessageTag, CBORTestMessageId) {} |
| 34 | + |
| 35 | +protected: |
| 36 | + MessageDecoder::Status decode(CborValue* iter, Message *msg) override { |
| 37 | + CBORTestMessage* test = (CBORTestMessage*) msg; |
| 38 | + size_t dest_size = 20; |
| 39 | + |
| 40 | + if(!cbor_value_is_text_string(iter)) { |
| 41 | + return MessageDecoder::Status::Error; |
| 42 | + } |
| 43 | + |
| 44 | + // NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string |
| 45 | + if(_cbor_value_copy_string(iter, test->parameter, &dest_size, NULL) != CborNoError) { |
| 46 | + return MessageDecoder::Status::Error; |
| 47 | + } |
| 48 | + |
| 49 | + return MessageDecoder::Status::Complete; |
| 50 | + } |
| 51 | +} customMessageDecoder; |
| 52 | + |
| 53 | +SCENARIO( "A custom decoder is defined", "[cbor][decode]" ) { |
| 54 | + CBORMessageDecoder decoder; |
| 55 | + GIVEN( "A buffer containing a cbor encoded message" ) { |
| 56 | + CBORTestMessage expected_result { |
| 57 | + CBORTestMessageId, |
| 58 | + "abcdef", |
| 59 | + }; |
| 60 | + |
| 61 | + uint8_t buffer[] { |
| 62 | + 0xD9, 0x03, 0x21, 0x81, 0x66, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 63 | + }; |
| 64 | + size_t buffer_len = sizeof(buffer); |
| 65 | + |
| 66 | + CBORTestMessage cmd_res; |
| 67 | + |
| 68 | + MessageDecoder::Status res = decoder.decode((Message*)&cmd_res, buffer, buffer_len); |
| 69 | + |
| 70 | + THEN( "Message decode result is Complete" ) { |
| 71 | + REQUIRE(res == MessageDecoder::Status::Complete); |
| 72 | + } |
| 73 | + |
| 74 | + THEN( "the decode result matches the expectations" ) { |
| 75 | + REQUIRE(buffer_len == sizeof(buffer)); |
| 76 | + |
| 77 | + REQUIRE(expected_result.m.id == cmd_res.m.id); |
| 78 | + |
| 79 | + std::string parameter_expected(expected_result.parameter); |
| 80 | + std::string parameter_result(cmd_res.parameter); |
| 81 | + |
| 82 | + REQUIRE_THAT(parameter_result, Catch::Matchers::Equals(parameter_expected)); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments