1 /*
2 * Copyright (C) 2024-2025 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 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 #include <memory>
18
19 #include "cloud_file_utils.h"
20 #include "cloud_status.h"
21 #include "clouddisk_notify_utils_mock.h"
22 #include "clouddisk_rdb_utils.h"
23 #include "clouddisk_rdbstore.cpp"
24 #include "dfs_error.h"
25 #include "rdb_assistant.h"
26 #include "result_set_mock.h"
27 #include "transaction_mock.cpp"
28
29 namespace OHOS::FileManagement::CloudDisk::Test {
30 using namespace testing;
31 using namespace testing::ext;
32 using namespace std;
33 using namespace NativeRdb;
34 using namespace OHOS::FileManagement::CloudDisk;
35
36 static const string BUNDLE_NAME = "com.ohos.photos";
37 static const int32_t USER_ID = 100;
38 class CloudDiskRdbStoreStaticTest : public testing::Test {
39 public:
40 static void SetUpTestCase(void);
41 static void TearDownTestCase(void);
42 void SetUp();
43 void TearDown();
44 static inline shared_ptr<CloudDiskRdbStore> clouddiskrdbStore_ = nullptr;
45 static inline shared_ptr<AssistantMock> insMock = nullptr;
46 };
47
SetUpTestCase(void)48 void CloudDiskRdbStoreStaticTest::SetUpTestCase(void)
49 {
50 GTEST_LOG_(INFO) << "SetUpTestCase";
51 insMock = make_shared<AssistantMock>();
52 Assistant::ins = insMock;
53 clouddiskrdbStore_ = make_shared<CloudDiskRdbStore>(BUNDLE_NAME, USER_ID);
54 }
55
TearDownTestCase(void)56 void CloudDiskRdbStoreStaticTest::TearDownTestCase(void)
57 {
58 Assistant::ins = nullptr;
59 insMock = nullptr;
60 clouddiskrdbStore_ = nullptr;
61 GTEST_LOG_(INFO) << "TearDownTestCase";
62 }
63
SetUp(void)64 void CloudDiskRdbStoreStaticTest::SetUp(void)
65 {
66 GTEST_LOG_(INFO) << "SetUp";
67 }
68
TearDown(void)69 void CloudDiskRdbStoreStaticTest::TearDown(void)
70 {
71 GTEST_LOG_(INFO) << "TearDown";
72 }
73
74 /**
75 * @tc.name: FileRenameTest001
76 * @tc.desc: Verify the FileRename
77 * @tc.type: FUNC
78 */
79 HWTEST_F(CloudDiskRdbStoreStaticTest, FileRenameTest001, TestSize.Level1)
80 {
81 ValuesBucket values;
82 const int32_t position = CLOUD;
83 const std::string newFileName = "test";
84 int32_t dirtyType = static_cast<int32_t>(DirtyType::TYPE_FDIRTY);
85
86 FileRename(values, position, newFileName, dirtyType);
87 EXPECT_EQ(newFileName, "test");
88 }
89
90 /**
91 * @tc.name: FileRenameTest002
92 * @tc.desc: Verify the FileRename
93 * @tc.type: FUNC
94 */
95 HWTEST_F(CloudDiskRdbStoreStaticTest, FileRenameTest002, TestSize.Level1)
96 {
97 ValuesBucket values;
98 const int32_t position = LOCAL;
99 const std::string newFileName = "test";
100 int32_t dirtyType = static_cast<int32_t>(DirtyType::TYPE_FDIRTY);
101
102 FileRename(values, position, newFileName, dirtyType);
103 EXPECT_EQ(newFileName, "test");
104 }
105
106 /**
107 * @tc.name: FileRenameTest003
108 * @tc.desc: Verify the FileRename
109 * @tc.type: FUNC
110 */
111 HWTEST_F(CloudDiskRdbStoreStaticTest, FileRenameTest003, TestSize.Level1)
112 {
113 ValuesBucket values;
114 const int32_t position = CLOUD;
115 const std::string newFileName = "test";
116 int32_t dirtyType = static_cast<int32_t>(DirtyType::TYPE_SYNCED);
117
118 FileRename(values, position, newFileName, dirtyType);
119 EXPECT_EQ(position, CLOUD);
120 }
121
122 /**
123 * @tc.name: RecycleSetValue
124 * @tc.desc: Verify the RecycleSetValue
125 * @tc.type: FUNC
126 */
127 HWTEST_F(CloudDiskRdbStoreStaticTest, RecycleSetValueTest1, TestSize.Level1)
128 {
129 ValuesBucket setXAttr;
130 int32_t position = CLOUD;
131 int32_t dirtyType = static_cast<int32_t>(DirtyType::TYPE_FDIRTY);
132
133 RecycleSetValue(TrashOptType::RECYCLE, setXAttr, position, dirtyType);
134 EXPECT_EQ(position, CLOUD);
135 }
136
137 /**
138 * @tc.name: FileMoveTest001
139 * @tc.desc: Verify the FileMove
140 * @tc.type: FUNC
141 */
142 HWTEST_F(CloudDiskRdbStoreStaticTest, FileMoveTest001, TestSize.Level1)
143 {
144 ValuesBucket values;
145 const int32_t position = CLOUD;
146 const std::string newParentCloudId = "test";
147 int32_t dirtyType = static_cast<int32_t>(DirtyType::TYPE_FDIRTY);
148
149 FileMove(values, position, newParentCloudId, dirtyType);
150 EXPECT_EQ(newParentCloudId, "test");
151 }
152
153 /**
154 * @tc.name: FileMoveTest002
155 * @tc.desc: Verify the FileMove
156 * @tc.type: FUNC
157 */
158 HWTEST_F(CloudDiskRdbStoreStaticTest, FileMoveTest002, TestSize.Level1)
159 {
160 ValuesBucket values;
161 const int32_t position = LOCAL;
162 const std::string newParentCloudId = "test";
163 int32_t dirtyType = static_cast<int32_t>(DirtyType::TYPE_FDIRTY);
164
165 FileMove(values, position, newParentCloudId, dirtyType);
166 EXPECT_EQ(newParentCloudId, "test");
167 }
168
169 /**
170 * @tc.name: FileMoveTest003
171 * @tc.desc: Verify the FileMove
172 * @tc.type: FUNC
173 */
174 HWTEST_F(CloudDiskRdbStoreStaticTest, FileMoveTest003, TestSize.Level1)
175 {
176 ValuesBucket values;
177 const int32_t position = CLOUD;
178 const std::string newParentCloudId = "test";
179 int32_t dirtyType = static_cast<int32_t>(DirtyType::TYPE_SYNCED);
180
181 FileMove(values, position, newParentCloudId, dirtyType);
182 EXPECT_EQ(newParentCloudId, "test");
183 }
184
185 /**
186 * @tc.name: FileMoveAndRenameTest001
187 * @tc.desc: Verify the FileMoveAndRename
188 * @tc.type: FUNC
189 */
190 HWTEST_F(CloudDiskRdbStoreStaticTest, FileMoveAndRenameTest001, TestSize.Level1)
191 {
192 ValuesBucket values;
193 const int32_t position = CLOUD;
194 const std::string newParentCloudId = "test";
195 const std::string newFileName = "test";
196 int32_t dirtyType = static_cast<int32_t>(DirtyType::TYPE_FDIRTY);
197
198 FileMoveAndRename(values, position, newParentCloudId, newFileName, dirtyType);
199 EXPECT_EQ(newFileName, "test");
200 }
201
202 /**
203 * @tc.name: FileMoveAndRenameTest002
204 * @tc.desc: Verify the FileMoveAndRename
205 * @tc.type: FUNC
206 */
207 HWTEST_F(CloudDiskRdbStoreStaticTest, FileMoveAndRenameTest002, TestSize.Level1)
208 {
209 ValuesBucket values;
210 const int32_t position = LOCAL;
211 const std::string newParentCloudId = "test";
212 const std::string newFileName = "test";
213 int32_t dirtyType = static_cast<int32_t>(DirtyType::TYPE_FDIRTY);
214
215 FileMoveAndRename(values, position, newParentCloudId, newFileName, dirtyType);
216 EXPECT_EQ(newFileName, "test");
217 }
218
219 /**
220 * @tc.name: FileMoveAndRenameTest003
221 * @tc.desc: Verify the FileMoveAndRename
222 * @tc.type: FUNC
223 */
224 HWTEST_F(CloudDiskRdbStoreStaticTest, FileMoveAndRenameTest003, TestSize.Level1)
225 {
226 ValuesBucket values;
227 const int32_t position = CLOUD;
228 const std::string newParentCloudId = "test";
229 const std::string newFileName = "test";
230 int32_t dirtyType = static_cast<int32_t>(DirtyType::TYPE_SYNCED);
231
232 FileMoveAndRename(values, position, newParentCloudId, newFileName, dirtyType);
233 EXPECT_EQ(newFileName, "test");
234 }
235
236 /**
237 * @tc.name: ExtAttributeSetValueTest001
238 * @tc.desc: Verify the ExtAttributeSetValue
239 * @tc.type: FUNC
240 */
241 HWTEST_F(CloudDiskRdbStoreStaticTest, ExtAttributeSetValueTest1, TestSize.Level1)
242 {
243 const std::string key = "testKey";
244 std::string value = "testValue";
245 std::string xattrList = "{}";
246 std::string jsonValue = "";
247
248 int32_t result = ExtAttributeSetValue(jsonValue, key, value, xattrList);
249 EXPECT_EQ(result, E_OK);
250 }
251 } // namespace OHOS::FileManagement::CloudDisk::Test