Skip to content

Commit

Permalink
Add test for sending a command to an unsupported endpoint (project-ch…
Browse files Browse the repository at this point in the history
…ip#8073)

* Add test for trying to send a command to an endpoint that does not exist.

The test script change is to allow it to detect crashes in the server
app as failures (due to the server app not being there to be killed).

* Regenerate generated files
  • Loading branch information
bzbarsky-apple authored and Nikita committed Sep 23, 2021
1 parent 5db1dcc commit 51f149e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
74 changes: 73 additions & 1 deletion examples/chip-tool/commands/tests/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -3818,6 +3818,9 @@ class TestCluster : public TestCommand
case 100:
err = TestSendClusterTestClusterCommandWriteAttribute_100();
break;
case 101:
err = TestSendClusterTestClusterCommandTest_101();
break;
}

if (CHIP_NO_ERROR != err)
Expand All @@ -3829,7 +3832,7 @@ class TestCluster : public TestCommand

private:
std::atomic_uint16_t mTestIndex;
const uint16_t mTestCount = 101;
const uint16_t mTestCount = 102;

//
// Tests methods
Expand Down Expand Up @@ -11301,6 +11304,75 @@ class TestCluster : public TestCommand

runner->NextTest();
}

// Test Send Test Command to unsupported endpoint
typedef void (*SuccessCallback_101)(void * context);
chip::Callback::Callback<SuccessCallback_101> * mOnSuccessCallback_101 = nullptr;
chip::Callback::Callback<DefaultFailureCallback> * mOnFailureCallback_101 = nullptr;
bool mIsFailureExpected_101 = 1;

CHIP_ERROR TestSendClusterTestClusterCommandTest_101()
{
ChipLogProgress(chipTool, "Test Cluster - Send Test Command to unsupported endpoint: Sending command...");

mOnFailureCallback_101 =
new chip::Callback::Callback<DefaultFailureCallback>(OnTestSendClusterTestClusterCommandTest_101_FailureResponse, this);
mOnSuccessCallback_101 =
new chip::Callback::Callback<SuccessCallback_101>(OnTestSendClusterTestClusterCommandTest_101_SuccessResponse, this);

chip::Controller::TestClusterCluster cluster;
cluster.Associate(mDevice, 200);

CHIP_ERROR err = CHIP_NO_ERROR;

err = cluster.Test(mOnSuccessCallback_101->Cancel(), mOnFailureCallback_101->Cancel());

if (CHIP_NO_ERROR != err)
{
delete mOnFailureCallback_101;
delete mOnSuccessCallback_101;
}

return err;
}

static void OnTestSendClusterTestClusterCommandTest_101_FailureResponse(void * context, uint8_t status)
{
ChipLogProgress(chipTool, "Test Cluster - Send Test Command to unsupported endpoint: Failure Response");

TestCluster * runner = reinterpret_cast<TestCluster *>(context);

delete runner->mOnFailureCallback_101;
delete runner->mOnSuccessCallback_101;

if (runner->mIsFailureExpected_101 == false)
{
ChipLogError(chipTool, "Error: The test was expecting a success callback. Got failure callback");
runner->SetCommandExitStatus(CHIP_ERROR_INTERNAL);
return;
}

runner->NextTest();
}

static void OnTestSendClusterTestClusterCommandTest_101_SuccessResponse(void * context)
{
ChipLogProgress(chipTool, "Test Cluster - Send Test Command to unsupported endpoint: Success Response");

TestCluster * runner = reinterpret_cast<TestCluster *>(context);

delete runner->mOnFailureCallback_101;
delete runner->mOnSuccessCallback_101;

if (runner->mIsFailureExpected_101 == true)
{
ChipLogError(chipTool, "Error: The test was expecting a failure callback. Got success callback");
runner->SetCommandExitStatus(CHIP_ERROR_INTERNAL);
return;
}

runner->NextTest();
}
};

class Test_TC_OO_1_1 : public TestCommand
Expand Down
2 changes: 1 addition & 1 deletion scripts/tests/test_suites.sh
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ for j in "${iter_array[@]}"; do
# Prevent cleanup trying to kill a process we already killed.
temp_background_pid=$background_pid
background_pid=0
kill -9 "$temp_background_pid" || true
kill -9 "$temp_background_pid"
echo " ===== Test complete: $i"
done
echo " ===== Iteration $j completed"
Expand Down
7 changes: 7 additions & 0 deletions src/app/tests/suites/TestCluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,10 @@ tests:
optional: true
arguments:
value: 0

- label: "Send Test Command to unsupported endpoint"
command: "test"
endpoint: 200
response:
# No cluster on that endpoint, so expect an error.
error: 1
17 changes: 17 additions & 0 deletions src/darwin/Framework/CHIPTests/CHIPClustersTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,23 @@ - (void)testSendClusterTestCluster_000100_WriteAttribute

[self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil];
}
- (void)testSendClusterTestCluster_000101_Test
{
XCTestExpectation * expectation = [self expectationWithDescription:@"Send Test Command to unsupported endpoint"];
CHIPDevice * device = GetPairedDevice(kDeviceId);
dispatch_queue_t queue = dispatch_get_main_queue();
CHIPTestCluster * cluster = [[CHIPTestCluster alloc] initWithDevice:device endpoint:200 queue:queue];
XCTAssertNotNil(cluster);

[cluster test:^(NSError * err, NSDictionary * values) {
NSLog(@"Send Test Command to unsupported endpoint Error: %@", err);

XCTAssertEqual(err.code, 1);
[expectation fulfill];
}];

[self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil];
}

- (void)testSendClusterTest_TC_OO_1_1_000000_ReadAttribute
{
Expand Down

0 comments on commit 51f149e

Please sign in to comment.