Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / mdns / minimal / core / tests / TestQName.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/core/QName.h>
20 #include <support/UnitTestRegistration.h>
21
22 #include <nlunit-test.h>
23
24 namespace {
25
26 using namespace mdns::Minimal;
27
28 void IteratorTest(nlTestSuite * inSuite, void * inContext)
29 {
30     {
31         static const uint8_t kOneItem[] = "\04test\00";
32         SerializedQNameIterator it(BytesRange(kOneItem, kOneItem + sizeof(kOneItem)), kOneItem);
33
34         NL_TEST_ASSERT(inSuite, it.Next());
35         NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "test") == 0);
36         NL_TEST_ASSERT(inSuite, !it.Next());
37         NL_TEST_ASSERT(inSuite, it.IsValid());
38     }
39
40     {
41         static const uint8_t kManyItems[] = "\04this\02is\01a\04test\00";
42         SerializedQNameIterator it(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems);
43
44         NL_TEST_ASSERT(inSuite, it.Next());
45         NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "this") == 0);
46
47         NL_TEST_ASSERT(inSuite, it.Next());
48         NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "is") == 0);
49
50         NL_TEST_ASSERT(inSuite, it.Next());
51         NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "a") == 0);
52
53         NL_TEST_ASSERT(inSuite, it.Next());
54         NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "test") == 0);
55
56         NL_TEST_ASSERT(inSuite, !it.Next());
57         NL_TEST_ASSERT(inSuite, it.IsValid());
58     }
59     {
60         static const uint8_t kPtrItems[] = "abc\02is\01a\04test\00\04this\xc0\03";
61         SerializedQNameIterator it(BytesRange(kPtrItems, kPtrItems + sizeof(kPtrItems)), kPtrItems + 14);
62
63         NL_TEST_ASSERT(inSuite, it.Next());
64         NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "this") == 0);
65
66         NL_TEST_ASSERT(inSuite, it.Next());
67         NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "is") == 0);
68
69         NL_TEST_ASSERT(inSuite, it.Next());
70         NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "a") == 0);
71
72         NL_TEST_ASSERT(inSuite, it.Next());
73         NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "test") == 0);
74
75         NL_TEST_ASSERT(inSuite, !it.Next());
76         NL_TEST_ASSERT(inSuite, it.IsValid());
77     }
78 }
79
80 void ErrorTest(nlTestSuite * inSuite, void * inContext)
81 {
82     {
83         // Truncated before the end
84         static const uint8_t kData[] = "\04test";
85         SerializedQNameIterator it(BytesRange(kData, kData + 5), kData);
86
87         NL_TEST_ASSERT(inSuite, !it.Next());
88         NL_TEST_ASSERT(inSuite, !it.IsValid());
89     }
90
91     {
92         // Truncated before the end
93         static const uint8_t kData[] = "\02";
94         SerializedQNameIterator it(BytesRange(kData, kData + 1), kData);
95
96         NL_TEST_ASSERT(inSuite, !it.Next());
97         NL_TEST_ASSERT(inSuite, !it.IsValid());
98     }
99
100     {
101         // Truncated before the end
102         static const uint8_t kData[] = "\xc0";
103         SerializedQNameIterator it(BytesRange(kData, kData + 1), kData);
104
105         NL_TEST_ASSERT(inSuite, !it.Next());
106         NL_TEST_ASSERT(inSuite, !it.IsValid());
107     }
108
109     {
110         // Truncated before the end (but seemingly valid in case of error)
111         static const uint8_t kData[] = "\00\xc0\x00";
112         SerializedQNameIterator it(BytesRange(kData, kData + 2), kData + 1);
113
114         NL_TEST_ASSERT(inSuite, !it.Next());
115         NL_TEST_ASSERT(inSuite, !it.IsValid());
116     }
117     {
118         // Infinite recursion
119         static const uint8_t kData[] = "\03test\xc0\x00";
120         SerializedQNameIterator it(BytesRange(kData, kData + 7), kData);
121
122         NL_TEST_ASSERT(inSuite, it.Next());
123         NL_TEST_ASSERT(inSuite, !it.Next());
124         NL_TEST_ASSERT(inSuite, !it.IsValid());
125     }
126 }
127
128 void Comparison(nlTestSuite * inSuite, void * inContext)
129 {
130     static const uint8_t kManyItems[] = "\04this\02is\01a\04test\00";
131
132     {
133         const QNamePart kTestName[] = { "this" };
134         NL_TEST_ASSERT(inSuite,
135                        SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) !=
136                            FullQName(kTestName));
137     }
138
139     {
140         const QNamePart kTestName[] = { "this", "is" };
141         NL_TEST_ASSERT(inSuite,
142                        SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) !=
143                            FullQName(kTestName));
144     }
145
146     {
147         const QNamePart kTestName[] = { "is", "a", "test" };
148         NL_TEST_ASSERT(inSuite,
149                        SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) !=
150                            FullQName(kTestName));
151     }
152
153     {
154         const QNamePart kTestName[] = { "this", "is", "a", "test" };
155         NL_TEST_ASSERT(inSuite,
156                        SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) ==
157                            FullQName(kTestName));
158     }
159
160     {
161         const QNamePart kTestName[] = { "this", "is", "a", "test", "suffix" };
162         NL_TEST_ASSERT(inSuite,
163                        SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) !=
164                            FullQName(kTestName));
165     }
166
167     {
168         const QNamePart kTestName[] = { "prefix", "this", "is", "a", "test" };
169         NL_TEST_ASSERT(inSuite,
170                        SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) !=
171                            FullQName(kTestName));
172     }
173 }
174
175 void CaseInsensitiveSerializedCompare(nlTestSuite * inSuite, void * inContext)
176 {
177     static const uint8_t kManyItems[] = "\04thIs\02iS\01a\04tEst\00";
178
179     {
180         const QNamePart kTestName[] = { "this", "is", "a", "test" };
181         NL_TEST_ASSERT(inSuite,
182                        SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) ==
183                            FullQName(kTestName));
184     }
185
186     {
187         const QNamePart kTestName[] = { "THIS", "IS", "A", "test" };
188         NL_TEST_ASSERT(inSuite,
189                        SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) ==
190                            FullQName(kTestName));
191     }
192
193     {
194         const QNamePart kTestName[] = { "THIS", "IS", "A", "TEST" };
195         NL_TEST_ASSERT(inSuite,
196                        SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) ==
197                            FullQName(kTestName));
198     }
199 }
200
201 void CaseInsensitiveFullQNameCompare(nlTestSuite * inSuite, void * inContext)
202 {
203     {
204         const QNamePart kName1[] = { "this", "is", "a", "test" };
205         const QNamePart kName2[] = { "this", "IS", "a", "TEST" };
206         NL_TEST_ASSERT(inSuite, FullQName(kName1) == FullQName(kName2));
207     }
208
209     {
210         const QNamePart kName1[] = { "THIS", "IS", "a", "tesT" };
211         const QNamePart kName2[] = { "this", "IS", "A", "TEst" };
212         NL_TEST_ASSERT(inSuite, FullQName(kName1) == FullQName(kName2));
213     }
214
215     {
216         const QNamePart kName1[] = { "THIS", "IS", "a", "test" };
217         const QNamePart kName2[] = { "this", "IS", "A", "NEST" };
218         NL_TEST_ASSERT(inSuite, FullQName(kName1) != FullQName(kName2));
219     }
220
221     {
222         const QNamePart kName1[] = { "THIS", "IS", "a" };
223         const QNamePart kName2[] = { "this", "IS", "A", "NEST" };
224         NL_TEST_ASSERT(inSuite, FullQName(kName1) != FullQName(kName2));
225     }
226
227     {
228         const QNamePart kName1[] = { "THIS", "IS", "a" };
229         const QNamePart kName2[] = { "this", "IS" };
230         NL_TEST_ASSERT(inSuite, FullQName(kName1) != FullQName(kName2));
231     }
232
233     {
234         const QNamePart kName[] = { "this" };
235         NL_TEST_ASSERT(inSuite, FullQName() != FullQName(kName));
236         NL_TEST_ASSERT(inSuite, FullQName(kName) != FullQName());
237     }
238 }
239
240 } // namespace
241
242 // clang-format off
243 static const nlTest sTests[] =
244 {
245     NL_TEST_DEF("IteratorTest", IteratorTest),
246     NL_TEST_DEF("ErrorTest", ErrorTest),
247     NL_TEST_DEF("Comparison", Comparison),
248     NL_TEST_DEF("CaseInsensitiveSerializedCompare", CaseInsensitiveSerializedCompare),
249     NL_TEST_DEF("CaseInsensitiveFullQNameCompare", CaseInsensitiveFullQNameCompare),
250
251     NL_TEST_SENTINEL()
252 };
253 // clang-format on
254
255 int TestQName(void)
256 {
257     // clang-format off
258     nlTestSuite theSuite =
259         {
260         "QName",
261         &sTests[0],
262         nullptr,
263         nullptr
264     };
265     // clang-format on
266
267     nlTestRunner(&theSuite, nullptr);
268
269     return (nlTestRunnerStats(&theSuite));
270 }
271
272 CHIP_REGISTER_TEST_SUITE(TestQName)