From f60be7f934495da49d272d2df2ccf748d109acc7 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Wed, 11 May 2022 06:23:52 +0200 Subject: [PATCH] [chip-tool-darwin] Enable TestConfigVariables (#18242) * [chip-tool-darwin] Enable TestConfigVariables * Update codegen --- .../tests/partials/check_test_value.zapt | 1 + .../templates/tests/partials/test_value.zapt | 34 ++-- .../chip-tool-darwin/templates/tests/tests.js | 3 - .../zap-generated/test/Commands.h | 167 +++++++++++++++++- 4 files changed, 190 insertions(+), 15 deletions(-) diff --git a/examples/chip-tool-darwin/templates/tests/partials/check_test_value.zapt b/examples/chip-tool-darwin/templates/tests/partials/check_test_value.zapt index 012d1e82426bf6..dec8b888ff692c 100644 --- a/examples/chip-tool-darwin/templates/tests/partials/check_test_value.zapt +++ b/examples/chip-tool-darwin/templates/tests/partials/check_test_value.zapt @@ -40,6 +40,7 @@ {{~#if (chip_tests_variables_has expected)}}{{expected}} {{~else if (isOctetString type)}}[[NSData alloc] initWithBytes:"{{octetStringEscapedForCLiteral expected}}" length:{{expected.length}}] {{~else if (isCharString type)}}@"{{expected}}" + {{else if (chip_tests_config_has expected)}}m{{asUpperCamelCase expected}}.HasValue() ? m{{asUpperCamelCase expected}}.Value() : {{asTypedLiteral (chip_tests_config_get_default_value expected) (chip_tests_config_get_type expected)}} {{~else}}{{asTypedExpressionFromObjectiveC expected type}} {{~/if}})); {{/if_is_struct}} diff --git a/examples/chip-tool-darwin/templates/tests/partials/test_value.zapt b/examples/chip-tool-darwin/templates/tests/partials/test_value.zapt index 687a6ada19210c..846c75ac96a980 100644 --- a/examples/chip-tool-darwin/templates/tests/partials/test_value.zapt +++ b/examples/chip-tool-darwin/templates/tests/partials/test_value.zapt @@ -1,7 +1,4 @@ -{{#if (chip_tests_config_has definedValue)}} - {{! Just replace the value's name with the actual value in the rest of the processing~}} - {{>test_value target=target definedValue=(chip_tests_config_get_default_value definedValue) cluster=cluster depth=depth}} -{{else if isOptional}} +{{#if isOptional}} {{! Just go ahead and assign to the value, stripping the optionality bit off. }} {{>test_value target=target definedValue=definedValue cluster=cluster isOptional=false depth=(incrementDepth depth)}} {{else if isNullable}} @@ -31,13 +28,28 @@ {{/if_include_struct_item_value}} {{/zcl_struct_items_by_struct_name}} - {{else if (chip_tests_variables_has definedValue)}} - {{target}} = [{{definedValue}} copy]; - {{else if (isCharString type)}} - {{target}} = @"{{definedValue}}"; - {{else if (isOctetString type)}} - {{target}} = [[NSData alloc] initWithBytes:"{{octetStringEscapedForCLiteral definedValue}}" length:{{definedValue.length}}]; {{else}} - {{target}} = [NSNumber numberWith{{asObjectiveCNumberType definedValue type false}}:{{asTypedExpressionFromObjectiveC definedValue type}}]; + {{target}} = + {{#if (chip_tests_variables_has definedValue)}} + [{{definedValue}} copy]; + {{else if (chip_tests_config_has definedValue)}} + m{{asUpperCamelCase definedValue}}.HasValue() ? + {{#if (isCharString type)}} + m{{asUpperCamelCase definedValue}}.Value() : @"{{chip_tests_config_get_default_value definedValue}}"; + {{else if (isOctetString type)}} + {{!-- TODO Extract the length of the default value for ByteSpan--}} + {{else}} + [NSNumber numberWith{{asObjectiveCNumberType definedValue type false}}:m{{asUpperCamelCase definedValue}}.Value()] : + [NSNumber numberWith{{asObjectiveCNumberType definedValue type false}}:{{asTypedExpressionFromObjectiveC (chip_tests_config_get_default_value definedValue) type}}]; + {{/if}} + {{else}} + {{#if (isCharString type)}} + @"{{definedValue}}"; + {{else if (isOctetString type)}} + [[NSData alloc] initWithBytes:"{{octetStringEscapedForCLiteral definedValue}}" length:{{definedValue.length}}]; + {{else}} + [NSNumber numberWith{{asObjectiveCNumberType definedValue type false}}:{{asTypedExpressionFromObjectiveC definedValue type}}]; + {{/if}} + {{/if}} {{/if_is_struct}} {{/if}} diff --git a/examples/chip-tool-darwin/templates/tests/tests.js b/examples/chip-tool-darwin/templates/tests/tests.js index 3ad187356ffce2..35826ba3d3ff51 100644 --- a/examples/chip-tool-darwin/templates/tests/tests.js +++ b/examples/chip-tool-darwin/templates/tests/tests.js @@ -42,9 +42,6 @@ function getTests() { // TODO: TestDiscovery needs FindCommissionable tests.disable('TestDiscovery'); - // TODO: TestConfigVariables not supported properly in codegen yet. - tests.disable('TestConfigVariables'); - // TODO: TestGroupMessaging does not work on Darwin for now. tests.disable('TestGroupMessaging'); diff --git a/zzz_generated/chip-tool-darwin/zap-generated/test/Commands.h b/zzz_generated/chip-tool-darwin/zap-generated/test/Commands.h index 29bd28264b455e..68fa4e6597cc5e 100644 --- a/zzz_generated/chip-tool-darwin/zap-generated/test/Commands.h +++ b/zzz_generated/chip-tool-darwin/zap-generated/test/Commands.h @@ -198,6 +198,7 @@ class TestList : public Command { printf("TestDelayCommands\n"); printf("TestLogCommands\n"); printf("TestSaveAs\n"); + printf("TestConfigVariables\n"); printf("TestDescriptorCluster\n"); printf("TestBasicInformation\n"); printf("TestGeneralCommissioning\n"); @@ -37978,7 +37979,8 @@ class Test_TC_MF_1_3 : public TestCommandBridge { "\330\242\021\2707\334\224\355\315V\364\321Cw\031\020v\277\305\235\231\267\3350S\357\326" "\360,D4\362\275\322z\244\371\316\247\015s\216L" length:97]; - params.discriminator = [NSNumber numberWithUnsignedShort:3840U]; + params.discriminator = mDiscriminator.HasValue() ? [NSNumber numberWithUnsignedShort:mDiscriminator.Value()] + : [NSNumber numberWithUnsignedShort:3840U]; params.iterations = [NSNumber numberWithUnsignedInt:1000UL]; params.salt = [[NSData alloc] initWithBytes:"SPAKE2P Key Salt" length:16]; [cluster openCommissioningWindowWithParams:params @@ -88353,6 +88355,168 @@ class TestSaveAs : public TestCommandBridge { } }; +class TestConfigVariables : public TestCommandBridge { +public: + // NOLINTBEGIN(clang-analyzer-nullability.NullPassedToNonnull): Test constructor nullability not enforced + TestConfigVariables() + : TestCommandBridge("TestConfigVariables") + , mTestIndex(0) + { + AddArgument("nodeId", 0, UINT64_MAX, &mNodeId); + AddArgument("cluster", &mCluster); + AddArgument("endpoint", 0, UINT16_MAX, &mEndpoint); + AddArgument("arg1", 0, UINT8_MAX, &mArg1); + AddArgument("returnValueWithArg1", 0, UINT8_MAX, &mReturnValueWithArg1); + AddArgument("timeout", 0, UINT16_MAX, &mTimeout); + } + // NOLINTEND(clang-analyzer-nullability.NullPassedToNonnull) + + ~TestConfigVariables() {} + + /////////// TestCommand Interface ///////// + void NextTest() override + { + CHIP_ERROR err = CHIP_NO_ERROR; + + if (0 == mTestIndex) { + ChipLogProgress(chipTool, " **** Test Start: TestConfigVariables\n"); + } + + if (mTestCount == mTestIndex) { + ChipLogProgress(chipTool, " **** Test Complete: TestConfigVariables\n"); + SetCommandExitStatus(CHIP_NO_ERROR); + return; + } + + Wait(); + + // Ensure we increment mTestIndex before we start running the relevant + // command. That way if we lose the timeslice after we send the message + // but before our function call returns, we won't end up with an + // incorrect mTestIndex value observed when we get the response. + switch (mTestIndex++) { + case 0: + ChipLogProgress(chipTool, " ***** Test Step 0 : Wait for the commissioned device to be retrieved\n"); + err = TestWaitForTheCommissionedDeviceToBeRetrieved_0(); + break; + case 1: + ChipLogProgress(chipTool, " ***** Test Step 1 : Send Test Add Arguments Command\n"); + err = TestSendTestAddArgumentsCommand_1(); + break; + case 2: + ChipLogProgress(chipTool, " ***** Test Step 2 : Send Test Add Arguments Command\n"); + err = TestSendTestAddArgumentsCommand_2(); + break; + } + + if (CHIP_NO_ERROR != err) { + ChipLogError(chipTool, " ***** Test Failure: %s\n", chip::ErrorStr(err)); + SetCommandExitStatus(err); + } + } + + void OnStatusUpdate(const chip::app::StatusIB & status) override + { + switch (mTestIndex - 1) { + case 0: + VerifyOrReturn(CheckValue("status", chip::to_underlying(status.mStatus), 0)); + break; + case 1: + VerifyOrReturn(CheckValue("status", chip::to_underlying(status.mStatus), 0)); + break; + case 2: + VerifyOrReturn(CheckValue("status", chip::to_underlying(status.mStatus), 0)); + break; + } + + // Go on to the next test. + WaitForMs(0); + } + + chip::System::Clock::Timeout GetWaitDuration() const override + { + return chip::System::Clock::Seconds16(mTimeout.ValueOr(kTimeoutInSeconds)); + } + +private: + std::atomic_uint16_t mTestIndex; + const uint16_t mTestCount = 3; + + chip::Optional mNodeId; + chip::Optional mCluster; + chip::Optional mEndpoint; + chip::Optional mArg1; + chip::Optional mReturnValueWithArg1; + chip::Optional mTimeout; + + CHIP_ERROR TestWaitForTheCommissionedDeviceToBeRetrieved_0() + { + chip::app::Clusters::DelayCommands::Commands::WaitForCommissionee::Type value; + value.nodeId = mNodeId.HasValue() ? mNodeId.Value() : 305414945ULL; + WaitForCommissionee("alpha", value); + return CHIP_NO_ERROR; + } + NSNumber * _Nonnull TestAddArgumentDefaultValue; + + CHIP_ERROR TestSendTestAddArgumentsCommand_1() + { + CHIPDevice * device = GetDevice("alpha"); + CHIPTestTestCluster * cluster = [[CHIPTestTestCluster alloc] initWithDevice:device endpoint:1 queue:mCallbackQueue]; + VerifyOrReturnError(cluster != nil, CHIP_ERROR_INCORRECT_STATE); + + __auto_type * params = [[CHIPTestClusterClusterTestAddArgumentsParams alloc] init]; + params.arg1 = [NSNumber numberWithUnsignedChar:3]; + params.arg2 = [NSNumber numberWithUnsignedChar:17]; + [cluster testAddArgumentsWithParams:params + completionHandler:^( + CHIPTestClusterClusterTestAddArgumentsResponseParams * _Nullable values, NSError * _Nullable err) { + NSLog(@"Send Test Add Arguments Command Error: %@", err); + + VerifyOrReturn(CheckValue("status", err, 0)); + + { + id actualValue = values.returnValue; + VerifyOrReturn(CheckValue("returnValue", actualValue, 20)); + } + { + TestAddArgumentDefaultValue = values.returnValue; + } + + NextTest(); + }]; + + return CHIP_NO_ERROR; + } + + CHIP_ERROR TestSendTestAddArgumentsCommand_2() + { + CHIPDevice * device = GetDevice("alpha"); + CHIPTestTestCluster * cluster = [[CHIPTestTestCluster alloc] initWithDevice:device endpoint:1 queue:mCallbackQueue]; + VerifyOrReturnError(cluster != nil, CHIP_ERROR_INCORRECT_STATE); + + __auto_type * params = [[CHIPTestClusterClusterTestAddArgumentsParams alloc] init]; + params.arg1 = mArg1.HasValue() ? [NSNumber numberWithUnsignedChar:mArg1.Value()] : [NSNumber numberWithUnsignedChar:5]; + params.arg2 = [TestAddArgumentDefaultValue copy]; + [cluster testAddArgumentsWithParams:params + completionHandler:^( + CHIPTestClusterClusterTestAddArgumentsResponseParams * _Nullable values, NSError * _Nullable err) { + NSLog(@"Send Test Add Arguments Command Error: %@", err); + + VerifyOrReturn(CheckValue("status", err, 0)); + + { + id actualValue = values.returnValue; + VerifyOrReturn(CheckValue("returnValue", actualValue, + mReturnValueWithArg1.HasValue() ? mReturnValueWithArg1.Value() : 25)); + } + + NextTest(); + }]; + + return CHIP_NO_ERROR; + } +}; + class TestDescriptorCluster : public TestCommandBridge { public: // NOLINTBEGIN(clang-analyzer-nullability.NullPassedToNonnull): Test constructor nullability not enforced @@ -105152,6 +105316,7 @@ void registerCommandsTests(Commands & commands) make_unique(), make_unique(), make_unique(), + make_unique(), make_unique(), make_unique(), make_unique(),