1 /*
2 * Copyright (c) 2023 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 "directory_ex.h"
20 #include "meta_file.h"
21
22 namespace OHOS::FileManagement::CloudSync::Test {
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace std;
26 constexpr uint32_t TEST_USER_ID = 201;
27 constexpr uint64_t TEST_ISIZE = 1024;
28
29 class DentryMetaFileTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 void SetUp();
34 void TearDown();
35 };
SetUpTestCase(void)36 void DentryMetaFileTest::SetUpTestCase(void)
37 {
38 GTEST_LOG_(INFO) << "SetUpTestCase";
39 }
40
TearDownTestCase(void)41 void DentryMetaFileTest::TearDownTestCase(void)
42 {
43 GTEST_LOG_(INFO) << "TearDownTestCase";
44 }
45
SetUp(void)46 void DentryMetaFileTest::SetUp(void)
47 {
48 GTEST_LOG_(INFO) << "SetUp";
49 }
50
TearDown(void)51 void DentryMetaFileTest::TearDown(void)
52 {
53 GTEST_LOG_(INFO) << "TearDown";
54 }
55 /**
56 * @tc.name: MetaFileCreate
57 * @tc.desc: Verify the MetaFile::DoCreate function
58 * @tc.type: FUNC
59 * @tc.require: SR000HRKKA
60 */
61 HWTEST_F(DentryMetaFileTest, MetaFileCreate, TestSize.Level1)
62 {
63 std::string cacheDir =
64 "/data/service/el2/" + std::to_string(TEST_USER_ID) + "/hmdfs/cache/cloud_cache/dentry_cache/cloud/";
65 ForceRemoveDirectory(cacheDir);
66
67 MetaFile mFile(TEST_USER_ID, "/");
68 MetaBase mBase1("file1", "id1");
69 mBase1.size = TEST_ISIZE;
70 int ret = mFile.DoCreate(mBase1);
71 EXPECT_EQ(ret, 0);
72 MetaBase mBase2("file2", "id2");
73 mBase2.size = TEST_ISIZE;
74 ret = mFile.DoCreate(mBase2);
75 EXPECT_EQ(ret, 0);
76 MetaFile mFile2(TEST_USER_ID, "/a/b");
77 MetaBase mBase3("file3", "id3");
78 ret = mFile2.DoCreate(mBase3);
79 }
80
81 /**
82 * @tc.name: MetaFileLookup
83 * @tc.desc: Verify the MetaFile::DoLookup function
84 * @tc.type: FUNC
85 * @tc.require: SR000HRKKA
86 */
87 HWTEST_F(DentryMetaFileTest, MetaFileLookup, TestSize.Level1)
88 {
89 MetaFile mFile(TEST_USER_ID, "/");
90 MetaBase mBase1("file1");
91 int ret = mFile.DoLookup(mBase1);
92 EXPECT_EQ(ret, 0);
93 EXPECT_EQ(mBase1.size, TEST_ISIZE);
94 }
95
96 /**
97 * @tc.name: MetaFileUpdate
98 * @tc.desc: Verify the MetaFile::DoUpdate function
99 * @tc.type: FUNC
100 * @tc.require: SR000HRKKC
101 */
102 HWTEST_F(DentryMetaFileTest, MetaFileUpdate, TestSize.Level1)
103 {
104 MetaFile mFile(TEST_USER_ID, "/");
105 MetaBase mBase1("file1", "id11");
106 mBase1.size = 0;
107 int ret = mFile.DoUpdate(mBase1);
108 EXPECT_EQ(ret, 0);
109 MetaBase mBase2("file1");
110 ret = mFile.DoLookup(mBase2);
111 EXPECT_EQ(ret, 0);
112 EXPECT_EQ(mBase2.size, 0);
113 }
114
115 /**
116 * @tc.name: MetaFileRename
117 * @tc.desc: Verify the MetaFile::DoRename function
118 * @tc.type: FUNC
119 * @tc.require: SR000HRKKC
120 */
121 HWTEST_F(DentryMetaFileTest, MetaFileRename, TestSize.Level1)
122 {
123 MetaFile mFile(TEST_USER_ID, "/");
124 MetaBase mBase1("file2");
125 int ret = mFile.DoRename(mBase1, "file4");
126 EXPECT_EQ(ret, 0);
127 MetaBase mBase2("file4");
128 ret = mFile.DoLookup(mBase2);
129 EXPECT_EQ(ret, 0);
130 EXPECT_EQ(mBase2.size, TEST_ISIZE);
131 }
132
133 /**
134 * @tc.name: MetaFileRemove
135 * @tc.desc: Verify the MetaFile::DoRemove function
136 * @tc.type: FUNC
137 * @tc.require: SR000HRKJB
138 */
139 HWTEST_F(DentryMetaFileTest, MetaFileRemove, TestSize.Level1)
140 {
141 MetaFile mFile(TEST_USER_ID, "/");
142 MetaBase mBase1("file1");
143 int ret = mFile.DoRemove(mBase1);
144 EXPECT_EQ(ret, 0);
145 MetaBase mBase2("file1");
146 ret = mFile.DoLookup(mBase2);
147 EXPECT_EQ(ret, ENOENT);
148 }
149
150 /**
151 * @tc.name: MetaFileMgr
152 * @tc.desc: Verify the MetaFileMgr
153 * @tc.type: FUNC
154 * @tc.require: SR000HRKJB
155 */
156 HWTEST_F(DentryMetaFileTest, MetaFileMgr, TestSize.Level1)
157 {
158 auto m = MetaFileMgr::GetInstance().GetMetaFile(TEST_USER_ID, "/o/p/q/r/s/t");
159 MetaBase mBase1("file1", "file1");
160 EXPECT_EQ(m->DoCreate(mBase1), 0);
161 m = nullptr;
162 MetaFileMgr::GetInstance().ClearAll();
163 }
164 } // namespace OHOS::FileManagement::CloudSync::Test
165