a327224e3f762558186008d62a9e406d1caead3d
[platform/upstream/connectedhomeip.git] / src / lib / mdns / minimal / tests / TestIPResourceRecord.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    All rights reserved.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include <mdns/minimal/IPResourceRecord.h>
20 #include <support/UnitTestRegistration.h>
21
22 #include <nlunit-test.h>
23
24 namespace {
25
26 using namespace mdns::Minimal;
27 using namespace chip;
28 using namespace chip::Inet;
29
30 constexpr uint16_t kTestQnameCount        = 3;
31 const char * kTestQnames[kTestQnameCount] = { "some", "test", "local" };
32
33 void WriteIPv4(nlTestSuite * inSuite, void * inContext)
34 {
35     IPAddress ipAddress;
36
37     NL_TEST_ASSERT(inSuite, IPAddress::FromString("10.20.30.40", ipAddress));
38
39     uint8_t headerBuffer[HeaderRef::kSizeBytes];
40     uint8_t dataBuffer[128];
41
42     HeaderRef header(headerBuffer);
43
44     {
45         BufBound output(dataBuffer, sizeof(dataBuffer));
46         IPResourceRecord ipResourceRecord(kTestQnames, kTestQnameCount, ipAddress);
47
48         ipResourceRecord.SetTtl(123);
49
50         header.Clear();
51
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
56             0,                           // QNAME ends
57             0,  1,                       // QClass IN
58             0,  1,                       // QType A
59             0,  0,   0,   123,           // TTL
60             0,  4,                       // data size - size for IPv4
61             10, 20,  30,  40             // IP Address
62         };
63
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);
70     }
71
72     {
73         BufBound output(dataBuffer, sizeof(dataBuffer));
74
75         IPResourceRecord ipResourceRecord(kTestQnames, kTestQnameCount, ipAddress);
76
77         ipResourceRecord.SetTtl(234);
78
79         header.Clear();
80
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
85             0,                           // QNAME ends
86             0,  1,                       // QClass IN
87             0,  1,                       // QType A
88             0,  0,   0,   234,           // TTL
89             0,  4,                       // data size - size for IPv4
90             10, 20,  30,  40             // IP Address
91         };
92
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);
99     }
100
101     {
102         BufBound output(dataBuffer, sizeof(dataBuffer));
103
104         IPResourceRecord ipResourceRecord(kTestQnames, kTestQnameCount, ipAddress);
105
106         ipResourceRecord.SetTtl(0x1234);
107
108         header.Clear();
109
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
114             0,                             // QNAME ends
115             0,  1,                         // QClass IN
116             0,  1,                         // QType A
117             0,  0,   0x12, 0x34,           // TTL
118             0,  4,                         // data size - size for IPv4
119             10, 20,  30,   40              // IP Address
120         };
121
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);
128     }
129 }
130
131 void WriteIPv6(nlTestSuite * inSuite, void * inContext)
132 {
133     IPAddress ipAddress;
134
135     NL_TEST_ASSERT(inSuite, IPAddress::FromString("fe80::224:32ff:fe19:359b", ipAddress));
136
137     uint8_t headerBuffer[HeaderRef::kSizeBytes];
138     uint8_t dataBuffer[128];
139
140     HeaderRef header(headerBuffer);
141
142     BufBound output(dataBuffer, sizeof(dataBuffer));
143     IPResourceRecord ipResourceRecord(kTestQnames, kTestQnameCount, ipAddress);
144
145     ipResourceRecord.SetTtl(0x12345678);
146
147     header.Clear();
148
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
153                                        0,                                // QNAME ends
154                                        0,    1,                          // QClass IN
155                                        0,    28,                         // QType AAAA
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
162     };
163
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);
170 }
171
172 const nlTest sTests[] = {
173     NL_TEST_DEF("IPV4", WriteIPv4), //
174     NL_TEST_DEF("IPV6", WriteIPv6), //
175     NL_TEST_SENTINEL()              //
176 };
177
178 } // namespace
179
180 int TestIPResourceRecord(void)
181 {
182
183     nlTestSuite theSuite = { "IPResourceRecord", sTests, nullptr, nullptr };
184     nlTestRunner(&theSuite, nullptr);
185
186     return (nlTestRunnerStats(&theSuite));
187 }
188
189 CHIP_REGISTER_TEST_SUITE(TestIPResourceRecord)