1 /*
2 * Copyright (c) 2023-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
19 #include "dfs_error.h"
20 #include "fuse_assistant.h"
21 #include "fuse_manager/fuse_manager.h"
22 #include "ipc/cloud_daemon.h"
23 #include "iremote_object.h"
24 #include "setting_data_helper.h"
25 #include "system_ability_definition.h"
26 #include "utils_log.h"
27
28 namespace OHOS::FileManagement::CloudFile::Test {
29 using namespace testing;
30 using namespace testing::ext;
31 constexpr int32_t USER_ID = 100;
32 constexpr int32_t DEV_FD = 10;
33
34 class CloudDaemonTest : public testing::Test {
35 public:
36 static void SetUpTestCase(void);
37 static void TearDownTestCase(void);
38 void SetUp();
39 void TearDown();
40 std::shared_ptr<CloudDaemon> cloudDaemon_;
41 static inline shared_ptr<FuseAssistantMock> insMock_ = nullptr;
42 };
43
SetUpTestCase(void)44 void CloudDaemonTest::SetUpTestCase(void)
45 {
46 FuseAssistantMock::EnableMock();
47 GTEST_LOG_(INFO) << "SetUpTestCase";
48 }
49
TearDownTestCase(void)50 void CloudDaemonTest::TearDownTestCase(void)
51 {
52 FuseAssistantMock::DisableMock();
53 GTEST_LOG_(INFO) << "TearDownTestCase";
54 }
55
SetUp(void)56 void CloudDaemonTest::SetUp(void)
57 {
58 int32_t saID = FILEMANAGEMENT_CLOUD_DAEMON_SERVICE_SA_ID;
59 bool runOnCreate = true;
60 insMock_ = make_shared<FuseAssistantMock>();
61 FuseAssistantMock::ins = insMock_;
62 insMock_->EnableMock();
63 cloudDaemon_ = std::make_shared<CloudDaemon>(saID, runOnCreate);
64 GTEST_LOG_(INFO) << "SetUp";
65 }
66
TearDown(void)67 void CloudDaemonTest::TearDown(void)
68 {
69 insMock_->DisableMock();
70 FuseAssistantMock::ins = nullptr;
71 insMock_ = nullptr;
72 GTEST_LOG_(INFO) << "TearDown";
73 }
74
75 /**
76 * @tc.name: OnStopTest
77 * @tc.desc: Verify the OnStop function
78 * @tc.type: FUNC
79 * @tc.require: I6H5MH
80 */
81 HWTEST_F(CloudDaemonTest, OnStopTest, TestSize.Level1)
82 {
83 GTEST_LOG_(INFO) << "OnStop Start";
84 try {
85 cloudDaemon_->state_ = ServiceRunningState::STATE_NOT_START;
86 cloudDaemon_->registerToService_ = false;
87 cloudDaemon_->OnStop();
88 EXPECT_TRUE(true);
89 } catch (...) {
90 EXPECT_TRUE(false);
91 GTEST_LOG_(INFO) << "OnStop ERROR";
92 }
93 GTEST_LOG_(INFO) << "OnStop End";
94 }
95
96 /**
97 * @tc.name: OnStartTest1
98 * @tc.desc: Verify the OnStart function
99 * @tc.type: FUNC
100 * @tc.require: I6H5MH
101 */
102 HWTEST_F(CloudDaemonTest, OnStartTest1, TestSize.Level1)
103 {
104 GTEST_LOG_(INFO) << "OnStart1 Start";
105 try {
106 cloudDaemon_->state_ = ServiceRunningState::STATE_RUNNING;
107 cloudDaemon_->OnStart();
108 EXPECT_TRUE(true);
109 } catch (...) {
110 EXPECT_TRUE(false);
111 GTEST_LOG_(INFO) << "OnStart1 ERROR";
112 }
113 GTEST_LOG_(INFO) << "OnStart1 End";
114 }
115
116 /**
117 * @tc.name: OnStartTest2
118 * @tc.desc: Verify the OnStart function
119 * @tc.type: FUNC
120 * @tc.require: I6H5MH
121 */
122 HWTEST_F(CloudDaemonTest, OnStartTest2, TestSize.Level1)
123 {
124 GTEST_LOG_(INFO) << "OnStart2 Start";
125 try {
126 cloudDaemon_->state_ = ServiceRunningState::STATE_NOT_START;
127 cloudDaemon_->registerToService_ = true;
128 cloudDaemon_->OnStart();
129 EXPECT_TRUE(true);
130 } catch (...) {
131 EXPECT_TRUE(false);
132 GTEST_LOG_(INFO) << "OnStart2 ERROR";
133 }
134 GTEST_LOG_(INFO) << "OnStart2 End";
135 }
136
137 /**
138 * @tc.name: StartFuseTest001
139 * @tc.desc: Verify the StartFuse function
140 * @tc.type: FUNC
141 * @tc.require: issuesIB538J
142 */
143 HWTEST_F(CloudDaemonTest, StartFuseTest001, TestSize.Level1)
144 {
145 GTEST_LOG_(INFO) << "StartFuseTest001 start";
146 std::cout << "StartFuseTest001 Start" << std::endl;
147 try {
148 const char* path = "/cloud/test";
149 int32_t ret = cloudDaemon_->StartFuse(USER_ID, DEV_FD, path);
150 EXPECT_EQ(ret, E_OK);
151 } catch (...) {
152 EXPECT_TRUE(false);
153 GTEST_LOG_(INFO) << "StartFuseTest001 failed";
154 }
155 GTEST_LOG_(INFO) << "StartFuseTest001 end";
156 }
157
158 /**
159 * @tc.name: StartFuseTest002
160 * @tc.desc: Verify the StartFuse function
161 * @tc.type: FUNC
162 * @tc.require: issuesIB538J
163 */
164 HWTEST_F(CloudDaemonTest, StartFuseTest002, TestSize.Level1)
165 {
166 GTEST_LOG_(INFO) << "StartFuseTest002 start";
167 try {
168 std::string path = "/cloud/test";
169 EXPECT_CALL(*insMock_, mkdir(_, _)).WillRepeatedly(Return(-1));
170 int32_t ret = cloudDaemon_->StartFuse(USER_ID, DEV_FD, path);
171 EXPECT_EQ(ret, E_PATH);
172 } catch (...) {
173 EXPECT_TRUE(false);
174 GTEST_LOG_(INFO) << "StartFuseTest002 failed";
175 }
176 GTEST_LOG_(INFO) << "StartFuseTest002 end";
177 }
178
179 /**
180 * @tc.name: StartFuseTest003
181 * @tc.desc: Verify the StartFuse function
182 * @tc.type: FUNC
183 * @tc.require: issuesIB538J
184 */
185 HWTEST_F(CloudDaemonTest, StartFuseTest003, TestSize.Level1)
186 {
187 GTEST_LOG_(INFO) << "StartFuseTest003 start";
188 try {
189 std::string path = "/cloud/test";
190 EXPECT_CALL(*insMock_, mkdir(_, _)).WillRepeatedly(Return(0));
191 EXPECT_CALL(*insMock_, chmod(_, _)).WillRepeatedly(Return(-1));
192 int32_t ret = cloudDaemon_->StartFuse(USER_ID, DEV_FD, path);
193 EXPECT_EQ(ret, E_OK);
194 } catch (...) {
195 EXPECT_TRUE(false);
196 GTEST_LOG_(INFO) << "StartFuseTest003 failed";
197 }
198 GTEST_LOG_(INFO) << "StartFuseTest003 end";
199 }
200
201 /**
202 * @tc.name: StartFuseTest004
203 * @tc.desc: Verify the StartFuse function
204 * @tc.type: FUNC
205 * @tc.require: issuesIB538J
206 */
207 HWTEST_F(CloudDaemonTest, StartFuseTest004, TestSize.Level1)
208 {
209 GTEST_LOG_(INFO) << "StartFuseTest004 start";
210 try {
211 std::string path = "/cloud/test";
212 EXPECT_CALL(*insMock_, mkdir(_, _)).WillRepeatedly(Return(0));
213 EXPECT_CALL(*insMock_, chown(_, _, _)).WillRepeatedly(Return(-1));
214 int32_t ret = cloudDaemon_->StartFuse(USER_ID, DEV_FD, path);
215 EXPECT_EQ(ret, E_OK);
216 } catch (...) {
217 EXPECT_TRUE(false);
218 GTEST_LOG_(INFO) << "StartFuseTest004 failed";
219 }
220 GTEST_LOG_(INFO) << "StartFuseTest004 end";
221 }
222
223 /**
224 * @tc.name: StartFuseTest005
225 * @tc.desc: Verify the StartFuse function
226 * @tc.type: FUNC
227 * @tc.require: issuesIB538J
228 */
229 HWTEST_F(CloudDaemonTest, StartFuseTest005, TestSize.Level1)
230 {
231 GTEST_LOG_(INFO) << "StartFuseTest005 start";
232 try {
233 std::string path = "/cloud_fuse/test";
234 EXPECT_CALL(*insMock_, mkdir(_, _)).WillRepeatedly(Return(0));
235 EXPECT_CALL(*insMock_, HandleStartMove(_)).WillRepeatedly(Return());
236 int32_t ret = cloudDaemon_->StartFuse(USER_ID, DEV_FD, path);
237 EXPECT_EQ(ret, E_OK);
238 } catch (...) {
239 EXPECT_TRUE(false);
240 GTEST_LOG_(INFO) << "StartFuseTest005 failed";
241 }
242 GTEST_LOG_(INFO) << "StartFuseTest005 end";
243 }
244
245 /**
246 * @tc.name: OnAddSystemAbilityTest001
247 * @tc.desc: Verify the OnStart function
248 * @tc.type: FUNC
249 * @tc.require: issuesIB538J
250 */
251 HWTEST_F(CloudDaemonTest, OnAddSystemAbility001, TestSize.Level1)
252 {
253 GTEST_LOG_(INFO) << "OnAddSystemAbility001 start";
254 try {
255 const int32_t systemAbilityId = 100;
256 const std::string deviceId = "device_test";
257 cloudDaemon_->OnAddSystemAbility(systemAbilityId, deviceId);
258 } catch (...) {
259 EXPECT_TRUE(false);
260 GTEST_LOG_(INFO) << "OnAddSystemAbilityTest001 failed";
261 }
262 GTEST_LOG_(INFO) << "OnAddSystemAbilityTest001 end";
263 }
264
265 /**
266 * @tc.name: OnAddSystemAbilityTest002
267 * @tc.desc: Verify the OnAddSystemAbility function
268 * @tc.type: FUNC
269 */
270 HWTEST_F(CloudDaemonTest, OnAddSystemAbilityTest002, TestSize.Level1)
271 {
272 GTEST_LOG_(INFO) << "OnAddSystemAbilityTest002 start";
273 try {
274 const int32_t systemAbilityId = DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID;
275 const std::string deviceId = "device_test";
276 SettingDataHelper::GetInstance().isBundleInited_ = true;
277 cloudDaemon_->OnAddSystemAbility(systemAbilityId, deviceId);
278 } catch (...) {
279 EXPECT_TRUE(false);
280 GTEST_LOG_(INFO) << "OnAddSystemAbilityTest002 failed";
281 }
282 GTEST_LOG_(INFO) << "OnAddSystemAbilityTest002 end";
283 }
284 } // namespace OHOS::FileManagement::CloudSync::Test