|
| 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/standards/StandardEncoders.h> |
| 14 | + |
| 15 | +/****************************************************************************** |
| 16 | + TEST CODE |
| 17 | + ******************************************************************************/ |
| 18 | + |
| 19 | +SCENARIO("Test the encoding of command messages") { |
| 20 | + |
| 21 | + WHEN("Encode a message with provisioning wifi fw version ") |
| 22 | + { |
| 23 | + VersionMessage command; |
| 24 | + command.c.id = StandardMessageId::WiFiFWVersionMessageId; |
| 25 | + command.params.version = "1.6.0"; |
| 26 | + uint8_t buffer[512]; |
| 27 | + size_t bytes_encoded = sizeof(buffer); |
| 28 | + |
| 29 | + CBORMessageEncoder encoder; |
| 30 | + MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded); |
| 31 | + |
| 32 | + uint8_t expected_result[] = { |
| 33 | + 0xda, 0x00, 0x01, 0x20, 0x14, 0x81, 0x65, 0x31, 0x2E, 0x36, 0x2E, 0x30 |
| 34 | + }; |
| 35 | + |
| 36 | + // Test the encoding is |
| 37 | + //DA 00012014 # tag(73748) |
| 38 | + // 81 # array(1) |
| 39 | + // 65 # text(5) |
| 40 | + // 312E362E30 # "1.6.0" |
| 41 | + THEN("The encoding is successful") { |
| 42 | + REQUIRE(err == MessageEncoder::Status::Complete); |
| 43 | + REQUIRE(bytes_encoded == sizeof(expected_result)); |
| 44 | + REQUIRE(memcmp(buffer, expected_result, sizeof(expected_result)) == 0); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + WHEN("Error encoding a message with provisioning wifi fw version not enough space for array") |
| 49 | + { |
| 50 | + VersionMessage command; |
| 51 | + command.c.id = StandardMessageId::WiFiFWVersionMessageId; |
| 52 | + command.params.version = "1.6.0"; |
| 53 | + uint8_t buffer[5]; // Not enough space |
| 54 | + size_t bytes_encoded = sizeof(buffer); |
| 55 | + |
| 56 | + CBORMessageEncoder encoder; |
| 57 | + MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded); |
| 58 | + |
| 59 | + // Test the encoding fails due to insufficient space |
| 60 | + THEN("The encoding fails with an error") { |
| 61 | + REQUIRE(err == MessageEncoder::Status::Error); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + WHEN("Error encoding a message with provisioning wifi fw version not enough space for version string") |
| 66 | + { |
| 67 | + VersionMessage command; |
| 68 | + command.c.id = StandardMessageId::WiFiFWVersionMessageId; |
| 69 | + command.params.version = "1.6.0"; |
| 70 | + uint8_t buffer[7]; // Not enough space |
| 71 | + size_t bytes_encoded = sizeof(buffer); |
| 72 | + |
| 73 | + CBORMessageEncoder encoder; |
| 74 | + MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded); |
| 75 | + |
| 76 | + // Test the encoding fails due to insufficient space |
| 77 | + THEN("The encoding fails with an error") { |
| 78 | + REQUIRE(err == MessageEncoder::Status::Error); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments