1 /*
2 * Copyright (c) 2021, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "common/dns_utils.hpp"
30
31 #include <assert.h>
32
33 #include <CppUTest/TestHarness.h>
34
TEST_GROUP(DnsUtils)35 TEST_GROUP(DnsUtils){};
36
CheckSplitFullDnsName(const std::string & aFullName,bool aIsServiceInstance,bool aIsService,bool aIsHost,const std::string & aInstanceName,const std::string & aServiceName,const std::string & aHostName,const std::string & aDomain)37 static void CheckSplitFullDnsName(const std::string &aFullName,
38 bool aIsServiceInstance,
39 bool aIsService,
40 bool aIsHost,
41 const std::string &aInstanceName,
42 const std::string &aServiceName,
43 const std::string &aHostName,
44 const std::string &aDomain)
45 {
46 DnsNameInfo info;
47
48 assert(aFullName.empty() || aFullName.back() != '.');
49
50 info = SplitFullDnsName(aFullName);
51
52 CHECK_EQUAL(aIsServiceInstance, info.IsServiceInstance());
53 CHECK_EQUAL(aIsService, info.IsService());
54 CHECK_EQUAL(aIsHost, info.IsHost());
55 CHECK_EQUAL(aInstanceName, info.mInstanceName);
56 CHECK_EQUAL(aServiceName, info.mServiceName);
57 CHECK_EQUAL(aHostName, info.mHostName);
58 CHECK_EQUAL(aDomain, info.mDomain);
59
60 info = SplitFullDnsName(aFullName + ".");
61
62 CHECK_EQUAL(aIsServiceInstance, info.IsServiceInstance());
63 CHECK_EQUAL(aIsService, info.IsService());
64 CHECK_EQUAL(aIsHost, info.IsHost());
65 CHECK_EQUAL(aInstanceName, info.mInstanceName);
66 CHECK_EQUAL(aServiceName, info.mServiceName);
67 CHECK_EQUAL(aHostName, info.mHostName);
68 CHECK_EQUAL(aDomain, info.mDomain);
69 }
70
TEST(DnsUtils,TestSplitFullDnsName)71 TEST(DnsUtils, TestSplitFullDnsName)
72 {
73 // Check service instance names
74 CheckSplitFullDnsName("ins1._ipps._tcp.default.service.arpa", true, false, false, "ins1", "_ipps._tcp", "",
75 "default.service.arpa.");
76 CheckSplitFullDnsName("Instance Name._ipps._tcp.default.service.arpa", true, false, false, "Instance Name",
77 "_ipps._tcp", "", "default.service.arpa.");
78 CheckSplitFullDnsName("Instance.Name.With.Dots._ipps._tcp.default.service.arpa", true, false, false,
79 "Instance.Name.With.Dots", "_ipps._tcp", "", "default.service.arpa.");
80
81 // Check service names
82 CheckSplitFullDnsName("_ipps._tcp.default.service.arpa", false, true, false, "", "_ipps._tcp", "",
83 "default.service.arpa.");
84 CheckSplitFullDnsName("_meshcop._udp.default.service.arpa", false, true, false, "", "_meshcop._udp", "",
85 "default.service.arpa.");
86
87 // Check invalid service names
88 CheckSplitFullDnsName("_meshcop._abc.default.service.arpa", false, false, true, "", "", "_meshcop",
89 "_abc.default.service.arpa.");
90 CheckSplitFullDnsName("_tcp.default.service.arpa", false, false, true, "", "", "_tcp", "default.service.arpa.");
91
92 // Check host names
93 CheckSplitFullDnsName("abc.example.com", false, false, true, "", "", "abc", "example.com.");
94 CheckSplitFullDnsName("example.com", false, false, true, "", "", "example", "com.");
95 CheckSplitFullDnsName("com", false, false, true, "", "", "com", ".");
96 CheckSplitFullDnsName("", false, false, true, "", "", "", ".");
97 }
98