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 "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 "inet_addr.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 "singleton.h"
32 #include "static_configuration.h"
33 #include "token_setproc.h"
34
35 namespace OHOS {
36 namespace NetManagerStandard {
37 namespace {
38 using namespace testing::ext;
39 using namespace Security::AccessToken;
40 using Security::AccessToken::AccessTokenID;
41 constexpr const char *DEV_NAME = "eth0";
42 constexpr const char *DEV_UP = "up";
43 constexpr const char *DEV_DOWN = "down";
44
45 HapInfoParams testInfoParms = {
46 .userID = 1,
47 .bundleName = "ethernet_manager_test",
48 .instIndex = 0,
49 .appIDDesc = "test"
50 };
51 PermissionDef testPermDef = {
52 .permissionName = "ohos.permission.GET_NETWORK_INFO",
53 .bundleName = "ethernet_manager_test",
54 .grantMode = 1,
55 .availableLevel = APL_SYSTEM_BASIC,
56 .label = "label",
57 .labelId = 1,
58 .description = "Test network share manager",
59 .descriptionId = 1,
60 };
61 PermissionStateFull testState = {
62 .permissionName = "ohos.permission.GET_NETWORK_INFO",
63 .isGeneral = true,
64 .resDeviceID = {"local"},
65 .grantStatus = {PermissionState::PERMISSION_GRANTED},
66 .grantFlags = {2},
67 };
68 HapPolicyParams testPolicyPrams1 = {
69 .apl = APL_SYSTEM_BASIC,
70 .domain = "test.domain",
71 .permList = {testPermDef},
72 .permStateList = {testState},
73 };
74
75 PermissionDef testPermDef2 = {
76 .permissionName = "ohos.permission.CONNECTIVITY_INTERNAL",
77 .bundleName = "ethernet_manager_test",
78 .grantMode = 1,
79 .availableLevel = APL_SYSTEM_BASIC,
80 .label = "label",
81 .labelId = 1,
82 .description = "Test network share manager",
83 .descriptionId = 1,
84 };
85 PermissionStateFull testState2 = {
86 .permissionName = "ohos.permission.CONNECTIVITY_INTERNAL",
87 .isGeneral = true,
88 .resDeviceID = {"local"},
89 .grantStatus = {PermissionState::PERMISSION_GRANTED},
90 .grantFlags = {2},
91 };
92 HapPolicyParams testPolicyPrams2 = {
93 .apl = APL_SYSTEM_BASIC,
94 .domain = "test.domain",
95 .permList = {testPermDef2},
96 .permStateList = {testState2},
97 };
98 std::string INFO = "info";
99 } // namespace
100
101 class AccessToken {
102 public:
AccessToken(HapPolicyParams & testPolicyPrams)103 explicit AccessToken(HapPolicyParams &testPolicyPrams) : currentID_(GetSelfTokenID())
104 {
105 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParms, testPolicyPrams);
106 accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
107 SetSelfTokenID(accessID_);
108 }
~AccessToken()109 ~AccessToken()
110 {
111 AccessTokenKit::DeleteToken(accessID_);
112 SetSelfTokenID(currentID_);
113 }
114
115 private:
116 AccessTokenID currentID_;
117 AccessTokenID accessID_ = 0;
118 };
119
120 class EthernetManagerTest : public testing::Test {
121 public:
122 static void SetUpTestCase();
123 static void TearDownTestCase();
124 void SetUp();
125 void TearDown();
126 sptr<InterfaceConfiguration> GetIfaceConfig();
127 bool CheckIfaceUp(const std::string &iface);
128 };
129
SetUpTestCase()130 void EthernetManagerTest::SetUpTestCase() {}
131
TearDownTestCase()132 void EthernetManagerTest::TearDownTestCase() {}
133
SetUp()134 void EthernetManagerTest::SetUp() {}
135
TearDown()136 void EthernetManagerTest::TearDown() {}
137
GetIfaceConfig()138 sptr<InterfaceConfiguration> EthernetManagerTest::GetIfaceConfig()
139 {
140 sptr<InterfaceConfiguration> ic = (std::make_unique<InterfaceConfiguration>()).release();
141 if (!ic) {
142 return ic;
143 }
144 ic->ipStatic_.ipAddr_.type_ = INetAddr::IPV4;
145 ic->ipStatic_.ipAddr_.family_ = 0x01;
146 ic->ipStatic_.ipAddr_.prefixlen_ = 0x01;
147 ic->ipStatic_.ipAddr_.address_ = "172.17.5.234";
148 ic->ipStatic_.ipAddr_.netMask_ = "255.255.254.0";
149 ic->ipStatic_.ipAddr_.hostName_ = "netAddr";
150 ic->ipStatic_.route_.type_ = INetAddr::IPV4;
151 ic->ipStatic_.route_.family_ = 0x01;
152 ic->ipStatic_.route_.prefixlen_ = 0x01;
153 ic->ipStatic_.route_.address_ = "0.0.0.0";
154 ic->ipStatic_.route_.netMask_ = "0.0.0.0";
155 ic->ipStatic_.route_.hostName_ = "netAddr";
156 ic->ipStatic_.gateway_.type_ = INetAddr::IPV4;
157 ic->ipStatic_.gateway_.family_ = 0x01;
158 ic->ipStatic_.gateway_.prefixlen_ = 0x01;
159 ic->ipStatic_.gateway_.address_ = "172.17.4.1";
160 ic->ipStatic_.gateway_.netMask_ = "0.0.0.0";
161 ic->ipStatic_.gateway_.hostName_ = "netAddr";
162 ic->ipStatic_.netMask_.type_ = INetAddr::IPV4;
163 ic->ipStatic_.netMask_.family_ = 0x01;
164 ic->ipStatic_.netMask_.netMask_ = "255.255.255.0";
165 ic->ipStatic_.netMask_.hostName_ = "netAddr";
166 INetAddr dns1;
167 dns1.type_ = INetAddr::IPV4;
168 dns1.family_ = 0x01;
169 dns1.address_ = "8.8.8.8";
170 dns1.hostName_ = "netAddr";
171 INetAddr dns2;
172 dns2.type_ = INetAddr::IPV4;
173 dns2.family_ = 0x01;
174 dns2.address_ = "114.114.114.114";
175 dns2.hostName_ = "netAddr";
176 ic->ipStatic_.dnsServers_.push_back(dns1);
177 ic->ipStatic_.dnsServers_.push_back(dns2);
178 return ic;
179 }
180
CheckIfaceUp(const std::string & iface)181 bool EthernetManagerTest::CheckIfaceUp(const std::string &iface)
182 {
183 AccessToken accessToken(testPolicyPrams1);
184 int32_t activeStatus = 0;
185 (void)DelayedSingleton<EthernetClient>::GetInstance()->IsIfaceActive(iface, activeStatus);
186 return activeStatus == 1;
187 }
188
189 /**
190 * @tc.name: EthernetManager001
191 * @tc.desc: Test EthernetManager SetIfaceConfig.
192 * @tc.type: FUNC
193 */
194 HWTEST_F(EthernetManagerTest, EthernetManager001, TestSize.Level1)
195 {
196 if (!CheckIfaceUp(DEV_NAME)) {
197 return;
198 }
199 AccessToken accessToken(testPolicyPrams2);
200 sptr<InterfaceConfiguration> ic = GetIfaceConfig();
201 ASSERT_EQ(DelayedSingleton<EthernetClient>::GetInstance()->SetIfaceConfig(DEV_NAME, ic), NETMANAGER_EXT_SUCCESS);
202 }
203
204 /**
205 * @tc.name: EthernetManager002
206 * @tc.desc: Test EthernetManager GetIfaceConfig.
207 * @tc.type: FUNC
208 */
209 HWTEST_F(EthernetManagerTest, EthernetManager002, TestSize.Level1)
210 {
211 if (!CheckIfaceUp(DEV_NAME)) {
212 return;
213 }
214 AccessToken accessToken(testPolicyPrams1);
215 sptr<InterfaceConfiguration> ic;
216 int32_t ret = DelayedSingleton<EthernetClient>::GetInstance()->GetIfaceConfig(DEV_NAME, ic);
217 ASSERT_TRUE(ic != nullptr);
218 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
219 }
220
221 /**
222 * @tc.name: EthernetManager003
223 * @tc.desc: Test EthernetManager IsIfaceActive.
224 * @tc.type: FUNC
225 */
226 HWTEST_F(EthernetManagerTest, EthernetManager003, TestSize.Level1)
227 {
228 if (!CheckIfaceUp(DEV_NAME)) {
229 return;
230 }
231 AccessToken accessToken(testPolicyPrams1);
232 int32_t activeStatus = -1;
233 int32_t ret = DelayedSingleton<EthernetClient>::GetInstance()->IsIfaceActive(DEV_NAME, activeStatus);
234 ASSERT_EQ(activeStatus, 1);
235 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
236 }
237
238 /**
239 * @tc.name: EthernetManager004
240 * @tc.desc: Test EthernetManager GetAllActiveIfaces.
241 * @tc.type: FUNC
242 */
243 HWTEST_F(EthernetManagerTest, EthernetManager004, TestSize.Level1)
244 {
245 if (!CheckIfaceUp(DEV_NAME)) {
246 return;
247 }
248 AccessToken accessToken(testPolicyPrams1);
249 std::vector<std::string> result;
250 int32_t ret = DelayedSingleton<EthernetClient>::GetInstance()->GetAllActiveIfaces(result);
251 std::vector<std::string>::iterator it = std::find(result.begin(), result.end(), DEV_NAME);
252 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
253 ASSERT_TRUE(it != result.end());
254 }
255
256 /**
257 * @tc.name: EthernetManager005
258 * @tc.desc: Test EthernetManager ResetFactory.
259 * @tc.type: FUNC
260 */
261 HWTEST_F(EthernetManagerTest, EthernetManager005, TestSize.Level1)
262 {
263 if (!CheckIfaceUp(DEV_NAME)) {
264 return;
265 }
266 int32_t ret = DelayedSingleton<EthernetClient>::GetInstance()->ResetFactory();
267 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
268 }
269
270 HWTEST_F(EthernetManagerTest, EthernetManager006, TestSize.Level1)
271 {
272 if (!CheckIfaceUp(DEV_NAME)) {
273 return;
274 }
275 OHOS::nmd::InterfaceConfigurationParcel cfg;
276 int32_t ret = DelayedSingleton<EthernetClient>::GetInstance()->GetInterfaceConfig(DEV_NAME, cfg);
277 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
278 ASSERT_FALSE(cfg.ifName.empty());
279 ASSERT_FALSE(cfg.hwAddr.empty());
280 }
281
282 HWTEST_F(EthernetManagerTest, EthernetManager007, TestSize.Level1)
283 {
284 if (!CheckIfaceUp(DEV_NAME)) {
285 return;
286 }
287 int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->SetInterfaceDown(DEV_NAME);
288 OHOS::nmd::InterfaceConfigurationParcel cfg;
289 int32_t ret = DelayedSingleton<EthernetClient>::GetInstance()->GetInterfaceConfig(DEV_NAME, cfg);
290 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
291 auto fit = std::find(cfg.flags.begin(), cfg.flags.end(), DEV_DOWN);
292 ASSERT_EQ(cfg.ifName, DEV_NAME);
293 ASSERT_TRUE(*fit == DEV_DOWN);
294 ASSERT_TRUE(result == 0);
295 }
296
297 HWTEST_F(EthernetManagerTest, EthernetManager008, TestSize.Level1)
298 {
299 if (!CheckIfaceUp(DEV_NAME)) {
300 return;
301 }
302 int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->SetInterfaceUp(DEV_NAME);
303 OHOS::nmd::InterfaceConfigurationParcel cfg;
304 int32_t ret = DelayedSingleton<EthernetClient>::GetInstance()->GetInterfaceConfig(DEV_NAME, cfg);
305 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
306 auto fit = std::find(cfg.flags.begin(), cfg.flags.end(), DEV_UP);
307 ASSERT_EQ(cfg.ifName, DEV_NAME);
308 ASSERT_TRUE(*fit == DEV_UP);
309 ASSERT_TRUE(result == 0);
310 }
311 } // namespace NetManagerStandard
312 } // namespace OHOS