3 * Copyright (c) 2020 Project CHIP Authors
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <mdns/minimal/records/Txt.h>
19 #include <support/UnitTestRegistration.h>
21 #include <nlunit-test.h>
26 using namespace chip::Encoding;
27 using namespace mdns::Minimal;
29 void TestSrv(nlTestSuite * inSuite, void * inContext)
31 uint8_t headerBuffer[HeaderRef::kSizeBytes];
32 uint8_t dataBuffer[128];
34 const QNamePart kName[] = { "some", "test", "local" };
35 const char * kData[] = { "a=b", "foo=bar", "flag" };
37 HeaderRef header(headerBuffer);
39 BigEndian::BufferWriter output(dataBuffer, sizeof(dataBuffer));
41 TxtResourceRecord record(kName, kData);
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);
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
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
65 NL_TEST_ASSERT(inSuite, output.Needed() == sizeof(expectedOutput));
66 NL_TEST_ASSERT(inSuite, memcmp(dataBuffer, expectedOutput, sizeof(expectedOutput)) == 0);
69 const nlTest sTests[] = {
70 NL_TEST_DEF("TestSrv", TestSrv), //
78 nlTestSuite theSuite = { "Srv", sTests, nullptr, nullptr };
79 nlTestRunner(&theSuite, nullptr);
80 return nlTestRunnerStats(&theSuite);
83 CHIP_REGISTER_TEST_SUITE(TestSrv)