Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[unittest] CheckGenerator should fail if generating decimal representation failed #27533

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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