• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "accesstoken_kit.h"
19 #include "ethernet_client.h"
20 #include "gtest/gtest-message.h"
21 #include "gtest/gtest-test-part.h"
22 #include "gtest/hwext/gtest-ext.h"
23 #include "gtest/hwext/gtest-tag.h"
24 #include "http_proxy.h"
25 #include "interface_configuration.h"
26 #include "interface_type.h"
27 #include "nativetoken_kit.h"
28 #include "net_manager_constants.h"
29 #include "netmgr_ext_log_wrapper.h"
30 #include "refbase.h"
31 #include "static_configuration.h"
32 #include "token_setproc.h"
33 #define private public
34 #define protected public
35 #include "ethernet_client.h"
36 #include "ethernet_dhcp_controller.h"
37 #include "ethernet_service.h"
38 #include "ethernet_management.h"
39 #include "ethernet_service_proxy.h"
40 
41 namespace OHOS {
42 namespace NetManagerStandard {
43 namespace {
44 using namespace testing::ext;
45 using namespace Security::AccessToken;
46 using Security::AccessToken::AccessTokenID;
47 constexpr const char *DEV_NAME = "eth0";
48 constexpr const char *TEST_PROXY_HOST = "127.0.0.1";
49 constexpr uint16_t TEST_PROXY_PORT = 8080;
50 HapInfoParams testInfoParms = {.userID = 1,
51                                .bundleName = "ethernet_manager_test",
52                                .instIndex = 0,
53                                .appIDDesc = "test",
54                                .isSystemApp = true};
55 PermissionDef testPermDef = {
56     .permissionName = "ohos.permission.GET_NETWORK_INFO",
57     .bundleName = "ethernet_manager_test",
58     .grantMode = 1,
59     .availableLevel = APL_SYSTEM_BASIC,
60     .label = "label",
61     .labelId = 1,
62     .description = "Test network share manager",
63     .descriptionId = 1,
64 };
65 PermissionStateFull testState = {
66     .permissionName = "ohos.permission.GET_NETWORK_INFO",
67     .isGeneral = true,
68     .resDeviceID = {"local"},
69     .grantStatus = {PermissionState::PERMISSION_GRANTED},
70     .grantFlags = {2},
71 };
72 HapPolicyParams testPolicyPrams1 = {
73     .apl = APL_SYSTEM_BASIC,
74     .domain = "test.domain",
75     .permList = {testPermDef},
76     .permStateList = {testState},
77 };
78 
79 PermissionDef testPermDef2 = {
80     .permissionName = "ohos.permission.CONNECTIVITY_INTERNAL",
81     .bundleName = "ethernet_manager_test",
82     .grantMode = 1,
83     .availableLevel = APL_SYSTEM_BASIC,
84     .label = "label",
85     .labelId = 1,
86     .description = "Test network share manager",
87     .descriptionId = 1,
88 };
89 PermissionStateFull testState2 = {
90     .permissionName = "ohos.permission.CONNECTIVITY_INTERNAL",
91     .isGeneral = true,
92     .resDeviceID = {"local"},
93     .grantStatus = {PermissionState::PERMISSION_GRANTED},
94     .grantFlags = {2},
95 };
96 HapPolicyParams testPolicyPrams2 = {
97     .apl = APL_SYSTEM_BASIC,
98     .domain = "test.domain",
99     .permList = {testPermDef2},
100     .permStateList = {testState2},
101 };
102 } // namespace
103 
104 class AccessToken {
105 public:
AccessToken(HapPolicyParams & testPolicyPrams)106     explicit AccessToken(HapPolicyParams &testPolicyPrams) : currentID_(GetSelfTokenID())
107     {
108         AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParms, testPolicyPrams);
109         accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
110         SetSelfTokenID(tokenIdEx.tokenIDEx);
111     }
~AccessToken()112     ~AccessToken()
113     {
114         AccessTokenKit::DeleteToken(accessID_);
115         SetSelfTokenID(currentID_);
116     }
117 
118 private:
119     AccessTokenID currentID_;
120     AccessTokenID accessID_ = 0;
121 };
122 
123 class EtherNetServiceProxyTest : public testing::Test {
124 public:
125     static void SetUpTestCase();
126     static void TearDownTestCase();
127     sptr<InterfaceConfiguration> GetIfaceConfig();
128     void SetUp();
129     void TearDown();
130 };
131 
GetIfaceConfig()132 sptr<InterfaceConfiguration> EtherNetServiceProxyTest::GetIfaceConfig()
133 {
134     sptr<InterfaceConfiguration> ic = (std::make_unique<InterfaceConfiguration>()).release();
135     if (!ic) {
136         return ic;
137     }
138     INetAddr ipv4Addr;
139     ipv4Addr.type_ = INetAddr::IPV4;
140     ipv4Addr.family_ = 0x01;
141     ipv4Addr.prefixlen_ = 0x01;
142     ipv4Addr.address_ = "172.17.5.234";
143     ipv4Addr.netMask_ = "255.255.254.0";
144     ipv4Addr.hostName_ = "netAddr";
145     ic->ipStatic_.ipAddrList_.push_back(ipv4Addr);
146     INetAddr route;
147     route.type_ = INetAddr::IPV4;
148     route.family_ = 0x01;
149     route.prefixlen_ = 0x01;
150     route.address_ = "0.0.0.0";
151     route.netMask_ = "0.0.0.0";
152     route.hostName_ = "netAddr";
153     ic->ipStatic_.routeList_.push_back(route);
154     INetAddr gateway;
155     gateway.type_ = INetAddr::IPV4;
156     gateway.family_ = 0x01;
157     gateway.prefixlen_ = 0x01;
158     gateway.address_ = "172.17.4.1";
159     gateway.netMask_ = "0.0.0.0";
160     gateway.hostName_ = "netAddr";
161     ic->ipStatic_.gatewayList_.push_back(gateway);
162     INetAddr netMask;
163     netMask.type_ = INetAddr::IPV4;
164     netMask.family_ = 0x01;
165     netMask.address_ = "255.255.255.0";
166     netMask.hostName_ = "netAddr";
167     ic->ipStatic_.netMaskList_.push_back(netMask);
168     ic->httpProxy_ = {TEST_PROXY_HOST, TEST_PROXY_PORT, {}};
169     INetAddr dns1;
170     dns1.type_ = INetAddr::IPV4;
171     dns1.family_ = 0x01;
172     dns1.address_ = "8.8.8.8";
173     dns1.hostName_ = "netAddr";
174     INetAddr dns2;
175     dns2.type_ = INetAddr::IPV4;
176     dns2.family_ = 0x01;
177     dns2.address_ = "114.114.114.114";
178     dns2.hostName_ = "netAddr";
179     ic->ipStatic_.dnsServers_.push_back(dns1);
180     ic->ipStatic_.dnsServers_.push_back(dns2);
181     return ic;
182 }
183 
SetUpTestCase()184 void EtherNetServiceProxyTest::SetUpTestCase() {}
185 
TearDownTestCase()186 void EtherNetServiceProxyTest::TearDownTestCase() {}
187 
SetUp()188 void EtherNetServiceProxyTest::SetUp() {}
189 
TearDown()190 void EtherNetServiceProxyTest::TearDown() {}
191 
192 HWTEST_F(EtherNetServiceProxyTest, SetIfaceConfigTest001, TestSize.Level1)
193 {
194     AccessToken accessToken(testPolicyPrams2);
195     EthernetServiceProxy ethernetServiceProxy(nullptr);
196     sptr<InterfaceConfiguration> ic = GetIfaceConfig();
197     auto ret = ethernetServiceProxy.SetIfaceConfig(DEV_NAME, ic);
198     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
199 }
200 
201 HWTEST_F(EtherNetServiceProxyTest, GetIfaceConfigTest001, TestSize.Level1)
202 {
203     AccessToken accessToken(testPolicyPrams1);
204     EthernetServiceProxy ethernetServiceProxy(nullptr);
205     sptr<InterfaceConfiguration> ifaceConfig = new (std::nothrow) InterfaceConfiguration();
206     int32_t ret = ethernetServiceProxy.GetIfaceConfig(DEV_NAME, ifaceConfig);
207     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
208 }
209 
210 HWTEST_F(EtherNetServiceProxyTest, IsIfaceActiveTest001, TestSize.Level1)
211 {
212     AccessToken accessToken(testPolicyPrams1);
213     EthernetServiceProxy ethernetServiceProxy(nullptr);
214     sptr<InterfaceConfiguration> ifaceConfig = new (std::nothrow) InterfaceConfiguration();
215     std::string ifcaeName = "eth0";
216     int32_t activeStatus = -1;
217     int32_t ret = ethernetServiceProxy.IsIfaceActive(ifcaeName, activeStatus);
218     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
219 }
220 
221 HWTEST_F(EtherNetServiceProxyTest, GetAllActiveIfacesTest001, TestSize.Level1)
222 {
223     AccessToken accessToken(testPolicyPrams1);
224     EthernetServiceProxy ethernetServiceProxy(nullptr);
225     std::vector<std::string> result;
226     int32_t ret = ethernetServiceProxy.GetAllActiveIfaces(result);
227     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
228 }
229 
230 HWTEST_F(EtherNetServiceProxyTest, ResetFactoryTest001, TestSize.Level1)
231 {
232     EthernetServiceProxy ethernetServiceProxy(nullptr);
233     int32_t ret = ethernetServiceProxy.ResetFactory();
234     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
235 }
236 
237 HWTEST_F(EtherNetServiceProxyTest, SetInterfaceUpTest001, TestSize.Level1)
238 {
239     EthernetServiceProxy ethernetServiceProxy(nullptr);
240     AccessToken accessToken(testPolicyPrams2);
241     int32_t ret = ethernetServiceProxy.SetInterfaceUp(DEV_NAME);
242     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
243     OHOS::nmd::InterfaceConfigurationParcel cfg;
244     ret = ethernetServiceProxy.GetInterfaceConfig(DEV_NAME, cfg);
245     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
246 }
247 
248 HWTEST_F(EtherNetServiceProxyTest, SetInterfaceDownTest001, TestSize.Level1)
249 {
250     EthernetServiceProxy ethernetServiceProxy(nullptr);
251     AccessToken accessToken(testPolicyPrams2);
252     int32_t ret = ethernetServiceProxy.SetInterfaceDown(DEV_NAME);
253     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
254 }
255 
256 HWTEST_F(EtherNetServiceProxyTest, SetInterfaceConfigTest001, TestSize.Level1)
257 {
258     EthernetServiceProxy ethernetServiceProxy(nullptr);
259     AccessToken accessToken(testPolicyPrams2);
260     OHOS::nmd::InterfaceConfigurationParcel config;
261     config.ifName = "eth0";
262     config.hwAddr = "";
263     config.ipv4Addr = "172.17.5.234";
264     config.prefixLength = 24;
265     config.flags.push_back("up");
266     config.flags.push_back("broadcast");
267     int32_t ret = ethernetServiceProxy.SetInterfaceConfig(DEV_NAME, config);
268     EXPECT_EQ(ret, NETMANAGER_EXT_ERR_IPC_CONNECT_STUB_FAIL);
269 }
270 } // namespace NetManagerStandard
271 } // namespace OHOS