3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 #include <mdns/minimal/responders/IP.h>
21 #include <support/UnitTestRegistration.h>
23 #include <nlunit-test.h>
28 using namespace chip::Inet;
29 using namespace mdns::Minimal;
31 constexpr uint16_t kMdnsPort = 5353;
33 const QNamePart kNames[] = { "some", "test", "local" };
35 class IPResponseAccumulator : public ResponderDelegate
38 IPResponseAccumulator(nlTestSuite * suite) : mSuite(suite) {}
39 void AddResponse(const ResourceRecord & record) override
42 NL_TEST_ASSERT(mSuite, (record.GetType() == QType::A) || (record.GetType() == QType::AAAA));
43 NL_TEST_ASSERT(mSuite, record.GetClass() == QClass::IN);
44 NL_TEST_ASSERT(mSuite, record.GetName() == kNames);
51 InterfaceId FindValidInterfaceId()
53 for (chip::Inet::InterfaceAddressIterator it; it.HasCurrent(); it.Next())
55 if (it.IsUp() && it.HasBroadcastAddress())
57 return it.GetInterfaceId();
60 return INET_NULL_INTERFACEID;
63 void TestIPv4(nlTestSuite * inSuite, void * inContext)
66 NL_TEST_ASSERT(inSuite, IPAddress::FromString("10.20.30.40", ipAddress));
68 IPv4Responder responder(kNames);
70 NL_TEST_ASSERT(inSuite, responder.GetQClass() == QClass::IN);
71 NL_TEST_ASSERT(inSuite, responder.GetQType() == QType::A);
72 NL_TEST_ASSERT(inSuite, responder.GetQName() == kNames);
74 IPResponseAccumulator acc(inSuite);
75 chip::Inet::IPPacketInfo packetInfo;
77 packetInfo.SrcAddress = ipAddress;
78 packetInfo.DestAddress = ipAddress;
79 packetInfo.SrcPort = kMdnsPort;
80 packetInfo.DestPort = kMdnsPort;
81 packetInfo.Interface = FindValidInterfaceId();
83 responder.AddAllResponses(&packetInfo, &acc);
86 void TestIPv6(nlTestSuite * inSuite, void * inContext)
89 NL_TEST_ASSERT(inSuite, IPAddress::FromString("fe80::224:32ff:aabb:ccdd", ipAddress));
91 IPv6Responder responder(kNames);
93 NL_TEST_ASSERT(inSuite, responder.GetQClass() == QClass::IN);
94 NL_TEST_ASSERT(inSuite, responder.GetQType() == QType::AAAA);
95 NL_TEST_ASSERT(inSuite, responder.GetQName() == kNames);
97 IPResponseAccumulator acc(inSuite);
98 chip::Inet::IPPacketInfo packetInfo;
100 packetInfo.SrcAddress = ipAddress;
101 packetInfo.DestAddress = ipAddress;
102 packetInfo.SrcPort = kMdnsPort;
103 packetInfo.DestPort = kMdnsPort;
104 packetInfo.Interface = FindValidInterfaceId();
106 responder.AddAllResponses(&packetInfo, &acc);
109 const nlTest sTests[] = {
110 NL_TEST_DEF("TestIPv4", TestIPv4), //
111 NL_TEST_DEF("TestIPv6", TestIPv6), //
112 NL_TEST_SENTINEL() //
119 nlTestSuite theSuite = { "IP", sTests, nullptr, nullptr };
120 nlTestRunner(&theSuite, nullptr);
121 return nlTestRunnerStats(&theSuite);
124 CHIP_REGISTER_TEST_SUITE(TestIP)