3 * Copyright (c) 2020 Project CHIP Authors
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include <mdns/minimal/IPResourceRecord.h>
20 #include <support/UnitTestRegistration.h>
22 #include <nlunit-test.h>
26 using namespace mdns::Minimal;
28 using namespace chip::Inet;
30 constexpr uint16_t kTestQnameCount = 3;
31 const char * kTestQnames[kTestQnameCount] = { "some", "test", "local" };
33 void WriteIPv4(nlTestSuite * inSuite, void * inContext)
37 NL_TEST_ASSERT(inSuite, IPAddress::FromString("10.20.30.40", ipAddress));
39 uint8_t headerBuffer[HeaderRef::kSizeBytes];
40 uint8_t dataBuffer[128];
42 HeaderRef header(headerBuffer);
45 BufBound output(dataBuffer, sizeof(dataBuffer));
46 IPResourceRecord ipResourceRecord(kTestQnames, kTestQnameCount, ipAddress);
48 ipResourceRecord.SetTtl(123);
52 const uint8_t expectedOutput[] = {
53 4, 's', 'o', 'm', 'e', // QNAME part: some
54 4, 't', 'e', 's', 't', // QNAME part: test
55 5, 'l', 'o', 'c', 'a', 'l', // QNAME part: local
60 0, 4, // data size - size for IPv4
61 10, 20, 30, 40 // IP Address
64 NL_TEST_ASSERT(inSuite, ipResourceRecord.Append(header, ResourceType::kAnswer, output));
65 NL_TEST_ASSERT(inSuite, header.GetAnswerCount() == 1);
66 NL_TEST_ASSERT(inSuite, header.GetAuthorityCount() == 0);
67 NL_TEST_ASSERT(inSuite, header.GetAdditionalCount() == 0);
68 NL_TEST_ASSERT(inSuite, output.Needed() == sizeof(expectedOutput));
69 NL_TEST_ASSERT(inSuite, memcmp(dataBuffer, expectedOutput, sizeof(expectedOutput)) == 0);
73 BufBound output(dataBuffer, sizeof(dataBuffer));
75 IPResourceRecord ipResourceRecord(kTestQnames, kTestQnameCount, ipAddress);
77 ipResourceRecord.SetTtl(234);
81 const uint8_t expectedOutput[] = {
82 4, 's', 'o', 'm', 'e', // QNAME part: some
83 4, 't', 'e', 's', 't', // QNAME part: test
84 5, 'l', 'o', 'c', 'a', 'l', // QNAME part: local
89 0, 4, // data size - size for IPv4
90 10, 20, 30, 40 // IP Address
93 NL_TEST_ASSERT(inSuite, ipResourceRecord.Append(header, ResourceType::kAuthority, output));
94 NL_TEST_ASSERT(inSuite, header.GetAnswerCount() == 0);
95 NL_TEST_ASSERT(inSuite, header.GetAuthorityCount() == 1);
96 NL_TEST_ASSERT(inSuite, header.GetAdditionalCount() == 0);
97 NL_TEST_ASSERT(inSuite, output.Needed() == sizeof(expectedOutput));
98 NL_TEST_ASSERT(inSuite, memcmp(dataBuffer, expectedOutput, sizeof(expectedOutput)) == 0);
102 BufBound output(dataBuffer, sizeof(dataBuffer));
104 IPResourceRecord ipResourceRecord(kTestQnames, kTestQnameCount, ipAddress);
106 ipResourceRecord.SetTtl(0x1234);
110 const uint8_t expectedOutput[] = {
111 4, 's', 'o', 'm', 'e', // QNAME part: some
112 4, 't', 'e', 's', 't', // QNAME part: test
113 5, 'l', 'o', 'c', 'a', 'l', // QNAME part: local
117 0, 0, 0x12, 0x34, // TTL
118 0, 4, // data size - size for IPv4
119 10, 20, 30, 40 // IP Address
122 NL_TEST_ASSERT(inSuite, ipResourceRecord.Append(header, ResourceType::kAdditional, output));
123 NL_TEST_ASSERT(inSuite, header.GetAnswerCount() == 0);
124 NL_TEST_ASSERT(inSuite, header.GetAuthorityCount() == 0);
125 NL_TEST_ASSERT(inSuite, header.GetAdditionalCount() == 1);
126 NL_TEST_ASSERT(inSuite, output.Needed() == sizeof(expectedOutput));
127 NL_TEST_ASSERT(inSuite, memcmp(dataBuffer, expectedOutput, sizeof(expectedOutput)) == 0);
131 void WriteIPv6(nlTestSuite * inSuite, void * inContext)
135 NL_TEST_ASSERT(inSuite, IPAddress::FromString("fe80::224:32ff:fe19:359b", ipAddress));
137 uint8_t headerBuffer[HeaderRef::kSizeBytes];
138 uint8_t dataBuffer[128];
140 HeaderRef header(headerBuffer);
142 BufBound output(dataBuffer, sizeof(dataBuffer));
143 IPResourceRecord ipResourceRecord(kTestQnames, kTestQnameCount, ipAddress);
145 ipResourceRecord.SetTtl(0x12345678);
149 const uint8_t expectedOutput[] = { //
150 4, 's', 'o', 'm', 'e', // QNAME part: some
151 4, 't', 'e', 's', 't', // QNAME part: test
152 5, 'l', 'o', 'c', 'a', 'l', // QNAME part: local
156 0x12, 0x34, 0x56, 0x78, // TTL
157 0, 16, // data size - size for IPv4
158 0xfe, 0x80, 0x00, 0x00, // IPv6
159 0x00, 0x00, 0x00, 0x00, //
160 0x02, 0x24, 0x32, 0xff, //
161 0xfe, 0x19, 0x35, 0x9b
164 NL_TEST_ASSERT(inSuite, ipResourceRecord.Append(header, ResourceType::kAnswer, output));
165 NL_TEST_ASSERT(inSuite, header.GetAnswerCount() == 1);
166 NL_TEST_ASSERT(inSuite, header.GetAuthorityCount() == 0);
167 NL_TEST_ASSERT(inSuite, header.GetAdditionalCount() == 0);
168 NL_TEST_ASSERT(inSuite, output.Needed() == sizeof(expectedOutput));
169 NL_TEST_ASSERT(inSuite, memcmp(dataBuffer, expectedOutput, sizeof(expectedOutput)) == 0);
172 const nlTest sTests[] = {
173 NL_TEST_DEF("IPV4", WriteIPv4), //
174 NL_TEST_DEF("IPV6", WriteIPv6), //
175 NL_TEST_SENTINEL() //
180 int TestIPResourceRecord(void)
183 nlTestSuite theSuite = { "IPResourceRecord", sTests, nullptr, nullptr };
184 nlTestRunner(&theSuite, nullptr);
186 return (nlTestRunnerStats(&theSuite));
189 CHIP_REGISTER_TEST_SUITE(TestIPResourceRecord)