1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #ifdef GTEST_API_
19 #define private public
20 #define protected public
21 #endif
22
23 #include "dns_manager.h"
24 #include "dns_config_client.h"
25
26 namespace OHOS {
27 namespace nmd {
28 namespace {
29 using namespace testing::ext;
30 std::string INTERFACE_NAME = "interface_name";
31 std::string INFO = "info";
32 const uint16_t NET_ID = 2;
33 uint16_t BASE_TIMEOUT_MILLIS = 2000;
34 uint8_t RETRY_COUNT = 3;
35 } // namespace
36
37 class DnsManagerTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41 void SetUp();
42 void TearDown();
43 };
44
SetUpTestCase()45 void DnsManagerTest::SetUpTestCase() {}
46
TearDownTestCase()47 void DnsManagerTest::TearDownTestCase() {}
48
SetUp()49 void DnsManagerTest::SetUp() {}
50
TearDown()51 void DnsManagerTest::TearDown() {}
52
53 HWTEST_F(DnsManagerTest, InterfaceTest001, TestSize.Level1)
54 {
55 DnsManager dnsManager;
56 std::vector<std::string> servers;
57 std::vector<std::string> domains;
58 int32_t ret = dnsManager.SetResolverConfig(NET_ID, BASE_TIMEOUT_MILLIS, RETRY_COUNT, servers, domains);
59 EXPECT_NE(ret, 0);
60
61 ret = dnsManager.GetResolverConfig(NET_ID, servers, domains, BASE_TIMEOUT_MILLIS, RETRY_COUNT);
62 EXPECT_NE(ret, 0);
63
64 ret = dnsManager.CreateNetworkCache(NET_ID);
65 EXPECT_EQ(ret, 0);
66
67 dnsManager.SetDefaultNetwork(NET_ID);
68
69 dnsManager.ShareDnsSet(NET_ID);
70
71 dnsManager.StartDnsProxyListen();
72
73 dnsManager.StopDnsProxyListen();
74
75 dnsManager.GetDumpInfo(INFO);
76
77 ret = dnsManager.DestroyNetworkCache(NET_ID);
78 EXPECT_EQ(ret, 0);
79
80 std::string hostName = "www.baidu.com";
81 std::string serverName;
82 AddrInfo hints;
83 std::vector<AddrInfo> res;
84 dnsManager.GetAddrInfo(hostName, serverName, hints, NET_ID, res);
85 }
86
87 HWTEST_F(DnsManagerTest, EnableIpv6Test001, TestSize.Level1)
88 {
89 DnsManager dnsManager;
90 uint16_t netId = 123;
91 std::string destination = "123";
92 std::string nextHop = "";
93 dnsManager.EnableIpv6(netId, destination, nextHop);
94 EXPECT_EQ(destination, "123");
95 destination = "2001:db8:85a3::8a2e:370:7334";
96 dnsManager.EnableIpv6(netId, destination, nextHop);
97 nextHop = "2001:db8:85a3::8a2e:370:7335";
98 dnsManager.EnableIpv6(netId, destination, nextHop);
99 destination = "123";
100 dnsManager.EnableIpv6(netId, destination, nextHop);
101 destination = "2001:db8:85a3::8a2e:370:7334/64";
102 dnsManager.EnableIpv6(netId, destination, nextHop);
103 destination = "::/0";
104 dnsManager.EnableIpv6(netId, destination, nextHop);
105 EXPECT_FALSE(DnsParamCache::GetInstance().IsIpv6Enable(netId));
106 }
107
108 HWTEST_F(DnsManagerTest, GetAddrInfoTest001, TestSize.Level1)
109 {
110 DnsManager dnsManager;
111 std::string hostName;
112 std::string serverName = "www.baidu.com";
113 AddrInfo hints;
114 std::vector<AddrInfo> res;
115 auto result = dnsManager.GetAddrInfo(hostName, serverName, hints, NET_ID, res);
116 EXPECT_NE(result, -1);
117 }
118
119 HWTEST_F(DnsManagerTest, FillAddrInfoTest001, TestSize.Level1)
120 {
121 DnsManager dnsManager;
122 std::vector<AddrInfo> addrInfo;
123 addrinfo *res = nullptr;
124 auto result = dnsManager.FillAddrInfo(addrInfo, res);
125 EXPECT_EQ(result, 0);
126 }
127
128 HWTEST_F(DnsManagerTest, SetDnsCacheTest001, TestSize.Level1)
129 {
130 DnsManager dnsManager;
131 uint16_t netId = 101;
132 std::string testHost = "test";
133 AddrInfo info;
134 auto result = dnsManager.SetDnsCache(netId, testHost, info);
135 EXPECT_EQ(result, 0);
136 }
137 } // namespace nmd
138 } // namespace OHOS
139