Skip to content

Commit

Permalink
CheckGenerator should fail if generating decimal representation failed
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca committed Jun 29, 2023
1 parent 96731f2 commit d15f392
Showing 1 changed file with 39 additions and 32 deletions.
71 changes: 39 additions & 32 deletions src/setup_payload/tests/TestManualCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down Expand Up @@ -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();
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit d15f392

Please sign in to comment.