1 /*
2 * Copyright (c) 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
16 #include "cloud_file_cache_core.h"
17
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include <sys/types.h>
21 #include <sys/xattr.h>
22
23 #include "cloud_sync_manager.h"
24 #include "dfs_error.h"
25 #include "download_callback_impl_ani.h"
26 #include "uri.h"
27 #include "utils_log.h"
28
29 namespace OHOS::FileManagement::CloudDisk::Test {
30 using namespace testing;
31 using namespace testing::ext;
32 using namespace std;
33 using namespace OHOS::FileManagement::CloudSync;
34
35 class CloudFileCacheCoreTest : public testing::Test {
36 public:
37 static void SetUpTestCase(void);
38 static void TearDownTestCase(void);
39 void SetUp();
40 void TearDown();
41 };
42
SetUpTestCase(void)43 void CloudFileCacheCoreTest::SetUpTestCase(void)
44 {
45 GTEST_LOG_(INFO) << "SetUpTestCase";
46 }
47
TearDownTestCase(void)48 void CloudFileCacheCoreTest::TearDownTestCase(void)
49 {
50 GTEST_LOG_(INFO) << "TearDownTestCase";
51 }
52
SetUp(void)53 void CloudFileCacheCoreTest::SetUp(void)
54 {
55 GTEST_LOG_(INFO) << "SetUp";
56 }
57
TearDown(void)58 void CloudFileCacheCoreTest::TearDown(void)
59 {
60 GTEST_LOG_(INFO) << "TearDown";
61 }
62
63 /**
64 * @tc.name: Constructor
65 * @tc.desc: Verify the CloudFileCacheCore::Constructor function
66 * @tc.type: FUNC
67 */
68 HWTEST_F(CloudFileCacheCoreTest, ConstructorTest1, TestSize.Level1)
69 {
70 auto data = CloudFileCacheCore::Constructor();
71 EXPECT_TRUE(data.IsSuccess());
72 }
73
74 /**
75 * @tc.name: DoOn
76 * @tc.desc: Verify the CloudFileCacheCore::DoOn function
77 * @tc.type: FUNC
78 */
79 HWTEST_F(CloudFileCacheCoreTest, DoOnTest1, TestSize.Level1)
80 {
81 CloudFileCacheCore *cloudFileCache = CloudFileCacheCore::Constructor().GetData().value();
82 auto callback = std::make_shared<CloudFileCacheCallbackImplAni>();
83 std::string event = "progress";
84 auto ret = cloudFileCache->DoOn(event, callback);
85 EXPECT_TRUE(ret.IsSuccess());
86 const auto &err = ret.GetError();
87 int errorCode = err.GetErrNo();
88 EXPECT_EQ(errorCode, 0);
89 }
90
91 /**
92 * @tc.name: DoOff
93 * @tc.desc: Verify the CloudFileCacheCore::DoOff function
94 * @tc.type: FUNC
95 */
96 HWTEST_F(CloudFileCacheCoreTest, DoOffTest1, TestSize.Level1)
97 {
98 CloudFileCacheCore *cloudFileCache = CloudFileCacheCore::Constructor().GetData().value();
99 std::string event = "progress";
100 auto ret = cloudFileCache->DoOff(event);
101 EXPECT_TRUE(ret.IsSuccess());
102 const auto &err = ret.GetError();
103 int errorCode = err.GetErrNo();
104 EXPECT_EQ(errorCode, 0);
105 }
106
107 /**
108 * @tc.name: DoStart
109 * @tc.desc: Verify the CloudFileCacheCore::DoStart function
110 * @tc.type: FUNC
111 */
112 HWTEST_F(CloudFileCacheCoreTest, DoStartTest1, TestSize.Level1)
113 {
114 CloudFileCacheCore *cloudFileCache = CloudFileCacheCore::Constructor().GetData().value();
115 std::string uri = "testuri";
116 auto ret = cloudFileCache->DoStart(uri);
117 EXPECT_FALSE(ret.IsSuccess());
118 const auto &err = ret.GetError();
119 int errorCode = err.GetErrNo();
120 EXPECT_EQ(errorCode, OHOS::FileManagement::E_INVALID_URI);
121 }
122
123 /**
124 * @tc.name: DoStart
125 * @tc.desc: Verify the CloudFileCacheCore::DoStart function
126 * @tc.type: FUNC
127 */
128 HWTEST_F(CloudFileCacheCoreTest, DoStartTest2, TestSize.Level1)
129 {
130 CloudFileCacheCore *cloudFileCache = CloudFileCacheCore::Constructor().GetData().value();
131 std::string uri = "testuri";
132 int32_t fieldKey = 0;
133 auto ret = cloudFileCache->DoStart({uri}, fieldKey);
134 EXPECT_FALSE(ret.IsSuccess());
135 const auto &err = ret.GetError();
136 int errorCode = err.GetErrNo();
137 EXPECT_EQ(errorCode, OHOS::FileManagement::E_INVALID_URI);
138 }
139
140 /**
141 * @tc.name: DoStop
142 * @tc.desc: Verify the CloudFileCacheCore::DoStop function
143 * @tc.type: FUNC
144 */
145 HWTEST_F(CloudFileCacheCoreTest, DoStopTest1, TestSize.Level1)
146 {
147 CloudFileCacheCore *cloudFileCache = CloudFileCacheCore::Constructor().GetData().value();
148 std::string uri = "testuri";
149 bool needClean = false;
150 auto ret = cloudFileCache->DoStop(uri, needClean);
151 EXPECT_FALSE(ret.IsSuccess());
152 const auto &err = ret.GetError();
153 int errorCode = err.GetErrNo();
154 EXPECT_EQ(errorCode, OHOS::FileManagement::FILEIO_SYS_CAP_TAG + E_INVAL_ARG);
155 }
156
157 /**
158 * @tc.name: DoStop
159 * @tc.desc: Verify the CloudFileCacheCore::DoStop function
160 * @tc.type: FUNC
161 */
162 HWTEST_F(CloudFileCacheCoreTest, DoStopTest2, TestSize.Level1)
163 {
164 CloudFileCacheCore *cloudFileCache = CloudFileCacheCore::Constructor().GetData().value();
165 int64_t downloadId = 0;
166 bool needClean = false;
167 auto ret = cloudFileCache->DoStop(downloadId, needClean);
168 EXPECT_FALSE(ret.IsSuccess());
169 const auto &err = ret.GetError();
170 int errorCode = err.GetErrNo();
171 EXPECT_EQ(errorCode, OHOS::FileManagement::FILEIO_SYS_CAP_TAG + ModuleFileIO::E_INVAL);
172 }
173
174 /**
175 * @tc.name: DoStop
176 * @tc.desc: Verify the CloudFileCacheCore::DoStop function
177 * @tc.type: FUNC
178 */
179 HWTEST_F(CloudFileCacheCoreTest, DoStopTest3, TestSize.Level1)
180 {
181 CloudFileCacheCore *cloudFileCache = CloudFileCacheCore::Constructor().GetData().value();
182 cloudFileCache->registerMap_.insert(
183 std::make_pair("batchDownload", std::make_shared<CloudFileCacheCallbackImplAni>()));
184 int64_t downloadId = 0;
185 bool needClean = false;
186 auto ret = cloudFileCache->DoStop(downloadId, needClean);
187 EXPECT_FALSE(ret.IsSuccess());
188 const auto &err = ret.GetError();
189 int errorCode = err.GetErrNo();
190 EXPECT_EQ(errorCode, OHOS::FileManagement::E_PERMISSION);
191 }
192
193 /**
194 * @tc.name: DoStop
195 * @tc.desc: Verify the CloudFileCacheCore::DoStop function
196 * @tc.type: FUNC
197 */
198 HWTEST_F(CloudFileCacheCoreTest, DoStopTest4, TestSize.Level1)
199 {
200 CloudFileCacheCore *cloudFileCache = CloudFileCacheCore::Constructor().GetData().value();
201 cloudFileCache->registerMap_.insert(
202 std::make_pair("batchDownload", std::make_shared<CloudFileCacheCallbackImplAni>()));
203 std::string uri = "testuri";
204 bool needClean = false;
205 auto ret = cloudFileCache->DoStop(uri, needClean);
206 const auto &err = ret.GetError();
207 int errorCode = err.GetErrNo();
208 if (ret.IsSuccess()) {
209 EXPECT_EQ(errorCode, 0);
210 } else {
211 EXPECT_EQ(errorCode, OHOS::FileManagement::FILEIO_SYS_CAP_TAG + E_INVAL_ARG);
212 }
213 }
214
215 /**
216 * @tc.name: CleanCache
217 * @tc.desc: Verify the CloudFileCacheCore::CleanCache function
218 * @tc.type: FUNC
219 */
220 HWTEST_F(CloudFileCacheCoreTest, CleanCacheTest1, TestSize.Level1)
221 {
222 CloudFileCacheCore *cloudFileCache = CloudFileCacheCore::Constructor().GetData().value();
223 std::string uri = "testuri";
224 auto ret = cloudFileCache->CleanCache(uri);
225 EXPECT_FALSE(ret.IsSuccess());
226 const auto &err = ret.GetError();
227 int errorCode = err.GetErrNo();
228 EXPECT_EQ(errorCode, OHOS::FileManagement::E_PERMISSION);
229 }
230 } // namespace OHOS::FileManagement::CloudDisk::Test