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
19 #include "clouddisk_notify_utils.h"
20 #include "clouddisk_rdb_utils.h"
21 #include "file_column.h"
22 #include "cloud_pref_impl.h"
23 #include "dfs_error.h"
24 #include "utils_log.h"
25
26 namespace OHOS {
27 namespace FileManagement::CloudDisk {
28 namespace Test {
29 using namespace testing::ext;
30 using namespace std;
31 const int32_t MOCKUSERID = 0;
32 const int32_t MOCK1 = 1;
33 const int32_t MOCK2 = 2;
34 const int32_t MOCK3 = 3;
35
MockFunc(CloudDiskFuseData * data,int64_t inode)36 std::shared_ptr<CloudDiskInode> MockFunc(CloudDiskFuseData* data, int64_t inode)
37 {
38 if (data->userId == MOCKUSERID) {
39 return nullptr;
40 }
41 std::shared_ptr<CloudDiskInode> inoPtr = make_shared<CloudDiskInode>();
42 if (data->userId == MOCK1) {
43 inoPtr->parent = FUSE_ROOT_ID;
44 }
45 if (data->userId == MOCK2) {
46 inoPtr->parent = 0;
47 inoPtr->fileName = "";
48 }
49 if (data->userId == MOCK3) {
50 inoPtr->fileName = "MOCK3";
51 }
52 return inoPtr;
53 }
54
55 class CloudDiskNotifyUtilsTest : public testing::Test {
56 public:
57 static void SetUpTestCase(void);
58 static void TearDownTestCase(void);
59 void SetUp();
60 void TearDown();
61 };
62
SetUpTestCase(void)63 void CloudDiskNotifyUtilsTest::SetUpTestCase(void)
64 {
65 std::cout << "SetUpTestCase" << std::endl;
66 }
67
TearDownTestCase(void)68 void CloudDiskNotifyUtilsTest::TearDownTestCase(void)
69 {
70 std::cout << "TearDownTestCase" << std::endl;
71 }
72
SetUp(void)73 void CloudDiskNotifyUtilsTest::SetUp(void)
74 {
75 std::cout << "SetUp" << std::endl;
76 }
77
TearDown(void)78 void CloudDiskNotifyUtilsTest::TearDown(void)
79 {
80 std::cout << "TearDown" << std::endl;
81 }
82
83 /**
84 * @tc.name: GetNotifyDataTest001
85 * @tc.desc: Verify the GetNotifyData function.
86 * @tc.type: FUNC
87 * @tc.require: I6H5MH
88 */
89 HWTEST_F(CloudDiskNotifyUtilsTest, GetNotifyDataTest001, TestSize.Level1)
90 {
91 GTEST_LOG_(INFO) << "GetNotifyData Start";
92 CloudDiskNotifyUtils CloudDiskNotifyUtils;
93 CloudDiskFuseData* data = new CloudDiskFuseData();
94 FindCloudDiskInodeFunc func = MockFunc;
95 fuse_ino_t ino = FUSE_ROOT_ID;
96 NotifyData notifyData;
97 int ret = CloudDiskNotifyUtils.GetNotifyData(data, func, ino, notifyData);
98 EXPECT_EQ(ret, E_INVAL_ARG);
99 delete data;
100 GTEST_LOG_(INFO) << "GetNotifyData End";
101 }
102
103 /**
104 * @tc.name: GetNotifyDataTest002
105 * @tc.desc: Verify the GetNotifyData function.
106 * @tc.type: FUNC
107 * @tc.require: I6H5MH
108 */
109 HWTEST_F(CloudDiskNotifyUtilsTest, GetNotifyDataTest002, TestSize.Level1)
110 {
111 GTEST_LOG_(INFO) << "GetNotifyData Start";
112 CloudDiskNotifyUtils CloudDiskNotifyUtils;
113 CloudDiskFuseData* data = new CloudDiskFuseData();
114 data->userId = MOCKUSERID;
115 FindCloudDiskInodeFunc func = MockFunc;
116 fuse_ino_t ino = 0;
117 NotifyData notifyData;
118 int ret = CloudDiskNotifyUtils.GetNotifyData(data, func, ino, notifyData);
119 EXPECT_EQ(ret, E_INVAL_ARG);
120 delete data;
121 GTEST_LOG_(INFO) << "GetNotifyData End";
122 }
123
124 /**
125 * @tc.name: GetNotifyDataTest003
126 * @tc.desc: Verify the GetNotifyData function.
127 * @tc.type: FUNC
128 * @tc.require: I6H5MH
129 */
130 HWTEST_F(CloudDiskNotifyUtilsTest, GetNotifyDataTest003, TestSize.Level1)
131 {
132 GTEST_LOG_(INFO) << "GetNotifyData Start";
133 CloudDiskNotifyUtils CloudDiskNotifyUtils;
134 CloudDiskFuseData* data = new CloudDiskFuseData();
135 data->userId = MOCK1;
136 FindCloudDiskInodeFunc func = MockFunc;
137 fuse_ino_t ino = 0;
138 NotifyData notifyData;
139 int ret = CloudDiskNotifyUtils.GetNotifyData(data, func, ino, notifyData);
140 EXPECT_EQ(ret, E_OK);
141 delete data;
142 GTEST_LOG_(INFO) << "GetNotifyData End";
143 }
144
145 /**
146 * @tc.name: GetNotifyDataTest004
147 * @tc.desc: Verify the GetNotifyData function.
148 * @tc.type: FUNC
149 * @tc.require: I6H5MH
150 */
151 HWTEST_F(CloudDiskNotifyUtilsTest, GetNotifyDataTest004, TestSize.Level1)
152 {
153 GTEST_LOG_(INFO) << "GetNotifyData Start";
154 CloudDiskNotifyUtils CloudDiskNotifyUtils;
155 CloudDiskFuseData* data = new CloudDiskFuseData();
156 data->userId = MOCK2;
157 FindCloudDiskInodeFunc func = MockFunc;
158 fuse_ino_t ino = 0;
159 NotifyData notifyData;
160 int ret = CloudDiskNotifyUtils.GetNotifyData(data, func, ino, notifyData);
161 EXPECT_EQ(ret, E_OK);
162 delete data;
163 GTEST_LOG_(INFO) << "GetNotifyData End";
164 }
165
166 /**
167 * @tc.name: GetNotifyDataTest005
168 * @tc.desc: Verify the GetNotifyData function.
169 * @tc.type: FUNC
170 * @tc.require: I6H5MH
171 */
172 HWTEST_F(CloudDiskNotifyUtilsTest, GetNotifyDataTest005, TestSize.Level1)
173 {
174 GTEST_LOG_(INFO) << "GetNotifyData Start";
175 CloudDiskNotifyUtils CloudDiskNotifyUtils;
176 CloudDiskFuseData* data = new CloudDiskFuseData();
177 data->userId = MOCKUSERID;
178 FindCloudDiskInodeFunc func = MockFunc;
179 fuse_ino_t parent = 0;
180 string name = "test";
181 NotifyData notifyData;
182 int ret = CloudDiskNotifyUtils.GetNotifyData(data, func, parent, name, notifyData);
183 EXPECT_EQ(ret, E_INVAL_ARG);
184 delete data;
185 GTEST_LOG_(INFO) << "GetNotifyData End";
186 }
187
188 /**
189 * @tc.name: GetNotifyDataTest006
190 * @tc.desc: Verify the GetNotifyData function.
191 * @tc.type: FUNC
192 * @tc.require: I6H5MH
193 */
194 HWTEST_F(CloudDiskNotifyUtilsTest, GetNotifyDataTest006, TestSize.Level1)
195 {
196 GTEST_LOG_(INFO) << "GetNotifyData Start";
197 CloudDiskNotifyUtils CloudDiskNotifyUtils;
198 CloudDiskFuseData* data = new CloudDiskFuseData();
199 data->userId = MOCK2;
200 FindCloudDiskInodeFunc func = MockFunc;
201 fuse_ino_t parent = 0;
202 string name = "test";
203 NotifyData notifyData;
204 int ret = CloudDiskNotifyUtils.GetNotifyData(data, func, parent, name, notifyData);
205 EXPECT_EQ(ret, E_OK);
206 delete data;
207 GTEST_LOG_(INFO) << "GetNotifyData End";
208 }
209
210 /**
211 * @tc.name: GetNotifyDataTest01
212 * @tc.desc: Verify the GetNotifyData function.
213 * @tc.type: FUNC
214 * @tc.require: I6H5MH
215 */
216 HWTEST_F(CloudDiskNotifyUtilsTest, GetNotifyDataTest01, TestSize.Level1)
217 {
218 GTEST_LOG_(INFO) << "GetNotifyData Start";
219 CloudDiskNotifyUtils CloudDiskNotifyUtils;
220 CloudDiskFuseData* data = new CloudDiskFuseData();
221 data->userId = MOCK3;
222 FindCloudDiskInodeFunc func = MockFunc;
223 fuse_ino_t parent = 0;
224 string name = "test";
225 NotifyData notifyData;
226 int ret = CloudDiskNotifyUtils.GetNotifyData(data, func, parent, name, notifyData);
227 EXPECT_EQ(ret, E_INVAL_ARG);
228 delete data;
229 GTEST_LOG_(INFO) << "GetNotifyData End";
230 }
231
232 /**
233 * @tc.name: GetNotifyDataTest007
234 * @tc.desc: Verify the GetNotifyData function.
235 * @tc.type: FUNC
236 * @tc.require: I6H5MH
237 */
238 HWTEST_F(CloudDiskNotifyUtilsTest, GetNotifyDataTest007, TestSize.Level1)
239 {
240 GTEST_LOG_(INFO) << "GetNotifyData Start";
241 CloudDiskNotifyUtils CloudDiskNotifyUtils;
242 CloudDiskFuseData* data = new CloudDiskFuseData();
243 FindCloudDiskInodeFunc func = MockFunc;
244 NotifyData notifyData;
245 int ret = CloudDiskNotifyUtils.GetNotifyData(data, func, nullptr, notifyData);
246 EXPECT_EQ(ret, E_INVAL_ARG);
247 delete data;
248 GTEST_LOG_(INFO) << "GetNotifyData End";
249 }
250
251 /**
252 * @tc.name: GetNotifyDataTest008
253 * @tc.desc: Verify the GetNotifyData function.
254 * @tc.type: FUNC
255 * @tc.require: I6H5MH
256 */
257 HWTEST_F(CloudDiskNotifyUtilsTest, GetNotifyDataTest008, TestSize.Level1)
258 {
259 GTEST_LOG_(INFO) << "GetNotifyData Start";
260 CloudDiskNotifyUtils CloudDiskNotifyUtils;
261 CloudDiskFuseData* data = new CloudDiskFuseData();
262 FindCloudDiskInodeFunc func = MockFunc;
263 shared_ptr<CloudDiskInode> inoPtr = make_shared<CloudDiskInode>();
264 NotifyData notifyData;
265 int ret = CloudDiskNotifyUtils.GetNotifyData(data, func, inoPtr, notifyData);
266 EXPECT_EQ(ret, E_OK);
267 delete data;
268 GTEST_LOG_(INFO) << "GetNotifyData End";
269 }
270
271 /**
272 * @tc.name: GetNotifyDataTest009
273 * @tc.desc: Verify the GetNotifyData function.
274 * @tc.type: FUNC
275 * @tc.require: I6H5MH
276 */
277 HWTEST_F(CloudDiskNotifyUtilsTest, GetNotifyDataTest009, TestSize.Level1)
278 {
279 GTEST_LOG_(INFO) << "GetNotifyData Start";
280 CloudDiskNotifyUtils CloudDiskNotifyUtils;
281 CloudDiskFuseData* data = new CloudDiskFuseData();
282 FindCloudDiskInodeFunc func = MockFunc;
283 string name = "test";
284 NotifyData notifyData;
285 int ret = CloudDiskNotifyUtils.GetNotifyData(data, func, nullptr, name, notifyData);
286 EXPECT_EQ(ret, E_INVAL_ARG);
287 delete data;
288 GTEST_LOG_(INFO) << "GetNotifyData End";
289 }
290
291 /**
292 * @tc.name: GetNotifyDataTest010
293 * @tc.desc: Verify the GetNotifyData function.
294 * @tc.type: FUNC
295 * @tc.require: I6H5MH
296 */
297 HWTEST_F(CloudDiskNotifyUtilsTest, GetNotifyDataTest010, TestSize.Level1)
298 {
299 GTEST_LOG_(INFO) << "GetNotifyData Start";
300 CloudDiskNotifyUtils CloudDiskNotifyUtils;
301 CloudDiskFuseData* data = new CloudDiskFuseData();
302 FindCloudDiskInodeFunc func = MockFunc;
303 shared_ptr<CloudDiskInode> pInoPtr = make_shared<CloudDiskInode>();
304 pInoPtr->fileName = "test";
305 string name = "test";
306 NotifyData notifyData;
307 int ret = CloudDiskNotifyUtils.GetNotifyData(data, func, pInoPtr, name, notifyData);
308 EXPECT_EQ(ret, E_OK);
309 delete data;
310 GTEST_LOG_(INFO) << "GetNotifyData End";
311 }
312
313 /**
314 * @tc.name: GetNotifyDataTest011
315 * @tc.desc: Verify the GetNotifyData function.
316 * @tc.type: FUNC
317 * @tc.require: I6H5MH
318 */
319 HWTEST_F(CloudDiskNotifyUtilsTest, GetNotifyDataTest011, TestSize.Level1)
320 {
321 GTEST_LOG_(INFO) << "GetNotifyData Start";
322 CloudDiskNotifyUtils CloudDiskNotifyUtils;
323 CloudDiskFuseData* data = new CloudDiskFuseData();
324 FindCloudDiskInodeFunc func = MockFunc;
325 shared_ptr<CloudDiskInode> pInoPtr = make_shared<CloudDiskInode>();
326 pInoPtr->fileName = "";
327 string name = "test";
328 NotifyData notifyData;
329 int ret = CloudDiskNotifyUtils.GetNotifyData(data, func, pInoPtr, name, notifyData);
330 EXPECT_EQ(ret, E_OK);
331 delete data;
332 GTEST_LOG_(INFO) << "GetNotifyData End";
333 }
334
335 /**
336 * @tc.name: GetCacheNodeTest001
337 * @tc.desc: Verify the GetCacheNode function.
338 * @tc.type: FUNC
339 * @tc.require: I6H5MH
340 */
341 HWTEST_F(CloudDiskNotifyUtilsTest, GetCacheNodeTest001, TestSize.Level1)
342 {
343 GTEST_LOG_(INFO) << "GetCacheNode Start";
344 CloudDiskNotifyUtils CloudDiskNotifyUtils;
345 string cloudId = "100";
346 CacheNode cacheNode;
347 int ret = CloudDiskNotifyUtils.GetCacheNode(cloudId, cacheNode);
348 EXPECT_EQ(ret, E_INVAL_ARG);
349 GTEST_LOG_(INFO) << "GetCacheNode End";
350 }
351
352 /**
353 * @tc.name: GetCacheNodeTest002
354 * @tc.desc: Verify the GetCacheNode function.
355 * @tc.type: FUNC
356 * @tc.require: I6H5MH
357 */
358 HWTEST_F(CloudDiskNotifyUtilsTest, GetCacheNodeTest002, TestSize.Level1)
359 {
360 GTEST_LOG_(INFO) << "GetCacheNode Start";
361 CacheNode cacheNode;
362 CloudDiskNotifyUtils CloudDiskNotifyUtils;
363 auto iter = (CloudDiskNotifyUtils.cacheList_)
364 .insert(CloudDiskNotifyUtils.cacheList_.end(), std::make_pair("key", cacheNode));
365 (CloudDiskNotifyUtils.cacheMap_).insert(std::make_pair("key", iter));
366 string cloudId = "key";
367
368 int ret = CloudDiskNotifyUtils.GetCacheNode(cloudId, cacheNode);
369 EXPECT_EQ(ret, E_OK);
370 GTEST_LOG_(INFO) << "GetCacheNode End";
371 }
372
373 /**
374 * @tc.name: PutCacheNodeTest001
375 * @tc.desc: Verify the PutCacheNode function.
376 * @tc.type: FUNC
377 * @tc.require: I6H5MH
378 */
379 HWTEST_F(CloudDiskNotifyUtilsTest, PutCacheNodeTest001, TestSize.Level1)
380 {
381 GTEST_LOG_(INFO) << "PutCacheNode Start";
382 CloudDiskNotifyUtils CloudDiskNotifyUtils;
383 string cloudId = "100";
384 CacheNode cacheNode;
385 cacheNode.isDir = "";
386 CloudDiskNotifyUtils.PutCacheNode(cloudId, cacheNode);
387 GTEST_LOG_(INFO) << "PutCacheNode End";
388 }
389
390 /**
391 * @tc.name: PutCacheNodeTest002
392 * @tc.desc: Verify the PutCacheNode function.
393 * @tc.type: FUNC
394 * @tc.require: I6H5MH
395 */
396 HWTEST_F(CloudDiskNotifyUtilsTest, PutCacheNodeTest002, TestSize.Level1)
397 {
398 GTEST_LOG_(INFO) << "PutCacheNode Start";
399 CloudDiskNotifyUtils CloudDiskNotifyUtils;
400 string cloudId = "100";
401 CacheNode cacheNode;
402 cacheNode.isDir = "directory";
403 CloudDiskNotifyUtils.PutCacheNode(cloudId, cacheNode);
404 GTEST_LOG_(INFO) << "PutCacheNode End";
405 }
406
407 /**
408 * @tc.name: PutCacheNodeTest003
409 * @tc.desc: Verify the PutCacheNode function.
410 * @tc.type: FUNC
411 * @tc.require: I6H5MH
412 */
413 HWTEST_F(CloudDiskNotifyUtilsTest, PutCacheNodeTest003, TestSize.Level1)
414 {
415 GTEST_LOG_(INFO) << "PutCacheNode Start";
416 CacheNode cacheNode;
417 CloudDiskNotifyUtils CloudDiskNotifyUtils;
418 auto iter = (CloudDiskNotifyUtils.cacheList_)
419 .insert(CloudDiskNotifyUtils.cacheList_.end(), std::make_pair("key", cacheNode));
420 (CloudDiskNotifyUtils.cacheMap_).insert(std::make_pair("key", iter));
421 string cloudId = "key";
422 cacheNode.isDir = TYPE_DIR_STR;
423
424 CloudDiskNotifyUtils.PutCacheNode(cloudId, cacheNode);
425 auto res = CloudDiskNotifyUtils.cacheMap_.find("key");
426 EXPECT_EQ(cloudId, res->first);
427 GTEST_LOG_(INFO) << "PutCacheNode End";
428 }
429
430 /**
431 * @tc.name: PutCacheNodeTest004
432 * @tc.desc: Verify the PutCacheNode function.
433 * @tc.type: FUNC
434 * @tc.require: I6H5MH
435 */
436 HWTEST_F(CloudDiskNotifyUtilsTest, PutCacheNodeTest004, TestSize.Level1)
437 {
438 GTEST_LOG_(INFO) << "PutCacheNode Start";
439 CacheNode cacheNode;
440 CloudDiskNotifyUtils CloudDiskNotifyUtils;
441 for (int i = 0; i < CloudDiskNotifyUtils::maxCacheCnt_ - 2; ++i) {
442 std::stringstream ss;
443 ss << i;
444 string str = ss.str();
445 auto iter = (CloudDiskNotifyUtils.cacheList_)
446 .insert(CloudDiskNotifyUtils.cacheList_.end(), std::make_pair(str, cacheNode));
447 (CloudDiskNotifyUtils.cacheMap_).insert(std::make_pair(str, iter));
448 }
449 string cloudId = "key";
450 cacheNode.isDir = TYPE_DIR_STR;
451
452 CloudDiskNotifyUtils.PutCacheNode(cloudId, cacheNode);
453 EXPECT_EQ(CloudDiskNotifyUtils.cacheMap_.size(), CloudDiskNotifyUtils::maxCacheCnt_);
454 GTEST_LOG_(INFO) << "PutCacheNode End";
455 }
456
457 /**
458 * @tc.name: GetUriFromCacheTest001
459 * @tc.desc: Verify the GetUriFromCache function.
460 * @tc.type: FUNC
461 * @tc.require: I6H5MH
462 */
463 HWTEST_F(CloudDiskNotifyUtilsTest, GetUriFromCacheTest001, TestSize.Level1)
464 {
465 GTEST_LOG_(INFO) << "GetUriFromCache Start";
466 CloudDiskNotifyUtils CloudDiskNotifyUtils;
467 string bundleName = "com.ohos.photos";
468 string rootId = "rootId";
469 CacheNode cacheNode;
470 cacheNode.isDir = "";
471 cacheNode.cloudId = "";
472 string uri = "";
473 int ret = CloudDiskNotifyUtils.GetUriFromCache(bundleName, rootId, cacheNode, uri);
474 EXPECT_EQ(ret, E_INVAL_ARG);
475 GTEST_LOG_(INFO) << "GetUriFromCache End";
476 }
477
478 /**
479 * @tc.name: GetUriFromCacheTest002
480 * @tc.desc: Verify the GetUriFromCache function.
481 * @tc.type: FUNC
482 * @tc.require: I6H5MH
483 */
484 HWTEST_F(CloudDiskNotifyUtilsTest, GetUriFromCacheTest002, TestSize.Level1)
485 {
486 GTEST_LOG_(INFO) << "GetUriFromCache Start";
487 CloudDiskNotifyUtils CloudDiskNotifyUtils;
488 string bundleName = "com.ohos.photos";
489 string rootId = "same";
490 CacheNode cacheNode;
491 cacheNode.isDir = "";
492 cacheNode.parentCloudId = "same";
493 string uri = "";
494 int ret = CloudDiskNotifyUtils.GetUriFromCache(bundleName, rootId, cacheNode, uri);
495 EXPECT_EQ(ret, E_OK);
496 GTEST_LOG_(INFO) << "GetUriFromCache End";
497 }
498 } // namespace Test
499 } // namespace FileManagement::CloudSync
500 } // namespace OHOS