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 "test.h"
25 #include "test_notify_callback.h"
26 using namespace OHOS::nmd;
27 using namespace OHOS;
28 using namespace OHOS::NetsysNative;
29
30 namespace {
31 OHOS::sptr<OHOS::NetsysNative::INotifyCallback> callback_ =
32 (std::make_unique<OHOS::NetsysNative::TestNotifyCallback>()).release();
GetProxyK()33 sptr<INetsysService> GetProxyK()
34 {
35 NETNATIVE_LOGE("Get samgr >>>>>>>>>>>>>>>>>>>>>>>>>>");
36 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
37 NETNATIVE_LOGE("Get samgr %{public}p", samgr.GetRefPtr());
38 std::cout << "Get samgr "<< samgr.GetRefPtr() << std::endl;
39
40 auto remote = samgr->GetSystemAbility(COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
41 NETNATIVE_LOGE("Get remote %{public}p", remote.GetRefPtr());
42 std::cout << "Get remote "<< remote.GetRefPtr() << std::endl;
43
44 auto proxy = iface_cast<NetsysNative::INetsysService>(remote);
45 if (proxy != nullptr) {
46 NETNATIVE_LOGE("Get proxy %{public}p", proxy.GetRefPtr());
47 std::cout << "Get proxy " << proxy.GetRefPtr() << std::endl;
48 } else {
49 std::cout << "Get proxy nullptr" << std::endl;
50 }
51
52 return proxy;
53 }
54
55 auto netsysServiceK_ = GetProxyK();
56 }
57
58 namespace {
freeNetsysAddrInfo(struct addrinfo * aihead)59 void freeNetsysAddrInfo(struct addrinfo *aihead)
60 {
61 struct addrinfo *ai;
62 struct addrinfo *ainext;
63 for (ai = aihead; ai != nullptr; ai = ainext) {
64 if (ai->ai_addr != nullptr) {
65 free(ai->ai_addr);
66 }
67
68 if (ai->ai_canonname != nullptr) {
69 free(ai->ai_canonname);
70 }
71 ainext = ai->ai_next;
72 free(ai);
73 }
74 }
75 } // namespace
76
TestSetResolverConfig()77 void TestSetResolverConfig()
78 {
79 int ret = netsysServiceK_->CreateNetworkCache(0);
80 NETNATIVE_LOGE("NETSYS: CreateNetworkCache0 ret=%{public}d", ret);
81 ret = netsysServiceK_->CreateNetworkCache(1);
82 NETNATIVE_LOGE("NETSYS: CreateNetworkCache1 ret=%{public}d", ret);
83 }
84
TestGetResolverConfig()85 void TestGetResolverConfig()
86 {
87 int ret = -1;
88 std::vector<std::string> servers;
89 std::vector<std::string> domains;
90 uint16_t baseTimeoutMsec;
91 uint8_t retryCount;
92 int num = 3;
93 for (int i = 0; i < num; i++) {
94 ret = netsysServiceK_->GetResolverConfig(i, servers, domains, baseTimeoutMsec, retryCount);
95 NETNATIVE_LOGE("NETSYS: getResolverConfig ret=%{public}d, iii=%{public}d", ret, i);
96 NETNATIVE_LOGE("NETSYS: server size %{public}d, domains size %{public}d",
97 static_cast<int32_t>(servers.size()), static_cast<int32_t>(domains.size()));
98 for (auto item:servers) {
99 std::cout<<"Server:" <<item.c_str()<<std::endl;
100 }
101 for (auto iter:domains) {
102 std::cout<<"Domains:" <<iter.c_str()<<std::endl;
103 }
104 }
105 }
106
TestInterfaceGetCfg()107 void TestInterfaceGetCfg()
108 {
109 std::cout<<"hello"<<std::endl;
110 }
111
TestCreateNetworkCache()112 void TestCreateNetworkCache()
113 {
114 int ret = -1;
115 ret = netsysServiceK_->CreateNetworkCache(0);
116 NETNATIVE_LOGE("NETSYS: CreateNetworkCache0 ret=%{public}d", ret);
117 ret = netsysServiceK_->CreateNetworkCache(1);
118 NETNATIVE_LOGE("NETSYS: CreateNetworkCache1 ret=%{public}d", ret);
119 }
120
TestFlushNetworkCache()121 void TestFlushNetworkCache()
122 {
123 int ret = -1 ;
124 ret = netsysServiceK_->FlushNetworkCache(0);
125 NETNATIVE_LOGE("NETSYS: FlushNetworkCache0 ret=%{public}d", ret);
126 ret = netsysServiceK_->FlushNetworkCache(1);
127 NETNATIVE_LOGE("NETSYS: FlushNetworkCache1 ret=%{public}d", ret);
128 }
129
TestDestroyNetworkCache()130 void TestDestroyNetworkCache()
131 {
132 int ret = -1;
133 ret = netsysServiceK_->DestroyNetworkCache(1);
134 NETNATIVE_LOGE("NETSYS: DestroyNetworkCache1 ret=%{public}d", ret);
135 }
136
TestInterfaceSetMtu()137 void TestInterfaceSetMtu()
138 {
139 int ret = -1;
140 std::string ifName = "eth0";
141 int mtu = 1200;
142 std::cout<<"begin to GetMtu"<<std::endl;
143 ret = netsysServiceK_->InterfaceGetMtu(ifName);
144 NETNATIVE_LOGE("NETSYS: GetMtu ago ret=%{public}d", ret);
145 std::cout<<"begin to SetMtu"<<std::endl;
146 ret = netsysServiceK_->InterfaceSetMtu(ifName, mtu);
147 NETNATIVE_LOGE("NETSYS: SetMtu ret=%{public}d", ret);
148 std::cout<<"begin22 to GetMtu"<<std::endl;
149 ret = netsysServiceK_->InterfaceGetMtu(ifName);
150 NETNATIVE_LOGE("NETSYS: GetMtu ret=%{public}d", ret);
151 }
152
TestInterfaceGetMtu()153 void TestInterfaceGetMtu()
154 {
155 int ret = -1;
156 std::string ifName = "eth0";
157 int mtu = 1200;
158 std::cout<<"begin to GetMtu"<<std::endl;
159 ret = netsysServiceK_->InterfaceGetMtu(ifName);
160 NETNATIVE_LOGE("NETSYS: GetMtu ago ret=%{public}d", ret);
161 std::cout<<"begin to SetMtu"<<std::endl;
162 ret = netsysServiceK_->InterfaceSetMtu(ifName, mtu);
163 NETNATIVE_LOGE("NETSYS: SetMtu ret=%{public}d", ret);
164 std::cout<<"begin22 to GetMtu"<<std::endl;
165 ret = netsysServiceK_->InterfaceGetMtu(ifName);
166 NETNATIVE_LOGE("NETSYS: GetMtu ret=%{public}d", ret);
167 }
168
TestRegisterNotifyCallback()169 void TestRegisterNotifyCallback()
170 {
171 if (netsysServiceK_ == nullptr || callback_ == nullptr) {
172 std::cout << "TestRegisterNotifyCallback netsysServiceK_ or callback is nullptr" << std::endl;
173 return;
174 }
175 std::cout << "enter TestRegisterNotifyCallback " << std::endl;
176 int32_t ret = netsysServiceK_->RegisterNotifyCallback(callback_);
177 std::cout << "TestRegisterNotifyCallback ret:" << ret << std::endl;
178
179 return;
180 }
181
182