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/core/QName.h>
20 #include <support/UnitTestRegistration.h>
22 #include <nlunit-test.h>
26 using namespace mdns::Minimal;
28 void IteratorTest(nlTestSuite * inSuite, void * inContext)
31 static const uint8_t kOneItem[] = "\04test\00";
32 SerializedQNameIterator it(BytesRange(kOneItem, kOneItem + sizeof(kOneItem)), kOneItem);
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());
41 static const uint8_t kManyItems[] = "\04this\02is\01a\04test\00";
42 SerializedQNameIterator it(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems);
44 NL_TEST_ASSERT(inSuite, it.Next());
45 NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "this") == 0);
47 NL_TEST_ASSERT(inSuite, it.Next());
48 NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "is") == 0);
50 NL_TEST_ASSERT(inSuite, it.Next());
51 NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "a") == 0);
53 NL_TEST_ASSERT(inSuite, it.Next());
54 NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "test") == 0);
56 NL_TEST_ASSERT(inSuite, !it.Next());
57 NL_TEST_ASSERT(inSuite, it.IsValid());
60 static const uint8_t kPtrItems[] = "abc\02is\01a\04test\00\04this\xc0\03";
61 SerializedQNameIterator it(BytesRange(kPtrItems, kPtrItems + sizeof(kPtrItems)), kPtrItems + 14);
63 NL_TEST_ASSERT(inSuite, it.Next());
64 NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "this") == 0);
66 NL_TEST_ASSERT(inSuite, it.Next());
67 NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "is") == 0);
69 NL_TEST_ASSERT(inSuite, it.Next());
70 NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "a") == 0);
72 NL_TEST_ASSERT(inSuite, it.Next());
73 NL_TEST_ASSERT(inSuite, strcmp(it.Value(), "test") == 0);
75 NL_TEST_ASSERT(inSuite, !it.Next());
76 NL_TEST_ASSERT(inSuite, it.IsValid());
80 void ErrorTest(nlTestSuite * inSuite, void * inContext)
83 // Truncated before the end
84 static const uint8_t kData[] = "\04test";
85 SerializedQNameIterator it(BytesRange(kData, kData + 5), kData);
87 NL_TEST_ASSERT(inSuite, !it.Next());
88 NL_TEST_ASSERT(inSuite, !it.IsValid());
92 // Truncated before the end
93 static const uint8_t kData[] = "\02";
94 SerializedQNameIterator it(BytesRange(kData, kData + 1), kData);
96 NL_TEST_ASSERT(inSuite, !it.Next());
97 NL_TEST_ASSERT(inSuite, !it.IsValid());
101 // Truncated before the end
102 static const uint8_t kData[] = "\xc0";
103 SerializedQNameIterator it(BytesRange(kData, kData + 1), kData);
105 NL_TEST_ASSERT(inSuite, !it.Next());
106 NL_TEST_ASSERT(inSuite, !it.IsValid());
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);
114 NL_TEST_ASSERT(inSuite, !it.Next());
115 NL_TEST_ASSERT(inSuite, !it.IsValid());
118 // Infinite recursion
119 static const uint8_t kData[] = "\03test\xc0\x00";
120 SerializedQNameIterator it(BytesRange(kData, kData + 7), kData);
122 NL_TEST_ASSERT(inSuite, it.Next());
123 NL_TEST_ASSERT(inSuite, !it.Next());
124 NL_TEST_ASSERT(inSuite, !it.IsValid());
128 void Comparison(nlTestSuite * inSuite, void * inContext)
130 static const uint8_t kManyItems[] = "\04this\02is\01a\04test\00";
133 const QNamePart kTestName[] = { "this" };
134 NL_TEST_ASSERT(inSuite,
135 SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) !=
136 FullQName(kTestName));
140 const QNamePart kTestName[] = { "this", "is" };
141 NL_TEST_ASSERT(inSuite,
142 SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) !=
143 FullQName(kTestName));
147 const QNamePart kTestName[] = { "is", "a", "test" };
148 NL_TEST_ASSERT(inSuite,
149 SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) !=
150 FullQName(kTestName));
154 const QNamePart kTestName[] = { "this", "is", "a", "test" };
155 NL_TEST_ASSERT(inSuite,
156 SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) ==
157 FullQName(kTestName));
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));
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));
175 void CaseInsensitiveSerializedCompare(nlTestSuite * inSuite, void * inContext)
177 static const uint8_t kManyItems[] = "\04thIs\02iS\01a\04tEst\00";
180 const QNamePart kTestName[] = { "this", "is", "a", "test" };
181 NL_TEST_ASSERT(inSuite,
182 SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) ==
183 FullQName(kTestName));
187 const QNamePart kTestName[] = { "THIS", "IS", "A", "test" };
188 NL_TEST_ASSERT(inSuite,
189 SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) ==
190 FullQName(kTestName));
194 const QNamePart kTestName[] = { "THIS", "IS", "A", "TEST" };
195 NL_TEST_ASSERT(inSuite,
196 SerializedQNameIterator(BytesRange(kManyItems, kManyItems + sizeof(kManyItems)), kManyItems) ==
197 FullQName(kTestName));
201 void CaseInsensitiveFullQNameCompare(nlTestSuite * inSuite, void * inContext)
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));
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));
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));
222 const QNamePart kName1[] = { "THIS", "IS", "a" };
223 const QNamePart kName2[] = { "this", "IS", "A", "NEST" };
224 NL_TEST_ASSERT(inSuite, FullQName(kName1) != FullQName(kName2));
228 const QNamePart kName1[] = { "THIS", "IS", "a" };
229 const QNamePart kName2[] = { "this", "IS" };
230 NL_TEST_ASSERT(inSuite, FullQName(kName1) != FullQName(kName2));
234 const QNamePart kName[] = { "this" };
235 NL_TEST_ASSERT(inSuite, FullQName() != FullQName(kName));
236 NL_TEST_ASSERT(inSuite, FullQName(kName) != FullQName());
243 static const nlTest sTests[] =
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),
258 nlTestSuite theSuite =
267 nlTestRunner(&theSuite, nullptr);
269 return (nlTestRunnerStats(&theSuite));
272 CHIP_REGISTER_TEST_SUITE(TestQName)