From 05a20b15e16ff19246077aeee829e0a2984c854d Mon Sep 17 00:00:00 2001 From: chrisdecenzo <61757564+chrisdecenzo@users.noreply.github.com> Date: Tue, 17 May 2022 08:53:08 -0700 Subject: [PATCH] Add unit tests for UDCClientState (#18449) * 12543 * Add overflow test * Restyled by clang-format (#18451) Co-authored-by: Restyled.io * debug CI * fix boundary test case * fix lint * fix CI * fix CI * Restyled by clang-format (#18493) Co-authored-by: Restyled.io Co-authored-by: restyled-io[bot] <32688539+restyled-io[bot]@users.noreply.github.com> Co-authored-by: Restyled.io --- .../UDCClientState.h | 5 +- .../UserDirectedCommissioningServer.cpp | 1 - .../tests/TestUdcMessages.cpp | 91 +++++++++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/src/protocols/user_directed_commissioning/UDCClientState.h b/src/protocols/user_directed_commissioning/UDCClientState.h index 25a47f55e2a1eb..0c5736d89b479e 100644 --- a/src/protocols/user_directed_commissioning/UDCClientState.h +++ b/src/protocols/user_directed_commissioning/UDCClientState.h @@ -85,8 +85,9 @@ class UDCClientState size_t GetRotatingIdLength() const { return mRotatingIdLen; } void SetRotatingId(const uint8_t * rotatingId, size_t rotatingIdLen) { - memcpy(mRotatingId, rotatingId, rotatingIdLen); - mRotatingIdLen = rotatingIdLen; + size_t maxSize = ArraySize(mRotatingId); + mRotatingIdLen = (maxSize < rotatingIdLen) ? maxSize : rotatingIdLen; + memcpy(mRotatingId, rotatingId, mRotatingIdLen); } UDCClientProcessingState GetUDCClientProcessingState() const { return mUDCClientProcessingState; } diff --git a/src/protocols/user_directed_commissioning/UserDirectedCommissioningServer.cpp b/src/protocols/user_directed_commissioning/UserDirectedCommissioningServer.cpp index 88c3d0a4ec7234..ef18a52190d141 100644 --- a/src/protocols/user_directed_commissioning/UserDirectedCommissioningServer.cpp +++ b/src/protocols/user_directed_commissioning/UserDirectedCommissioningServer.cpp @@ -173,7 +173,6 @@ void UserDirectedCommissioningServer::OnCommissionableNodeFound(const Dnssd::Dis client->SetLongDiscriminator(nodeData.commissionData.longDiscriminator); client->SetVendorId(nodeData.commissionData.vendorId); client->SetProductId(nodeData.commissionData.productId); - client->SetDeviceName(nodeData.commissionData.deviceName); client->SetRotatingId(nodeData.commissionData.rotatingId, nodeData.commissionData.rotatingIdLen); // Call the registered mUserConfirmationProvider, if any. diff --git a/src/protocols/user_directed_commissioning/tests/TestUdcMessages.cpp b/src/protocols/user_directed_commissioning/tests/TestUdcMessages.cpp index 7c2fe6279e49e9..9cbe13582c68fa 100644 --- a/src/protocols/user_directed_commissioning/tests/TestUdcMessages.cpp +++ b/src/protocols/user_directed_commissioning/tests/TestUdcMessages.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -15,7 +16,16 @@ using namespace chip; using namespace chip::Protocols::UserDirectedCommissioning; +using namespace chip::Dnssd; +using namespace chip::Dnssd::Internal; +ByteSpan GetSpan(char * key) +{ + size_t len = strlen(key); + // Stop the string from being null terminated to ensure the code makes no assumptions. + key[len] = '1'; + return ByteSpan(Uint8::from_char(key), len); +} class DLL_EXPORT TestCallback : public UserConfirmationProvider, public InstanceNameResolver { public: @@ -267,6 +277,86 @@ void TestUDCClients(nlTestSuite * inSuite, void * inContext) NL_TEST_ASSERT(inSuite, (expirationTime - System::Clock::Milliseconds64(1)) < state->GetExpirationTime()); } +void TestUDCClientState(nlTestSuite * inSuite, void * inContext) +{ + UDCClients<3> mUdcClients; + const char * instanceName1 = "test1"; + Inet::IPAddress address; + Inet::IPAddress::FromString("127.0.0.1", address); + uint16_t port = 333; + uint16_t longDiscriminator = 1234; + uint16_t vendorId = 1111; + uint16_t productId = 2222; + const char * deviceName = "test name"; + + // Rotating ID is given as up to 50 hex bytes + char rotatingIdString[chip::Dnssd::kMaxRotatingIdLen * 2 + 1]; + uint8_t rotatingId[chip::Dnssd::kMaxRotatingIdLen]; + size_t rotatingIdLen; + strcpy(rotatingIdString, "92873498273948734534"); + GetRotatingDeviceId(GetSpan(rotatingIdString), rotatingId, &rotatingIdLen); + + // create a Rotating ID longer than kMaxRotatingIdLen + char rotatingIdLongString[chip::Dnssd::kMaxRotatingIdLen * 4 + 1]; + uint8_t rotatingIdLong[chip::Dnssd::kMaxRotatingIdLen * 2]; + size_t rotatingIdLongLen; + strcpy( + rotatingIdLongString, + "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); + + const ByteSpan & value = GetSpan(rotatingIdLongString); + rotatingIdLongLen = Encoding::HexToBytes(reinterpret_cast(value.data()), value.size(), rotatingIdLong, + chip::Dnssd::kMaxRotatingIdLen * 2); + + NL_TEST_ASSERT(inSuite, rotatingIdLongLen > chip::Dnssd::kMaxRotatingIdLen); + + // test base case + UDCClientState * state = mUdcClients.FindUDCClientState(instanceName1); + NL_TEST_ASSERT(inSuite, state == nullptr); + + // add a default state + NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == mUdcClients.CreateNewUDCClientState(instanceName1, &state)); + + // get the state + state = mUdcClients.FindUDCClientState(instanceName1); + NL_TEST_ASSERT(inSuite, nullptr != state); + NL_TEST_ASSERT(inSuite, strcmp(state->GetInstanceName(), instanceName1) == 0); + + state->SetPeerAddress(chip::Transport::PeerAddress::UDP(address, port)); + NL_TEST_ASSERT(inSuite, port == state->GetPeerAddress().GetPort()); + + state->SetDeviceName(deviceName); + NL_TEST_ASSERT(inSuite, strcmp(state->GetDeviceName(), deviceName) == 0); + + state->SetLongDiscriminator(longDiscriminator); + NL_TEST_ASSERT(inSuite, longDiscriminator == state->GetLongDiscriminator()); + + state->SetVendorId(vendorId); + NL_TEST_ASSERT(inSuite, vendorId == state->GetVendorId()); + + state->SetProductId(productId); + NL_TEST_ASSERT(inSuite, productId == state->GetProductId()); + + state->SetRotatingId(rotatingId, rotatingIdLen); + NL_TEST_ASSERT(inSuite, rotatingIdLen == state->GetRotatingIdLength()); + + const uint8_t * testRotatingId = state->GetRotatingId(); + for (size_t i = 0; i < rotatingIdLen; i++) + { + NL_TEST_ASSERT(inSuite, testRotatingId[i] == rotatingId[i]); + } + + state->SetRotatingId(rotatingIdLong, rotatingIdLongLen); + + NL_TEST_ASSERT(inSuite, chip::Dnssd::kMaxRotatingIdLen == state->GetRotatingIdLength()); + + const uint8_t * testRotatingIdLong = state->GetRotatingId(); + for (size_t i = 0; i < chip::Dnssd::kMaxRotatingIdLen; i++) + { + NL_TEST_ASSERT(inSuite, testRotatingIdLong[i] == rotatingIdLong[i]); + } +} + // Test Suite /** @@ -280,6 +370,7 @@ static const nlTest sTests[] = NL_TEST_DEF("TestUDCServerInstanceNameResolver", TestUDCServerInstanceNameResolver), NL_TEST_DEF("TestUserDirectedCommissioningClientMessage", TestUserDirectedCommissioningClientMessage), NL_TEST_DEF("TestUDCClients", TestUDCClients), + NL_TEST_DEF("TestUDCClientState", TestUDCClientState), NL_TEST_SENTINEL() };