1 /*
2  * Copyright (c) 2021- 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 #include <dlfcn.h>
19 
20 #include "dns_config_client.h"
21 #include "netnative_log_wrapper.h"
22 #include "netsys_client.h"
23 #include "netsys_native_service_proxy.h"
24 
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27 
28 using SetCache = int32_t (*)(uint16_t netId, struct ParamWrapper param, struct addrinfo *res);
29 using GetCache = int32_t (*)(uint16_t netId, struct ParamWrapper param,
30                             struct AddrInfo addr_info[MAX_RESULTS],
31                              uint32_t *num);
32 using GetConfig = int32_t (*)(uint16_t netId, struct ResolvConfig *config);
33 
34 namespace OHOS {
35 namespace NetsysNative {
36 using namespace testing::ext;
37 namespace {
ConnManagerGetProxy()38 sptr<NetsysNative::INetsysService> ConnManagerGetProxy()
39 {
40     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
41     if (samgr == nullptr) {
42         return nullptr;
43     }
44 
45     auto remote = samgr->GetSystemAbility(COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
46     if (remote == nullptr) {
47         return nullptr;
48     }
49 
50     auto proxy = iface_cast<NetsysNative::INetsysService>(remote);
51     if (proxy == nullptr) {
52         return nullptr;
53     }
54     return proxy;
55 }
56 } // namespace
57 class ResolverConfigTest : public testing::Test {
58 public:
59     static void SetUpTestCase();
60     static void TearDownTestCase();
61     void SetUp();
62     void TearDown();
63 };
64 
SetUpTestCase()65 void ResolverConfigTest::SetUpTestCase() {}
66 
TearDownTestCase()67 void ResolverConfigTest::TearDownTestCase() {}
68 
SetUp()69 void ResolverConfigTest::SetUp() {}
70 
TearDown()71 void ResolverConfigTest::TearDown() {}
72 
73 HWTEST_F(ResolverConfigTest, ResolverConfigTest001, TestSize.Level1)
74 {
75     OHOS::sptr<OHOS::NetsysNative::INetsysService> netsysNativeService = ConnManagerGetProxy();
76     ASSERT_NE(netsysNativeService, nullptr);
77 
78     int32_t ret = 0;
79     ret = netsysNativeService->CreateNetworkCache(0);
80     NETNATIVE_LOGE("NETSYS: CreateNetworkCache0   ret=%{public}d", ret);
81     NETNATIVE_LOGE("NETSYS: SetResolverConfig0   ret=%{public}d", ret);
82     NETNATIVE_LOGE("ResolverConfigTest001 ResolverConfigTest001 ResolverConfigTest001");
83     EXPECT_EQ(ret, 0);
84 }
85 
86 HWTEST_F(ResolverConfigTest, ResolverConfigTest002, TestSize.Level1)
87 {
88     OHOS::sptr<OHOS::NetsysNative::INetsysService> netsysNativeService = ConnManagerGetProxy();
89     ASSERT_NE(netsysNativeService, nullptr);
90 
91     int32_t ret = 0;
92     ret = netsysNativeService->CreateNetworkCache(0);
93     NETNATIVE_LOGE("ResolverConfigTest002 ResolverConfigTest002 ResolverConfigTest002");
94     EXPECT_EQ(ret, 0);
95 }
96 
97 HWTEST_F(ResolverConfigTest, ResolverConfigTest003, TestSize.Level1)
98 {
99     OHOS::sptr<OHOS::NetsysNative::INetsysService> netsysNativeService = ConnManagerGetProxy();
100     ASSERT_NE(netsysNativeService, nullptr);
101 
102     int32_t ret = 0;
103     ret = netsysNativeService->CreateNetworkCache(0);
104     NETNATIVE_LOGE("ResolverConfigTest003 ResolverConfigTest003 ResolverConfigTest003");
105     EXPECT_EQ(ret, 0);
106 }
107 
108 HWTEST_F(ResolverConfigTest, ResolverConfigTest004, TestSize.Level1)
109 {
110     NETNATIVE_LOGI("ResolverConfigTest004 enter");
111     OHOS::sptr<OHOS::NetsysNative::INetsysService> netsysNativeService = ConnManagerGetProxy();
112     ASSERT_NE(netsysNativeService, nullptr);
113 
114     uint16_t netId = 1;
115     uint16_t baseTimeoutMsec = 5000;
116     uint8_t retryCount = 3;
117     std::vector<std::string> servers = {"114.114.114.114", "8.8.8.8"};
118     std::vector<std::string> domains = {"yahoo.com", "baidu.com"};
119 
120     uint16_t getBaseTimeoutMsec;
121     uint8_t getRetryCount;
122     std::vector<std::string> getServers;
123     std::vector<std::string> getDomains;
124 
125     int res = netsysNativeService->CreateNetworkCache(netId);
126     res = netsysNativeService->SetResolverConfig(netId, baseTimeoutMsec, retryCount, servers, domains);
127     res = netsysNativeService->GetResolverConfig(netId, getServers, getDomains, getBaseTimeoutMsec,
128                                                  getRetryCount);
129     EXPECT_EQ(getBaseTimeoutMsec, baseTimeoutMsec);
130     EXPECT_EQ(getRetryCount, retryCount);
131     int serversNum = getServers.size() < servers.size() ? getServers.size() : servers.size();
132     for (int i = 0; i < serversNum; i++) {
133         EXPECT_EQ(servers[i], getServers[i]);
134     }
135     int domainsNum = getDomains.size() < domains.size() ? getDomains.size() : domains.size();
136     for (int i = 0; i < domainsNum; i++) {
137         EXPECT_EQ(domains[i], getDomains[i]);
138     }
139     res = netsysNativeService->DestroyNetworkCache(netId);
140 }
141 } // namespace NetsysNative
142 } // namespace OHOS
143