Skip to content

Commit

Permalink
Switched to pw_unit_test in src/lib/dnssd/minimal_mdns/responders/ (#…
Browse files Browse the repository at this point in the history
…33071)

* Switched to pw_unit_test in src/lib/dnssd/minimal_mdns/responders/

* Update src/lib/dnssd/minimal_mdns/responders/tests/TestIPResponder.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 Nov 11, 2024
1 parent 7bb539f commit 1383042
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 164 deletions.
5 changes: 1 addition & 4 deletions src/lib/dnssd/minimal_mdns/responders/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@

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")

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

test_sources = [
Expand All @@ -34,7 +33,5 @@ chip_test_suite_using_nltest("tests") {
"${chip_root}/src/lib/dnssd/minimal_mdns",
"${chip_root}/src/lib/dnssd/minimal_mdns:default_policy",
"${chip_root}/src/lib/dnssd/minimal_mdns/responders",
"${chip_root}/src/lib/support:testing_nlunit",
"${nlunit_test_root}:nlunit-test",
]
}
80 changes: 27 additions & 53 deletions src/lib/dnssd/minimal_mdns/responders/tests/TestIPResponder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

#include <lib/dnssd/minimal_mdns/AddressPolicy_DefaultImpl.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/UnitTestRegistration.h>

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

namespace {

Expand All @@ -37,17 +36,13 @@ const QNamePart kNames[] = { "some", "test", "local" };
class IPResponseAccumulator : public ResponderDelegate
{
public:
IPResponseAccumulator(nlTestSuite * suite) : mSuite(suite) {}
void AddResponse(const ResourceRecord & record) override
{

NL_TEST_ASSERT(mSuite, (record.GetType() == QType::A) || (record.GetType() == QType::AAAA));
NL_TEST_ASSERT(mSuite, record.GetClass() == QClass::IN_FLUSH);
NL_TEST_ASSERT(mSuite, record.GetName() == kNames);
EXPECT_TRUE((record.GetType() == QType::A) || (record.GetType() == QType::AAAA));
EXPECT_EQ(record.GetClass(), QClass::IN_FLUSH);
EXPECT_EQ(record.GetName(), kNames);
}

private:
nlTestSuite * mSuite;
};

InterfaceId FindValidInterfaceId()
Expand All @@ -62,19 +57,30 @@ InterfaceId FindValidInterfaceId()
return InterfaceId::Null();
}

class TestIPResponder : public ::testing::Test
{
public:
static void SetUpTestSuite()
{
mdns::Minimal::SetDefaultAddressPolicy();
ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR);
}
static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }
};

#if INET_CONFIG_ENABLE_IPV4
void TestIPv4(nlTestSuite * inSuite, void * inContext)
TEST_F(TestIPResponder, TestIPv4)
{
IPAddress ipAddress;
NL_TEST_ASSERT(inSuite, IPAddress::FromString("10.20.30.40", ipAddress));
EXPECT_TRUE(IPAddress::FromString("10.20.30.40", ipAddress));

IPv4Responder responder(kNames);

NL_TEST_ASSERT(inSuite, responder.GetQClass() == QClass::IN);
NL_TEST_ASSERT(inSuite, responder.GetQType() == QType::A);
NL_TEST_ASSERT(inSuite, responder.GetQName() == kNames);
EXPECT_EQ(responder.GetQClass(), QClass::IN);
EXPECT_EQ(responder.GetQType(), QType::A);
EXPECT_EQ(responder.GetQName(), kNames);

IPResponseAccumulator acc(inSuite);
IPResponseAccumulator acc;
chip::Inet::IPPacketInfo packetInfo;

packetInfo.SrcAddress = ipAddress;
Expand All @@ -87,18 +93,18 @@ void TestIPv4(nlTestSuite * inSuite, void * inContext)
}
#endif // INET_CONFIG_ENABLE_IPV4

void TestIPv6(nlTestSuite * inSuite, void * inContext)
TEST_F(TestIPResponder, TestIPv6)
{
IPAddress ipAddress;
NL_TEST_ASSERT(inSuite, IPAddress::FromString("fe80::224:32ff:aabb:ccdd", ipAddress));
EXPECT_TRUE(IPAddress::FromString("fe80::224:32ff:aabb:ccdd", ipAddress));

IPv6Responder responder(kNames);

NL_TEST_ASSERT(inSuite, responder.GetQClass() == QClass::IN);
NL_TEST_ASSERT(inSuite, responder.GetQType() == QType::AAAA);
NL_TEST_ASSERT(inSuite, responder.GetQName() == kNames);
EXPECT_EQ(responder.GetQClass(), QClass::IN);
EXPECT_EQ(responder.GetQType(), QType::AAAA);
EXPECT_EQ(responder.GetQName(), kNames);

IPResponseAccumulator acc(inSuite);
IPResponseAccumulator acc;
chip::Inet::IPPacketInfo packetInfo;

packetInfo.SrcAddress = ipAddress;
Expand All @@ -110,36 +116,4 @@ void TestIPv6(nlTestSuite * inSuite, void * inContext)
responder.AddAllResponses(&packetInfo, &acc, ResponseConfiguration());
}

int Setup(void * inContext)
{
mdns::Minimal::SetDefaultAddressPolicy();

CHIP_ERROR error = chip::Platform::MemoryInit();

return (error == CHIP_NO_ERROR) ? SUCCESS : FAILURE;
}

int Teardown(void * inContext)
{
chip::Platform::MemoryShutdown();
return SUCCESS;
}

const nlTest sTests[] = {
#if INET_CONFIG_ENABLE_IPV4
NL_TEST_DEF("TestIPv4", TestIPv4), //
#endif // INET_CONFIG_ENABLE_IPV4
NL_TEST_DEF("TestIPv6", TestIPv6), //
NL_TEST_SENTINEL() //
};

} // namespace

int TestIP()
{
nlTestSuite theSuite = { "IP", sTests, &Setup, &Teardown };
nlTestRunner(&theSuite, nullptr);
return nlTestRunnerStats(&theSuite);
}

CHIP_REGISTER_TEST_SUITE(TestIP)
70 changes: 26 additions & 44 deletions src/lib/dnssd/minimal_mdns/responders/tests/TestPtrResponder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

#include <lib/dnssd/minimal_mdns/Parser.h>
#include <lib/dnssd/minimal_mdns/RecordData.h>
#include <lib/support/UnitTestRegistration.h>

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

namespace {

Expand All @@ -39,13 +38,13 @@ const QNamePart kTargetNames[] = { "point", "to", "this" };
class PtrResponseAccumulator : public ResponderDelegate
{
public:
PtrResponseAccumulator(nlTestSuite * suite, const uint32_t expectedTtl) : mSuite(suite), mExpectedTtl(expectedTtl) {}
PtrResponseAccumulator(const uint32_t expectedTtl) : mExpectedTtl(expectedTtl) {}
void AddResponse(const ResourceRecord & record) override
{

NL_TEST_ASSERT(mSuite, record.GetType() == QType::PTR);
NL_TEST_ASSERT(mSuite, record.GetClass() == QClass::IN);
NL_TEST_ASSERT(mSuite, record.GetName() == kNames);
EXPECT_EQ(record.GetType(), QType::PTR);
EXPECT_EQ(record.GetClass(), QClass::IN);
EXPECT_EQ(record.GetName(), kNames);

if (record.GetType() == QType::PTR)
{
Expand All @@ -58,44 +57,43 @@ class PtrResponseAccumulator : public ResponderDelegate
HeaderRef hdr(headerBuffer);
hdr.Clear();

NL_TEST_ASSERT(mSuite, record.Append(hdr, ResourceType::kAnswer, writer));
EXPECT_TRUE(record.Append(hdr, ResourceType::kAnswer, writer));

ResourceData data;
SerializedQNameIterator target;
const uint8_t * start = buffer;

NL_TEST_ASSERT(mSuite, out.Fit());
EXPECT_TRUE(out.Fit());

BytesRange validDataRange(buffer, buffer + out.Needed());

NL_TEST_ASSERT(mSuite, data.Parse(validDataRange, &start));
NL_TEST_ASSERT(mSuite, start == (buffer + out.Needed()));
NL_TEST_ASSERT(mSuite, data.GetName() == FullQName(kNames));
NL_TEST_ASSERT(mSuite, data.GetType() == QType::PTR);
NL_TEST_ASSERT(mSuite, data.GetTtlSeconds() == mExpectedTtl);
EXPECT_TRUE(data.Parse(validDataRange, &start));
EXPECT_EQ(start, (buffer + out.Needed()));
EXPECT_EQ(data.GetName(), FullQName(kNames));
EXPECT_EQ(data.GetType(), QType::PTR);
EXPECT_EQ(data.GetTtlSeconds(), mExpectedTtl);

NL_TEST_ASSERT(mSuite, ParsePtrRecord(data.GetData(), validDataRange, &target));
NL_TEST_ASSERT(mSuite, target == FullQName(kTargetNames));
EXPECT_TRUE(ParsePtrRecord(data.GetData(), validDataRange, &target));
EXPECT_EQ(target, FullQName(kTargetNames));
}
}

private:
nlTestSuite * mSuite;
const uint32_t mExpectedTtl;
};

void TestPtrResponse(nlTestSuite * inSuite, void * inContext)
TEST(TestPtrResponder, TestPtrResponse)
{
IPAddress ipAddress;
NL_TEST_ASSERT(inSuite, IPAddress::FromString("2607:f8b0:4005:804::200e", ipAddress));
EXPECT_TRUE(IPAddress::FromString("2607:f8b0:4005:804::200e", ipAddress));

PtrResponder responder(kNames, kTargetNames);

NL_TEST_ASSERT(inSuite, responder.GetQClass() == QClass::IN);
NL_TEST_ASSERT(inSuite, responder.GetQType() == QType::PTR);
NL_TEST_ASSERT(inSuite, responder.GetQName() == kNames);
EXPECT_EQ(responder.GetQClass(), QClass::IN);
EXPECT_EQ(responder.GetQType(), QType::PTR);
EXPECT_EQ(responder.GetQName(), kNames);

PtrResponseAccumulator acc(inSuite, ResourceRecord::kDefaultTtl);
PtrResponseAccumulator acc(ResourceRecord::kDefaultTtl);
chip::Inet::IPPacketInfo packetInfo;

packetInfo.SrcAddress = ipAddress;
Expand All @@ -107,18 +105,18 @@ void TestPtrResponse(nlTestSuite * inSuite, void * inContext)
responder.AddAllResponses(&packetInfo, &acc, ResponseConfiguration());
}

void TestPtrResponseOverrideTtl(nlTestSuite * inSuite, void * inContext)
TEST(TestPtrResponder, TestPtrResponseOverrideTtl)
{
IPAddress ipAddress;
NL_TEST_ASSERT(inSuite, IPAddress::FromString("2607:f8b0:4005:804::200e", ipAddress));
EXPECT_TRUE(IPAddress::FromString("2607:f8b0:4005:804::200e", ipAddress));

PtrResponder responder(kNames, kTargetNames);

NL_TEST_ASSERT(inSuite, responder.GetQClass() == QClass::IN);
NL_TEST_ASSERT(inSuite, responder.GetQType() == QType::PTR);
NL_TEST_ASSERT(inSuite, responder.GetQName() == kNames);
EXPECT_EQ(responder.GetQClass(), QClass::IN);
EXPECT_EQ(responder.GetQType(), QType::PTR);
EXPECT_EQ(responder.GetQName(), kNames);

PtrResponseAccumulator acc(inSuite, 123);
PtrResponseAccumulator acc(123);
chip::Inet::IPPacketInfo packetInfo;

packetInfo.SrcAddress = ipAddress;
Expand All @@ -129,20 +127,4 @@ void TestPtrResponseOverrideTtl(nlTestSuite * inSuite, void * inContext)

responder.AddAllResponses(&packetInfo, &acc, ResponseConfiguration().SetTtlSecondsOverride(123));
}

const nlTest sTests[] = {
NL_TEST_DEF("TestPtrResponse", TestPtrResponse), //
NL_TEST_DEF("TestPtrResponseOverrideTtl", TestPtrResponseOverrideTtl), //
NL_TEST_SENTINEL() //
};

} // namespace

int TestPtr()
{
nlTestSuite theSuite = { "IP", sTests, nullptr, nullptr };
nlTestRunner(&theSuite, nullptr);
return nlTestRunnerStats(&theSuite);
}

CHIP_REGISTER_TEST_SUITE(TestPtr)
Loading

0 comments on commit 1383042

Please sign in to comment.