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 <filesystem>
17 #include <fstream>
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20
21 #include "all_connect_manager_mock.h"
22 #include "daemon_execute.h"
23 #include "dfs_error.h"
24 #include "sandbox_helper.h"
25 #include "softbus_handler_asset_mock.h"
26 #include "softbus_handler_mock.h"
27
28 namespace {
29 std::string g_physicalPath;
30 int32_t g_getPhysicalPath = 0;
31 bool g_checkValidPath = false;
32 bool g_isFolder;
33 } // namespace
34
35 namespace OHOS::AppFileService {
GetPhysicalPath(const std::string & fileUri,const std::string & userId,std::string & physicalPath)36 int32_t SandboxHelper::GetPhysicalPath(const std::string &fileUri, const std::string &userId, std::string &physicalPath)
37 {
38 physicalPath = g_physicalPath;
39 return g_getPhysicalPath;
40 }
41
CheckValidPath(const std::string & filePath)42 bool SandboxHelper::CheckValidPath(const std::string &filePath)
43 {
44 return g_checkValidPath;
45 }
46 } // namespace OHOS::AppFileService
47
48 namespace OHOS::Storage::DistributedFile::Utils {
IsFolder(const std::string & name)49 bool IsFolder(const std::string &name)
50 {
51 return g_isFolder;
52 }
53 } // namespace OHOS::Storage::DistributedFile::Utils
54
55 namespace OHOS::Storage::DistributedFile::Test {
56 using namespace OHOS::FileManagement;
57 using namespace testing;
58 using namespace testing::ext;
59 using namespace std;
60 constexpr int32_t BLOCK_INTERVAL_SEND_FILE = 8 * 1000;
61 class DaemonExecuteTest : public testing::Test {
62 public:
63 static void SetUpTestCase(void);
64 static void TearDownTestCase(void);
65 void SetUp();
66 void TearDown();
67 std::shared_ptr<DaemonExecute> daemonExecute_;
68
69 public:
70 static inline std::shared_ptr<SoftBusHandlerAssetMock> softBusHandlerAssetMock_ = nullptr;
71 static inline std::shared_ptr<SoftBusHandlerMock> softBusHandlerMock_ = nullptr;
72 static inline std::shared_ptr<AllConnectManagerMock> allConnectManagerMock_ = nullptr;
73 };
74
SetUpTestCase(void)75 void DaemonExecuteTest::SetUpTestCase(void)
76 {
77 GTEST_LOG_(INFO) << "SetUpTestCase";
78 softBusHandlerAssetMock_ = std::make_shared<SoftBusHandlerAssetMock>();
79 ISoftBusHandlerAssetMock::iSoftBusHandlerAssetMock_ = softBusHandlerAssetMock_;
80 allConnectManagerMock_ = std::make_shared<AllConnectManagerMock>();
81 IAllConnectManagerMock::iAllConnectManagerMock_ = allConnectManagerMock_;
82 softBusHandlerMock_ = std::make_shared<SoftBusHandlerMock>();
83 ISoftBusHandlerMock::iSoftBusHandlerMock_ = softBusHandlerMock_;
84
85 std::string path = "/mnt/hmdfs/100/account/device_view/local/data/com.example.app";
86 if (!std::filesystem::exists(path)) {
87 std::filesystem::create_directory(path);
88 std::filesystem::create_directory(path + "/docs");
89 }
90 std::ofstream file1(path + "/1.txt");
91 std::ofstream file2(path + "/2.txt");
92 }
93
TearDownTestCase(void)94 void DaemonExecuteTest::TearDownTestCase(void)
95 {
96 GTEST_LOG_(INFO) << "TearDownTestCase";
97 ISoftBusHandlerAssetMock::iSoftBusHandlerAssetMock_ = nullptr;
98 softBusHandlerAssetMock_ = nullptr;
99 IAllConnectManagerMock::iAllConnectManagerMock_ = nullptr;
100 allConnectManagerMock_ = nullptr;
101 ISoftBusHandlerMock::iSoftBusHandlerMock_ = nullptr;
102 softBusHandlerMock_ = nullptr;
103
104 std::string path = "/mnt/hmdfs/100/account/device_view/local/data/com.example.app";
105 if (std::filesystem::exists(path)) {
106 std::filesystem::remove_all(path);
107 }
108 }
109
SetUp(void)110 void DaemonExecuteTest::SetUp(void)
111 {
112 GTEST_LOG_(INFO) << "SetUp";
113 daemonExecute_ = std::make_shared<DaemonExecute>();
114 }
115
TearDown(void)116 void DaemonExecuteTest::TearDown(void)
117 {
118 GTEST_LOG_(INFO) << "TearDown";
119 daemonExecute_ = nullptr;
120 }
121
122 /**
123 * @tc.name: DaemonExecute_ExecutePushAsset_001
124 * @tc.desc: verify ExecutePushAsset.
125 * @tc.type: FUNC
126 * @tc.require: I7TDJK
127 */
128 HWTEST_F(DaemonExecuteTest, DaemonExecute_ExecutePushAsset_001, TestSize.Level1)
129 {
130 GTEST_LOG_(INFO) << "DaemonExecute_ExecutePushAsset_001 begin";
131 std::shared_ptr<PushAssetData> pushData1 = nullptr;
132 auto eventptr = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_PUSH_ASSET, pushData1, 0);
133 eventptr.reset(nullptr);
134 daemonExecute_->ExecutePushAsset(eventptr);
135
136 std::shared_ptr<PushAssetData> pushData = nullptr;
137 auto event = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_PUSH_ASSET, pushData, 0);
138 daemonExecute_->ExecutePushAsset(event);
139
140 int32_t userId = 100;
141 sptr<AssetObj> assetObj = nullptr;
142 pushData = std::make_shared<PushAssetData>(userId, assetObj);
143 event = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_PUSH_ASSET, pushData, 0);
144 daemonExecute_->ExecutePushAsset(event);
145
146 assetObj = new (std::nothrow) AssetObj();
147 pushData = std::make_shared<PushAssetData>(userId, assetObj);
148 event = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_PUSH_ASSET, pushData, 0);
149 EXPECT_CALL(*softBusHandlerAssetMock_, AssetBind(_, _)).WillOnce(Return(-1));
150 daemonExecute_->ExecutePushAsset(event);
151
152 assetObj->uris_.push_back("test");
153 assetObj->srcBundleName_ = "srcBundleName_";
154 pushData = std::make_shared<PushAssetData>(userId, assetObj);
155 event = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_PUSH_ASSET, pushData, 0);
156 EXPECT_CALL(*softBusHandlerAssetMock_, AssetBind(_, _)).WillOnce(Return(E_OK));
157 daemonExecute_->ExecutePushAsset(event);
158
159 assetObj->uris_.clear();
160 assetObj->uris_.push_back("file://com.example.app/data/storage/el2/distributedfiles/docs/1.txt");
161 assetObj->srcBundleName_ = "com.example.app";
162 g_getPhysicalPath = E_OK;
163 g_checkValidPath = true;
164 g_isFolder = false;
165 g_physicalPath = "/mnt/hmdfs/100/account/device_view/local/data/com.example.app/docs/1.txt";
166 pushData = std::make_shared<PushAssetData>(userId, assetObj);
167 event = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_PUSH_ASSET, pushData, 0);
168 EXPECT_CALL(*softBusHandlerAssetMock_, AssetBind(_, _)).WillOnce(Return(E_OK));
169 EXPECT_CALL(*softBusHandlerAssetMock_, AssetSendFile(_, _, _)).WillOnce(Return(-1));
170 daemonExecute_->ExecutePushAsset(event);
171
172 EXPECT_CALL(*softBusHandlerAssetMock_, AssetBind(_, _)).WillOnce(Return(E_OK));
173 EXPECT_CALL(*softBusHandlerAssetMock_, AssetSendFile(_, _, _)).WillOnce(Return(E_OK));
174 daemonExecute_->ExecutePushAsset(event);
175
176 assetObj->uris_.push_back("file://com.example.app/data/storage/el2/distributedfiles/docs/2.txt");
177 pushData = std::make_shared<PushAssetData>(userId, assetObj);
178 event = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_PUSH_ASSET, pushData, 0);
179 EXPECT_CALL(*softBusHandlerAssetMock_, AssetBind(_, _)).WillOnce(Return(E_OK));
180 EXPECT_CALL(*softBusHandlerAssetMock_, CompressFile(_, _, _)).WillOnce(Return(-1));
181 daemonExecute_->ExecutePushAsset(event);
182 GTEST_LOG_(INFO) << "DaemonExecute_ExecutePushAsset_001 end";
183 }
184
185 /**
186 * @tc.name: DaemonExecute_ExecuteRequestSendFile_001
187 * @tc.desc: verify ExecuteRequestSendFile.
188 * @tc.type: FUNC
189 * @tc.require: I7TDJK
190 */
191 HWTEST_F(DaemonExecuteTest, DaemonExecute_ExecuteRequestSendFile_001, TestSize.Level1)
192 {
193 GTEST_LOG_(INFO) << "DaemonExecute_ExecuteRequestSendFile_001 begin";
194 std::shared_ptr<RequestSendFileData> requestSendFileData1 = nullptr;
195 auto eventptr = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_REQUEST_SEND_FILE, requestSendFileData1, 0);
196 eventptr.reset(nullptr);
197 daemonExecute_->ExecuteRequestSendFile(eventptr);
198
199 std::shared_ptr<RequestSendFileData> requestSendFileData = nullptr;
200 auto event = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_REQUEST_SEND_FILE, requestSendFileData, 0);
201 daemonExecute_->ExecuteRequestSendFile(event);
202
203 requestSendFileData = std::make_shared<RequestSendFileData>("", "", "", "", nullptr);
204 event = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_REQUEST_SEND_FILE, requestSendFileData, 0);
205 daemonExecute_->ExecuteRequestSendFile(event);
206
207 auto requestSendFileBlock = std::make_shared<BlockObject<int32_t>>(BLOCK_INTERVAL_SEND_FILE, ERR_BAD_VALUE);
208 requestSendFileData = std::make_shared<RequestSendFileData>("", "", "", "", requestSendFileBlock);
209 event = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_REQUEST_SEND_FILE, requestSendFileData, 0);
210 daemonExecute_->ExecuteRequestSendFile(event);
211 EXPECT_EQ(requestSendFileBlock->GetValue(), ERR_BAD_VALUE);
212
213 requestSendFileData = std::make_shared<RequestSendFileData>("test", "", "", "", requestSendFileBlock);
214 event = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_REQUEST_SEND_FILE, requestSendFileData, 0);
215 daemonExecute_->ExecuteRequestSendFile(event);
216 EXPECT_EQ(requestSendFileBlock->GetValue(), ERR_BAD_VALUE);
217
218 requestSendFileData = std::make_shared<RequestSendFileData>("test", "", "test", "", requestSendFileBlock);
219 event = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_REQUEST_SEND_FILE, requestSendFileData, 0);
220 daemonExecute_->ExecuteRequestSendFile(event);
221 EXPECT_EQ(requestSendFileBlock->GetValue(), ERR_BAD_VALUE);
222
223 requestSendFileData = std::make_shared<RequestSendFileData>("test", "", "test", "test", requestSendFileBlock);
224 event = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_REQUEST_SEND_FILE, requestSendFileData, 0);
225 EXPECT_CALL(*allConnectManagerMock_, ApplyAdvancedResource(_, _)).WillOnce(Return(-1));
226 daemonExecute_->ExecuteRequestSendFile(event);
227 EXPECT_EQ(requestSendFileBlock->GetValue(), ERR_APPLY_RESULT);
228 GTEST_LOG_(INFO) << "DaemonExecute_ExecuteRequestSendFile_001 end";
229 }
230
231 /**
232 * @tc.name: DaemonExecute_RequestSendFileInner_001
233 * @tc.desc: verify RequestSendFileInner.
234 * @tc.type: FUNC
235 * @tc.require: I7TDJK
236 */
237 HWTEST_F(DaemonExecuteTest, DaemonExecute_RequestSendFileInner_001, TestSize.Level1)
238 {
239 GTEST_LOG_(INFO) << "DaemonExecute_RequestSendFileInner_001 begin";
240 std::string srcUri;
241 std::string dstPath;
242 std::string dstDeviceId;
243 std::string sessionName;
244 EXPECT_CALL(*allConnectManagerMock_, ApplyAdvancedResource(_, _)).WillOnce(Return(-1));
245 EXPECT_EQ(daemonExecute_->RequestSendFileInner(srcUri, dstPath, dstDeviceId, sessionName), ERR_APPLY_RESULT);
246
247 EXPECT_CALL(*allConnectManagerMock_, ApplyAdvancedResource(_, _)).WillOnce(Return(E_OK));
248 EXPECT_CALL(*softBusHandlerMock_, OpenSession(_, _, _, _)).WillOnce(Return(-1));
249 EXPECT_EQ(daemonExecute_->RequestSendFileInner(srcUri, dstPath, dstDeviceId, sessionName), -1);
250
251 EXPECT_CALL(*allConnectManagerMock_, ApplyAdvancedResource(_, _)).WillOnce(Return(E_OK));
252 EXPECT_CALL(*softBusHandlerMock_, OpenSession(_, _, _, _)).WillOnce(Return(E_OK));
253 EXPECT_CALL(*softBusHandlerMock_, CopySendFile(_, _, _, _)).WillOnce(Return(-1));
254 EXPECT_EQ(daemonExecute_->RequestSendFileInner(srcUri, dstPath, dstDeviceId, sessionName), -1);
255
256 EXPECT_CALL(*allConnectManagerMock_, ApplyAdvancedResource(_, _)).WillOnce(Return(E_OK));
257 EXPECT_CALL(*softBusHandlerMock_, OpenSession(_, _, _, _)).WillOnce(Return(E_OK));
258 EXPECT_CALL(*softBusHandlerMock_, CopySendFile(_, _, _, _)).WillOnce(Return(E_OK));
259 EXPECT_EQ(daemonExecute_->RequestSendFileInner(srcUri, dstPath, dstDeviceId, sessionName), E_OK);
260 GTEST_LOG_(INFO) << "DaemonExecute_RequestSendFileInner_001 end";
261 }
262
263 /**
264 * @tc.name: DaemonExecute_GetFileList_001
265 * @tc.desc: verify GetFileList.
266 * @tc.type: FUNC
267 * @tc.require: I7TDJK
268 */
269 HWTEST_F(DaemonExecuteTest, DaemonExecute_GetFileList_001, TestSize.Level1)
270 {
271 GTEST_LOG_(INFO) << "DaemonExecute_GetFileList_001 begin";
272 std::vector<std::string> uris;
273 int32_t userId = 100;
274 std::string srcBundleName;
275
276 uris.push_back("test");
277 srcBundleName = "com.example.app";
278 auto ret = daemonExecute_->GetFileList(uris, userId, srcBundleName);
279 EXPECT_TRUE(ret.empty());
280
281 uris.clear();
282 uris.push_back("file://com.example.app/data/storage/el2/distributedfiles/docs/1.txt");
283 g_getPhysicalPath = -1;
284 ret = daemonExecute_->GetFileList(uris, userId, srcBundleName);
285 EXPECT_TRUE(ret.empty());
286
287 ret = daemonExecute_->GetFileList(uris, userId, srcBundleName);
288 g_getPhysicalPath = E_OK;
289 g_checkValidPath = false;
290 EXPECT_TRUE(ret.empty());
291
292 g_checkValidPath = true;
293 g_isFolder = true;
294 ret = daemonExecute_->GetFileList(uris, userId, srcBundleName);
295 EXPECT_TRUE(ret.empty());
296
297 g_isFolder = false;
298 g_physicalPath = "test";
299 ret = daemonExecute_->GetFileList(uris, userId, srcBundleName);
300 EXPECT_TRUE(!ret.empty());
301
302 g_physicalPath = "/mnt/hmdfs/100/account/device_view/local/data/com.example.app/docs/1.txt";
303 ret = daemonExecute_->GetFileList(uris, userId, srcBundleName);
304 EXPECT_TRUE(!ret.empty());
305 GTEST_LOG_(INFO) << "DaemonExecute_GetFileList_001 end";
306 }
307
308 /**
309 * @tc.name: DaemonExecute_HandleZip_001
310 * @tc.desc: verify HandleZip.
311 * @tc.type: FUNC
312 * @tc.require: I7TDJK
313 */
314 HWTEST_F(DaemonExecuteTest, DaemonExecute_HandleZip_001, TestSize.Level1)
315 {
316 GTEST_LOG_(INFO) << "DaemonExecute_HandleZip_001 begin";
317 std::vector<std::string> fileList;
318 sptr<AssetObj> assetObj(new (std::nothrow) AssetObj());
319 std::string sendFileName;
320 bool isSingleFile;
321
322 fileList.emplace_back("/mnt/hmdfs/100/account/device_view/local/data/com.example.app/docs/1.txt");
323 EXPECT_EQ(daemonExecute_->HandleZip(fileList, assetObj, sendFileName, isSingleFile), E_OK);
324
325 fileList.emplace_back("/mnt/hmdfs/100/account/device_view/local/data/com.example.app/docs/2.txt");
326 assetObj->srcBundleName_ = "com.example.appsa";
327 EXPECT_EQ(daemonExecute_->HandleZip(fileList, assetObj, sendFileName, isSingleFile), E_ZIP);
328
329 assetObj->srcBundleName_ = "com.example.app";
330 EXPECT_CALL(*softBusHandlerAssetMock_, CompressFile(_, _, _)).WillOnce(Return(-1));
331 EXPECT_EQ(daemonExecute_->HandleZip(fileList, assetObj, sendFileName, isSingleFile), E_ZIP);
332
333 EXPECT_CALL(*softBusHandlerAssetMock_, CompressFile(_, _, _)).WillOnce(Return(E_OK));
334 EXPECT_EQ(daemonExecute_->HandleZip(fileList, assetObj, sendFileName, isSingleFile), E_OK);
335 GTEST_LOG_(INFO) << "DaemonExecute_HandleZip_001 end";
336 }
337 } // namespace OHOS::Storage::DistributedFile::Test