From d15f3927624da787069a6f10a14f3799c4c3a12f Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Wed, 28 Jun 2023 14:59:50 -0700 Subject: [PATCH] CheckGenerator should fail if generating decimal representation failed --- src/setup_payload/tests/TestManualCode.cpp | 71 ++++++++++++---------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/src/setup_payload/tests/TestManualCode.cpp b/src/setup_payload/tests/TestManualCode.cpp index 43d51f4284df4d..cfe7c59c06e8b8 100644 --- a/src/setup_payload/tests/TestManualCode.cpp +++ b/src/setup_payload/tests/TestManualCode.cpp @@ -46,7 +46,14 @@ bool CheckGenerator(const PayloadContents & payload, std::string expectedResult, std::string result; ManualSetupPayloadGenerator generator(payload); generator.SetAllowInvalidPayload(allowInvalidPayload); - generator.payloadDecimalStringRepresentation(result); + + CHIP_ERROR err = generator.payloadDecimalStringRepresentation(result); + + if (err != CHIP_NO_ERROR) + { + printf("Failed to generate decimal representation with error: %s\n", ErrorStr(err)); + return false; + } if (!expectedResult.empty()) { @@ -103,18 +110,6 @@ void TestDecimalRepresentation_FullPayloadWithZeros(nlTestSuite * inSuite, void NL_TEST_ASSERT(inSuite, CheckGenerator(payload, expectedResult)); } -void TestDecimalRepresentation_FullPayloadWithoutZeros(nlTestSuite * inSuite, void * inContext) -{ - PayloadContents payload = GetDefaultPayload(); - payload.commissioningFlow = CommissioningFlow::kCustom; - payload.vendorID = 45367; - payload.productID = 14526; - - std::string expectedResult = "64129507534536714526"; - - NL_TEST_ASSERT(inSuite, CheckGenerator(payload, expectedResult)); -} - void TestDecimalRepresentation_FullPayloadWithoutZeros_DoesNotRequireCustomFlow(nlTestSuite * inSuite, void * inContext) { PayloadContents payload = GetDefaultPayload(); @@ -126,29 +121,16 @@ void TestDecimalRepresentation_FullPayloadWithoutZeros_DoesNotRequireCustomFlow( NL_TEST_ASSERT(inSuite, CheckGenerator(payload, expectedResult)); } -void TestDecimalRepresentation_AllZeros(nlTestSuite * inSuite, void * inContext) -{ - PayloadContents payload; - payload.setUpPINCode = 0; - payload.discriminator.SetLongValue(0); - - std::string expectedResult; - - NL_TEST_ASSERT(inSuite, CheckGenerator(payload, expectedResult)); -} - -void TestDecimalRepresentation_AllOnes(nlTestSuite * inSuite, void * inContext) +void TestDecimalRepresentation_FullPayloadWithoutZeros(nlTestSuite * inSuite, void * inContext) { - PayloadContents payload; - payload.setUpPINCode = 0x7FFFFFF; - payload.discriminator.SetLongValue(0xFFF); + PayloadContents payload = GetDefaultPayload(); payload.commissioningFlow = CommissioningFlow::kCustom; - payload.vendorID = 65535; - payload.productID = 65535; + payload.vendorID = 45367; + payload.productID = 14526; - std::string expectedResult = "76553581916553565535"; + std::string expectedResult = "64129507534536714526"; - NL_TEST_ASSERT(inSuite, CheckGenerator(payload, expectedResult, /*allowInvalidPayload*/ true)); + NL_TEST_ASSERT(inSuite, CheckGenerator(payload, expectedResult)); } void assertPayloadValues(nlTestSuite * inSuite, CHIP_ERROR actualError, CHIP_ERROR expectedError, const PayloadContents & payload, @@ -204,6 +186,31 @@ void TestGenerateAndParser_ManualSetupCodeWithLongDiscriminator(nlTestSuite * in } } +void TestDecimalRepresentation_AllZeros(nlTestSuite * inSuite, void * inContext) +{ + PayloadContents payload; + payload.setUpPINCode = 0; + payload.discriminator.SetLongValue(0); + + std::string expectedResult; + + NL_TEST_ASSERT(inSuite, CheckGenerator(payload, expectedResult) == false); +} + +void TestDecimalRepresentation_AllOnes(nlTestSuite * inSuite, void * inContext) +{ + PayloadContents payload; + payload.setUpPINCode = 0x7FFFFFF; + payload.discriminator.SetLongValue(0xFFF); + payload.commissioningFlow = CommissioningFlow::kCustom; + payload.vendorID = 65535; + payload.productID = 65535; + + std::string expectedResult = "76553581916553565535"; + + NL_TEST_ASSERT(inSuite, CheckGenerator(payload, expectedResult, /*allowInvalidPayload*/ true)); +} + char ComputeCheckChar(const std::string & str) { // Strip out dashes, if any, from the string before computing the checksum.