From 39221940e4683d91962811bbd85096de97815799 Mon Sep 17 00:00:00 2001 From: Maksymilian Knust Date: Tue, 23 Apr 2024 18:29:15 +0200 Subject: [PATCH] Switched to pw_unit_test in src/lib/dnssd/minimal_mdns/tests/ (#33068) * 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 Co-authored-by: Arkadiusz Bokowy --- src/lib/dnssd/minimal_mdns/tests/BUILD.gn | 5 +- .../minimal_mdns/tests/CheckOnlyServer.h | 33 +- .../minimal_mdns/tests/TestAdvertiser.cpp | 288 +++++++++--------- .../tests/TestMinimalMdnsAllocator.cpp | 209 ++++++------- .../tests/TestQueryReplyFilter.cpp | 80 ++--- .../minimal_mdns/tests/TestRecordData.cpp | 149 ++++----- .../minimal_mdns/tests/TestResponseSender.cpp | 169 +++++----- 7 files changed, 402 insertions(+), 531 deletions(-) diff --git a/src/lib/dnssd/minimal_mdns/tests/BUILD.gn b/src/lib/dnssd/minimal_mdns/tests/BUILD.gn index 8d99854046e307..dc09e4b4b59d5d 100644 --- a/src/lib/dnssd/minimal_mdns/tests/BUILD.gn +++ b/src/lib/dnssd/minimal_mdns/tests/BUILD.gn @@ -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" ] @@ -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", ] } diff --git a/src/lib/dnssd/minimal_mdns/tests/CheckOnlyServer.h b/src/lib/dnssd/minimal_mdns/tests/CheckOnlyServer.h index c5b8f9e21f060b..1aabc6738d00e6 100644 --- a/src/lib/dnssd/minimal_mdns/tests/CheckOnlyServer.h +++ b/src/lib/dnssd/minimal_mdns/tests/CheckOnlyServer.h @@ -29,10 +29,9 @@ #include #include #include -#include #include -#include +#include namespace mdns { namespace Minimal { @@ -78,23 +77,19 @@ class CheckOnlyServer : private chip::PoolImpl(this)), mInSuite(inSuite) - { - Reset(); - } - CheckOnlyServer() : ServerBase(*static_cast(this)), mInSuite(nullptr) { Reset(); } + CheckOnlyServer() : ServerBase(*static_cast(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()); @@ -114,7 +109,7 @@ class CheckOnlyServer : private chip::PoolImplGetNumEntries(); ++t) { bool ok = AddExpectedTxtRecord(expectedTxt->GetEntries()[t]); - NL_TEST_ASSERT(mInSuite, ok); + EXPECT_TRUE(ok); } ParseTxtRecord(data.GetData(), this); if (CheckTxtRecordMatches()) @@ -164,7 +159,7 @@ class CheckOnlyServer : private chip::PoolImpl #include #include -#include -#include + #include #include -#include +#include namespace { @@ -237,29 +236,57 @@ CHIP_ERROR SendQuery(FullQName qname) return CHIP_NO_ERROR; } -void OperationalAdverts(nlTestSuite * inSuite, void * inContext) +class TestAdvertiser : public ::testing::Test { - auto & mdnsAdvertiser = chip::Dnssd::ServiceAdvertiser::Instance(); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.RemoveServices() == CHIP_NO_ERROR); +public: + static chip::Test::IOContext context; + static CheckOnlyServer server; + static chip::Dnssd::ServiceAdvertiser * mdnsAdvertiser; + + static void SetUpTestSuite() + { + chip::Platform::MemoryInit(); + context.Init(); + chip::Dnssd::GlobalMinimalMdnsServer::Instance().Server().Shutdown(); + chip::Dnssd::GlobalMinimalMdnsServer::Instance().SetReplacementServer(&server); + mdnsAdvertiser = &chip::Dnssd::ServiceAdvertiser::Instance(); + mdnsAdvertiser->Init(context.GetUDPEndPointManager()); + } + static void TearDownTestSuite() + { + server.Shutdown(); + context.Shutdown(); + mdnsAdvertiser->RemoveServices(); + mdnsAdvertiser->Shutdown(); + chip::Dnssd::GlobalMinimalMdnsServer::Instance().SetReplacementServer(nullptr); + chip::Platform::MemoryShutdown(); + } +}; + +chip::Test::IOContext TestAdvertiser::context; +CheckOnlyServer TestAdvertiser::server; +chip::Dnssd::ServiceAdvertiser * TestAdvertiser::mdnsAdvertiser; + +TEST_F(TestAdvertiser, OperationalAdverts) +{ + EXPECT_EQ(mdnsAdvertiser->RemoveServices(), CHIP_NO_ERROR); - auto & server = static_cast(GlobalMinimalMdnsServer::Server()); - server.SetTestSuite(inSuite); server.Reset(); // Start a single operational advertiser ChipLogProgress(Discovery, "Testing single operational advertiser"); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(operationalParams1) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.FinalizeServiceUpdate() == CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->Advertise(operationalParams1), CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->FinalizeServiceUpdate(), CHIP_NO_ERROR); // Test for PTR response to _services request. ChipLogProgress(Discovery, "Checking response to _services._dns-sd._udp.local"); server.AddExpectedRecord(&ptrOperationalService); server.AddExpectedRecord(&ptrServiceSubCompressedId1); - NL_TEST_ASSERT(inSuite, SendQuery(kDnsSdQueryName) == CHIP_NO_ERROR); + EXPECT_EQ(SendQuery(kDnsSdQueryName), CHIP_NO_ERROR); // These check that the requested records added are sent out correctly. - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); // Want PTR response to _matter.tcp. We will also get the SRV and TXT as additionals. // We won't get any A/AAAA because this is a test and we don't have addresses. @@ -271,31 +298,31 @@ void OperationalAdverts(nlTestSuite * inSuite, void * inContext) server.AddExpectedRecord(&srvOperational1); server.AddExpectedRecord(&txtOperational1); - NL_TEST_ASSERT(inSuite, SendQuery(kMatterOperationalQueryName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kMatterOperationalQueryName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); ChipLogProgress(Discovery, "Testing response to instance name"); server.Reset(); // Just the SRV and TXT should return server.AddExpectedRecord(&srvOperational1); server.AddExpectedRecord(&txtOperational1); - NL_TEST_ASSERT(inSuite, SendQuery(kInstanceName1) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kInstanceName1), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); // If we try to re-advertise with the same operational parameters, we should not get duplicates - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(operationalParams1) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.FinalizeServiceUpdate() == CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->Advertise(operationalParams1), CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->FinalizeServiceUpdate(), CHIP_NO_ERROR); ChipLogProgress(Discovery, "Testing single operational advertiser with Advertise called twice"); // We should get a single PTR back for _services ChipLogProgress(Discovery, "Checking response to _services._dns-sd._udp.local"); server.Reset(); server.AddExpectedRecord(&ptrOperationalService); server.AddExpectedRecord(&ptrServiceSubCompressedId1); - NL_TEST_ASSERT(inSuite, SendQuery(kDnsSdQueryName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kDnsSdQueryName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); // Same records should come back for _matter._tcp.local queries. ChipLogProgress(Discovery, "Testing response to _matter._tcp.local"); @@ -303,16 +330,16 @@ void OperationalAdverts(nlTestSuite * inSuite, void * inContext) server.AddExpectedRecord(&ptrOperational1); server.AddExpectedRecord(&srvOperational1); server.AddExpectedRecord(&txtOperational1); - NL_TEST_ASSERT(inSuite, SendQuery(kMatterOperationalQueryName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kMatterOperationalQueryName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); // Adding a second operational advertiser. ChipLogProgress(Discovery, "Adding a second operational Advertiser"); server.Reset(); // Mac is the same, peer id is different - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(operationalParams2) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.FinalizeServiceUpdate() == CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->Advertise(operationalParams2), CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->FinalizeServiceUpdate(), CHIP_NO_ERROR); // For now, we'll get back two copies of the PTR. Not sure if that's totally correct, but for now, that's expected. ChipLogProgress(Discovery, "Checking response to _services._dns-sd._udp.local"); @@ -320,9 +347,9 @@ void OperationalAdverts(nlTestSuite * inSuite, void * inContext) server.AddExpectedRecord(&ptrOperationalService); server.AddExpectedRecord(&ptrServiceSubCompressedId1); server.AddExpectedRecord(&ptrServiceSubCompressedId2); - NL_TEST_ASSERT(inSuite, SendQuery(kDnsSdQueryName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kDnsSdQueryName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); // Requests for _matter._tcp.local will respond with all records from both parameter sets ChipLogProgress(Discovery, "Testing response to _matter._tcp.local"); @@ -333,9 +360,9 @@ void OperationalAdverts(nlTestSuite * inSuite, void * inContext) server.AddExpectedRecord(&ptrOperational2); server.AddExpectedRecord(&srvOperational2); server.AddExpectedRecord(&txtOperational2); - NL_TEST_ASSERT(inSuite, SendQuery(kMatterOperationalQueryName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kMatterOperationalQueryName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); // Requests for each SRV record should give only records specific to that fabric. ChipLogProgress(Discovery, "Testing response to instance name for fabric 1"); @@ -343,79 +370,75 @@ void OperationalAdverts(nlTestSuite * inSuite, void * inContext) // Just the SRV and TXT should return server.AddExpectedRecord(&srvOperational1); server.AddExpectedRecord(&txtOperational1); - NL_TEST_ASSERT(inSuite, SendQuery(kInstanceName1) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kInstanceName1), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); ChipLogProgress(Discovery, "Testing response to instance name for fabric 2"); server.Reset(); // Just the SRV and TXT should return server.AddExpectedRecord(&srvOperational2); server.AddExpectedRecord(&txtOperational2); - NL_TEST_ASSERT(inSuite, SendQuery(kInstanceName2) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kInstanceName2), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); // All devices should support at least 5 operational network additions (spec min) // however larger devices may support more. - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(operationalParams3) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(operationalParams4) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(operationalParams5) == CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->Advertise(operationalParams3), CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->Advertise(operationalParams4), CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->Advertise(operationalParams5), CHIP_NO_ERROR); } -void CommissionableAdverts(nlTestSuite * inSuite, void * inContext) +TEST_F(TestAdvertiser, CommissionableAdverts) { - auto & mdnsAdvertiser = chip::Dnssd::ServiceAdvertiser::Instance(); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.RemoveServices() == CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->RemoveServices(), CHIP_NO_ERROR); - auto & server = static_cast(GlobalMinimalMdnsServer::Server()); - server.SetTestSuite(inSuite); server.Reset(); // Start a single operational advertiser ChipLogProgress(Discovery, "Testing commissionable advertiser"); // Start very basic - only the mandatory values (short and long discriminator and commissioning modes) - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(commissionableNodeParamsSmall) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.FinalizeServiceUpdate() == CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->Advertise(commissionableNodeParamsSmall), CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->FinalizeServiceUpdate(), CHIP_NO_ERROR); // Test for PTR response to _services request. ChipLogProgress(Discovery, "Checking response to _services._dns-sd._udp.local for small parameters"); server.AddExpectedRecord(&ptrCommissionableNodeService); server.AddExpectedRecord(&ptrServiceSubLFullLen); server.AddExpectedRecord(&ptrServiceSubSFullLen); - NL_TEST_ASSERT(inSuite, SendQuery(kDnsSdQueryName) == CHIP_NO_ERROR); + EXPECT_EQ(SendQuery(kDnsSdQueryName), CHIP_NO_ERROR); // These check that the requested records added are sent out correctly. - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); // Want PTR response to _matterc._udp. We will also get the SRV and TXT as additionals. // We won't get any A/AAAA because this is a test and we don't have addresses. // First fill in the instance name - FullQNames already have this space included. - NL_TEST_ASSERT(inSuite, - mdnsAdvertiser.GetCommissionableInstanceName(instanceNamePrefix, sizeof(instanceNamePrefix)) == CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->GetCommissionableInstanceName(instanceNamePrefix, sizeof(instanceNamePrefix)), CHIP_NO_ERROR); ChipLogProgress(Discovery, "Testing response to _matterc._udp.local for small parameters"); server.Reset(); server.AddExpectedRecord(&ptrCommissionableNode); server.AddExpectedRecord(&srvCommissionableNode); server.AddExpectedRecord(&txtCommissionableNodeParamsSmall); - NL_TEST_ASSERT(inSuite, SendQuery(kMatterCommissionableNodeQueryName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kMatterCommissionableNodeQueryName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); ChipLogProgress(Discovery, "Testing response to instance name for small parameters"); server.Reset(); // Just the SRV and TXT should return server.AddExpectedRecord(&srvCommissionableNode); server.AddExpectedRecord(&txtCommissionableNodeParamsSmall); - NL_TEST_ASSERT(inSuite, SendQuery(instanceName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(instanceName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); // Add more parameters, check that the subtypes and TXT values get set correctly. // Also check that we get proper values when the discriminators are small (no leading 0's) - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(commissionableNodeParamsLargeBasic) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.FinalizeServiceUpdate() == CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->Advertise(commissionableNodeParamsLargeBasic), CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->FinalizeServiceUpdate(), CHIP_NO_ERROR); ChipLogProgress(Discovery, "Checking response to _services._dns-sd._udp.local for large basic parameters"); server.Reset(); server.AddExpectedRecord(&ptrCommissionableNodeService); @@ -424,30 +447,30 @@ void CommissionableAdverts(nlTestSuite * inSuite, void * inContext) server.AddExpectedRecord(&ptrServiceSubCM); server.AddExpectedRecord(&ptrServiceSubVendor); server.AddExpectedRecord(&ptrServiceSubDeviceType); - NL_TEST_ASSERT(inSuite, SendQuery(kDnsSdQueryName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kDnsSdQueryName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); ChipLogProgress(Discovery, "Testing response to _matterc._udp.local for large basic parameters"); server.Reset(); server.AddExpectedRecord(&ptrCommissionableNode); server.AddExpectedRecord(&srvCommissionableNode); server.AddExpectedRecord(&txtCommissionableNodeParamsLargeBasic); - NL_TEST_ASSERT(inSuite, SendQuery(kMatterCommissionableNodeQueryName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kMatterCommissionableNodeQueryName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); ChipLogProgress(Discovery, "Testing response to instance name for large basic parameters"); server.Reset(); // Just the SRV and TXT should return server.AddExpectedRecord(&srvCommissionableNode); server.AddExpectedRecord(&txtCommissionableNodeParamsLargeBasic); - NL_TEST_ASSERT(inSuite, SendQuery(instanceName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(instanceName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(commissionableNodeParamsLargeEnhanced) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.FinalizeServiceUpdate() == CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->Advertise(commissionableNodeParamsLargeEnhanced), CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->FinalizeServiceUpdate(), CHIP_NO_ERROR); ChipLogProgress(Discovery, "Checking response to _services._dns-sd._udp.local for large enhanced parameters"); server.Reset(); server.AddExpectedRecord(&ptrCommissionableNodeService); @@ -456,65 +479,62 @@ void CommissionableAdverts(nlTestSuite * inSuite, void * inContext) server.AddExpectedRecord(&ptrServiceSubCM); server.AddExpectedRecord(&ptrServiceSubVendor); server.AddExpectedRecord(&ptrServiceSubDeviceType); - NL_TEST_ASSERT(inSuite, SendQuery(kDnsSdQueryName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kDnsSdQueryName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); ChipLogProgress(Discovery, "Testing response to _matterc._udp.local for large enhanced parameters"); server.Reset(); server.AddExpectedRecord(&ptrCommissionableNode); server.AddExpectedRecord(&srvCommissionableNode); server.AddExpectedRecord(&txtCommissionableNodeParamsLargeEnhanced); - NL_TEST_ASSERT(inSuite, SendQuery(kMatterCommissionableNodeQueryName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kMatterCommissionableNodeQueryName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); ChipLogProgress(Discovery, "Testing response to instance name for large enhanced parameters"); server.Reset(); // Just the SRV and TXT should return server.AddExpectedRecord(&srvCommissionableNode); server.AddExpectedRecord(&txtCommissionableNodeParamsLargeEnhanced); - NL_TEST_ASSERT(inSuite, SendQuery(instanceName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(instanceName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); #if CHIP_CONFIG_ENABLE_ICD_SERVER - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(commissionableNodeParamsEnhancedAsICDLIT) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.FinalizeServiceUpdate() == CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->Advertise(commissionableNodeParamsEnhancedAsICDLIT), CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->FinalizeServiceUpdate(), CHIP_NO_ERROR); ChipLogProgress(Discovery, "Testing response to _matterc._udp.local for enhanced parameters With ICD as LIT"); server.Reset(); server.AddExpectedRecord(&ptrCommissionableNode); server.AddExpectedRecord(&srvCommissionableNode); server.AddExpectedRecord(&txtCommissionableNodeParamsEnhancedAsICDLIT); - NL_TEST_ASSERT(inSuite, SendQuery(kMatterCommissionableNodeQueryName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kMatterCommissionableNodeQueryName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); ChipLogProgress(Discovery, "Testing response to instance name for enhanced parameters With ICD as LIT"); server.Reset(); // Just the SRV and TXT should return server.AddExpectedRecord(&srvCommissionableNode); server.AddExpectedRecord(&txtCommissionableNodeParamsEnhancedAsICDLIT); - NL_TEST_ASSERT(inSuite, SendQuery(instanceName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(instanceName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); #endif } -void CommissionableAndOperationalAdverts(nlTestSuite * inSuite, void * inContext) +TEST_F(TestAdvertiser, CommissionableAndOperationalAdverts) { - auto & mdnsAdvertiser = chip::Dnssd::ServiceAdvertiser::Instance(); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.RemoveServices() == CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->RemoveServices(), CHIP_NO_ERROR); - auto & server = static_cast(GlobalMinimalMdnsServer::Server()); - server.SetTestSuite(inSuite); server.Reset(); // Add two operational and a commissionable and test that we get the correct values back. - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(operationalParams1) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(operationalParams2) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(commissionableNodeParamsLargeEnhanced) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, mdnsAdvertiser.FinalizeServiceUpdate() == CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->Advertise(operationalParams1), CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->Advertise(operationalParams2), CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->Advertise(commissionableNodeParamsLargeEnhanced), CHIP_NO_ERROR); + EXPECT_EQ(mdnsAdvertiser->FinalizeServiceUpdate(), CHIP_NO_ERROR); // Services listing should have two operational ptrs, the base commissionable node ptr and the various _sub ptrs ChipLogProgress(Discovery, "Checking response to _services._dns-sd._udp.local"); @@ -529,9 +549,9 @@ void CommissionableAndOperationalAdverts(nlTestSuite * inSuite, void * inContext server.AddExpectedRecord(&ptrServiceSubDeviceType); server.AddExpectedRecord(&ptrServiceSubCompressedId1); server.AddExpectedRecord(&ptrServiceSubCompressedId2); - NL_TEST_ASSERT(inSuite, SendQuery(kDnsSdQueryName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kDnsSdQueryName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); // Requests for _matter._tcp.local will respond with all records from both operational records, but no commissionable. ChipLogProgress(Discovery, "Testing response to _matter._tcp.local"); @@ -542,9 +562,9 @@ void CommissionableAndOperationalAdverts(nlTestSuite * inSuite, void * inContext server.AddExpectedRecord(&ptrOperational2); server.AddExpectedRecord(&srvOperational2); server.AddExpectedRecord(&txtOperational2); - NL_TEST_ASSERT(inSuite, SendQuery(kMatterOperationalQueryName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kMatterOperationalQueryName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); // Responses to _matterc query should return commissionable node, but no operational. ChipLogProgress(Discovery, "Testing response to _matterc._udp.local"); @@ -552,9 +572,9 @@ void CommissionableAndOperationalAdverts(nlTestSuite * inSuite, void * inContext server.AddExpectedRecord(&ptrCommissionableNode); server.AddExpectedRecord(&srvCommissionableNode); server.AddExpectedRecord(&txtCommissionableNodeParamsLargeEnhanced); - NL_TEST_ASSERT(inSuite, SendQuery(kMatterCommissionableNodeQueryName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kMatterCommissionableNodeQueryName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); // Requests for each SRV record should give only records specific to that fabric. ChipLogProgress(Discovery, "Testing response to operational instance name for fabric 1"); @@ -562,56 +582,26 @@ void CommissionableAndOperationalAdverts(nlTestSuite * inSuite, void * inContext // Just the SRV and TXT should return server.AddExpectedRecord(&srvOperational1); server.AddExpectedRecord(&txtOperational1); - NL_TEST_ASSERT(inSuite, SendQuery(kInstanceName1) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kInstanceName1), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); ChipLogProgress(Discovery, "Testing response to operational instance name for fabric 2"); server.Reset(); // Just the SRV and TXT should return server.AddExpectedRecord(&srvOperational2); server.AddExpectedRecord(&txtOperational2); - NL_TEST_ASSERT(inSuite, SendQuery(kInstanceName2) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(kInstanceName2), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); ChipLogProgress(Discovery, "Testing response to commissionable instance name"); server.Reset(); // Just the SRV and TXT should return server.AddExpectedRecord(&srvCommissionableNode); server.AddExpectedRecord(&txtCommissionableNodeParamsLargeEnhanced); - NL_TEST_ASSERT(inSuite, SendQuery(instanceName) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); + EXPECT_EQ(SendQuery(instanceName), CHIP_NO_ERROR); + EXPECT_TRUE(server.GetSendCalled()); + EXPECT_TRUE(server.GetHeaderFound()); } - -const nlTest sTests[] = { - NL_TEST_DEF("OperationalAdverts", OperationalAdverts), // - NL_TEST_DEF("CommissionableNodeAdverts", CommissionableAdverts), // - NL_TEST_DEF("CommissionableAndOperationalAdverts", CommissionableAndOperationalAdverts), // - NL_TEST_SENTINEL() // -}; - } // namespace - -int TestAdvertiser() -{ - chip::Platform::MemoryInit(); - chip::Test::IOContext context; - context.Init(); - nlTestSuite theSuite = { "AdvertiserImplMinimal", sTests, nullptr, nullptr }; - CheckOnlyServer server(&theSuite); - test::ServerSwapper swapper(&server); - auto & mdnsAdvertiser = chip::Dnssd::ServiceAdvertiser::Instance(); - mdnsAdvertiser.Init(context.GetUDPEndPointManager()); - nlTestRunner(&theSuite, &server); - server.Shutdown(); - context.Shutdown(); - mdnsAdvertiser.RemoveServices(); - mdnsAdvertiser.Shutdown(); - chip::Platform::MemoryShutdown(); - - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestAdvertiser) diff --git a/src/lib/dnssd/minimal_mdns/tests/TestMinimalMdnsAllocator.cpp b/src/lib/dnssd/minimal_mdns/tests/TestMinimalMdnsAllocator.cpp index c712b1bd647f47..f571e81fcf27d1 100644 --- a/src/lib/dnssd/minimal_mdns/tests/TestMinimalMdnsAllocator.cpp +++ b/src/lib/dnssd/minimal_mdns/tests/TestMinimalMdnsAllocator.cpp @@ -16,11 +16,9 @@ * limitations under the License. */ -#include - -#include +#include -#include +#include using namespace chip; using namespace chip::Dnssd; @@ -42,21 +40,21 @@ class TestAllocator : public QueryResponderAllocator // void dmalloc_track(const dmalloc_track_t track_func) #endif } - void TestAllQNamesAreNull(nlTestSuite * inSuite) + void TestAllQNamesAreNull() { for (size_t i = 0; i < GetMaxAllocatedQNames(); ++i) { - NL_TEST_ASSERT(inSuite, GetQNamePart(i) == nullptr); + EXPECT_EQ(GetQNamePart(i), nullptr); } } - void TestAllRecordRespondersAreNull(nlTestSuite * inSuite) + void TestAllRecordRespondersAreNull() { for (size_t i = 0; i < kMaxRecords; ++i) { - NL_TEST_ASSERT(inSuite, GetRecordResponder(i) == nullptr); + EXPECT_EQ(GetRecordResponder(i), nullptr); } } - void TestRecordRespondersMatchQuery(nlTestSuite * inSuite) + void TestRecordRespondersMatchQuery() { mdns::Minimal::QueryResponderRecordFilter noFilter; auto queryResponder = GetQueryResponder(); @@ -64,56 +62,63 @@ class TestAllocator : public QueryResponderAllocator for (auto it = queryResponder->begin(&noFilter); it != queryResponder->end(); it++, idx++) { // TODO: Once the responders are exposed in the query responder, check that they match. - NL_TEST_ASSERT(inSuite, idx < kMaxRecords); + EXPECT_LT(idx, kMaxRecords); } } size_t GetMaxAllocatedQNames() { return QueryResponderAllocator::GetMaxAllocatedQNames(); } }; -void TestQueryAllocatorQName(nlTestSuite * inSuite, void * inContext) +class TestMinimalMdnsAllocator : public ::testing::Test +{ +public: + static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); } + static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); } +}; + +TEST_F(TestMinimalMdnsAllocator, TestQueryAllocatorQName) { TestAllocator test; #if CHIP_CONFIG_MEMORY_DEBUG_DMALLOC unsigned long mark = dmalloc_mark(); #endif // Start empty. - test.TestAllRecordRespondersAreNull(inSuite); - test.TestAllQNamesAreNull(inSuite); + test.TestAllRecordRespondersAreNull(); + test.TestAllQNamesAreNull(); // We should be able to add up to GetMaxAllocatedQNames QNames for (size_t i = 0; i < test.GetMaxAllocatedQNames(); ++i) { - NL_TEST_ASSERT(inSuite, test.AllocateQName("test", "testy", "udp") != FullQName()); - test.TestAllRecordRespondersAreNull(inSuite); + EXPECT_NE(test.AllocateQName("test", "testy", "udp"), FullQName()); + test.TestAllRecordRespondersAreNull(); } #if CHIP_CONFIG_MEMORY_DEBUG_DMALLOC // Count the memory that has not been freed at this point (since mark) unsigned long nAllocated = dmalloc_count_changed(mark, 1, 0); - NL_TEST_ASSERT(inSuite, nAllocated != 0); + EXPECT_NE(nAllocated, 0); #endif // Adding one more should fail. - NL_TEST_ASSERT(inSuite, test.AllocateQName("test", "testy", "udp") == FullQName()); - test.TestAllRecordRespondersAreNull(inSuite); + EXPECT_EQ(test.AllocateQName("test", "testy", "udp"), FullQName()); + test.TestAllRecordRespondersAreNull(); #if CHIP_CONFIG_MEMORY_DEBUG_DMALLOC // We should not have allocated any more memory - NL_TEST_ASSERT(inSuite, nAllocated == dmalloc_count_changed(mark, 1, 0)); + EXPECT_EQ(nAllocated, dmalloc_count_changed(mark, 1, 0)); #endif // Clear should take us back to all empty. test.Clear(); - test.TestAllQNamesAreNull(inSuite); - test.TestAllRecordRespondersAreNull(inSuite); + test.TestAllQNamesAreNull(); + test.TestAllRecordRespondersAreNull(); #if CHIP_CONFIG_MEMORY_DEBUG_DMALLOC // The amount of unfreed pointers should be 0. - NL_TEST_ASSERT(inSuite, dmalloc_count_changed(mark, 1, 0) == 0); + EXPECT_EQ(dmalloc_count_changed(mark, 1, 0), 0); #endif } -void TestQueryAllocatorQNameArray(nlTestSuite * inSuite, void * inContext) +TEST_F(TestMinimalMdnsAllocator, TestQueryAllocatorQNameArray) { TestAllocator test; @@ -125,43 +130,43 @@ void TestQueryAllocatorQNameArray(nlTestSuite * inSuite, void * inContext) const char * kArray[kNumParts] = { "this", "is", "a", "test" }; // Start empty. - test.TestAllRecordRespondersAreNull(inSuite); - test.TestAllQNamesAreNull(inSuite); + test.TestAllRecordRespondersAreNull(); + test.TestAllQNamesAreNull(); // We should be able to add up to GetMaxAllocatedQNames QNames for (size_t i = 0; i < test.GetMaxAllocatedQNames(); ++i) { - NL_TEST_ASSERT(inSuite, test.AllocateQNameFromArray(kArray, kNumParts) != FullQName()); - test.TestAllRecordRespondersAreNull(inSuite); + EXPECT_NE(test.AllocateQNameFromArray(kArray, kNumParts), FullQName()); + test.TestAllRecordRespondersAreNull(); } #if CHIP_CONFIG_MEMORY_DEBUG_DMALLOC // Count the memory that has not been freed at this point (since mark) unsigned long nAllocated = dmalloc_count_changed(mark, 1, 0); - NL_TEST_ASSERT(inSuite, nAllocated != 0); + EXPECT_NE(nAllocated, 0); #endif // Adding one more should fail. - NL_TEST_ASSERT(inSuite, test.AllocateQNameFromArray(kArray, kNumParts) == FullQName()); - test.TestAllRecordRespondersAreNull(inSuite); + EXPECT_EQ(test.AllocateQNameFromArray(kArray, kNumParts), FullQName()); + test.TestAllRecordRespondersAreNull(); #if CHIP_CONFIG_MEMORY_DEBUG_DMALLOC // We should not have allocated any more memory - NL_TEST_ASSERT(inSuite, nAllocated == dmalloc_count_changed(mark, 1, 0)); + EXPECT_EQ(nAllocated, dmalloc_count_changed(mark, 1, 0)); #endif // Clear should take us back to all empty. test.Clear(); - test.TestAllQNamesAreNull(inSuite); - test.TestAllRecordRespondersAreNull(inSuite); + test.TestAllQNamesAreNull(); + test.TestAllRecordRespondersAreNull(); #if CHIP_CONFIG_MEMORY_DEBUG_DMALLOC // The amount of unfreed pointers should be 0. - NL_TEST_ASSERT(inSuite, dmalloc_count_changed(mark, 1, 0) == 0); + EXPECT_EQ(dmalloc_count_changed(mark, 1, 0), 0); #endif } -void TestQueryAllocatorRecordResponder(nlTestSuite * inSuite, void * inContext) +TEST_F(TestMinimalMdnsAllocator, TestQueryAllocatorRecordResponder) { TestAllocator test; @@ -169,42 +174,42 @@ void TestQueryAllocatorRecordResponder(nlTestSuite * inSuite, void * inContext) unsigned long mark = dmalloc_mark(); #endif // Start empty. - test.TestAllRecordRespondersAreNull(inSuite); - test.TestAllQNamesAreNull(inSuite); + test.TestAllRecordRespondersAreNull(); + test.TestAllQNamesAreNull(); FullQName serviceName = test.AllocateQName("test", "service"); FullQName instanceName = test.AllocateQName("test", "instance"); for (size_t i = 0; i < kMaxRecords; ++i) { - NL_TEST_ASSERT(inSuite, test.AddResponder(serviceName, instanceName).IsValid()); + EXPECT_TRUE(test.AddResponder(serviceName, instanceName).IsValid()); } #if CHIP_CONFIG_MEMORY_DEBUG_DMALLOC // Count the memory that has not been freed at this point (since mark) unsigned long nAllocated = dmalloc_count_changed(mark, 1, 0); - NL_TEST_ASSERT(inSuite, nAllocated != 0); + EXPECT_NE(nAllocated, 0); #endif // Adding one more should fail. - NL_TEST_ASSERT(inSuite, !test.AddResponder(serviceName, instanceName).IsValid()); + EXPECT_FALSE(test.AddResponder(serviceName, instanceName).IsValid()); #if CHIP_CONFIG_MEMORY_DEBUG_DMALLOC // We should not have allocated any more memory - NL_TEST_ASSERT(inSuite, nAllocated == dmalloc_count_changed(mark, 1, 0)); + EXPECT_EQ(nAllocated, dmalloc_count_changed(mark, 1, 0)); #endif // Clear should take us back to all empty. test.Clear(); - test.TestAllQNamesAreNull(inSuite); - test.TestAllRecordRespondersAreNull(inSuite); + test.TestAllQNamesAreNull(); + test.TestAllRecordRespondersAreNull(); #if CHIP_CONFIG_MEMORY_DEBUG_DMALLOC // The amount of unfreed pointers should be 0. - NL_TEST_ASSERT(inSuite, dmalloc_count_changed(mark, 1, 0) == 0); + EXPECT_EQ(dmalloc_count_changed(mark, 1, 0), 0); #endif } -void TestQueryAllocatorRecordResponderTypes(nlTestSuite * inSuite, void * inContext) +TEST_F(TestMinimalMdnsAllocator, TestQueryAllocatorRecordResponderTypes) { TestAllocator test; @@ -212,49 +217,49 @@ void TestQueryAllocatorRecordResponderTypes(nlTestSuite * inSuite, void * inCont unsigned long mark = dmalloc_mark(); #endif // Start empty. - test.TestAllRecordRespondersAreNull(inSuite); - test.TestAllQNamesAreNull(inSuite); + test.TestAllRecordRespondersAreNull(); + test.TestAllQNamesAreNull(); FullQName serviceName = test.AllocateQName("test", "service"); FullQName instanceName = test.AllocateQName("test", "instance"); FullQName hostName = test.AllocateQName("test", "host"); FullQName someTxt = test.AllocateQName("L1=some text", "L2=some other text"); - NL_TEST_ASSERT(inSuite, serviceName != FullQName()); - NL_TEST_ASSERT(inSuite, instanceName != FullQName()); - NL_TEST_ASSERT(inSuite, hostName != FullQName()); - NL_TEST_ASSERT(inSuite, someTxt != FullQName()); + EXPECT_NE(serviceName, FullQName()); + EXPECT_NE(instanceName, FullQName()); + EXPECT_NE(hostName, FullQName()); + EXPECT_NE(someTxt, FullQName()); // Test that we can add all types - NL_TEST_ASSERT(inSuite, test.AddResponder(serviceName, instanceName).IsValid()); - NL_TEST_ASSERT(inSuite, test.AddResponder(SrvResourceRecord(instanceName, hostName, 57)).IsValid()); - NL_TEST_ASSERT(inSuite, test.AddResponder(TxtResourceRecord(instanceName, someTxt)).IsValid()); - NL_TEST_ASSERT(inSuite, test.AddResponder(hostName).IsValid()); - NL_TEST_ASSERT(inSuite, test.AddResponder(hostName).IsValid()); + EXPECT_TRUE(test.AddResponder(serviceName, instanceName).IsValid()); + EXPECT_TRUE(test.AddResponder(SrvResourceRecord(instanceName, hostName, 57)).IsValid()); + EXPECT_TRUE(test.AddResponder(TxtResourceRecord(instanceName, someTxt)).IsValid()); + EXPECT_TRUE(test.AddResponder(hostName).IsValid()); + EXPECT_TRUE(test.AddResponder(hostName).IsValid()); #if CHIP_CONFIG_MEMORY_DEBUG_DMALLOC // Count the memory that has not been freed at this point (since mark) unsigned long nAllocated = dmalloc_count_changed(mark, 1, 0); - NL_TEST_ASSERT(inSuite, nAllocated != 0); + EXPECT_NE(nAllocated, 0); #endif // Clear should take us back to all empty. test.Clear(); - test.TestAllQNamesAreNull(inSuite); - test.TestAllRecordRespondersAreNull(inSuite); + test.TestAllQNamesAreNull(); + test.TestAllRecordRespondersAreNull(); #if CHIP_CONFIG_MEMORY_DEBUG_DMALLOC // The amount of unfreed pointers should be 0. - NL_TEST_ASSERT(inSuite, dmalloc_count_changed(mark, 1, 0) == 0); + EXPECT_EQ(dmalloc_count_changed(mark, 1, 0), 0); #endif } -void TestGetResponder(nlTestSuite * inSuite, void * inContext) +TEST_F(TestMinimalMdnsAllocator, TestGetResponder) { TestAllocator test; // Start empty. - test.TestAllRecordRespondersAreNull(inSuite); - test.TestAllQNamesAreNull(inSuite); + test.TestAllRecordRespondersAreNull(); + test.TestAllQNamesAreNull(); FullQName serviceName = test.AllocateQName("test", "service"); FullQName instanceName = test.AllocateQName("test", "instance"); @@ -262,68 +267,40 @@ void TestGetResponder(nlTestSuite * inSuite, void * inContext) FullQName someTxt = test.AllocateQName("L1=some text", "L2=some other text"); FullQName notAdded = test.AllocateQName("not", "added"); - NL_TEST_ASSERT(inSuite, serviceName != FullQName()); - NL_TEST_ASSERT(inSuite, instanceName != FullQName()); - NL_TEST_ASSERT(inSuite, hostName != FullQName()); - NL_TEST_ASSERT(inSuite, someTxt != FullQName()); + EXPECT_NE(serviceName, FullQName()); + EXPECT_NE(instanceName, FullQName()); + EXPECT_NE(hostName, FullQName()); + EXPECT_NE(someTxt, FullQName()); - NL_TEST_ASSERT(inSuite, test.AddResponder(serviceName, instanceName).IsValid()); - NL_TEST_ASSERT(inSuite, test.AddResponder(SrvResourceRecord(instanceName, hostName, 57)).IsValid()); - NL_TEST_ASSERT(inSuite, test.AddResponder(TxtResourceRecord(instanceName, someTxt)).IsValid()); - NL_TEST_ASSERT(inSuite, test.AddResponder(hostName).IsValid()); - NL_TEST_ASSERT(inSuite, test.AddResponder(hostName).IsValid()); + EXPECT_TRUE(test.AddResponder(serviceName, instanceName).IsValid()); + EXPECT_TRUE(test.AddResponder(SrvResourceRecord(instanceName, hostName, 57)).IsValid()); + EXPECT_TRUE(test.AddResponder(TxtResourceRecord(instanceName, someTxt)).IsValid()); + EXPECT_TRUE(test.AddResponder(hostName).IsValid()); + EXPECT_TRUE(test.AddResponder(hostName).IsValid()); // These should all exist - NL_TEST_ASSERT(inSuite, test.GetResponder(QType::PTR, serviceName) != nullptr); - NL_TEST_ASSERT(inSuite, test.GetResponder(QType::SRV, instanceName) != nullptr); - NL_TEST_ASSERT(inSuite, test.GetResponder(QType::TXT, instanceName) != nullptr); - NL_TEST_ASSERT(inSuite, test.GetResponder(QType::A, hostName) != nullptr); - NL_TEST_ASSERT(inSuite, test.GetResponder(QType::AAAA, hostName) != nullptr); + ASSERT_NE(test.GetResponder(QType::PTR, serviceName), nullptr); + ASSERT_NE(test.GetResponder(QType::SRV, instanceName), nullptr); + ASSERT_NE(test.GetResponder(QType::TXT, instanceName), nullptr); + ASSERT_NE(test.GetResponder(QType::A, hostName), nullptr); + ASSERT_NE(test.GetResponder(QType::AAAA, hostName), nullptr); // incorrect types - NL_TEST_ASSERT(inSuite, test.GetResponder(QType::SRV, notAdded) == nullptr); - NL_TEST_ASSERT(inSuite, test.GetResponder(QType::AAAA, instanceName) == nullptr); - NL_TEST_ASSERT(inSuite, test.GetResponder(QType::A, instanceName) == nullptr); - NL_TEST_ASSERT(inSuite, test.GetResponder(QType::PTR, hostName) == nullptr); - NL_TEST_ASSERT(inSuite, test.GetResponder(QType::TXT, hostName) == nullptr); + EXPECT_EQ(test.GetResponder(QType::SRV, notAdded), nullptr); + EXPECT_EQ(test.GetResponder(QType::AAAA, instanceName), nullptr); + EXPECT_EQ(test.GetResponder(QType::A, instanceName), nullptr); + EXPECT_EQ(test.GetResponder(QType::PTR, hostName), nullptr); + EXPECT_EQ(test.GetResponder(QType::TXT, hostName), nullptr); // incorrect names - NL_TEST_ASSERT(inSuite, test.GetResponder(QType::PTR, notAdded) == nullptr); - NL_TEST_ASSERT(inSuite, test.GetResponder(QType::SRV, notAdded) == nullptr); - NL_TEST_ASSERT(inSuite, test.GetResponder(QType::TXT, notAdded) == nullptr); - NL_TEST_ASSERT(inSuite, test.GetResponder(QType::A, notAdded) == nullptr); - NL_TEST_ASSERT(inSuite, test.GetResponder(QType::AAAA, notAdded) == nullptr); + EXPECT_EQ(test.GetResponder(QType::PTR, notAdded), nullptr); + EXPECT_EQ(test.GetResponder(QType::SRV, notAdded), nullptr); + EXPECT_EQ(test.GetResponder(QType::TXT, notAdded), nullptr); + EXPECT_EQ(test.GetResponder(QType::A, notAdded), nullptr); + EXPECT_EQ(test.GetResponder(QType::AAAA, notAdded), nullptr); test.Clear(); } -const nlTest sTests[] = { - NL_TEST_DEF("TestQueryAllocatorQName", TestQueryAllocatorQName), // - NL_TEST_DEF("TestQueryAllocatorQNameArray", TestQueryAllocatorQNameArray), // - NL_TEST_DEF("TestQueryAllocatorRecordResponder", TestQueryAllocatorRecordResponder), // - NL_TEST_DEF("TestQueryAllocatorRecordResponderTypes", TestQueryAllocatorRecordResponderTypes), // - NL_TEST_DEF("TestGetResponder", TestGetResponder), // - - NL_TEST_SENTINEL() // -}; - -int TestSetup(void * inContext) -{ - return chip::Platform::MemoryInit() == CHIP_NO_ERROR ? SUCCESS : FAILURE; -} - -int TestTeardown(void * inContext) -{ - chip::Platform::MemoryShutdown(); - return SUCCESS; -} - } // namespace -int TestMinimalMdnsAllocator() -{ - nlTestSuite theSuite = { "MinimalMdnsAllocator", &sTests[0], &TestSetup, &TestTeardown }; - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestMinimalMdnsAllocator); +; diff --git a/src/lib/dnssd/minimal_mdns/tests/TestQueryReplyFilter.cpp b/src/lib/dnssd/minimal_mdns/tests/TestQueryReplyFilter.cpp index 690c28b6193929..234b6876c6b7fe 100644 --- a/src/lib/dnssd/minimal_mdns/tests/TestQueryReplyFilter.cpp +++ b/src/lib/dnssd/minimal_mdns/tests/TestQueryReplyFilter.cpp @@ -14,10 +14,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include -#include +#include -#include +#include namespace { @@ -35,7 +34,7 @@ QueryData buildQueryData(QType qType, QClass qClass, const uint8_t (&query)[N]) return QueryData(qType, qClass, false, query, BytesRange(query, query + N)); } -void TestQueryReplyFilter(nlTestSuite * inSuite, void * inContext) +TEST(TestQueryReplyFilter, TestQueryReplyFilter) { const uint8_t query[] = { 4, 's', 'o', 'm', 'e', // @@ -44,45 +43,34 @@ void TestQueryReplyFilter(nlTestSuite * inSuite, void * inContext) }; // sanity test that the serialized qname was build correctly - NL_TEST_ASSERT(inSuite, SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query) == FullQName(kName1)); - NL_TEST_ASSERT(inSuite, SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query) != FullQName(kName2)); - NL_TEST_ASSERT(inSuite, SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query) != FullQName(kName3)); - NL_TEST_ASSERT(inSuite, SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query) != FullQName(kName4)); + EXPECT_EQ(SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query), FullQName(kName1)); + EXPECT_NE(SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query), FullQName(kName2)); + EXPECT_NE(SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query), FullQName(kName3)); + EXPECT_NE(SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query), FullQName(kName4)); // Acceptable cases - NL_TEST_ASSERT( - inSuite, QueryReplyFilter(buildQueryData(QType::ANY, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); - NL_TEST_ASSERT(inSuite, - QueryReplyFilter(buildQueryData(QType::A, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); + EXPECT_TRUE(QueryReplyFilter(buildQueryData(QType::ANY, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); + EXPECT_TRUE(QueryReplyFilter(buildQueryData(QType::A, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); - NL_TEST_ASSERT(inSuite, - QueryReplyFilter(buildQueryData(QType::ANY, QClass::IN, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); + EXPECT_TRUE(QueryReplyFilter(buildQueryData(QType::ANY, QClass::IN, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); - NL_TEST_ASSERT(inSuite, - QueryReplyFilter(buildQueryData(QType::A, QClass::IN, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); + EXPECT_TRUE(QueryReplyFilter(buildQueryData(QType::A, QClass::IN, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); // Reject cases - NL_TEST_ASSERT( - inSuite, !QueryReplyFilter(buildQueryData(QType::ANY, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName2))); + EXPECT_FALSE(QueryReplyFilter(buildQueryData(QType::ANY, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName2))); - NL_TEST_ASSERT( - inSuite, !QueryReplyFilter(buildQueryData(QType::ANY, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName3))); + EXPECT_FALSE(QueryReplyFilter(buildQueryData(QType::ANY, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName3))); - NL_TEST_ASSERT( - inSuite, !QueryReplyFilter(buildQueryData(QType::ANY, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName4))); + EXPECT_FALSE(QueryReplyFilter(buildQueryData(QType::ANY, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName4))); - NL_TEST_ASSERT( - inSuite, - !QueryReplyFilter(buildQueryData(QType::AAAA, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); + EXPECT_FALSE(QueryReplyFilter(buildQueryData(QType::AAAA, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); - NL_TEST_ASSERT( - inSuite, !QueryReplyFilter(buildQueryData(QType::SRV, QClass::IN, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); + EXPECT_FALSE(QueryReplyFilter(buildQueryData(QType::SRV, QClass::IN, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); - NL_TEST_ASSERT( - inSuite, !QueryReplyFilter(buildQueryData(QType::PTR, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); + EXPECT_FALSE(QueryReplyFilter(buildQueryData(QType::PTR, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); } -void TestLongerQueryPath(nlTestSuite * inSuite, void * inContext) +TEST(TestQueryReplyFilter, TestLongerQueryPath) { const uint8_t query[] = { 4, 'm', 'o', 'r', 'e', // @@ -93,30 +81,12 @@ void TestLongerQueryPath(nlTestSuite * inSuite, void * inContext) }; // sanity test that the serialized qname was build correctly - NL_TEST_ASSERT(inSuite, SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query) != FullQName(kName1)); - NL_TEST_ASSERT(inSuite, SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query) != FullQName(kName2)); - NL_TEST_ASSERT(inSuite, SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query) == FullQName(kName3)); - NL_TEST_ASSERT(inSuite, SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query) != FullQName(kName4)); - - NL_TEST_ASSERT( - inSuite, !QueryReplyFilter(buildQueryData(QType::ANY, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); - NL_TEST_ASSERT( - inSuite, QueryReplyFilter(buildQueryData(QType::ANY, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName3))); -} + EXPECT_NE(SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query), FullQName(kName1)); + EXPECT_NE(SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query), FullQName(kName2)); + EXPECT_EQ(SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query), FullQName(kName3)); + EXPECT_NE(SerializedQNameIterator(BytesRange(query, query + sizeof(query)), query), FullQName(kName4)); -const nlTest sTests[] = { - NL_TEST_DEF("TestQueryReplyFilter", TestQueryReplyFilter), // - NL_TEST_DEF("TestLongerQueryPath", TestLongerQueryPath), // - NL_TEST_SENTINEL() // -}; - -} // namespace - -int TestQueryReplyFilter() -{ - nlTestSuite theSuite = { "QueryReplyFilter", sTests, nullptr, nullptr }; - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); + EXPECT_FALSE(QueryReplyFilter(buildQueryData(QType::ANY, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName1))); + EXPECT_TRUE(QueryReplyFilter(buildQueryData(QType::ANY, QClass::ANY, query)).Accept(QType::A, QClass::IN, FullQName(kName3))); } - -CHIP_REGISTER_TEST_SUITE(TestQueryReplyFilter) +} // namespace diff --git a/src/lib/dnssd/minimal_mdns/tests/TestRecordData.cpp b/src/lib/dnssd/minimal_mdns/tests/TestRecordData.cpp index 181b9d3b6f93fa..295ed2296215bf 100644 --- a/src/lib/dnssd/minimal_mdns/tests/TestRecordData.cpp +++ b/src/lib/dnssd/minimal_mdns/tests/TestRecordData.cpp @@ -19,9 +19,7 @@ #include #include -#include - -#include +#include namespace { @@ -29,7 +27,7 @@ using namespace std; using namespace chip; using namespace mdns::Minimal; -void SrvRecordSimpleParsing(nlTestSuite * inSuite, void * inContext) +TEST(TestRecordData, SrvRecordSimpleParsing) { const uint8_t record[] = { 0, 12, // Priority @@ -46,27 +44,27 @@ void SrvRecordSimpleParsing(nlTestSuite * inSuite, void * inContext) SrvRecord srv; - NL_TEST_ASSERT(inSuite, srv.Parse(data, packet)); - NL_TEST_ASSERT(inSuite, srv.GetPriority() == 12); - NL_TEST_ASSERT(inSuite, srv.GetWeight() == 3); - NL_TEST_ASSERT(inSuite, srv.GetPort() == 0x1234); + EXPECT_TRUE(srv.Parse(data, packet)); + EXPECT_EQ(srv.GetPriority(), 12); + EXPECT_EQ(srv.GetWeight(), 3); + EXPECT_EQ(srv.GetPort(), 0x1234); // name can be read several times for (int i = 0; i < 3; i++) { SerializedQNameIterator name = srv.GetName(); - NL_TEST_ASSERT(inSuite, name.Next()); - NL_TEST_ASSERT(inSuite, strcmp(name.Value(), "some") == 0); - NL_TEST_ASSERT(inSuite, name.Next()); - NL_TEST_ASSERT(inSuite, strcmp(name.Value(), "test") == 0); - NL_TEST_ASSERT(inSuite, name.Next()); - NL_TEST_ASSERT(inSuite, strcmp(name.Value(), "local") == 0); - NL_TEST_ASSERT(inSuite, name.Next() == false); + EXPECT_TRUE(name.Next()); + EXPECT_STREQ(name.Value(), "some"); + EXPECT_TRUE(name.Next()); + EXPECT_STREQ(name.Value(), "test"); + EXPECT_TRUE(name.Next()); + EXPECT_STREQ(name.Value(), "local"); + EXPECT_EQ(name.Next(), false); } } -void SrvWithPtrRecord(nlTestSuite * inSuite, void * inContext) +TEST(TestRecordData, SrvWithPtrRecord) { const uint8_t record[] = { 'x', 'y', 'z', // dummy data (3 bytes) @@ -86,27 +84,27 @@ void SrvWithPtrRecord(nlTestSuite * inSuite, void * inContext) SrvRecord srv; - NL_TEST_ASSERT(inSuite, srv.Parse(data, packet)); - NL_TEST_ASSERT(inSuite, srv.GetPriority() == 12); - NL_TEST_ASSERT(inSuite, srv.GetWeight() == 3); - NL_TEST_ASSERT(inSuite, srv.GetPort() == 0x1234); + EXPECT_TRUE(srv.Parse(data, packet)); + EXPECT_EQ(srv.GetPriority(), 12); + EXPECT_EQ(srv.GetWeight(), 3); + EXPECT_EQ(srv.GetPort(), 0x1234); // name can be read several times for (int i = 0; i < 3; i++) { SerializedQNameIterator name = srv.GetName(); - NL_TEST_ASSERT(inSuite, name.Next()); - NL_TEST_ASSERT(inSuite, strcmp(name.Value(), "foo") == 0); - NL_TEST_ASSERT(inSuite, name.Next()); - NL_TEST_ASSERT(inSuite, strcmp(name.Value(), "some") == 0); - NL_TEST_ASSERT(inSuite, name.Next()); - NL_TEST_ASSERT(inSuite, strcmp(name.Value(), "test") == 0); - NL_TEST_ASSERT(inSuite, name.Next() == false); + EXPECT_TRUE(name.Next()); + EXPECT_STREQ(name.Value(), "foo"); + EXPECT_TRUE(name.Next()); + EXPECT_STREQ(name.Value(), "some"); + EXPECT_TRUE(name.Next()); + EXPECT_STREQ(name.Value(), "test"); + EXPECT_EQ(name.Next(), false); } } -void ARecordParsing(nlTestSuite * inSuite, void * inContext) +TEST(TestRecordData, ARecordParsing) { const uint8_t record[] = { 10, @@ -120,15 +118,15 @@ void ARecordParsing(nlTestSuite * inSuite, void * inContext) #if INET_CONFIG_ENABLE_IPV4 Inet::IPAddress expected; - NL_TEST_ASSERT(inSuite, ParseARecord(BytesRange(record, record + sizeof(record)), &addr)); - NL_TEST_ASSERT(inSuite, Inet::IPAddress::FromString("10.11.12.13", expected)); - NL_TEST_ASSERT(inSuite, addr == expected); + EXPECT_TRUE(ParseARecord(BytesRange(record, record + sizeof(record)), &addr)); + EXPECT_TRUE(Inet::IPAddress::FromString("10.11.12.13", expected)); + EXPECT_EQ(addr, expected); #else - NL_TEST_ASSERT(inSuite, !ParseARecord(BytesRange(record, record + sizeof(record)), &addr)); + EXPECT_FALSE(ParseARecord(BytesRange(record, record + sizeof(record)), &addr)); #endif // INET_CONFIG_ENABLE_IPV4 } -void AAAARecordParsing(nlTestSuite * inSuite, void * inContext) +TEST(TestRecordData, AAAARecordParsing) { const uint8_t record[] = { 0x12, 0x23, 0x00, 0x00, // @@ -140,12 +138,12 @@ void AAAARecordParsing(nlTestSuite * inSuite, void * inContext) Inet::IPAddress addr; Inet::IPAddress expected; - NL_TEST_ASSERT(inSuite, ParseAAAARecord(BytesRange(record, record + sizeof(record)), &addr)); - NL_TEST_ASSERT(inSuite, Inet::IPAddress::FromString("1223::3456:789A", expected)); - NL_TEST_ASSERT(inSuite, addr == expected); + EXPECT_TRUE(ParseAAAARecord(BytesRange(record, record + sizeof(record)), &addr)); + EXPECT_TRUE(Inet::IPAddress::FromString("1223::3456:789A", expected)); + EXPECT_EQ(addr, expected); } -void PtrRecordSimpleParsing(nlTestSuite * inSuite, void * inContext) +TEST(TestRecordData, PtrRecordSimpleParsing) { const uint8_t record[] = { 4, 's', 'o', 'm', 'e', // QNAME part: some @@ -159,17 +157,17 @@ void PtrRecordSimpleParsing(nlTestSuite * inSuite, void * inContext) SerializedQNameIterator name; - NL_TEST_ASSERT(inSuite, ParsePtrRecord(data, packet, &name)); - NL_TEST_ASSERT(inSuite, name.Next()); - NL_TEST_ASSERT(inSuite, strcmp(name.Value(), "some") == 0); - NL_TEST_ASSERT(inSuite, name.Next()); - NL_TEST_ASSERT(inSuite, strcmp(name.Value(), "test") == 0); - NL_TEST_ASSERT(inSuite, name.Next()); - NL_TEST_ASSERT(inSuite, strcmp(name.Value(), "local") == 0); - NL_TEST_ASSERT(inSuite, name.Next() == false); + EXPECT_TRUE(ParsePtrRecord(data, packet, &name)); + EXPECT_TRUE(name.Next()); + EXPECT_STREQ(name.Value(), "some"); + EXPECT_TRUE(name.Next()); + EXPECT_STREQ(name.Value(), "test"); + EXPECT_TRUE(name.Next()); + EXPECT_STREQ(name.Value(), "local"); + EXPECT_EQ(name.Next(), false); } -void PtrRecordComplexParsing(nlTestSuite * inSuite, void * inContext) +TEST(TestRecordData, PtrRecordComplexParsing) { const uint8_t record[] = { 'x', 'y', 'z', // dummy data (3 bytes) @@ -187,18 +185,18 @@ void PtrRecordComplexParsing(nlTestSuite * inSuite, void * inContext) BytesRange data(record + 24, record + sizeof(record)); SerializedQNameIterator name; - NL_TEST_ASSERT(inSuite, ParsePtrRecord(data, packet, &name)); - NL_TEST_ASSERT(inSuite, name.Next()); - NL_TEST_ASSERT(inSuite, strcmp(name.Value(), "foo") == 0); - NL_TEST_ASSERT(inSuite, name.Next()); - NL_TEST_ASSERT(inSuite, strcmp(name.Value(), "bar") == 0); - NL_TEST_ASSERT(inSuite, name.Next()); - NL_TEST_ASSERT(inSuite, strcmp(name.Value(), "baz") == 0); - NL_TEST_ASSERT(inSuite, name.Next()); - NL_TEST_ASSERT(inSuite, strcmp(name.Value(), "some") == 0); - NL_TEST_ASSERT(inSuite, name.Next()); - NL_TEST_ASSERT(inSuite, strcmp(name.Value(), "test") == 0); - NL_TEST_ASSERT(inSuite, name.Next() == false); + EXPECT_TRUE(ParsePtrRecord(data, packet, &name)); + EXPECT_TRUE(name.Next()); + EXPECT_STREQ(name.Value(), "foo"); + EXPECT_TRUE(name.Next()); + EXPECT_STREQ(name.Value(), "bar"); + EXPECT_TRUE(name.Next()); + EXPECT_STREQ(name.Value(), "baz"); + EXPECT_TRUE(name.Next()); + EXPECT_STREQ(name.Value(), "some"); + EXPECT_TRUE(name.Next()); + EXPECT_STREQ(name.Value(), "test"); + EXPECT_EQ(name.Next(), false); } class TxtRecordAccumulator : public TxtRecordDelegate @@ -223,7 +221,7 @@ class TxtRecordAccumulator : public TxtRecordDelegate } }; -void TxtRecord(nlTestSuite * inSuite, void * inContext) +TEST(TestRecordData, TxtRecord) { const uint8_t record[] = { 4, 's', 'o', 'm', 'e', // some @@ -234,32 +232,11 @@ void TxtRecord(nlTestSuite * inSuite, void * inContext) TxtRecordAccumulator accumulator; - NL_TEST_ASSERT(inSuite, ParseTxtRecord(BytesRange(record, record + sizeof(record)), &accumulator)); - NL_TEST_ASSERT(inSuite, accumulator.Data().size() == 4); - NL_TEST_ASSERT(inSuite, (accumulator.Data()[0] == make_pair("some", ""))); - NL_TEST_ASSERT(inSuite, (accumulator.Data()[1] == make_pair("foo", "bar"))); - NL_TEST_ASSERT(inSuite, (accumulator.Data()[2] == make_pair("x", "y=z"))); - NL_TEST_ASSERT(inSuite, (accumulator.Data()[3] == make_pair("a", ""))); + EXPECT_TRUE(ParseTxtRecord(BytesRange(record, record + sizeof(record)), &accumulator)); + EXPECT_EQ(accumulator.Data().size(), 4u); + EXPECT_EQ(accumulator.Data()[0], (make_pair("some", ""))); + EXPECT_EQ(accumulator.Data()[1], (make_pair("foo", "bar"))); + EXPECT_EQ(accumulator.Data()[2], (make_pair("x", "y=z"))); + EXPECT_EQ(accumulator.Data()[3], (make_pair("a", ""))); } - -const nlTest sTests[] = { - NL_TEST_DEF("SrvRecordSimpleParsing", SrvRecordSimpleParsing), // - NL_TEST_DEF("SrvWithPtrRecord", SrvWithPtrRecord), // - NL_TEST_DEF("ARecordParsing", ARecordParsing), // - NL_TEST_DEF("AAAARecordParsing", AAAARecordParsing), // - NL_TEST_DEF("PtrRecordSimpleParsing", PtrRecordSimpleParsing), // - NL_TEST_DEF("PtrRecordComplexParsing", PtrRecordComplexParsing), // - NL_TEST_DEF("TxtRecord", TxtRecord), // - NL_TEST_SENTINEL() // -}; - } // namespace - -int TestRecordData() -{ - nlTestSuite theSuite = { "RecordData", sTests, nullptr, nullptr }; - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestRecordData) diff --git a/src/lib/dnssd/minimal_mdns/tests/TestResponseSender.cpp b/src/lib/dnssd/minimal_mdns/tests/TestResponseSender.cpp index 39316ae129bd36..80ae9dc1b2c55b 100644 --- a/src/lib/dnssd/minimal_mdns/tests/TestResponseSender.cpp +++ b/src/lib/dnssd/minimal_mdns/tests/TestResponseSender.cpp @@ -19,6 +19,8 @@ #include #include +#include + #include #include #include @@ -28,9 +30,6 @@ #include #include -#include - -#include namespace { @@ -72,24 +71,31 @@ struct CommonTestElements QueryResponder<10> queryResponder; Inet::IPPacketInfo packetInfo; - CommonTestElements(nlTestSuite * inSuite, const char * tag) : + CommonTestElements(const char * tag) : recordWriter(&requestBufferWriter), dnsSd(FlatAllocatedQName::Build(dnsSdServiceStorage, "_services", "_dns-sd", "_udp", "local")), service(FlatAllocatedQName::Build(serviceNameStorage, tag, "service")), instance(FlatAllocatedQName::Build(instanceNameStorage, tag, "instance")), host(FlatAllocatedQName::Build(hostNameStorage, tag, "host")), - txt(FlatAllocatedQName::Build(txtStorage, tag, "L1=something", "L2=other")), server(inSuite) + txt(FlatAllocatedQName::Build(txtStorage, tag, "L1=something", "L2=other")), server() { queryResponder.Init(); header.SetQueryCount(1); } }; -void SrvAnyResponseToInstance(nlTestSuite * inSuite, void * inContext) +class TestResponseSender : public ::testing::Test +{ +public: + static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); } + static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); } +}; + +TEST_F(TestResponseSender, SrvAnyResponseToInstance) { - CommonTestElements common(inSuite, "test"); + CommonTestElements common("test"); ResponseSender responseSender(&common.server); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&common.queryResponder) == CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&common.queryResponder), CHIP_NO_ERROR); common.queryResponder.AddResponder(&common.srvResponder); // Build a query for our srv record @@ -100,15 +106,15 @@ void SrvAnyResponseToInstance(nlTestSuite * inSuite, void * inContext) common.server.AddExpectedRecord(&common.srvRecord); responseSender.Respond(1, queryData, &common.packetInfo, ResponseConfiguration()); - NL_TEST_ASSERT(inSuite, common.server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, common.server.GetHeaderFound()); + EXPECT_TRUE(common.server.GetSendCalled()); + EXPECT_TRUE(common.server.GetHeaderFound()); } -void SrvTxtAnyResponseToInstance(nlTestSuite * inSuite, void * inContext) +TEST_F(TestResponseSender, SrvTxtAnyResponseToInstance) { - CommonTestElements common(inSuite, "test"); + CommonTestElements common("test"); ResponseSender responseSender(&common.server); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&common.queryResponder) == CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&common.queryResponder), CHIP_NO_ERROR); common.queryResponder.AddResponder(&common.srvResponder); common.queryResponder.AddResponder(&common.txtResponder); @@ -122,15 +128,15 @@ void SrvTxtAnyResponseToInstance(nlTestSuite * inSuite, void * inContext) common.server.AddExpectedRecord(&common.txtRecord); responseSender.Respond(1, queryData, &common.packetInfo, ResponseConfiguration()); - NL_TEST_ASSERT(inSuite, common.server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, common.server.GetHeaderFound()); + EXPECT_TRUE(common.server.GetSendCalled()); + EXPECT_TRUE(common.server.GetHeaderFound()); } -void PtrSrvTxtAnyResponseToServiceName(nlTestSuite * inSuite, void * inContext) +TEST_F(TestResponseSender, PtrSrvTxtAnyResponseToServiceName) { - CommonTestElements common(inSuite, "test"); + CommonTestElements common("test"); ResponseSender responseSender(&common.server); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&common.queryResponder) == CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&common.queryResponder), CHIP_NO_ERROR); common.queryResponder.AddResponder(&common.ptrResponder).SetReportAdditional(common.instance); common.queryResponder.AddResponder(&common.srvResponder); common.queryResponder.AddResponder(&common.txtResponder); @@ -147,15 +153,15 @@ void PtrSrvTxtAnyResponseToServiceName(nlTestSuite * inSuite, void * inContext) responseSender.Respond(1, queryData, &common.packetInfo, ResponseConfiguration()); - NL_TEST_ASSERT(inSuite, common.server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, common.server.GetHeaderFound()); + EXPECT_TRUE(common.server.GetSendCalled()); + EXPECT_TRUE(common.server.GetHeaderFound()); } -void PtrSrvTxtAnyResponseToInstance(nlTestSuite * inSuite, void * inContext) +TEST_F(TestResponseSender, PtrSrvTxtAnyResponseToInstance) { - CommonTestElements common(inSuite, "test"); + CommonTestElements common("test"); ResponseSender responseSender(&common.server); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&common.queryResponder) == CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&common.queryResponder), CHIP_NO_ERROR); common.queryResponder.AddResponder(&common.ptrResponder); common.queryResponder.AddResponder(&common.srvResponder); common.queryResponder.AddResponder(&common.txtResponder); @@ -171,15 +177,15 @@ void PtrSrvTxtAnyResponseToInstance(nlTestSuite * inSuite, void * inContext) responseSender.Respond(1, queryData, &common.packetInfo, ResponseConfiguration()); - NL_TEST_ASSERT(inSuite, common.server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, common.server.GetHeaderFound()); + EXPECT_TRUE(common.server.GetSendCalled()); + EXPECT_TRUE(common.server.GetHeaderFound()); } -void PtrSrvTxtSrvResponseToInstance(nlTestSuite * inSuite, void * inContext) +TEST_F(TestResponseSender, PtrSrvTxtSrvResponseToInstance) { - CommonTestElements common(inSuite, "test"); + CommonTestElements common("test"); ResponseSender responseSender(&common.server); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&common.queryResponder) == CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&common.queryResponder), CHIP_NO_ERROR); common.queryResponder.AddResponder(&common.ptrResponder).SetReportInServiceListing(true); common.queryResponder.AddResponder(&common.srvResponder); common.queryResponder.AddResponder(&common.txtResponder); @@ -194,15 +200,15 @@ void PtrSrvTxtSrvResponseToInstance(nlTestSuite * inSuite, void * inContext) responseSender.Respond(1, queryData, &common.packetInfo, ResponseConfiguration()); - NL_TEST_ASSERT(inSuite, common.server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, common.server.GetHeaderFound()); + EXPECT_TRUE(common.server.GetSendCalled()); + EXPECT_TRUE(common.server.GetHeaderFound()); } -void PtrSrvTxtAnyResponseToServiceListing(nlTestSuite * inSuite, void * inContext) +TEST_F(TestResponseSender, PtrSrvTxtAnyResponseToServiceListing) { - CommonTestElements common(inSuite, "test"); + CommonTestElements common("test"); ResponseSender responseSender(&common.server); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&common.queryResponder) == CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&common.queryResponder), CHIP_NO_ERROR); common.queryResponder.AddResponder(&common.ptrResponder).SetReportInServiceListing(true); common.queryResponder.AddResponder(&common.srvResponder); common.queryResponder.AddResponder(&common.txtResponder); @@ -218,31 +224,31 @@ void PtrSrvTxtAnyResponseToServiceListing(nlTestSuite * inSuite, void * inContex responseSender.Respond(1, queryData, &common.packetInfo, ResponseConfiguration()); - NL_TEST_ASSERT(inSuite, common.server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, common.server.GetHeaderFound()); + EXPECT_TRUE(common.server.GetSendCalled()); + EXPECT_TRUE(common.server.GetHeaderFound()); } -void NoQueryResponder(nlTestSuite * inSuite, void * inContext) +TEST_F(TestResponseSender, NoQueryResponder) { - CommonTestElements common(inSuite, "test"); + CommonTestElements common("test"); ResponseSender responseSender(&common.server); QueryData queryData = QueryData(QType::ANY, QClass::IN, false, common.requestNameStart, common.requestBytesRange); common.recordWriter.WriteQName(common.dnsSd); responseSender.Respond(1, queryData, &common.packetInfo, ResponseConfiguration()); - NL_TEST_ASSERT(inSuite, !common.server.GetSendCalled()); + EXPECT_FALSE(common.server.GetSendCalled()); common.recordWriter.WriteQName(common.service); responseSender.Respond(1, queryData, &common.packetInfo, ResponseConfiguration()); - NL_TEST_ASSERT(inSuite, !common.server.GetSendCalled()); + EXPECT_FALSE(common.server.GetSendCalled()); common.recordWriter.WriteQName(common.instance); responseSender.Respond(1, queryData, &common.packetInfo, ResponseConfiguration()); - NL_TEST_ASSERT(inSuite, !common.server.GetSendCalled()); + EXPECT_FALSE(common.server.GetSendCalled()); } -void AddManyQueryResponders(nlTestSuite * inSuite, void * inContext) +TEST_F(TestResponseSender, AddManyQueryResponders) { // TODO(cecille): Fix this test once #8000 gets resolved. ResponseSender responseSender(nullptr); @@ -259,39 +265,39 @@ void AddManyQueryResponders(nlTestSuite * inSuite, void * inContext) constexpr size_t kAddLoopSize = 1000; for (size_t i = 0; i < kAddLoopSize; ++i) { - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&q1) == CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&q1), CHIP_NO_ERROR); } // removing the only copy should clear out everything responseSender.RemoveQueryResponder(&q1); - NL_TEST_ASSERT(inSuite, !responseSender.HasQueryResponders()); + EXPECT_FALSE(responseSender.HasQueryResponders()); // At least 7 should be supported: // - 5 is the spec minimum // - 2 for commissionable and commisioner responders - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&q1) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&q2) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&q3) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&q4) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&q5) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&q6) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&q7) == CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&q1), CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&q2), CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&q3), CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&q4), CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&q5), CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&q6), CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&q7), CHIP_NO_ERROR); } -void PtrSrvTxtMultipleRespondersToInstance(nlTestSuite * inSuite, void * inContext) +TEST_F(TestResponseSender, PtrSrvTxtMultipleRespondersToInstance) { - CommonTestElements common1(inSuite, "test1"); - CommonTestElements common2(inSuite, "test2"); + CommonTestElements common1("test1"); + CommonTestElements common2("test2"); // Just use the server from common1. ResponseSender responseSender(&common1.server); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&common1.queryResponder) == CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&common1.queryResponder), CHIP_NO_ERROR); common1.queryResponder.AddResponder(&common1.ptrResponder).SetReportInServiceListing(true); common1.queryResponder.AddResponder(&common1.srvResponder); common1.queryResponder.AddResponder(&common1.txtResponder); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&common2.queryResponder) == CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&common2.queryResponder), CHIP_NO_ERROR); common2.queryResponder.AddResponder(&common2.ptrResponder).SetReportInServiceListing(true); common2.queryResponder.AddResponder(&common2.srvResponder); common2.queryResponder.AddResponder(&common2.txtResponder); @@ -306,23 +312,23 @@ void PtrSrvTxtMultipleRespondersToInstance(nlTestSuite * inSuite, void * inConte responseSender.Respond(1, queryData, &common1.packetInfo, ResponseConfiguration()); - NL_TEST_ASSERT(inSuite, common1.server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, common1.server.GetHeaderFound()); + EXPECT_TRUE(common1.server.GetSendCalled()); + EXPECT_TRUE(common1.server.GetHeaderFound()); } -void PtrSrvTxtMultipleRespondersToServiceListing(nlTestSuite * inSuite, void * inContext) +TEST_F(TestResponseSender, PtrSrvTxtMultipleRespondersToServiceListing) { - auto common1 = std::make_unique(inSuite, "test1"); - auto common2 = std::make_unique(inSuite, "test2"); + auto common1 = std::make_unique("test1"); + auto common2 = std::make_unique("test2"); // Just use the server from common1. ResponseSender responseSender(&common1->server); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&common1->queryResponder) == CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&common1->queryResponder), CHIP_NO_ERROR); common1->queryResponder.AddResponder(&common1->ptrResponder).SetReportInServiceListing(true); common1->queryResponder.AddResponder(&common1->srvResponder); common1->queryResponder.AddResponder(&common1->txtResponder); - NL_TEST_ASSERT(inSuite, responseSender.AddQueryResponder(&common2->queryResponder) == CHIP_NO_ERROR); + EXPECT_EQ(responseSender.AddQueryResponder(&common2->queryResponder), CHIP_NO_ERROR); common2->queryResponder.AddResponder(&common2->ptrResponder).SetReportInServiceListing(true); common2->queryResponder.AddResponder(&common2->srvResponder); common2->queryResponder.AddResponder(&common2->txtResponder); @@ -339,44 +345,9 @@ void PtrSrvTxtMultipleRespondersToServiceListing(nlTestSuite * inSuite, void * i responseSender.Respond(1, queryData, &common1->packetInfo, ResponseConfiguration()); - NL_TEST_ASSERT(inSuite, common1->server.GetSendCalled()); + EXPECT_TRUE(common1->server.GetSendCalled()); - NL_TEST_ASSERT(inSuite, common1->server.GetHeaderFound()); -} - -const nlTest sTests[] = { - NL_TEST_DEF("SrvAnyResponseToInstance", SrvAnyResponseToInstance), // - NL_TEST_DEF("SrvTxtAnyResponseToInstance", SrvTxtAnyResponseToInstance), // - NL_TEST_DEF("PtrSrvTxtAnyResponseToServiceName", PtrSrvTxtAnyResponseToServiceName), // - NL_TEST_DEF("PtrSrvTxtAnyResponseToInstance", PtrSrvTxtAnyResponseToInstance), // - NL_TEST_DEF("PtrSrvTxtSrvResponseToInstance", PtrSrvTxtSrvResponseToInstance), // - NL_TEST_DEF("PtrSrvTxtAnyResponseToServiceListing", PtrSrvTxtAnyResponseToServiceListing), // - NL_TEST_DEF("NoQueryResponder", NoQueryResponder), // - NL_TEST_DEF("AddManyQueryResponders", AddManyQueryResponders), // - NL_TEST_DEF("PtrSrvTxtMultipleRespondersToInstance", PtrSrvTxtMultipleRespondersToInstance), // - NL_TEST_DEF("PtrSrvTxtMultipleRespondersToServiceListing", PtrSrvTxtMultipleRespondersToServiceListing), // - - NL_TEST_SENTINEL() // -}; - -int TestSetup(void * inContext) -{ - return chip::Platform::MemoryInit() == CHIP_NO_ERROR ? SUCCESS : FAILURE; -} - -int TestTeardown(void * inContext) -{ - chip::Platform::MemoryShutdown(); - return SUCCESS; + EXPECT_TRUE(common1->server.GetHeaderFound()); } } // namespace - -int TestResponseSender() -{ - nlTestSuite theSuite = { "RecordData", sTests, &TestSetup, &TestTeardown }; - nlTestRunner(&theSuite, nullptr); - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestResponseSender)