1 /*
2 * Copyright (c) 2021 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 #include <iostream>
16 #include <memory>
17 #include <securec.h>
18
19 #include "i_netsys_service.h"
20 #include "iservice_registry.h"
21 #include "netsys_native_service_proxy.h"
22 #include "netnative_log_wrapper.h"
23 #include "system_ability_definition.h"
24 #include "dnsresolv.h"
25 #include "test.h"
26 #include "test_notify_callback.h"
27 using namespace OHOS::nmd;
28 using namespace OHOS;
29 using namespace OHOS::NetsysNative;
30
31 namespace {
32 OHOS::sptr<OHOS::NetsysNative::INotifyCallback> callback_ =
33 (std::make_unique<OHOS::NetsysNative::TestNotifyCallback>()).release();
GetProxyK()34 sptr<INetsysService> GetProxyK()
35 {
36 NETNATIVE_LOGE("Get samgr >>>>>>>>>>>>>>>>>>>>>>>>>>");
37 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
38 NETNATIVE_LOGE("Get samgr %{public}p", samgr.GetRefPtr());
39 std::cout << "Get samgr "<< samgr.GetRefPtr() << std::endl;
40
41 auto remote = samgr->GetSystemAbility(COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
42 NETNATIVE_LOGE("Get remote %{public}p", remote.GetRefPtr());
43 std::cout << "Get remote "<< remote.GetRefPtr() << std::endl;
44
45 auto proxy = iface_cast<NetsysNative::INetsysService>(remote);
46 if (proxy != nullptr) {
47 NETNATIVE_LOGE("Get proxy %{public}p", proxy.GetRefPtr());
48 std::cout << "Get proxy " << proxy.GetRefPtr() << std::endl;
49 } else {
50 std::cout << "Get proxy nullptr" << std::endl;
51 }
52
53 return proxy;
54 }
55
56 auto netsysServiceK_ = GetProxyK();
57 }
58
59 namespace {
freeNetsysAddrInfo(struct addrinfo * aihead)60 void freeNetsysAddrInfo(struct addrinfo *aihead)
61 {
62 struct addrinfo *ai, *ainext ;
63 for (ai = aihead ; ai != nullptr ; ai = ainext) {
64 if (ai->ai_addr != nullptr)
65 free(ai->ai_addr);
66
67 if (ai->ai_canonname != nullptr)
68 free(ai->ai_canonname) ;
69 ainext = ai->ai_next ;
70 free(ai);
71 }
72 }
73 }
74
TestSetResolverConfig()75 void TestSetResolverConfig()
76 {
77 nmd::DnsresolverParams param0 = {
78 0, 0, 1, {"8.8.8.8", "114.114.114.114"}, {"baidu.com", "sohu.com"}};
79 nmd::DnsresolverParams param1 = {
80 1, 0, 1, {"8.8.8.8", "114.114.114.114"}, {"yahoo.com", "163.com"}};
81 int ret = -1;
82 ret = netsysServiceK_->CreateNetworkCache(0);
83 NETNATIVE_LOGE("NETSYS: CreateNetworkCache0 ret=%{public}d", ret);
84 ret = netsysServiceK_->CreateNetworkCache(1);
85 NETNATIVE_LOGE("NETSYS: CreateNetworkCache1 ret=%{public}d", ret);
86 ret = netsysServiceK_->SetResolverConfig(param0);
87 NETNATIVE_LOGE("NETSYS: SetResolverConfig0 ret=%{public}d", ret);
88 ret = netsysServiceK_->SetResolverConfig(param1);
89 NETNATIVE_LOGE("NETSYS: SetResolverConfig1 ret=%{public}d", ret);
90 }
91
TestGetResolverConfig()92 void TestGetResolverConfig()
93 {
94 int ret = -1;
95 std::vector<std::string> servers;
96 std::vector<std::string> domains;
97 DnsResParams dnsParam;
98 int num = 3;
99 for (int i = 0; i < num; i++) {
100 ret = netsysServiceK_->GetResolverConfig(i, servers, domains, dnsParam);
101 NETNATIVE_LOGE("NETSYS: getResolverConfig ret=%{public}d, iii=%{public}d", ret, i);
102 NETNATIVE_LOGE("NETSYS: server size %{public}d, domains size %{public}d",
103 static_cast<int32_t>(servers.size()), static_cast<int32_t>(domains.size()));
104 NETNATIVE_LOGE("NETSYS: TimeOut %{public}d, retryCount %{public}d", dnsParam.baseTimeoutMsec,
105 dnsParam.retryCount);
106 for (auto item:servers) {
107 std::cout<<"Server:" <<item.c_str()<<std::endl;
108 }
109 for (auto iter:domains) {
110 std::cout<<"Domains:" <<iter.c_str()<<std::endl;
111 }
112 }
113 }
114
TestInterfaceGetCfg()115 void TestInterfaceGetCfg()
116 {
117 std::cout<<"hello"<<std::endl;
118 }
119
TestCreateNetworkCache()120 void TestCreateNetworkCache()
121 {
122 int ret = -1;
123 ret = netsysServiceK_->CreateNetworkCache(0);
124 NETNATIVE_LOGE("NETSYS: CreateNetworkCache0 ret=%{public}d", ret);
125 ret = netsysServiceK_->CreateNetworkCache(1);
126 NETNATIVE_LOGE("NETSYS: CreateNetworkCache1 ret=%{public}d", ret);
127 }
128
TestFlushNetworkCache()129 void TestFlushNetworkCache()
130 {
131 int ret = -1 ;
132 ret = netsysServiceK_->FlushNetworkCache(0);
133 NETNATIVE_LOGE("NETSYS: FlushNetworkCache0 ret=%{public}d", ret);
134 ret = netsysServiceK_->FlushNetworkCache(1);
135 NETNATIVE_LOGE("NETSYS: FlushNetworkCache1 ret=%{public}d", ret);
136 }
137
TestDestroyNetworkCache()138 void TestDestroyNetworkCache()
139 {
140 int ret = -1;
141 ret = netsysServiceK_->DestroyNetworkCache(1);
142 NETNATIVE_LOGE("NETSYS: DestroyNetworkCache1 ret=%{public}d", ret);
143 }
144
TestGetaddrinfo()145 void TestGetaddrinfo()
146 {
147 char hostName[OHOS::nmd::MAX_NAME_LEN];
148 struct addrinfo hints;
149 struct addrinfo *res1;
150 struct addrinfo *resP1;
151 int ret;
152 bzero(&hints, sizeof(addrinfo));
153 hints.ai_family = AF_INET;
154 hints.ai_socktype = SOCK_DGRAM;
155 hints.ai_flags = AI_PASSIVE;
156 hints.ai_protocol = 0;
157 int copyRet = strncpy_s(hostName, OHOS::nmd::MAX_NAME_LEN, "www.ifeng.com", strlen("www.ifeng.com") + 1);
158 NETNATIVE_LOGI("copyRet = %{public}d", copyRet);
159 ret = netsysServiceK_->Getaddrinfo(hostName, NULL, &hints, &res1, 0);
160 NETNATIVE_LOGE("NETSYS: Getaddrinfo ret=%{public}d,HostName %{public}s", ret, hostName);
161 if (res1 == nullptr) {
162 NETNATIVE_LOGE("res1 res1 is nullptr");
163 }
164
165 int k = 0;
166 for (resP1 = res1; resP1 != nullptr; resP1 = resP1->ai_next) {
167 char host[1024] = {0};
168 NETNATIVE_LOGE("LOOP kkkk start k=%{public}d \n", k);
169 NETNATIVE_LOGE("LOOP ai_addrlen %{public}d\n", resP1->ai_addrlen);
170 k++ ;
171 ret = getnameinfo(resP1->ai_addr, resP1->ai_addrlen, host, sizeof(host), NULL, 0, NI_NUMERICHOST);
172 if (ret != 0) {
173 NETNATIVE_LOGE("getnameinfo: %{public}s\n", gai_strerror(ret));
174 } else {
175 NETNATIVE_LOGE("IPIP1: %{public}s,HOSTNAME %{public}s", host, hostName);
176 }
177 }
178
179 printf("overover\n");
180 if (res1 != nullptr) {
181 NETNATIVE_LOGI("TestGetaddrinfo:: end freeNetsysAddrInfo");
182 freeNetsysAddrInfo(res1);
183 }
184 }
185
TestInterfaceSetMtu()186 void TestInterfaceSetMtu()
187 {
188 int ret = -1;
189 std::string ifName = "eth0";
190 int mtu = 1200;
191 std::cout<<"begin to GetMtu"<<std::endl;
192 ret = netsysServiceK_->InterfaceGetMtu(ifName);
193 NETNATIVE_LOGE("NETSYS: GetMtu ago ret=%{public}d", ret);
194 std::cout<<"begin to SetMtu"<<std::endl;
195 ret = netsysServiceK_->InterfaceSetMtu(ifName, mtu);
196 NETNATIVE_LOGE("NETSYS: SetMtu ret=%{public}d", ret);
197 std::cout<<"begin22 to GetMtu"<<std::endl;
198 ret = netsysServiceK_->InterfaceGetMtu(ifName);
199 NETNATIVE_LOGE("NETSYS: GetMtu ret=%{public}d", ret);
200 }
201
TestInterfaceGetMtu()202 void TestInterfaceGetMtu()
203 {
204 int ret = -1;
205 std::string ifName = "eth0";
206 int mtu = 1200;
207 std::cout<<"begin to GetMtu"<<std::endl;
208 ret = netsysServiceK_->InterfaceGetMtu(ifName);
209 NETNATIVE_LOGE("NETSYS: GetMtu ago ret=%{public}d", ret);
210 std::cout<<"begin to SetMtu"<<std::endl;
211 ret = netsysServiceK_->InterfaceSetMtu(ifName, mtu);
212 NETNATIVE_LOGE("NETSYS: SetMtu ret=%{public}d", ret);
213 std::cout<<"begin22 to GetMtu"<<std::endl;
214 ret = netsysServiceK_->InterfaceGetMtu(ifName);
215 NETNATIVE_LOGE("NETSYS: GetMtu ret=%{public}d", ret);
216 }
217
TestRegisterNotifyCallback()218 void TestRegisterNotifyCallback()
219 {
220 if (netsysServiceK_ == nullptr || callback_ == nullptr) {
221 std::cout << "TestRegisterNotifyCallback netsysServiceK_ or callback is nullptr" << std::endl;
222 return;
223 }
224 std::cout << "enter TestRegisterNotifyCallback " << std::endl;
225 int32_t ret = netsysServiceK_->RegisterNotifyCallback(callback_);
226 std::cout << "TestRegisterNotifyCallback ret:" << ret << std::endl;
227
228 return;
229 }
230
231