xref: /aosp_15_r20/external/ot-br-posix/tests/gtest/test_dns_utils.cpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
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 #include <gtest/gtest.h>
33 
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)34 static void CheckSplitFullDnsName(const std::string &aFullName,
35                                   bool               aIsServiceInstance,
36                                   bool               aIsService,
37                                   bool               aIsHost,
38                                   const std::string &aInstanceName,
39                                   const std::string &aServiceName,
40                                   const std::string &aHostName,
41                                   const std::string &aDomain)
42 {
43     DnsNameInfo info;
44 
45     assert(aFullName.empty() || aFullName.back() != '.');
46 
47     info = SplitFullDnsName(aFullName);
48 
49     EXPECT_EQ(aIsServiceInstance, info.IsServiceInstance());
50     EXPECT_EQ(aIsService, info.IsService());
51     EXPECT_EQ(aIsHost, info.IsHost());
52     EXPECT_EQ(aInstanceName, info.mInstanceName);
53     EXPECT_EQ(aServiceName, info.mServiceName);
54     EXPECT_EQ(aHostName, info.mHostName);
55     EXPECT_EQ(aDomain, info.mDomain);
56 
57     info = SplitFullDnsName(aFullName + ".");
58 
59     EXPECT_EQ(aIsServiceInstance, info.IsServiceInstance());
60     EXPECT_EQ(aIsService, info.IsService());
61     EXPECT_EQ(aIsHost, info.IsHost());
62     EXPECT_EQ(aInstanceName, info.mInstanceName);
63     EXPECT_EQ(aServiceName, info.mServiceName);
64     EXPECT_EQ(aHostName, info.mHostName);
65     EXPECT_EQ(aDomain, info.mDomain);
66 }
67 
TEST(DnsUtils,TestSplitFullDnsName)68 TEST(DnsUtils, TestSplitFullDnsName)
69 {
70     // Check service instance names
71     CheckSplitFullDnsName("ins1._ipps._tcp.default.service.arpa", true, false, false, "ins1", "_ipps._tcp", "",
72                           "default.service.arpa.");
73     CheckSplitFullDnsName("Instance Name._ipps._tcp.default.service.arpa", true, false, false, "Instance Name",
74                           "_ipps._tcp", "", "default.service.arpa.");
75     CheckSplitFullDnsName("Instance.Name.With.Dots._ipps._tcp.default.service.arpa", true, false, false,
76                           "Instance.Name.With.Dots", "_ipps._tcp", "", "default.service.arpa.");
77 
78     // Check service names
79     CheckSplitFullDnsName("_ipps._tcp.default.service.arpa", false, true, false, "", "_ipps._tcp", "",
80                           "default.service.arpa.");
81     CheckSplitFullDnsName("_meshcop._udp.default.service.arpa", false, true, false, "", "_meshcop._udp", "",
82                           "default.service.arpa.");
83 
84     // Check invalid service names
85     CheckSplitFullDnsName("_meshcop._abc.default.service.arpa", false, false, true, "", "", "_meshcop",
86                           "_abc.default.service.arpa.");
87     CheckSplitFullDnsName("_tcp.default.service.arpa", false, false, true, "", "", "_tcp", "default.service.arpa.");
88 
89     // Check host names
90     CheckSplitFullDnsName("abc.example.com", false, false, true, "", "", "abc", "example.com.");
91     CheckSplitFullDnsName("example.com", false, false, true, "", "", "example", "com.");
92     CheckSplitFullDnsName("com", false, false, true, "", "", "com", ".");
93     CheckSplitFullDnsName("", false, false, true, "", "", "", ".");
94 }
95