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 "netnative_log_wrapper.h"
22 #include "netsys_native_service_proxy.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 } // namespace
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     std::vector<std::string> servers;
88     std::vector<std::string> domains;
89     uint16_t baseTimeoutMsec;
90     uint8_t retryCount;
91     int num = 3;
92     for (int i = 0; i < num; i++) {
93         int32_t ret = netsysServiceK_->GetResolverConfig(i, servers, domains, baseTimeoutMsec, retryCount);
94         NETNATIVE_LOGE("NETSYS: getResolverConfig   ret=%{public}d, iii=%{public}d", ret, i);
95         NETNATIVE_LOGE("NETSYS:  server size %{public}d, domains  size %{public}d",
96                        static_cast<int32_t>(servers.size()), static_cast<int32_t>(domains.size()));
97     }
98 }
99 
TestInterfaceGetCfg()100 void TestInterfaceGetCfg()
101 {
102     std::cout << "hello" << std::endl;
103 }
104 
TestCreateNetworkCache()105 void TestCreateNetworkCache()
106 {
107     int ret = -1;
108     ret = netsysServiceK_->CreateNetworkCache(0);
109     NETNATIVE_LOGE("NETSYS: CreateNetworkCache0   ret=%{public}d", ret);
110     ret = netsysServiceK_->CreateNetworkCache(1);
111     NETNATIVE_LOGE("NETSYS: CreateNetworkCache1   ret=%{public}d", ret);
112 }
113 
TestFlushNetworkCache()114 void TestFlushNetworkCache()
115 {
116     int ret = -1;
117     ret = netsysServiceK_->FlushNetworkCache(0);
118     NETNATIVE_LOGE("NETSYS: FlushNetworkCache0   ret=%{public}d", ret);
119     ret = netsysServiceK_->FlushNetworkCache(1);
120     NETNATIVE_LOGE("NETSYS: FlushNetworkCache1   ret=%{public}d", ret);
121 }
122 
TestDestroyNetworkCache()123 void TestDestroyNetworkCache()
124 {
125     int ret = -1;
126     ret = netsysServiceK_->DestroyNetworkCache(1);
127     NETNATIVE_LOGE("NETSYS: DestroyNetworkCache1   ret=%{public}d", ret);
128 }
129 
TestInterfaceSetMtu()130 void TestInterfaceSetMtu()
131 {
132     int ret = -1;
133     std::string ifName = "eth0";
134     int mtu = 1200;
135     std::cout << "begin to GetMtu" << std::endl;
136     ret = netsysServiceK_->GetInterfaceMtu(ifName);
137     NETNATIVE_LOGE("NETSYS: GetMtu   ago  ret=%{public}d", ret);
138     std::cout << "begin to SetMtu" << std::endl;
139     ret = netsysServiceK_->SetInterfaceMtu(ifName, mtu);
140     NETNATIVE_LOGE("NETSYS: SetMtu   ret=%{public}d", ret);
141     std::cout << "begin22 to GetMtu" << std::endl;
142     ret = netsysServiceK_->GetInterfaceMtu(ifName);
143     NETNATIVE_LOGE("NETSYS: GetMtu   ret=%{public}d", ret);
144 }
145 
TestInterfaceGetMtu()146 void TestInterfaceGetMtu()
147 {
148     int ret = -1;
149     std::string ifName = "eth0";
150     int mtu = 1200;
151     std::cout << "begin to GetMtu" << std::endl;
152     ret = netsysServiceK_->GetInterfaceMtu(ifName);
153     NETNATIVE_LOGE("NETSYS: GetMtu   ago  ret=%{public}d", ret);
154     std::cout << "begin to SetMtu" << std::endl;
155     ret = netsysServiceK_->SetInterfaceMtu(ifName, mtu);
156     NETNATIVE_LOGE("NETSYS: SetMtu   ret=%{public}d", ret);
157     std::cout << "begin22 to GetMtu" << std::endl;
158     ret = netsysServiceK_->GetInterfaceMtu(ifName);
159     NETNATIVE_LOGE("NETSYS: GetMtu   ret=%{public}d", ret);
160 }
161 
TestRegisterNotifyCallback()162 void TestRegisterNotifyCallback()
163 {
164     if (netsysServiceK_ == nullptr || callback_ == nullptr) {
165         std::cout << "TestRegisterNotifyCallback netsysServiceK_ or callback is nullptr" << std::endl;
166         return;
167     }
168     std::cout << "enter TestRegisterNotifyCallback " << std::endl;
169     int32_t ret = netsysServiceK_->RegisterNotifyCallback(callback_);
170     std::cout << "TestRegisterNotifyCallback ret:" << ret << std::endl;
171 
172     return;
173 }
174 
TestSetTcpBufferSizes()175 void TestSetTcpBufferSizes()
176 {
177     std::string tcpBufferSizes = "524288,1048576,2097152,262144,524288,1048576";
178     std::cout << "begin to SetTcpBufferSizes" << std::endl;
179     ret = netsysServiceK_->SetTcpBufferSizes(tcpBufferSizes);
180     NETNATIVE_LOGE("NETSYS: SetTcpBufferSizes   ret=%{public}d", ret);
181 }