1 /*
2 * Copyright (c) 2024 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include <cstdint>
19 #include <unistd.h>
20
21 #include "network_set_manager.h"
22 #include "data_ability_observer_stub.h"
23 #include "accesstoken_kit.h"
24 #include "cloud_file_kit_mock.h"
25 #include "datashare_errno.h"
26 #include "datashare_result_set.h"
27 #include "dfs_error.h"
28 #include "ipc_skeleton.h"
29 #include "parameters.h"
30 #include "utils_log.h"
31
32 namespace OHOS::FileManagement::CloudSync::Test {
33 using namespace testing;
34 using namespace testing::ext;
35 using namespace std;
36 using namespace CloudFile;
37
38 class NetworkSetManagerTest : public testing::Test {
39 public:
40 static void SetUpTestCase(void);
41 static void TearDownTestCase(void);
42 void SetUp();
43 void TearDown();
44 static inline shared_ptr<NetworkSetManager> networkSetManager_ = nullptr;
45 static inline shared_ptr<MockCloudFileKit> proxy_ = nullptr;
46 };
47
SetUpTestCase(void)48 void NetworkSetManagerTest::SetUpTestCase(void)
49 {
50 networkSetManager_ = make_shared<NetworkSetManager>();
51 proxy_ = make_shared<MockCloudFileKit>();
52 ICloudFileKit::proxy_ = proxy_;
53 GTEST_LOG_(INFO) << "SetUpTestCase";
54 }
55
TearDownTestCase(void)56 void NetworkSetManagerTest::TearDownTestCase(void)
57 {
58 GTEST_LOG_(INFO) << "TearDownTestCase";
59 networkSetManager_.reset();
60 ICloudFileKit::proxy_ = nullptr;
61 proxy_ = nullptr;
62 }
63
SetUp(void)64 void NetworkSetManagerTest::SetUp(void)
65 {
66 GTEST_LOG_(INFO) << "SetUp";
67 }
68
TearDown(void)69 void NetworkSetManagerTest::TearDown(void)
70 {
71 GTEST_LOG_(INFO) << "TearDown";
72 }
73
74 /**
75 * @tc.name: QueryCellularConnectTest001
76 * @tc.desc: Verify the QueryCellularConnect function
77 * @tc.type: FUNC
78 * @tc.require: I6JPKG
79 */
80 HWTEST_F(NetworkSetManagerTest, QueryCellularConnectTest001, TestSize.Level1)
81 {
82 GTEST_LOG_(INFO) << "QueryCellularConnectTest Start";
83 try {
84 int32_t userId = 100;
85 string bundleName = "com.ohos.photos";
86
87 int32_t ret = networkSetManager_->QueryCellularConnect(userId, bundleName);
88 EXPECT_EQ(ret, E_RDB);
89 } catch (...) {
90 EXPECT_TRUE(false);
91 GTEST_LOG_(INFO) << "QueryCellularConnectTest FAILED";
92 }
93 GTEST_LOG_(INFO) << "QueryCellularConnectTest End";
94 }
95
96 /**
97 * @tc.name: QueryNetConnectTest001
98 * @tc.desc: Verify the QueryNetConnect function
99 * @tc.type: FUNC
100 * @tc.require: I6JPKG
101 */
102 HWTEST_F(NetworkSetManagerTest, QueryNetConnectTest001, TestSize.Level1)
103 {
104 GTEST_LOG_(INFO) << "QueryNetConnectTest Start";
105 try {
106 int32_t userId = 100;
107 string bundleName = "com.ohos.photos";
108
109 int32_t ret = networkSetManager_->QueryNetConnect(userId, bundleName);
110 EXPECT_EQ(ret, E_RDB);
111 } catch (...) {
112 EXPECT_TRUE(false);
113 GTEST_LOG_(INFO) << "QueryNetConnectTest FAILED";
114 }
115 GTEST_LOG_(INFO) << "QueryNetConnectTest End";
116 }
117
118 /**
119 * @tc.name: GetNetConnectTest001
120 * @tc.desc: Verify the GetNetConnect function
121 * @tc.type: FUNC
122 * @tc.require: I6JPKG
123 */
124 HWTEST_F(NetworkSetManagerTest, GetNetConnectTest001, TestSize.Level1)
125 {
126 GTEST_LOG_(INFO) << "GetNetConnectTest Start";
127 try {
128 int32_t userId = 100;
129 string bundleName = "com.ohos.photos";
130
131 networkSetManager_->GetNetConnect(bundleName, userId);
132 } catch (...) {
133 EXPECT_TRUE(false);
134 GTEST_LOG_(INFO) << "GetNetConnectTest FAILED";
135 }
136 GTEST_LOG_(INFO) << "GetNetConnectTest End";
137 }
138
139 /**
140 * @tc.name: UnregisterObserverTest001
141 * @tc.desc: Verify the UnregisterObserver function
142 * @tc.type: FUNC
143 * @tc.require: I6JPKG
144 */
145 HWTEST_F(NetworkSetManagerTest, UnregisterObserverTest001, TestSize.Level1)
146 {
147 GTEST_LOG_(INFO) << "UnregisterObserverTest Start";
148 try {
149 int32_t userId = 100;
150 string bundleName = "com.ohos.photos";
151 int32_t type = NetworkSetManager::CELLULARCONNECT;
152
153 networkSetManager_->UnregisterObserver(bundleName, userId, type);
154 } catch (...) {
155 EXPECT_TRUE(false);
156 GTEST_LOG_(INFO) << "UnregisterObserverTest FAILED";
157 }
158 GTEST_LOG_(INFO) << "UnregisterObserverTest End";
159 }
160
161 /**
162 * @tc.name: UnregisterObserverTest002
163 * @tc.desc: Verify the UnregisterObserver function
164 * @tc.type: FUNC
165 * @tc.require: I6JPKG
166 */
167 HWTEST_F(NetworkSetManagerTest, UnregisterObserverTest002, TestSize.Level1)
168 {
169 GTEST_LOG_(INFO) << "UnregisterObserverTest Start";
170 try {
171 int32_t userId = 100;
172 string bundleName = "com.ohos.photos";
173 int32_t type = NetworkSetManager::NETCONNECT;
174
175 networkSetManager_->UnregisterObserver(bundleName, userId, type);
176 } catch (...) {
177 EXPECT_TRUE(false);
178 GTEST_LOG_(INFO) << "UnregisterObserverTest FAILED";
179 }
180 GTEST_LOG_(INFO) << "UnregisterObserverTest End";
181 }
182
183 /**
184 * @tc.name: RegisterObserverTest001
185 * @tc.desc: Verify the RegisterObserver function
186 * @tc.type: FUNC
187 * @tc.require: I6JPKG
188 */
189 HWTEST_F(NetworkSetManagerTest, RegisterObserverTest001, TestSize.Level1)
190 {
191 GTEST_LOG_(INFO) << "RegisterObserverTest Start";
192 try {
193 int32_t userId = 100;
194 string bundleName = "com.ohos.photos";
195 int32_t type = NetworkSetManager::CELLULARCONNECT;
196
197 networkSetManager_->RegisterObserver(bundleName, userId, type);
198 } catch (...) {
199 EXPECT_TRUE(false);
200 GTEST_LOG_(INFO) << "RegisterObserverTest FAILED";
201 }
202 GTEST_LOG_(INFO) << "RegisterObserverTest End";
203 }
204
205 /**
206 * @tc.name: GetCellularConnectTest
207 * @tc.desc: Verify the GetCellularConnect function
208 * @tc.type: FUNC
209 * @tc.require: I6JPKG
210 */
211 HWTEST_F(NetworkSetManagerTest, GetCellularConnectTest, TestSize.Level1)
212 {
213 GTEST_LOG_(INFO) << "GetCellularConnectTest Start";
214 try {
215 int32_t userId = 100;
216 string bundleName = "com.ohos.photos";
217
218 (networkSetManager_->cellularNetMap_).Insert(std::to_string(userId) + "/" + bundleName, true);
219 networkSetManager_->dataSyncManager_ = std::make_shared<CloudFile::DataSyncManager>();
220 networkSetManager_->GetCellularConnect(bundleName, userId);
221 EXPECT_NE(networkSetManager_->netStatus_, NetworkSetManager::NetConnStatus::WIFI_CONNECT);
222 } catch (...) {
223 EXPECT_TRUE(false);
224 GTEST_LOG_(INFO) << "GetCellularConnectTest FAILED";
225 }
226 GTEST_LOG_(INFO) << "GetCellularConnectTest End";
227 }
228
229 /**
230 * @tc.name: OnChangeTest001
231 * @tc.desc: Verify the OnChange function
232 * @tc.type: FUNC
233 * @tc.require: I6JPKG
234 */
235 HWTEST_F(NetworkSetManagerTest, OnChangeTest001, TestSize.Level1)
236 {
237 GTEST_LOG_(INFO) << "OnChangeTest Start";
238 try {
239 std::string bundleName;
240 int32_t userId = 0;
241 int32_t type = 0;
242 MobileNetworkObserver mobile(bundleName, userId, type);
__anone4e0d6c20102() 243 mobile.observerCallback_ = [] () {};
244 mobile.type_ = 0;
245
246 mobile.OnChange();
247 EXPECT_NE(mobile.observerCallback_, nullptr);
248 } catch (...) {
249 EXPECT_TRUE(false);
250 GTEST_LOG_(INFO) << "OnChange FAILED";
251 }
252 GTEST_LOG_(INFO) << "OnChange End";
253 }
254
255 /**
256 * @tc.name: OnChangeTest002
257 * @tc.desc: Verify the OnChange function
258 * @tc.type: FUNC
259 * @tc.require: I6JPKG
260 */
261 HWTEST_F(NetworkSetManagerTest, OnChangeTest002, TestSize.Level1)
262 {
263 GTEST_LOG_(INFO) << "OnChangeTest Start";
264 try {
265 std::string bundleName;
266 int32_t userId = 0;
267 int32_t type = -1;
268 MobileNetworkObserver mobile(bundleName, userId, type);
269
270 mobile.OnChange();
271 EXPECT_EQ(mobile.observerCallback_, nullptr);
272 } catch (...) {
273 EXPECT_TRUE(false);
274 GTEST_LOG_(INFO) << "OnChange FAILED";
275 }
276 GTEST_LOG_(INFO) << "OnChange End";
277 }
278
279 /**
280 * @tc.name: GetConfigParams000
281 * @tc.desc: Verify the GetConfigParams function when driveKit is null.
282 * @tc.type: FUNC
283 * @tc.require: ICEGLJ
284 */
285 HWTEST_F(NetworkSetManagerTest, GetConfigParams000, TestSize.Level1)
286 {
287 GTEST_LOG_(INFO) << "GetConfigParams Start";
288 try {
289 int32_t userId = 100;
290 string bundleName = "com.ohos.photos";
291 EXPECT_CALL(*proxy_, GetInstance()).WillOnce(Return(nullptr));
292 bool ret = networkSetManager_->GetConfigParams(bundleName, userId);
293 EXPECT_EQ(ret, false);
294 } catch (...) {
295 EXPECT_TRUE(false);
296 GTEST_LOG_(INFO) << "GetConfigParams FAILED";
297 }
298 GTEST_LOG_(INFO) << "GetConfigParams End";
299 }
300
301 /**
302 * @tc.name: GetConfigParams001
303 * @tc.desc: Verify the GetConfigParams function when driveKit is not null.
304 * @tc.type: FUNC
305 * @tc.require: ICEGLJ
306 */
307 HWTEST_F(NetworkSetManagerTest, GetConfigParams001, TestSize.Level1)
308 {
309 GTEST_LOG_(INFO) << "GetConfigParams Start";
310 try {
311 int32_t userId = 100;
312 string bundleName = "com.ohos.photos";
313 CloudFileKit *driveKit = new CloudFileKit();
314 EXPECT_CALL(*proxy_, GetInstance()).WillOnce(Return(driveKit));
315 EXPECT_CALL(*proxy_, GetAppConfigParams(_, _, _)).WillOnce(Return(E_OK));
316 bool ret = networkSetManager_->GetConfigParams(bundleName, userId);
317 EXPECT_EQ(ret, false);
318 } catch (...) {
319 EXPECT_TRUE(false);
320 GTEST_LOG_(INFO) << "GetConfigParams FAILED";
321 }
322 GTEST_LOG_(INFO) << "GetConfigParams End";
323 }
324
325 /**
326 * @tc.name: InitNetworkSetManagerTest001
327 * @tc.desc: Verify the GetConfigParams function when driveKit is not null.
328 * @tc.type: FUNC
329 * @tc.require: ICEGLJ
330 */
331 HWTEST_F(NetworkSetManagerTest, InitNetworkSetManager001, TestSize.Level1)
332 {
333 GTEST_LOG_(INFO) << "InitNetworkSetManager001 Start";
334 try {
335 ICloudFileKit::proxy_ = nullptr;
336 int32_t userId = 100;
337 string bundleName = "com.ohos.photos";
338 networkSetManager_->InitNetworkSetManager(bundleName, userId);
339 EXPECT_TRUE(true);
340 } catch (...) {
341 EXPECT_TRUE(false);
342 GTEST_LOG_(INFO) << "InitNetworkSetManager001 FAILED";
343 }
344 GTEST_LOG_(INFO) << "InitNetworkSetManager001 End";
345 }
346
347 /**
348 * @tc.name: InitNetworkSetManagerTest002
349 * @tc.desc: Verify the GetConfigParams function when driveKit is not null.
350 * @tc.type: FUNC
351 * @tc.require: ICEGLJ
352 */
353 HWTEST_F(NetworkSetManagerTest, InitNetworkSetManager002, TestSize.Level1)
354 {
355 GTEST_LOG_(INFO) << "InitNetworkSetManager002 Start";
356 try {
357 ICloudFileKit::proxy_ = nullptr;
358 int32_t userId = 100;
359 string bundleName = "com.ohos.ailife";
360 networkSetManager_->InitNetworkSetManager(bundleName, userId);
361 EXPECT_TRUE(true);
362 } catch (...) {
363 EXPECT_TRUE(false);
364 GTEST_LOG_(INFO) << "InitNetworkSetManager002 FAILED";
365 }
366 GTEST_LOG_(INFO) << "InitNetworkSetManager002 End";
367 }
368
369 /**
370 * @tc.name: InitNetworkSetManagerTest003
371 * @tc.desc: Verify the GetConfigParams function when driveKit is not null.
372 * @tc.type: FUNC
373 * @tc.require: ICEGLJ
374 */
375 HWTEST_F(NetworkSetManagerTest, InitNetworkSetManager003, TestSize.Level1)
376 {
377 GTEST_LOG_(INFO) << "InitNetworkSetManager003 Start";
378 try {
379 ICloudFileKit::proxy_ = nullptr;
380 int32_t userId = 100;
381 string bundleName = "xxxxxxx";
382 networkSetManager_->InitNetworkSetManager(bundleName, userId);
383 EXPECT_TRUE(true);
384 } catch (...) {
385 EXPECT_TRUE(false);
386 GTEST_LOG_(INFO) << "InitNetworkSetManager003 FAILED";
387 }
388 GTEST_LOG_(INFO) << "InitNetworkSetManager003 End";
389 }
390 }