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 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17
18 #include "cloud_file_utils.h"
19 #include "parameters.h"
20
21 namespace OHOS::FileManagement::CloudDisk::Test {
22 using namespace std;
23 using namespace OHOS;
24 using namespace testing::ext;
25
26 static const std::string FILEMANAGER_KEY = "persist.kernel.bundle_name.filemanager";
27
28 class CloudFileUtilsTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 };
35
SetUpTestCase(void)36 void CloudFileUtilsTest::SetUpTestCase(void) {}
37
TearDownTestCase(void)38 void CloudFileUtilsTest::TearDownTestCase(void) {}
39
SetUp(void)40 void CloudFileUtilsTest::SetUp(void) {}
41
TearDown(void)42 void CloudFileUtilsTest::TearDown(void) {}
43
44 /**
45 * @tc.name: DfsService_CloudDisk_GetCloudId_001
46 * @tc.desc: Verify the sub function.
47 * @tc.type: FUNC
48 * @tc.require: issueNumber
49 */
50 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_GetCloudId_001, TestSize.Level0)
51 {
52 string cloudId = CloudFileUtils::GetCloudId("");
53 EXPECT_EQ((cloudId == ""), true);
54
55 cloudId = CloudFileUtils::GetCloudId("test");
56 EXPECT_EQ((cloudId == ""), true);
57 }
58
59 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_GetLocalBucketPath_002, TestSize.Level0)
60 {
61 string cloudId;
62 string bundleName;
63 int32_t userId = -1;
64 string bucketPath = CloudFileUtils::GetLocalBucketPath(cloudId, bundleName, userId);
65 EXPECT_EQ((cloudId == ""), true);
66 }
67
68 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_GetLocalBucketPath_003, TestSize.Level0)
69 {
70 string cloudId = "testId";
71 string bundleName = "testBundleName";
72 int32_t userId = -1;
73 string bucketPath = CloudFileUtils::GetLocalBucketPath(cloudId, bundleName, userId);
74 EXPECT_EQ((cloudId == ""), false);
75 }
76
77 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_CheckIsCloud_004, TestSize.Level0)
78 {
79 string key;
80 EXPECT_EQ(CloudFileUtils::CheckIsCloud(key), false);
81
82 key = CLOUD_CLOUD_ID_XATTR;
83 EXPECT_EQ(CloudFileUtils::CheckIsCloud(key), true);
84 }
85
86 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_CheckIsCloudLocation_005, TestSize.Level0)
87 {
88 string key;
89 EXPECT_EQ(CloudFileUtils::CheckIsCloudLocation(key), false);
90
91 key = CLOUD_FILE_LOCATION;
92 EXPECT_EQ(CloudFileUtils::CheckIsCloudLocation(key), true);
93 }
94
95 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_CheckIsHmdfsPermission_006, TestSize.Level0)
96 {
97 string key;
98 EXPECT_EQ(CloudFileUtils::CheckIsHmdfsPermission(key), false);
99
100 key = HMDFS_PERMISSION_XATTR;
101 EXPECT_EQ(CloudFileUtils::CheckIsHmdfsPermission(key), true);
102 }
103
104 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_GetLocalFilePath_007, TestSize.Level0)
105 {
106 string cloudId;
107 string bundleName;
108 int32_t userId = 0;
109 string ret = CloudFileUtils::GetLocalFilePath(cloudId, bundleName, userId);
110 EXPECT_EQ((ret == ""), false);
111
112 userId = -1;
113 ret = CloudFileUtils::GetLocalFilePath(cloudId, bundleName, userId);
114 EXPECT_EQ((ret == ""), false);
115 }
116
117 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_GetPathWithoutTmp_008, TestSize.Level0)
118 {
119 string path;
120 string ret = CloudFileUtils::GetPathWithoutTmp(path);
121 EXPECT_EQ(ret, path);
122
123 path = "path/test";
124 ret = CloudFileUtils::GetPathWithoutTmp(path);
125 EXPECT_EQ(ret, path);
126 }
127
128 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_LocalWriteOpen_009, TestSize.Level0)
129 {
130 string dfsPath;
131 EXPECT_EQ(CloudFileUtils::LocalWriteOpen(dfsPath), false);
132
133 dfsPath = "testPath";
134 EXPECT_EQ(CloudFileUtils::LocalWriteOpen(dfsPath), false);
135 }
136
137 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_EndsWith_010, TestSize.Level0)
138 {
139 string fullString;
140 string ending;
141 EXPECT_EQ(CloudFileUtils::EndsWith(fullString, ending), true);
142
143 fullString = "testPath";
144 EXPECT_EQ(CloudFileUtils::EndsWith(fullString, ending), true);
145 }
146
147 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_EndsWith_011, TestSize.Level0)
148 {
149 string fullString = "testPath";
150 string ending = "test@@text##";
151 EXPECT_EQ(CloudFileUtils::EndsWith(fullString, ending), false);
152 }
153
154 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_DentryHash_012, TestSize.Level0)
155 {
156 try {
157 string inputStr = ".";
158 EXPECT_EQ(CloudFileUtils::DentryHash(inputStr), 0);
159
160 inputStr = "..";
161 EXPECT_EQ(CloudFileUtils::DentryHash(inputStr), 0);
162 } catch (...) {
163 EXPECT_FALSE(false);
164 GTEST_LOG_(INFO) << " DfsService_CloudDisk_DentryHash_012 ERROR";
165 }
166 }
167
168 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_LocalWriteOpen_013, TestSize.Level0)
169 {
170 string dfsPath = "./";
171 EXPECT_EQ(CloudFileUtils::LocalWriteOpen(dfsPath), false);
172
173 dfsPath = "testPath";
174 EXPECT_EQ(CloudFileUtils::LocalWriteOpen(dfsPath), false);
175 }
176
177 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_GetCloudId_014, TestSize.Level1)
178 {
179 string dfsPath = "/data/service/el1/public/cloudfile";
180 string cloudId = CloudFileUtils::GetCloudId(dfsPath);
181 EXPECT_EQ((cloudId == ""), true);
182
183 dfsPath = "/data/service/el1/public/cloudfile/cloud_data_statistic";
184 cloudId = CloudFileUtils::GetCloudId(dfsPath);
185 EXPECT_EQ((cloudId == ""), true);
186 }
187
188 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_ClearCache_01, TestSize.Level1)
189 {
190 string dfsPath = "/mnt/hmdfs/100/cloud/data/" + system::GetParameter(FILEMANAGER_KEY, "");
191 string cloudPath = "/mnt/data/100/cloud_fuse";
192 bool res = CloudFileUtils::ClearCache(dfsPath, cloudPath);
193 #if CLOUD_ADAPTER_ENABLED
194 EXPECT_EQ(res, true);
195 #else
196 EXPECT_EQ(res, false);
197 #endif
198 }
199
200 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_ClearCache_02, TestSize.Level1)
201 {
202 string dfsPath = "/mnt/hmdfs/100/cloud/data/" + system::GetParameter(FILEMANAGER_KEY, "");
203 string cloudPath = "./";
204 bool res = CloudFileUtils::ClearCache(dfsPath, cloudPath);
205 EXPECT_EQ(res, false);
206 }
207
208 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_ClearCache_03, TestSize.Level1)
209 {
210 string dfsPath = "/invalid/path";
211 string cloudPath = "/mnt/data/100/cloud_fuse";
212 bool res = CloudFileUtils::ClearCache(dfsPath, cloudPath);
213 EXPECT_EQ(res, false);
214 }
215
216 HWTEST_F(CloudFileUtilsTest, DfsService_CloudDisk_ClearCache_04, TestSize.Level1)
217 {
218 string dfsPath = "/mnt/hmdfs/100/cloud/data/" + system::GetParameter(FILEMANAGER_KEY, "");
219 string cloudPath = "/invalid/path";
220 bool res = CloudFileUtils::ClearCache(dfsPath, cloudPath);
221 EXPECT_EQ(res, false);
222 }
223 } // OHOS