Skip to content

Commit

Permalink
Switched to pw_unit_test in src/lib/dnssd/minimal_mdns/tests/ (#33068)
Browse files Browse the repository at this point in the history
* Switched to pw_unit_test in src/lib/dnssd/minimal_mdns/tests/

* Update src/lib/dnssd/minimal_mdns/tests/TestMinimalMdnsAllocator.cpp

* Update src/lib/dnssd/minimal_mdns/tests/TestResponseSender.cpp

---------

Co-authored-by: Arkadiusz Bokowy <[email protected]>
Co-authored-by: Arkadiusz Bokowy <[email protected]>
  • Loading branch information
3 people authored and pull[bot] committed May 22, 2024
1 parent 593d69c commit 3922194
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 531 deletions.
5 changes: 1 addition & 4 deletions src/lib/dnssd/minimal_mdns/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")
import("//build_overrides/nlunit_test.gni")

import("${chip_root}/build/chip/chip_test_suite.gni")
import("${chip_root}/build/chip/fuzz_test.gni")

chip_test_suite_using_nltest("tests") {
chip_test_suite("tests") {
output_name = "libMinimalMdnstests"

sources = [ "CheckOnlyServer.h" ]
Expand All @@ -40,9 +39,7 @@ chip_test_suite_using_nltest("tests") {
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/dnssd",
"${chip_root}/src/lib/dnssd/minimal_mdns",
"${chip_root}/src/lib/support:testing_nlunit",
"${chip_root}/src/transport/raw/tests:helpers",
"${nlunit_test_root}:nlunit-test",
]
}

Expand Down
33 changes: 11 additions & 22 deletions src/lib/dnssd/minimal_mdns/tests/CheckOnlyServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@
#include <lib/dnssd/minimal_mdns/records/Srv.h>
#include <lib/dnssd/minimal_mdns/records/Txt.h>
#include <lib/support/CHIPMemString.h>
#include <lib/support/UnitTestRegistration.h>
#include <system/SystemMutex.h>

#include <nlunit-test.h>
#include <gtest/gtest.h>

namespace mdns {
namespace Minimal {
Expand Down Expand Up @@ -78,23 +77,19 @@ class CheckOnlyServer : private chip::PoolImpl<ServerBase::EndpointInfo, 0, chip
public TxtRecordDelegate
{
public:
CheckOnlyServer(nlTestSuite * inSuite) : ServerBase(*static_cast<ServerBase::EndpointInfoPoolType *>(this)), mInSuite(inSuite)
{
Reset();
}
CheckOnlyServer() : ServerBase(*static_cast<ServerBase::EndpointInfoPoolType *>(this)), mInSuite(nullptr) { Reset(); }
CheckOnlyServer() : ServerBase(*static_cast<ServerBase::EndpointInfoPoolType *>(this)) { Reset(); }
~CheckOnlyServer() {}

// Parser delegates
void OnHeader(ConstHeaderRef & header) override
{
NL_TEST_ASSERT(mInSuite, header.GetFlags().IsResponse());
NL_TEST_ASSERT(mInSuite, header.GetFlags().IsValidMdns());
EXPECT_TRUE(header.GetFlags().IsResponse());
EXPECT_TRUE(header.GetFlags().IsValidMdns());
mTotalRecords += header.GetAnswerCount() + header.GetAdditionalCount();

if (!header.GetFlags().IsTruncated())
{
NL_TEST_ASSERT(mInSuite, mTotalRecords == GetNumExpectedRecords());
EXPECT_EQ(mTotalRecords, GetNumExpectedRecords());
if (mTotalRecords != GetNumExpectedRecords())
{
ChipLogError(Discovery, "Received %d records, expected %d", mTotalRecords, GetNumExpectedRecords());
Expand All @@ -114,7 +109,7 @@ class CheckOnlyServer : private chip::PoolImpl<ServerBase::EndpointInfo, 0, chip
case QType::SRV: {
SrvRecord srv;
bool srvParseOk = srv.Parse(data.GetData(), mPacketData);
NL_TEST_ASSERT(mInSuite, srvParseOk);
EXPECT_TRUE(srvParseOk);
if (!srvParseOk)
{
return;
Expand Down Expand Up @@ -146,7 +141,7 @@ class CheckOnlyServer : private chip::PoolImpl<ServerBase::EndpointInfo, 0, chip
for (size_t t = 0; t < expectedTxt->GetNumEntries(); ++t)
{
bool ok = AddExpectedTxtRecord(expectedTxt->GetEntries()[t]);
NL_TEST_ASSERT(mInSuite, ok);
EXPECT_TRUE(ok);
}
ParseTxtRecord(data.GetData(), this);
if (CheckTxtRecordMatches())
Expand All @@ -164,7 +159,7 @@ class CheckOnlyServer : private chip::PoolImpl<ServerBase::EndpointInfo, 0, chip
}
}
}
NL_TEST_ASSERT(mInSuite, recordIsExpected);
EXPECT_TRUE(recordIsExpected);
if (!recordIsExpected)
{
char nameStr[64];
Expand Down Expand Up @@ -253,7 +248,7 @@ class CheckOnlyServer : private chip::PoolImpl<ServerBase::EndpointInfo, 0, chip
void AddExpectedRecord(SrvResourceRecord * srv)
{
RecordInfo * info = AddExpectedRecordBase(srv);
NL_TEST_ASSERT(mInSuite, info != nullptr);
ASSERT_NE(info, nullptr);
if (info == nullptr)
{
return;
Expand All @@ -263,7 +258,7 @@ class CheckOnlyServer : private chip::PoolImpl<ServerBase::EndpointInfo, 0, chip
void AddExpectedRecord(TxtResourceRecord * txt)
{
RecordInfo * info = AddExpectedRecordBase(txt);
NL_TEST_ASSERT(mInSuite, info != nullptr);
ASSERT_NE(info, nullptr);
if (info == nullptr)
{
return;
Expand All @@ -272,7 +267,6 @@ class CheckOnlyServer : private chip::PoolImpl<ServerBase::EndpointInfo, 0, chip
}
bool GetSendCalled() { return mSendCalled; }
bool GetHeaderFound() { return mHeaderFound; }
void SetTestSuite(nlTestSuite * suite) { mInSuite = suite; }
void Reset()
{
for (auto & info : mExpectedRecordInfo)
Expand All @@ -287,7 +281,6 @@ class CheckOnlyServer : private chip::PoolImpl<ServerBase::EndpointInfo, 0, chip
}

private:
nlTestSuite * mInSuite;
static constexpr size_t kMaxExpectedRecords = 10;
struct RecordInfo
{
Expand Down Expand Up @@ -335,17 +328,13 @@ class CheckOnlyServer : private chip::PoolImpl<ServerBase::EndpointInfo, 0, chip
}
void TestGotAllExpectedPackets()
{
if (mInSuite == nullptr)
{
return;
}
for (auto & info : mExpectedRecordInfo)
{
if (info.record == nullptr)
{
continue;
}
NL_TEST_ASSERT(mInSuite, info.found == true);
EXPECT_TRUE(info.found);
if (!info.found)
{
char name[64];
Expand Down
Loading

0 comments on commit 3922194

Please sign in to comment.