Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / mdns / minimal / records / tests / TestResourceRecordTxt.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17 #include <mdns/minimal/records/Txt.h>
18
19 #include <support/UnitTestRegistration.h>
20
21 #include <nlunit-test.h>
22
23 namespace {
24
25 using namespace chip;
26 using namespace chip::Encoding;
27 using namespace mdns::Minimal;
28
29 void TestSrv(nlTestSuite * inSuite, void * inContext)
30 {
31     uint8_t headerBuffer[HeaderRef::kSizeBytes];
32     uint8_t dataBuffer[128];
33
34     const QNamePart kName[] = { "some", "test", "local" };
35     const char * kData[]    = { "a=b", "foo=bar", "flag" };
36
37     HeaderRef header(headerBuffer);
38
39     BigEndian::BufferWriter output(dataBuffer, sizeof(dataBuffer));
40
41     TxtResourceRecord record(kName, kData);
42     record.SetTtl(128);
43
44     header.Clear();
45
46     NL_TEST_ASSERT(inSuite, record.Append(header, ResourceType::kAdditional, output));
47     NL_TEST_ASSERT(inSuite, header.GetAnswerCount() == 0);
48     NL_TEST_ASSERT(inSuite, header.GetAuthorityCount() == 0);
49     NL_TEST_ASSERT(inSuite, header.GetAdditionalCount() == 1);
50
51     const uint8_t expectedOutput[] = {
52         4, 's', 'o', 'm', 'e',                // QNAME part: some
53         4, 't', 'e', 's', 't',                // QNAME part: test
54         5, 'l', 'o', 'c', 'a', 'l',           // QNAME part: local
55         0,                                    // QNAME ends
56         0, 16,                                // QType TXT
57         0, 1,                                 // QClass IN
58         0, 0,   0,   128,                     // TTL
59         0, 17,                                // data size
60         3, 'a', '=', 'b',                     // ENTRY: a=b
61         7, 'f', 'o', 'o', '=', 'b', 'a', 'r', // ENTRY: foo=bar
62         4, 'f', 'l', 'a', 'g'                 // ENTRY: flag
63     };
64
65     NL_TEST_ASSERT(inSuite, output.Needed() == sizeof(expectedOutput));
66     NL_TEST_ASSERT(inSuite, memcmp(dataBuffer, expectedOutput, sizeof(expectedOutput)) == 0);
67 }
68
69 const nlTest sTests[] = {
70     NL_TEST_DEF("TestSrv", TestSrv), //
71     NL_TEST_SENTINEL()               //
72 };
73
74 } // namespace
75
76 int TestSrv(void)
77 {
78     nlTestSuite theSuite = { "Srv", sTests, nullptr, nullptr };
79     nlTestRunner(&theSuite, nullptr);
80     return nlTestRunnerStats(&theSuite);
81 }
82
83 CHIP_REGISTER_TEST_SUITE(TestSrv)