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.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 };
46
SetUpTestCase(void)47 void NetworkSetManagerTest::SetUpTestCase(void)
48 {
49 networkSetManager_ = make_shared<NetworkSetManager>();
50 GTEST_LOG_(INFO) << "SetUpTestCase";
51 }
52
TearDownTestCase(void)53 void NetworkSetManagerTest::TearDownTestCase(void)
54 {
55 GTEST_LOG_(INFO) << "TearDownTestCase";
56 }
57
SetUp(void)58 void NetworkSetManagerTest::SetUp(void)
59 {
60 GTEST_LOG_(INFO) << "SetUp";
61 }
62
TearDown(void)63 void NetworkSetManagerTest::TearDown(void)
64 {
65 GTEST_LOG_(INFO) << "TearDown";
66 }
67
68 /**
69 * @tc.name: QueryCellularConnectTest001
70 * @tc.desc: Verify the QueryCellularConnect function
71 * @tc.type: FUNC
72 * @tc.require: I6JPKG
73 */
74 HWTEST_F(NetworkSetManagerTest, QueryCellularConnectTest001, TestSize.Level1)
75 {
76 GTEST_LOG_(INFO) << "QueryCellularConnectTest Start";
77 try {
78 int32_t userId = 100;
79 string bundleName = "com.ohos.photos";
80
81 int32_t ret = networkSetManager_->QueryCellularConnect(userId, bundleName);
82 EXPECT_EQ(ret, E_OK);
83 } catch (...) {
84 EXPECT_TRUE(false);
85 GTEST_LOG_(INFO) << "QueryCellularConnectTest FAILED";
86 }
87 GTEST_LOG_(INFO) << "QueryCellularConnectTest End";
88 }
89
90 /**
91 * @tc.name: QueryNetConnectTest001
92 * @tc.desc: Verify the QueryNetConnect function
93 * @tc.type: FUNC
94 * @tc.require: I6JPKG
95 */
96 HWTEST_F(NetworkSetManagerTest, QueryNetConnectTest001, TestSize.Level1)
97 {
98 GTEST_LOG_(INFO) << "QueryNetConnectTest Start";
99 try {
100 int32_t userId = 100;
101 string bundleName = "com.ohos.photos";
102
103 int32_t ret = networkSetManager_->QueryNetConnect(userId, bundleName);
104 EXPECT_EQ(ret, E_OK);
105 } catch (...) {
106 EXPECT_TRUE(false);
107 GTEST_LOG_(INFO) << "QueryNetConnectTest FAILED";
108 }
109 GTEST_LOG_(INFO) << "QueryNetConnectTest End";
110 }
111
112 /**
113 * @tc.name: GetCellularConnectTest001
114 * @tc.desc: Verify the GetCellularConnect function
115 * @tc.type: FUNC
116 * @tc.require: I6JPKG
117 */
118 HWTEST_F(NetworkSetManagerTest, GetCellularConnectTest001, TestSize.Level1)
119 {
120 GTEST_LOG_(INFO) << "GetCellularConnectTest Start";
121 try {
122 int32_t userId = 100;
123 string bundleName = "com.ohos.photos";
124
125 networkSetManager_->GetCellularConnect(bundleName, userId);
126 } catch (...) {
127 EXPECT_TRUE(false);
128 GTEST_LOG_(INFO) << "GetCellularConnectTest FAILED";
129 }
130 GTEST_LOG_(INFO) << "GetCellularConnectTest End";
131 }
132
133 /**
134 * @tc.name: GetNetConnectTest001
135 * @tc.desc: Verify the GetNetConnect function
136 * @tc.type: FUNC
137 * @tc.require: I6JPKG
138 */
139 HWTEST_F(NetworkSetManagerTest, GetNetConnectTest001, TestSize.Level1)
140 {
141 GTEST_LOG_(INFO) << "GetNetConnectTest Start";
142 try {
143 int32_t userId = 100;
144 string bundleName = "com.ohos.photos";
145
146 networkSetManager_->GetNetConnect(bundleName, userId);
147 } catch (...) {
148 EXPECT_TRUE(false);
149 GTEST_LOG_(INFO) << "GetNetConnectTest FAILED";
150 }
151 GTEST_LOG_(INFO) << "GetNetConnectTest End";
152 }
153
154 /**
155 * @tc.name: UnregisterObserverTest001
156 * @tc.desc: Verify the UnregisterObserver function
157 * @tc.type: FUNC
158 * @tc.require: I6JPKG
159 */
160 HWTEST_F(NetworkSetManagerTest, UnregisterObserverTest001, TestSize.Level1)
161 {
162 GTEST_LOG_(INFO) << "UnregisterObserverTest Start";
163 try {
164 int32_t userId = 100;
165 string bundleName = "com.ohos.photos";
166 int32_t type = NetworkSetManager::CELLULARCONNECT;
167
168 networkSetManager_->UnregisterObserver(bundleName, userId, type);
169 } catch (...) {
170 EXPECT_TRUE(false);
171 GTEST_LOG_(INFO) << "UnregisterObserverTest FAILED";
172 }
173 GTEST_LOG_(INFO) << "UnregisterObserverTest End";
174 }
175
176 /**
177 * @tc.name: UnregisterObserverTest002
178 * @tc.desc: Verify the UnregisterObserver function
179 * @tc.type: FUNC
180 * @tc.require: I6JPKG
181 */
182 HWTEST_F(NetworkSetManagerTest, UnregisterObserverTest002, TestSize.Level1)
183 {
184 GTEST_LOG_(INFO) << "UnregisterObserverTest Start";
185 try {
186 int32_t userId = 100;
187 string bundleName = "com.ohos.photos";
188 int32_t type = NetworkSetManager::NETCONNECT;
189
190 networkSetManager_->UnregisterObserver(bundleName, userId, type);
191 } catch (...) {
192 EXPECT_TRUE(false);
193 GTEST_LOG_(INFO) << "UnregisterObserverTest FAILED";
194 }
195 GTEST_LOG_(INFO) << "UnregisterObserverTest End";
196 }
197
198 /**
199 * @tc.name: RegisterObserverTest001
200 * @tc.desc: Verify the RegisterObserver function
201 * @tc.type: FUNC
202 * @tc.require: I6JPKG
203 */
204 HWTEST_F(NetworkSetManagerTest, RegisterObserverTest001, TestSize.Level1)
205 {
206 GTEST_LOG_(INFO) << "RegisterObserverTest Start";
207 try {
208 int32_t userId = 100;
209 string bundleName = "com.ohos.photos";
210 int32_t type = NetworkSetManager::CELLULARCONNECT;
211
212 networkSetManager_->RegisterObserver(bundleName, userId, type);
213 } catch (...) {
214 EXPECT_TRUE(false);
215 GTEST_LOG_(INFO) << "RegisterObserverTest FAILED";
216 }
217 GTEST_LOG_(INFO) << "RegisterObserverTest End";
218 }
219
220 /**
221 * @tc.name: IsAllowCellularConnectTest001
222 * @tc.desc: Verify the IsAllowCellularConnect function
223 * @tc.type: FUNC
224 * @tc.require: I6JPKG
225 */
226 HWTEST_F(NetworkSetManagerTest, IsAllowCellularConnectTest001, TestSize.Level1)
227 {
228 GTEST_LOG_(INFO) << "IsAllowCellularConnectTest Start";
229 try {
230 int32_t userId = 100;
231 string bundleName = "com.ohos.photos";
232
233 networkSetManager_->IsAllowCellularConnect(bundleName, userId);
234 } catch (...) {
235 EXPECT_TRUE(false);
236 GTEST_LOG_(INFO) << "IsAllowCellularConnectTest FAILED";
237 }
238 GTEST_LOG_(INFO) << "IsAllowCellularConnectTest End";
239 }
240
241 /**
242 * @tc.name: IsAllowNetConnectTest001
243 * @tc.desc: Verify the IsAllowNetConnect function
244 * @tc.type: FUNC
245 * @tc.require: I6JPKG
246 */
247 HWTEST_F(NetworkSetManagerTest, IsAllowNetConnectTest001, TestSize.Level1)
248 {
249 GTEST_LOG_(INFO) << "IsAllowNetConnectTest Start";
250 try {
251 int32_t userId = 100;
252 string bundleName = "com.ohos.photos";
253
254 networkSetManager_->IsAllowCellularConnect(bundleName, userId);
255 } catch (...) {
256 EXPECT_TRUE(false);
257 GTEST_LOG_(INFO) << "IsAllowNetConnectTest FAILED";
258 }
259 GTEST_LOG_(INFO) << "IsAllowNetConnectTest End";
260 }
261
262 /**
263 * @tc.name: InitNetworkSetManagerTest001
264 * @tc.desc: Verify the InitNetworkSetManager function
265 * @tc.type: FUNC
266 * @tc.require: I6JPKG
267 */
268 HWTEST_F(NetworkSetManagerTest, InitNetworkSetManagerTest001, TestSize.Level1)
269 {
270 GTEST_LOG_(INFO) << "InitNetworkSetManagerTest Start";
271 try {
272 int32_t userId = 100;
273 string bundleName = "com.ohos.photos";
274
275 networkSetManager_->InitNetworkSetManager(bundleName, userId);
276 } catch (...) {
277 EXPECT_TRUE(false);
278 GTEST_LOG_(INFO) << "InitNetworkSetManagerTest FAILED";
279 }
280 GTEST_LOG_(INFO) << "InitNetworkSetManagerTest End";
281 }
282 }