1 /*
2 * Copyright (c) 2021 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 "hks_test_file_operator.h"
17
18 #ifndef __LITEOS_M__
19 #include <dirent.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <limits.h>
23 #include <stdint.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 /* use product definitions temporarily */
28 #define DEFAULT_FILE_PERMISSION 0666
29 #else
30
31 #include <utils_file.h>
32
33 #endif /* _STORAGE_LITE_ */
34
35
36 #include "hks_param.h"
37 #include "hks_test_log.h"
38 #include "hks_test_mem.h"
39 #include "hks_type.h"
40 #include "securec.h"
41
42 #define HKS_MAX_FILE_NAME_LEN 512
43
GetFileName(const char * path,const char * fileName,char * fullFileName,uint32_t fullFileNameLen)44 static int32_t GetFileName(const char *path, const char *fileName, char *fullFileName, uint32_t fullFileNameLen)
45 {
46 if (path != NULL) {
47 if (strncpy_s(fullFileName, fullFileNameLen, path, strlen(path)) != EOK) {
48 return HKS_ERROR_INTERNAL_ERROR;
49 }
50
51 if (path[strlen(path) - 1] != '/') {
52 if (strncat_s(fullFileName, fullFileNameLen, "/", strlen("/")) != EOK) {
53 return HKS_ERROR_INTERNAL_ERROR;
54 }
55 }
56
57 if (strncat_s(fullFileName, fullFileNameLen, fileName, strlen(fileName)) != EOK) {
58 return HKS_ERROR_INTERNAL_ERROR;
59 }
60 } else {
61 if (strncpy_s(fullFileName, fullFileNameLen, fileName, strlen(fileName)) != EOK) {
62 return HKS_ERROR_INTERNAL_ERROR;
63 }
64 }
65
66 return HKS_SUCCESS;
67 }
68
GetFullFileName(const char * path,const char * fileName,char ** fullFileName)69 static int32_t GetFullFileName(const char *path, const char *fileName, char **fullFileName)
70 {
71 uint32_t nameLen = HKS_MAX_FILE_NAME_LEN;
72 char *tmpFileName = (char *)HksTestMalloc(nameLen);
73 if (tmpFileName == NULL) {
74 return HKS_ERROR_MALLOC_FAIL;
75 }
76 (void)memset_s(tmpFileName, nameLen, 0, nameLen);
77
78 int32_t ret = GetFileName(path, fileName, tmpFileName, nameLen);
79 if (ret != HKS_SUCCESS) {
80 HKS_TEST_LOG_E("get full fileName failed");
81 HksTestFree(tmpFileName);
82 return ret;
83 }
84
85 *fullFileName = tmpFileName;
86 return HKS_SUCCESS;
87 }
88
89 #ifndef __LITEOS_M__
IsFileExist(const char * fileName)90 static int32_t IsFileExist(const char *fileName)
91 {
92 if (access(fileName, F_OK) != 0) {
93 return HKS_ERROR_NOT_EXIST;
94 }
95
96 return HKS_SUCCESS;
97 }
98
FileRead(const char * fileName,uint32_t offset,uint8_t * buf,uint32_t len)99 static uint32_t FileRead(const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len)
100 {
101 (void)offset;
102 if (IsFileExist(fileName) != HKS_SUCCESS) {
103 return 0;
104 }
105
106 char filePath[PATH_MAX + 1] = {0};
107 if (memcpy_s(filePath, sizeof(filePath) - 1, fileName, strlen(fileName)) != EOK) {
108 return HKS_ERROR_BAD_STATE;
109 }
110 if (strstr(filePath, "../") != NULL) {
111 HKS_TEST_LOG_E("invalid filePath, path %s", filePath);
112 return 0;
113 }
114
115 FILE *fp = fopen(filePath, "rb");
116 if (fp == NULL) {
117 HKS_TEST_LOG_E("failed to open file");
118 return 0;
119 }
120
121 uint32_t size = fread(buf, 1, len, fp);
122 if (fclose(fp) < 0) {
123 HKS_TEST_LOG_E("failed to close file");
124 return 0;
125 }
126
127 return size;
128 }
129
FileSize(const char * fileName)130 static uint32_t FileSize(const char *fileName)
131 {
132 if (IsFileExist(fileName) != HKS_SUCCESS) {
133 return 0;
134 }
135
136 struct stat fileStat;
137 (void)memset_s(&fileStat, sizeof(fileStat), 0, sizeof(fileStat));
138 if (stat(fileName, &fileStat) != 0) {
139 HKS_TEST_LOG_E("file stat fail.");
140 return 0;
141 }
142
143 return fileStat.st_size;
144 }
145
FileWrite(const char * fileName,uint32_t offset,const uint8_t * buf,uint32_t len)146 static int32_t FileWrite(const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len)
147 {
148 (void)offset;
149 char filePath[PATH_MAX + 1] = {0};
150 if (memcpy_s(filePath, sizeof(filePath) - 1, fileName, strlen(fileName)) != EOK) {
151 return HKS_ERROR_BAD_STATE;
152 }
153 if (strstr(filePath, "../") != NULL) {
154 HKS_TEST_LOG_E("invalid filePath, path %s", filePath);
155 return HKS_ERROR_INVALID_KEY_FILE;
156 }
157
158 /* caller function ensures that the folder exists */
159 FILE *fp = fopen(filePath, "wb+");
160 if (fp == NULL) {
161 HKS_TEST_LOG_E("open file fail");
162 return HKS_ERROR_OPEN_FILE_FAIL;
163 }
164
165 if (chmod(filePath, S_IRUSR | S_IWUSR) < 0) {
166 HKS_TEST_LOG_E("chmod file fail.");
167 fclose(fp);
168 return HKS_ERROR_OPEN_FILE_FAIL;
169 }
170
171 uint32_t size = fwrite(buf, 1, len, fp);
172 if (size != len) {
173 HKS_TEST_LOG_E("write file size fail.");
174 fclose(fp);
175 return HKS_ERROR_WRITE_FILE_FAIL;
176 }
177
178 if (fclose(fp) < 0) {
179 HKS_TEST_LOG_E("failed to close file");
180 return HKS_ERROR_CLOSE_FILE_FAIL;
181 }
182
183 return HKS_SUCCESS;
184 }
185
186 #else
FileRead(const char * fileName,uint32_t offset,uint8_t * buf,uint32_t len)187 static uint32_t FileRead(const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len)
188 {
189 /* now offset is 0, but we maybe extend hi1131 file interfaces in the future */
190 if (offset != 0) {
191 return (uint32_t)(HKS_ERROR_INVALID_ARGUMENT);
192 }
193
194 unsigned int fileSize;
195 int32_t ret = UtilsFileStat(fileName, &fileSize);
196 if (ret < 0) {
197 HKS_TEST_LOG_E("stat file failed, errno = 0x%x", ret);
198 return 0;
199 }
200
201 if (len > fileSize) {
202 HKS_TEST_LOG_E("read data over file size!\n, file size = %d\n, buf len = %d\n", fileSize, len);
203 return 0;
204 }
205
206 int fd = UtilsFileOpen(fileName, O_RDONLY_FS, 0);
207 if (fd < 0) {
208 HKS_TEST_LOG_E("failed to open file, errno = 0x%x", fd);
209 return 0;
210 }
211
212 ret = UtilsFileRead(fd, (char *)(buf), len);
213 UtilsFileClose(fd);
214 if (ret < 0) {
215 HKS_TEST_LOG_E("failed to read file, errno = 0x%x", ret);
216 return 0;
217 }
218
219 return len;
220 }
221
FileWrite(const char * fileName,uint32_t offset,const uint8_t * buf,uint32_t len)222 static int32_t FileWrite(const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len)
223 {
224 /* now offset is 0, but we maybe extend hi1131 file interfaces in the future */
225 if (offset != 0) {
226 return HKS_ERROR_INVALID_ARGUMENT;
227 }
228
229 int fd = UtilsFileOpen(fileName, O_CREAT_FS | O_TRUNC_FS | O_RDWR_FS, 0);
230 if (fd < 0) {
231 HKS_TEST_LOG_E("failed to open key file, errno = 0x%x\n", fd);
232 return HKS_ERROR_OPEN_FILE_FAIL;
233 }
234
235 int32_t ret = UtilsFileWrite(fd, (const char *)buf, len);
236 if (ret < 0) {
237 HKS_TEST_LOG_E("failed to write key file, errno = 0x%x\n", ret);
238 ret = HKS_ERROR_WRITE_FILE_FAIL;
239 }
240
241 ret = UtilsFileClose(fd);
242 if (ret < 0) {
243 HKS_TEST_LOG_E("failed to close file, errno = 0x%x\n", ret);
244 ret = HKS_ERROR_CLOSE_FILE_FAIL;
245 }
246
247 return ret;
248 }
249
FileSize(const char * fileName)250 static uint32_t FileSize(const char *fileName)
251 {
252 unsigned int fileSize;
253 int32_t ret = UtilsFileStat(fileName, &fileSize);
254 if (ret < 0) {
255 HKS_TEST_LOG_E("stat file failed, errno = 0x%x", ret);
256 return 0;
257 }
258 return fileSize;
259 }
260
HksTestFileRemove(const char * path,const char * fileName)261 int32_t HksTestFileRemove(const char *path, const char *fileName)
262 {
263 char *fullFileName = NULL;
264 int32_t ret = GetFullFileName(path, fileName, &fullFileName);
265 if (ret != HKS_SUCCESS) {
266 return 0;
267 }
268
269 return UtilsFileDelete(fullFileName);
270 }
271
272 #endif
HksTestFileRead(const char * path,const char * fileName,uint32_t offset,uint8_t * buf,uint32_t len)273 uint32_t HksTestFileRead(const char *path, const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len)
274 {
275 if ((fileName == NULL) || (buf == NULL) || (len == 0)) {
276 return 0;
277 }
278
279 char *fullFileName = NULL;
280 int32_t ret = GetFullFileName(path, fileName, &fullFileName);
281 if (ret != HKS_SUCCESS) {
282 return 0;
283 }
284
285 uint32_t size = FileRead(fullFileName, offset, buf, len);
286 HksTestFree(fullFileName);
287 return size;
288 }
289
HksTestFileWrite(const char * path,const char * fileName,uint32_t offset,const uint8_t * buf,uint32_t len)290 int32_t HksTestFileWrite(const char *path, const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len)
291 {
292 if ((fileName == NULL) || (buf == NULL) || (len == 0)) {
293 return HKS_ERROR_INVALID_ARGUMENT;
294 }
295
296 char *fullFileName = NULL;
297 int32_t ret = GetFullFileName(path, fileName, &fullFileName);
298 if (ret != HKS_SUCCESS) {
299 return ret;
300 }
301
302 ret = FileWrite(fullFileName, offset, buf, len);
303 HksTestFree(fullFileName);
304 return ret;
305 }
306
HksTestFileSize(const char * path,const char * fileName)307 uint32_t HksTestFileSize(const char *path, const char *fileName)
308 {
309 if (fileName == NULL) {
310 return 0;
311 }
312
313 char *fullFileName = NULL;
314 int32_t ret = GetFullFileName(path, fileName, &fullFileName);
315 if (ret != HKS_SUCCESS) {
316 return 0;
317 }
318
319 uint32_t size = FileSize(fullFileName);
320 HksTestFree(fullFileName);
321 return size;
322 }
323