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 "hc_file_f_test.h"
17
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <hctest.h>
21 #include <securec.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 #include "hc_file_common.h"
28 #include "print_log.h"
29 #include "test_timer.h"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 #if F_API_IMPLEMENTED
36
ReadFile(char * buffer,FILE * file,int size)37 static void ReadFile(char *buffer, FILE *file, int size)
38 {
39 int total = 0;
40 while (total < size) {
41 int readCount = fread(buffer + total, 1, size - total, file);
42 TEST_ASSERT_GREATER_OR_EQUAL(0, readCount);
43 TEST_ASSERT_LESS_OR_EQUAL(size - total, readCount);
44 total += readCount;
45 if (readCount <= 0) {
46 break;
47 }
48 }
49 }
50
WriteFile(const char * buffer,FILE * file,int size)51 static void WriteFile(const char *buffer, FILE *file, int size)
52 {
53 int total = 0;
54 while (total < size) {
55 int writeCount = fwrite(buffer + total, 1, size - total, file);
56 TEST_ASSERT_GREATER_OR_EQUAL(0, writeCount);
57 TEST_ASSERT_LESS_OR_EQUAL(size - total, writeCount);
58 total += writeCount;
59 if (writeCount <= 0) {
60 break;
61 }
62 }
63 }
64
TestHcFileFopenAndFclose(void)65 static void TestHcFileFopenAndFclose(void)
66 {
67 const char *fileName = TEST_FILE_NAME;
68 LOGI("begin to open file: %s", fileName);
69
70 FILE *file;
71 RUN_AND_PRINT_ELAPSED_TIME(file, fopen(fileName, "w+"));
72 TEST_ASSERT_NOT_NULL(file);
73
74 int ret;
75 LOGI("begin to close file: %s", fileName);
76 RUN_AND_PRINT_ELAPSED_TIME(ret, fclose(file));
77 TEST_ASSERT_EQUAL(0, ret);
78 }
79
TestHcFileFreadAndFwrite(void)80 static void TestHcFileFreadAndFwrite(void)
81 {
82 const char *fileName = TEST_FILE_NAME;
83 for (int i = 0; i < TEST_FILE_SIZE_LIST_LEN; i++) {
84 FILE *file = fopen(fileName, "w+");
85 TEST_ASSERT_NOT_NULL(file);
86 int size = testFileSizeList[i];
87
88 LOGI("begin to write file [%s] for size: %d", fileName, size);
89 char *writeBuffer = GenerateTestingText(size);
90 TEST_ASSERT_NOT_NULL(writeBuffer);
91 RUN_AND_PRINT_ELAPSED_TIME_WITHOUT_RESULT(WriteFile(writeBuffer, file, size));
92
93 TEST_ASSERT_EQUAL(0, fseek(file, 0, SEEK_END));
94 const int fileSize = ftell(file);
95 TEST_ASSERT_EQUAL(size, fileSize);
96 TEST_ASSERT_EQUAL(0, fseek(file, 0, SEEK_SET));
97
98 LOGI("begin to read file [%s] for size: %d", fileName, fileSize);
99 TEST_ASSERT_LESS_OR_EQUAL(TEN_KILOBYTE, fileSize);
100 char *readBuffer = (char *)malloc(fileSize + 1);
101 TEST_ASSERT_NOT_NULL(readBuffer);
102 TEST_ASSERT_EQUAL(0, memset_s(readBuffer, fileSize + 1, 0, fileSize + 1));
103 RUN_AND_PRINT_ELAPSED_TIME_WITHOUT_RESULT(ReadFile(readBuffer, file, fileSize));
104 TEST_ASSERT_EQUAL_STRING(writeBuffer, readBuffer);
105
106 free(readBuffer);
107 free(writeBuffer);
108 TEST_ASSERT_EQUAL(0, fclose(file));
109 sleep(1);
110 }
111 }
112
TestHcFileFseekAndFtell(void)113 static void TestHcFileFseekAndFtell(void)
114 {
115 const char *fileName = TEST_FILE_NAME;
116 LOGI("begin to count file size");
117
118 FILE *file = fopen(fileName, "rb");
119 TEST_ASSERT_NOT_NULL(file);
120
121 int ret;
122 RUN_AND_PRINT_ELAPSED_TIME(ret, fseek(file, 0, SEEK_END));
123 TEST_ASSERT_EQUAL(0, ret);
124
125 int size;
126 RUN_AND_PRINT_ELAPSED_TIME(size, ftell(file));
127 TEST_ASSERT_EQUAL(TEN_KILOBYTE, size);
128 TEST_ASSERT_EQUAL(0, fseek(file, 0, SEEK_SET));
129 LOGI("the file size is: %d", size);
130
131 TEST_ASSERT_EQUAL(0, fclose(file));
132
133 RUN_AND_PRINT_ELAPSED_TIME(ret, unlink(fileName));
134 TEST_ASSERT_EQUAL(0, ret);
135 }
136
TestHcFileMkdir(void)137 static void TestHcFileMkdir(void)
138 {
139 #if MKDIR_IMPLEMENTED
140 const char *dir = TEST_FILE_DIR;
141 LOGI("begin to make directory: %s", dir);
142 int ret;
143 RUN_AND_PRINT_ELAPSED_TIME(ret, mkdir(dir, DEFAULT_FILE_PERMISSION));
144 TEST_ASSERT_EQUAL(0, ret);
145 TEST_ASSERT_EQUAL(-1, mkdir(dir, DEFAULT_FILE_PERMISSION));
146 TEST_ASSERT_EQUAL(0, rmdir(dir));
147 #endif
148 }
149
TestHcFileStat(void)150 static void TestHcFileStat(void)
151 {
152 #if STAT_IMPLEMENTED
153 const char *fileName = TEST_FILE_NAME;
154 LOGI("begin to check [%s] state", fileName);
155
156 FILE *file = fopen(fileName, "w+");
157 TEST_ASSERT_NOT_NULL(file);
158 TEST_ASSERT_EQUAL(0, fclose(file));
159
160 struct stat fileStat;
161 int ret;
162 ret = memset_s(&fileStat, sizeof(fileStat), 0, sizeof(fileStat));
163 TEST_ASSERT_EQUAL(0, ret);
164 RUN_AND_PRINT_ELAPSED_TIME(ret, stat(fileName, &fileStat));
165 TEST_ASSERT_EQUAL(0, ret);
166
167 TEST_ASSERT_EQUAL(0, unlink(fileName));
168 ret = memset_s(&fileStat, sizeof(fileStat), 0, sizeof(fileStat));
169 TEST_ASSERT_EQUAL(0, ret);
170 TEST_ASSERT_EQUAL(-1, stat(fileName, &fileStat));
171 TEST_ASSERT_EQUAL(ENOENT, errno);
172 #endif
173 }
174
TestHcFileRemove(void)175 static void TestHcFileRemove(void)
176 {
177 const char *fileName = TEST_FILE_NAME;
178 LOGI("begin to remove file: %s", fileName);
179
180 int ret;
181 RUN_AND_PRINT_ELAPSED_TIME(ret, unlink(fileName));
182 TEST_ASSERT_EQUAL(0, ret);
183 }
184
TestHcFileAccess(void)185 static void TestHcFileAccess(void)
186 {
187 #if ACCESS_IMPLEMENTED
188 const char *fileName = TEST_FILE_NAME;
189 LOGI("begin to check access of [%s]", fileName);
190
191 FILE *file = fopen(fileName, "w+");
192 TEST_ASSERT_NOT_NULL(file);
193 TEST_ASSERT_EQUAL(0, fclose(file));
194
195 int ret;
196 RUN_AND_PRINT_ELAPSED_TIME(ret, access(fileName, F_OK));
197 TEST_ASSERT_EQUAL(0, ret);
198
199 TEST_ASSERT_EQUAL(0, unlink(fileName));
200 TEST_ASSERT_EQUAL(-1, access(fileName, F_OK));
201 #endif
202 }
203
TestHcFileFApi(void)204 void TestHcFileFApi(void)
205 {
206 LOGI("test opening and closing file");
207 TestHcFileFopenAndFclose();
208
209 LOGI("test removing file");
210 TestHcFileRemove();
211
212 LOGI("test reading and writing file");
213 TestHcFileFreadAndFwrite();
214
215 LOGI("test counting file size");
216 TestHcFileFseekAndFtell();
217
218 LOGI("test making directory");
219 TestHcFileMkdir();
220
221 LOGI("test getting file state");
222 TestHcFileStat();
223
224 LOGI("test accessing a file");
225 TestHcFileAccess();
226 }
227
228 #else // F_API_IMPLEMENTED
229 void TestHcFileFApi(void)
230 {
231 LOGE("no F_API_IMPLEMENTED, do not test fopen series!");
232 }
233 #endif // F_API_IMPLEMENTED
234
235 #ifdef __cplusplus
236 }
237 #endif
238