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