ab9e1b45035fd7b18898c9760a4fb53b2304d214
[platform/upstream/connectedhomeip.git] / src / lib / mdns / minimal / 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/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 } // namespace
129
130 // clang-format off
131 static const nlTest sTests[] =
132 {
133     NL_TEST_DEF("IteratorTest", IteratorTest),
134     NL_TEST_DEF("ErrorTest", ErrorTest),
135
136     NL_TEST_SENTINEL()
137 };
138 // clang-format on
139
140 int TestQName(void)
141 {
142     // clang-format off
143     nlTestSuite theSuite =
144         {
145         "QName",
146         &sTests[0],
147         nullptr,
148         nullptr
149     };
150     // clang-format on
151
152     nlTestRunner(&theSuite, nullptr);
153
154     return (nlTestRunnerStats(&theSuite));
155 }
156
157 CHIP_REGISTER_TEST_SUITE(TestQName)