Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erjiaqing committed May 21, 2021
1 parent ad7b3d9 commit 60e1022
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
87 changes: 81 additions & 6 deletions src/app/tests/integration/chip_im_initiator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@
namespace {
// Max value for the number of message request sent.

constexpr size_t kMaxCommandMessageCount = 3;
constexpr size_t kMaxReadMessageCount = 3;
constexpr int32_t gMessageIntervalSeconds = 1;
constexpr chip::Transport::AdminId gAdminId = 0;
constexpr size_t kMaxCommandMessageCount = 3;
constexpr size_t kTotalFailureCommandMessageCount = 1;
constexpr size_t kMaxReadMessageCount = 3;
constexpr int32_t gMessageIntervalSeconds = 1;
constexpr chip::Transport::AdminId gAdminId = 0;

// The ReadClient object.
chip::app::ReadClient * gpReadClient = nullptr;
Expand All @@ -74,6 +75,16 @@ uint64_t gReadCount = 0;
// Count of the number of CommandResponses received.
uint64_t gReadRespCount = 0;

// If the last command successed.
enum class TestCommandResult : uint8_t
{
kUndefined,
kSuccess,
kFailure
};

TestCommandResult gLastCommandResult = TestCommandResult::kUndefined;

std::condition_variable gCond;

CHIP_ERROR SendCommandRequest(chip::app::CommandSender * commandSender)
Expand Down Expand Up @@ -126,6 +137,43 @@ CHIP_ERROR SendCommandRequest(chip::app::CommandSender * commandSender)
return err;
}

CHIP_ERROR SendBadCommandRequest(chip::app::CommandSender * commandSender)
{
CHIP_ERROR err = CHIP_NO_ERROR;

VerifyOrReturnError(commandSender != nullptr, CHIP_ERROR_INCORRECT_STATE);

gLastMessageTime = chip::System::Timer::GetCurrentEpoch();

printf("\nSend invoke command request message to Node: %" PRIu64 "\n", chip::kTestDeviceNodeId);

chip::app::CommandPathParams commandPathParams = { 0xDE, // Bad Endpoint
0xADBE, // Bad GroupId
0xEFCA, // Bad ClusterId
0xFE, // Bad CommandId
chip::app::CommandPathFlags::kEndpointIdValid };

err = commandSender->PrepareCommand(&commandPathParams);
SuccessOrExit(err);

err = commandSender->FinishCommand();
SuccessOrExit(err);

err = commandSender->SendCommandRequest(chip::kTestDeviceNodeId, gAdminId);
SuccessOrExit(err);

if (err == CHIP_NO_ERROR)
{
gCommandCount++;
}
else
{
printf("Send invoke command request failed, err: %s\n", chip::ErrorStr(err));
}
exit:
return err;
}

CHIP_ERROR SendReadRequest(void)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down Expand Up @@ -220,6 +268,9 @@ class MockInteractionModelApp : public chip::app::InteractionModelDelegate
printf("CommandResponseStatus with GeneralCode %d, ProtocolId %d, ProtocolCode %d, EndpointId %d, ClusterId %d, CommandId "
"%d, CommandIndex %d",
static_cast<uint16_t>(aGeneralCode), aProtocolId, aProtocolCode, aEndpointId, aClusterId, aCommandId, aCommandIndex);
gLastCommandResult = (aGeneralCode == chip::Protocols::SecureChannel::GeneralStatusCode::kSuccess && aProtocolCode == 0)
? TestCommandResult::kSuccess
: TestCommandResult::kFailure;
return CHIP_NO_ERROR;
}

Expand Down Expand Up @@ -273,6 +324,8 @@ void DispatchSingleClusterCommand(chip::ClusterId aClusterId, chip::CommandId aC
{
chip::TLV::Debug::Dump(aReader, TLVPrettyPrinter);
}

gLastCommandResult = TestCommandResult::kSuccess;
}

CHIP_ERROR WriteSingleClusterData(ClusterInfo & aClusterInfo, TLV::TLVReader & aReader)
Expand Down Expand Up @@ -353,13 +406,35 @@ int main(int argc, char * argv[])
if (err != CHIP_NO_ERROR)
{
printf("Send command request failed: %s\n", chip::ErrorStr(err));
goto exit;
ExitNow();
}

if (gCond.wait_for(lock, std::chrono::seconds(gMessageIntervalSeconds)) == std::cv_status::timeout)
{
printf("Invoke Command: No response received\n");
}

VerifyOrExit(gLastCommandResult == TestCommandResult::kSuccess, err = CHIP_ERROR_INCORRECT_STATE);
}

// Test with invalid endpoint / cluster / command combination.
{
chip::app::CommandSender * commandSender;
err = chip::app::InteractionModelEngine::GetInstance()->NewCommandSender(&commandSender);
SuccessOrExit(err);
err = SendBadCommandRequest(commandSender);
if (err != CHIP_NO_ERROR)
{
printf("Send command request failed: %s\n", chip::ErrorStr(err));
ExitNow();
}

if (gCond.wait_for(lock, std::chrono::seconds(gMessageIntervalSeconds)) == std::cv_status::timeout)
{
printf("Invoke Command: No response received\n");
}

VerifyOrExit(gLastCommandResult == TestCommandResult::kFailure, err = CHIP_ERROR_INCORRECT_STATE);
}

// Connection has been established. Now send the ReadRequests.
Expand All @@ -383,7 +458,7 @@ int main(int argc, char * argv[])
ShutdownChip();

exit:
if (err != CHIP_NO_ERROR || (gCommandRespCount != kMaxCommandMessageCount))
if (err != CHIP_NO_ERROR || (gCommandRespCount != kMaxCommandMessageCount + kTotalFailureCommandMessageCount))
{
printf("ChipCommandSender failed: %s\n", chip::ErrorStr(err));
exit(EXIT_FAILURE);
Expand Down
4 changes: 3 additions & 1 deletion src/app/tests/integration/chip_im_responder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ namespace app {

CHIP_ERROR CheckIfClusterCommandExists(chip::ClusterId aClusterId, chip::CommandId aCommandId, chip::EndpointId aEndPointId)
{
// Always return no error in test.
// The Mock cluster catalog -- only have one command on one cluster on one endpoint.
VerifyOrReturnError(aEndPointId == kTestEndpointId && aClusterId == kTestClusterId && aCommandId == kTestCommandId,
CHIP_ERROR_INVALID_PROFILE_ID);
return CHIP_NO_ERROR;
}

Expand Down

0 comments on commit 60e1022

Please sign in to comment.