Skip to content

[TEMP] [TEST] coverage #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 42 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
ce0ac7e
Add Property write policy to manually handle local values update
pennam Feb 20, 2024
91e78b0
Remove public functions to handle thing_id discovery protocol
pennam Feb 20, 2024
bd7f19c
Remove public functions to handle timezone changes
pennam Feb 20, 2024
2c62f7c
Add writeOnDemand testcase
pennam Apr 8, 2024
b0a694f
Add writeOnChange testcase
pennam Apr 9, 2024
6cca93d
ArduinoIoTCloudClass: Remove unused variable
pennam Apr 9, 2024
3931c02
Add missing debug print
pennam Oct 3, 2023
2a7aae7
Simplify Connect/Disconnect Attach/Detach logic
pennam Oct 3, 2023
41d9bff
Simplify Attach/Detach timeout logic
pennam Oct 4, 2023
3941f42
Add comments and DEBUG
pennam Oct 4, 2023
716557e
Reorder functions implementation
pennam Oct 5, 2023
7c09eda
Unify naming and logic of thing susbscribe retry
pennam Oct 5, 2023
2f257fe
Reorder timeout variables declaration and initialization
pennam Oct 6, 2023
3b39b9a
Unify naming and logic of last values request retry
pennam Oct 6, 2023
0b13f1d
Remove WaitDeviceConfig state
pennam Oct 6, 2023
5415f83
We can safely send another subscribe request without unsubscribe
pennam Oct 6, 2023
95ffac4
Merge disconnect conditions
pennam Oct 6, 2023
46d335d
Reorder timeout and retry defines
pennam Oct 9, 2023
cf258c4
Add specific define for max number of retry connecting to device topic
pennam Oct 9, 2023
63f76bc
Add Class to handle retries and state transitions
pennam Oct 9, 2023
34ac07c
TimedAttempt: add variable to store next attempt wait time
pennam Mar 14, 2024
5282db0
TimedAttempt: add getWaitTime()
pennam Mar 14, 2024
89250de
ArduinoIoTCloudTCP: use getWaitTime() for broker connection retry
pennam Mar 14, 2024
452df01
Move properties containers inside class implementation
pennam Apr 9, 2024
a5fd864
Add commands definitions
andreagilardoni Feb 5, 2024
4e6c336
Add MessageStream interface
pennam Mar 7, 2024
6ec2e9a
Add CloudProcess interface
pennam Mar 7, 2024
cfe6095
Add ArduinoCloudThing
pennam Apr 11, 2024
92e81cf
TimeService: make isTimeValid public
pennam Feb 28, 2024
edf064d
ArduinoIoTCloudTCP: Use isTimeValid() to check NTP time
pennam Feb 28, 2024
72806d4
ArduinoIoTCloudTCP: adapt state machine to use Thing process
pennam Apr 11, 2024
552d6fb
Add ArduinoCloudDevice
pennam Apr 11, 2024
9dad742
ArduinoIoTCloudTCP: adapt state machine to use Device process
pennam Apr 16, 2024
6665dc9
Cloud: initialize thing id to non null
pennam Apr 15, 2024
f53cbb3
Defining encoder/decoder interfaces
andreagilardoni Feb 5, 2024
a351cda
CBOR encoder and decoder implementation for Command protocol model
andreagilardoni Apr 17, 2024
aaad0fa
Add tests for encoder and decoder
andreagilardoni Apr 17, 2024
aafb61d
Extend Commands.h to include new Command protocol model
andreagilardoni Mar 19, 2024
673f470
ArduinoIoTCloudTCP: switch to messages
pennam Feb 29, 2024
4750902
ArduinoIoTCloudDevice: switch to messages
pennam Mar 20, 2024
cd99532
message decoder improve coverage
pennam May 8, 2024
011e912
test decode fix formatting
pennam May 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions extras/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ set(TEST_SRCS
src/test_CloudSchedule.cpp
src/test_decode.cpp
src/test_encode.cpp
src/test_command_decode.cpp
src/test_command_encode.cpp
src/test_publishEvery.cpp
src/test_publishOnChange.cpp
src/test_publishOnChangeRateLimit.cpp
src/test_readOnly.cpp
src/test_writeOnly.cpp
src/test_writeOnDemand.cpp
src/test_writeOnChange.cpp
)

set(TEST_UTIL_SRCS
Expand All @@ -53,6 +57,9 @@ set(TEST_DUT_SRCS
../../src/property/PropertyContainer.cpp
../../src/cbor/CBORDecoder.cpp
../../src/cbor/CBOREncoder.cpp
../../src/cbor/MessageDecoder.cpp
../../src/cbor/MessageEncoder.cpp
../../src/cbor/CBOR.cpp
../../src/cbor/lib/tinycbor/src/cborencoder.c
../../src/cbor/lib/tinycbor/src/cborencoder_close_container_checked.c
../../src/cbor/lib/tinycbor/src/cborerrorstrings.c
Expand Down
736 changes: 736 additions & 0 deletions extras/test/src/test_command_decode.cpp

Large diffs are not rendered by default.

322 changes: 322 additions & 0 deletions extras/test/src/test_command_encode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
/*
Copyright (c) 2024 Arduino. All rights reserved.
*/

/******************************************************************************
INCLUDE
******************************************************************************/

#include <catch.hpp>

#include <memory>

#include <util/CBORTestUtil.h>
#include <MessageEncoder.h>

/******************************************************************************
TEST CODE
******************************************************************************/

SCENARIO("Test the encoding of command messages") {
/****************************************************************************/

WHEN("Encode the OtaBeginUp message")
{
OtaBeginUp command;
uint8_t sha[SHA256_SIZE] = {0x01, 0x02, 0x03, 0x04};
memcpy(command.params.sha, sha, SHA256_SIZE);

command.c.id = CommandId::OtaBeginUpId;

uint8_t buffer[512];
size_t bytes_encoded = sizeof(buffer);

CBORMessageEncoder encoder;
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);

uint8_t expected_result[] = {
0xda, 0x00, 0x01, 0x00, 0x00, 0x81, 0x58, 0x20,
0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

// Test the encoding is
// DA 00010000 # tag(65536)
// 81 # array(1)
// 58 20 # bytes(32)
// 01020304
THEN("The encoding is successful") {
REQUIRE(err == Encoder::Status::Complete);
REQUIRE(bytes_encoded == sizeof(expected_result));
REQUIRE(memcmp(buffer, expected_result, sizeof(expected_result)) == 0);
}
}


/****************************************************************************/

WHEN("Encode the ThingBeginCmd message")
{
ThingBeginCmd command;
String thing_id = "thing_id";
strcpy(command.params.thing_id, thing_id.c_str());

command.c.id = CommandId::ThingBeginCmdId;

uint8_t buffer[512];
size_t bytes_encoded = sizeof(buffer);

CBORMessageEncoder encoder;
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);

uint8_t expected_result[] = {
0xda, 0x00, 0x01, 0x03, 0x00, 0x81, 0x68, 0x74,
0x68, 0x69, 0x6e, 0x67, 0x5f, 0x69, 0x64
};

// Test the encoding is
// DA 00010300 # tag(66304)
// 81 # array(1)
// 68 # text(8)
// 7468696E675F6964 # "thing_id"

THEN("The encoding is successful") {
REQUIRE(err == Encoder::Status::Complete);
REQUIRE(bytes_encoded == sizeof(expected_result));
REQUIRE(memcmp(buffer, expected_result, sizeof(expected_result)) == 0);
}
}

/****************************************************************************/

WHEN("Encode the LastValuesBeginCmd message")
{
LastValuesBeginCmd command;
command.c.id = CommandId::LastValuesBeginCmdId;

uint8_t buffer[512];
size_t bytes_encoded = sizeof(buffer);

CBORMessageEncoder encoder;
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);

uint8_t expected_result[] = {
0xda, 0x00, 0x01, 0x05, 0x00, 0x80
};

// Test the encoding is
// DA 00010500 # tag(66816)
// 80 # array(0)
THEN("The encoding is successful") {
REQUIRE(err == Encoder::Status::Complete);
REQUIRE(bytes_encoded == sizeof(expected_result));
REQUIRE(memcmp(buffer, expected_result, sizeof(expected_result)) == 0);
}
}

/**************************************************************************/

WHEN("Encode the DeviceBeginCmd message")
{
DeviceBeginCmd command;
String lib_version = "2.0.0";
strcpy(command.params.lib_version, lib_version.c_str());

command.c.id = CommandId::DeviceBeginCmdId;

uint8_t buffer[512];
size_t bytes_encoded = sizeof(buffer);

CBORMessageEncoder encoder;
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);

uint8_t expected_result[] = {
0xda, 0x00, 0x01, 0x07, 0x00, 0x81, 0x65, 0x32,
0x2e, 0x30, 0x2e, 0x30
};

// Test the encoding is
// DA 00010700 # tag(67328)
// 81 # array(1)
// 65 # text(5)
// 322E302E30 # "2.0.0"
THEN("The encoding is successful") {
REQUIRE(err == Encoder::Status::Complete);
REQUIRE(bytes_encoded == sizeof(expected_result));
REQUIRE(memcmp(buffer, expected_result, sizeof(expected_result)) == 0);
}
}

/****************************************************************************/

WHEN("Encode the OtaProgressCmdUp message")
{
OtaProgressCmdUp command;
command.params.time = 2;

uint8_t id[ID_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
memcpy(command.params.id, id, ID_SIZE);
command.params.state = 1;
command.params.state_data = -1;
command.params.time = 100;

command.c.id = CommandId::OtaProgressCmdUpId;

uint8_t buffer[512];
size_t bytes_encoded = sizeof(buffer);

CBORMessageEncoder encoder;
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);

uint8_t expected_result[] = {
0xda, 0x00, 0x01, 0x02, 0x00, 0x84, 0x50, 0x00,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xe1,
0x20, 0x18, 0x64
};

// Test the encoding is
// DA 00010200 # tag(66048)
// 84 # array(4)
// 50 # bytes(16)
// 000102030405060708090A0B0C0D0E0F # "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f"
// E1 # primitive(1)
// 20 # negative(0)
// 18 64 # unsigned(100)
THEN("The encoding is successful") {
REQUIRE(err == Encoder::Status::Complete);
REQUIRE(bytes_encoded == sizeof(expected_result));
REQUIRE(memcmp(buffer, expected_result, sizeof(expected_result)) == 0);
}
}

/****************************************************************************/

WHEN("Encode the TimezoneCommandUp message")
{
TimezoneCommandUp command;
command.c.id = CommandId::TimezoneCommandUpId;

uint8_t buffer[512];
size_t bytes_encoded = sizeof(buffer);

CBORMessageEncoder encoder;
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);

uint8_t expected_result[] = {
0xda, 0x00, 0x01, 0x08, 0x00, 0x80
};

// Test the encoding is
// DA 00010800 # tag(67584)
// 80 # array(0)
THEN("The encoding is successful") {
REQUIRE(err == Encoder::Status::Complete);
REQUIRE(bytes_encoded == sizeof(expected_result));
REQUIRE(memcmp(buffer, expected_result, sizeof(expected_result)) == 0);
}
}

/****************************************************************************/

WHEN("Encode the ThingUpdateCmdId message")
{
ThingUpdateCmd command;
command.c.id = CommandId::ThingUpdateCmdId;

String thing_id = "e4494d55-872a-4fd2-9646-92f87949394c";
strcpy(command.params.thing_id, thing_id.c_str());

uint8_t buffer[512];
size_t bytes_encoded = sizeof(buffer);

CBORMessageEncoder encoder;
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);

THEN("The encoding is unsuccessful - ThingUpdateCmdId is not supported") {
REQUIRE(err == Encoder::Status::Error);
}
}

/****************************************************************************/

WHEN("Encode the SetTimezoneCommand message")
{
TimezoneCommandDown command;
command.c.id = CommandId::TimezoneCommandDownId;

command.params.offset = 1708963873;
command.params.until = 2024579473;

uint8_t buffer[512];
size_t bytes_encoded = sizeof(buffer);

CBORMessageEncoder encoder;
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);

THEN("The encoding is unsuccessful - SetTimezoneCommand is not supported") {
REQUIRE(err == Encoder::Status::Error);
}
}

/****************************************************************************/

WHEN("Encode the LastValuesUpdateCmd message")
{
LastValuesUpdateCmd command;
command.c.id = CommandId::LastValuesUpdateCmdId;

command.params.length = 13;
uint8_t last_values[13] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x10, 0x11, 0x12};
command.params.last_values = last_values;

uint8_t buffer[512];
size_t bytes_encoded = sizeof(buffer);

CBORMessageEncoder encoder;
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);

THEN("The encoding is unsuccessful - LastValuesUpdateCmd is not supported") {
REQUIRE(err == Encoder::Status::Error);
}
}

/****************************************************************************/

WHEN("Encode the OtaUpdateCmdDown message")
{
OtaUpdateCmdDown command;
command.c.id = CommandId::OtaUpdateCmdDownId;

uint8_t buffer[512];
size_t bytes_encoded = sizeof(buffer);

CBORMessageEncoder encoder;
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);

THEN("The encoding is unsuccessful - OtaUpdateCmdDown is not supported") {
REQUIRE(err == Encoder::Status::Error);
}
}

/****************************************************************************/

WHEN("Encode a message with unknown command Id")
{
OtaUpdateCmdDown command;
command.c.id = CommandId::UnknownCmdId;

uint8_t buffer[512];
size_t bytes_encoded = sizeof(buffer);

CBORMessageEncoder encoder;
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);

THEN("The encoding is unsuccessful - UnknownCmdId is not supported") {
REQUIRE(err == Encoder::Status::Error);
}
}
}
33 changes: 33 additions & 0 deletions extras/test/src/test_writeOnChange.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright (c) 2019 Arduino. All rights reserved.
*/

/**************************************************************************************
INCLUDE
**************************************************************************************/

#include <catch.hpp>

#include <util/CBORTestUtil.h>

#include <CBORDecoder.h>
#include <PropertyContainer.h>

/**************************************************************************************
TEST CODE
**************************************************************************************/

SCENARIO("An Arduino cloud property is marked 'write on change'", "[ArduinoCloudThing::decode]")
{
PropertyContainer property_container;

CloudInt test = 0;
addPropertyToContainer(property_container, test, "test", Permission::ReadWrite).writeOnChange();

/* [{0: "test", 2: 7}] = 81 A2 00 64 74 65 73 74 02 07 */
uint8_t const payload[] = {0x81, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0x07};
int const payload_length = sizeof(payload) / sizeof(uint8_t);
CBORDecoder::decode(property_container, payload, payload_length);

REQUIRE(test == 7);
}
Loading
Loading