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_RDB);
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_RDB);
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: GetNetConnectTest001
114 * @tc.desc: Verify the GetNetConnect function
115 * @tc.type: FUNC
116 * @tc.require: I6JPKG
117 */
118 HWTEST_F(NetworkSetManagerTest, GetNetConnectTest001, TestSize.Level1)
119 {
120 GTEST_LOG_(INFO) << "GetNetConnectTest Start";
121 try {
122 int32_t userId = 100;
123 string bundleName = "com.ohos.photos";
124
125 networkSetManager_->GetNetConnect(bundleName, userId);
126 } catch (...) {
127 EXPECT_TRUE(false);
128 GTEST_LOG_(INFO) << "GetNetConnectTest FAILED";
129 }
130 GTEST_LOG_(INFO) << "GetNetConnectTest End";
131 }
132
133 /**
134 * @tc.name: UnregisterObserverTest001
135 * @tc.desc: Verify the UnregisterObserver function
136 * @tc.type: FUNC
137 * @tc.require: I6JPKG
138 */
139 HWTEST_F(NetworkSetManagerTest, UnregisterObserverTest001, TestSize.Level1)
140 {
141 GTEST_LOG_(INFO) << "UnregisterObserverTest Start";
142 try {
143 int32_t userId = 100;
144 string bundleName = "com.ohos.photos";
145 int32_t type = NetworkSetManager::CELLULARCONNECT;
146
147 networkSetManager_->UnregisterObserver(bundleName, userId, type);
148 } catch (...) {
149 EXPECT_TRUE(false);
150 GTEST_LOG_(INFO) << "UnregisterObserverTest FAILED";
151 }
152 GTEST_LOG_(INFO) << "UnregisterObserverTest End";
153 }
154
155 /**
156 * @tc.name: UnregisterObserverTest002
157 * @tc.desc: Verify the UnregisterObserver function
158 * @tc.type: FUNC
159 * @tc.require: I6JPKG
160 */
161 HWTEST_F(NetworkSetManagerTest, UnregisterObserverTest002, TestSize.Level1)
162 {
163 GTEST_LOG_(INFO) << "UnregisterObserverTest Start";
164 try {
165 int32_t userId = 100;
166 string bundleName = "com.ohos.photos";
167 int32_t type = NetworkSetManager::NETCONNECT;
168
169 networkSetManager_->UnregisterObserver(bundleName, userId, type);
170 } catch (...) {
171 EXPECT_TRUE(false);
172 GTEST_LOG_(INFO) << "UnregisterObserverTest FAILED";
173 }
174 GTEST_LOG_(INFO) << "UnregisterObserverTest End";
175 }
176
177 /**
178 * @tc.name: RegisterObserverTest001
179 * @tc.desc: Verify the RegisterObserver function
180 * @tc.type: FUNC
181 * @tc.require: I6JPKG
182 */
183 HWTEST_F(NetworkSetManagerTest, RegisterObserverTest001, TestSize.Level1)
184 {
185 GTEST_LOG_(INFO) << "RegisterObserverTest Start";
186 try {
187 int32_t userId = 100;
188 string bundleName = "com.ohos.photos";
189 int32_t type = NetworkSetManager::CELLULARCONNECT;
190
191 networkSetManager_->RegisterObserver(bundleName, userId, type);
192 } catch (...) {
193 EXPECT_TRUE(false);
194 GTEST_LOG_(INFO) << "RegisterObserverTest FAILED";
195 }
196 GTEST_LOG_(INFO) << "RegisterObserverTest End";
197 }
198
199 }