1 /*
2 * Copyright (c) 2022 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 "help_utils.h"
17
18 #include <cerrno>
19 #include <dirent.h>
20 #include <fcntl.h>
21 #include <mntent.h>
22 #include <sys/mount.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25
26 #include "ipc/istorage_daemon.h"
27 #include "user/user_manager.h"
28 #include "utils/file_utils.h"
29
30 namespace OHOS {
31 namespace StorageDaemon {
32 namespace StorageTest {
33 const uid_t StorageTestUtils::OID_ROOT = 0;
34 const uid_t StorageTestUtils::OID_SYSTEM = 1000;
35
36 const int32_t StorageTestUtils::USER_ID1 = 70001;
37 const int32_t StorageTestUtils::USER_ID2 = 70002;
38 const int32_t StorageTestUtils::USER_ID3 = 70003;
39 const int32_t StorageTestUtils::USER_ID4 = 70004;
40 const int32_t StorageTestUtils::USER_ID5 = 70005;
41 const mode_t StorageTestUtils::MODE = 0711;
42
43 const std::vector<Dir> StorageTestUtils::g_rootDirs = {
44 {"/data/app/%s/%d", 0711, OID_ROOT, OID_ROOT},
45 {"/data/service/%s/%d", 0711, OID_ROOT, OID_ROOT},
46 {"/data/chipset/%s/%d", 0711, OID_ROOT, OID_ROOT}
47 };
48
49 const std::vector<Dir> StorageTestUtils::g_subDirs = {
50 {"/data/app/%s/%d/base", 0711, OID_ROOT, OID_ROOT},
51 {"/data/app/%s/%d/database", 0711, OID_ROOT, OID_ROOT}
52 };
53
54 const std::vector<Dir> StorageTestUtils::g_hmdfsDirs = {
55 {"/data/service/el2/%d/hmdfs", 0711, OID_SYSTEM, OID_SYSTEM},
56 {"/data/service/el2/%d/hmdfs/files", 0711, OID_SYSTEM, OID_SYSTEM},
57 {"/data/service/el2/%d/hmdfs/data", 0711, OID_SYSTEM, OID_SYSTEM},
58 {"/storage/media/%d", 0711, OID_ROOT, OID_ROOT},
59 {"/storage/media/%d/local", 0711, OID_ROOT, OID_ROOT}
60 };
61
62 const std::string StorageTestUtils::HMDFS_SOURCE = "/data/service/el2/%d/hmdfs/files";
63 const std::string StorageTestUtils::HMDFS_TARGET = "/storage/media/%d/local";
64
CheckMount(const std::string & dstPath)65 bool StorageTestUtils::CheckMount(const std::string& dstPath)
66 {
67 const std::string fileName = "/proc/mounts";
68 FILE *mntFile;
69 struct mntent *mntent = nullptr;
70
71 mntFile = setmntent(fileName.c_str(), "r");
72 if (!mntFile) {
73 return false;
74 }
75
76 while ((mntent = getmntent(mntFile)) != nullptr) {
77 if (dstPath.compare(mntent->mnt_dir) == 0) {
78 endmntent(mntFile);
79 return true;
80 }
81 }
82 endmntent(mntFile);
83 return false;
84 }
85
CheckDir(const std::string & path)86 bool StorageTestUtils::CheckDir(const std::string &path)
87 {
88 struct stat st;
89 if (lstat(path.c_str(), &st) != 0) {
90 return false;
91 }
92 return S_ISDIR(st.st_mode) == 1;
93 }
94
CheckUserDir(int32_t userId,uint32_t flags)95 bool StorageTestUtils::CheckUserDir(int32_t userId, uint32_t flags)
96 {
97 for (const Dir &dir : g_rootDirs) {
98 std::string path(dir.path);
99 path.replace(path.find("%d"), strlen("%d"), std::to_string(userId));
100
101 if (flags & IStorageDaemon::CRYPTO_FLAG_EL1) {
102 std::string realPath(path);
103 realPath.replace(realPath.find("%s"), strlen("%s"), "el1");
104 if (CheckDir(realPath) == false) {
105 return false;
106 }
107 }
108 if (flags & IStorageDaemon::CRYPTO_FLAG_EL2) {
109 std::string realPath(path);
110 realPath.replace(realPath.find("%s"), strlen("%s"), "el2");
111 if (CheckDir(realPath) == false) {
112 return false;
113 }
114 }
115 }
116
117 for (const Dir &dir : g_subDirs) {
118 if (flags & IStorageDaemon::CRYPTO_FLAG_EL1) {
119 std::string path(dir.path);
120 path.replace(path.find("%d"), strlen("%d"), std::to_string(userId));
121 path.replace(path.find("%s"), strlen("%s"), "el1");
122 if (CheckDir(path) == false) {
123 return false;
124 }
125 }
126
127 if (flags & IStorageDaemon::CRYPTO_FLAG_EL2) {
128 std::string path(dir.path);
129 path.replace(path.find("%d"), strlen("%d"), std::to_string(userId));
130 path.replace(path.find("%s"), strlen("%s"), "el2");
131 if (CheckDir(path) == false) {
132 return false;
133 }
134 }
135 }
136
137 for (const Dir &dir : g_hmdfsDirs) {
138 std::string path(dir.path);
139 path.replace(path.find("%d"), strlen("%d"), std::to_string(userId));
140 if (CheckDir(path) == false) {
141 return false;
142 }
143 }
144 return true;
145 }
146
CreateFile(const std::string & path)147 bool StorageTestUtils::CreateFile(const std::string &path)
148 {
149 (void)RmDirRecurse(path);
150 int fd = open(path.c_str(), O_RDWR | O_CREAT | O_TRUNC, MODE);
151 if (fd == -1) {
152 return false;
153 }
154 (void)close(fd);
155 return true;
156 }
157
MkDir(const std::string & path,mode_t mode)158 bool StorageTestUtils::MkDir(const std::string &path, mode_t mode)
159 {
160 if (access(path.c_str(), 0) == 0) {
161 if (rmdir(path.c_str()) != 0) {
162 return false;
163 }
164 }
165 if (mkdir(path.c_str(), mode) != 0) {
166 return false;
167 }
168 if (access(path.c_str(), 0) != 0) {
169 return false;
170 }
171 return true;
172 }
173
RmDirRecurse(const std::string & path)174 bool StorageTestUtils::RmDirRecurse(const std::string &path)
175 {
176 struct stat st;
177 if (lstat(path.c_str(), &st) != 0) {
178 return false;
179 }
180 if (S_ISDIR(st.st_mode) != 1) {
181 return (unlink(path.c_str()) == 0);
182 }
183
184 DIR *dir = opendir(path.c_str());
185 if (!dir) {
186 if (errno == ENOENT) {
187 return true;
188 }
189 return false;
190 }
191
192 for (struct dirent *ent = readdir(dir); ent != nullptr; ent = readdir(dir)) {
193 if (ent->d_type == DT_DIR) {
194 if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
195 continue;
196 }
197
198 if (!RmDirRecurse(path + "/" + ent->d_name)) {
199 (void)closedir(dir);
200 return false;
201 }
202 } else {
203 if (unlink((path + "/" + ent->d_name).c_str())) {
204 (void)closedir(dir);
205 return false;
206 }
207 }
208 }
209
210 (void)closedir(dir);
211 if (rmdir(path.c_str())) {
212 return false;
213 }
214
215 return true;
216 }
217
RmDir(const int32_t userId)218 void StorageTestUtils::RmDir(const int32_t userId)
219 {
220 std::vector<std::string> paths = {
221 "/data/app/el1/",
222 "/data/app/el2/",
223 "/data/service/el1/",
224 "/data/service/el2/",
225 "/data/chipset/el1/",
226 "/data/chipset/el2/",
227 "/storage/media/"
228 };
229 std::string cmd;
230 for (auto path : paths) {
231 path.append(std::to_string(userId));
232 RmDirRecurse(path);
233 }
234 }
235
ClearTestResource()236 void StorageTestUtils::ClearTestResource()
237 {
238 int32_t userIds[] = {
239 USER_ID1,
240 USER_ID2,
241 USER_ID3,
242 USER_ID4,
243 USER_ID5
244 };
245 for (auto id : userIds) {
246 std::string dstPath(HMDFS_TARGET);
247 dstPath.replace(dstPath.find("%d"), strlen("%d"), std::to_string(id));
248 UMount(dstPath);
249 RmDir(id);
250 }
251 }
252 } // StorageTest
253 } // STORAGE_DAEMON
254 } // OHOS
255