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_test.h"
17
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <hctest.h>
21 #include <securec.h>
22 #include <stdlib.h>
23 #include <string.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 TEST_HC_FILE_OPEN_SERIES
36
ReadFile(char * buffer,int fd,int size)37 static void ReadFile(char *buffer, int fd, int size)
38 {
39 int total = 0;
40 while (total < size) {
41 int readCount = read(fd, buffer + total, size - total);
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(char * buffer,int fd,int size)51 static void WriteFile(char *buffer, int fd, int size)
52 {
53 int total = 0;
54 while (total < size) {
55 int writeCount = write(fd, buffer + total, size - total);
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
TestHcFileOpenAndClose(void)65 static void TestHcFileOpenAndClose(void)
66 {
67 const char *file = TEST_FILE_NAME;
68 LOGI("begin to open file: %s", file);
69
70 int fd;
71 RUN_AND_PRINT_ELAPSED_TIME(fd, open(file, O_RDWR | O_CREAT));
72 TEST_ASSERT_GREATER_OR_EQUAL(0, fd);
73
74 int ret;
75 LOGI("begin to close file: %s", file);
76 RUN_AND_PRINT_ELAPSED_TIME(ret, close(fd));
77 TEST_ASSERT_EQUAL(0, ret);
78 }
79
TestHcFileReadAndWrite(void)80 static void TestHcFileReadAndWrite(void)
81 {
82 const char *file = TEST_FILE_NAME;
83 for (int i = 0; i < TEST_FILE_SIZE_LIST_LEN; i++) {
84 int fd = open(file, O_RDWR | O_CREAT);
85 TEST_ASSERT_GREATER_OR_EQUAL(0, fd);
86 int size = testFileSizeList[i];
87
88 LOGI("begin to write file [%s] for size: %d", file, size);
89 char *writeBuffer = GenerateTestingText(size);
90 TEST_ASSERT_NOT_NULL(writeBuffer);
91 RUN_AND_PRINT_ELAPSED_TIME_WITHOUT_RESULT(WriteFile(writeBuffer, fd, size));
92 const int fileSize = lseek(fd, 0, SEEK_END);
93 TEST_ASSERT_EQUAL(size, fileSize);
94 TEST_ASSERT_EQUAL(0, lseek(fd, 0, SEEK_SET));
95
96 LOGI("begin to read file [%s] for size: %d", file, fileSize);
97 TEST_ASSERT_LESS_OR_EQUAL(TEN_KILOBYTE, fileSize);
98 char *readBuffer = (char *)malloc(fileSize + 1);
99 TEST_ASSERT_NOT_NULL(readBuffer);
100 TEST_ASSERT_EQUAL(0, memset_s(readBuffer, fileSize + 1, 0, fileSize + 1));
101 RUN_AND_PRINT_ELAPSED_TIME_WITHOUT_RESULT(ReadFile(readBuffer, fd, fileSize));
102 TEST_ASSERT_EQUAL_STRING(writeBuffer, readBuffer);
103
104 free(readBuffer);
105 free(writeBuffer);
106 TEST_ASSERT_EQUAL(0, close(fd));
107 sleep(1);
108 }
109 }
110
TestHcFileSize(void)111 static void TestHcFileSize(void)
112 {
113 const char *file = TEST_FILE_NAME;
114 LOGI("begin to count file size");
115
116 int fd = open(file, O_RDWR);
117 TEST_ASSERT_GREATER_OR_EQUAL(0, fd);
118
119 int size;
120 RUN_AND_PRINT_ELAPSED_TIME(size, lseek(fd, 0, SEEK_END));
121 TEST_ASSERT_EQUAL(TEN_KILOBYTE, size);
122 TEST_ASSERT_EQUAL(0, lseek(fd, 0, SEEK_SET));
123 LOGI("the file size is: %d", size);
124
125 TEST_ASSERT_EQUAL(0, close(fd));
126 int ret;
127 RUN_AND_PRINT_ELAPSED_TIME(ret, unlink(file));
128 TEST_ASSERT_EQUAL(0, ret);
129 }
130
TestHcFileMkdir(void)131 static void TestHcFileMkdir(void)
132 {
133 #if MKDIR_IMPLEMENTED
134 const char *dir = TEST_FILE_DIR;
135 LOGI("begin to make directory: %s", dir);
136 int ret;
137 RUN_AND_PRINT_ELAPSED_TIME(ret, mkdir(dir, DEFAULT_FILE_PERMISSION));
138 TEST_ASSERT_EQUAL(0, ret);
139 TEST_ASSERT_EQUAL(-1, mkdir(dir, DEFAULT_FILE_PERMISSION));
140 TEST_ASSERT_EQUAL(0, rmdir(dir));
141 #else
142 LOGE("no MKDIR_IMPLEMENTED, do not test mkdir() !");
143 #endif
144 }
145
TestHcFileStat(void)146 static void TestHcFileStat(void)
147 {
148 #if STAT_IMPLEMENTED
149 const char *file = TEST_FILE_NAME;
150 LOGI("begin to check [%s] state", file);
151 TEST_ASSERT_GREATER_OR_EQUAL(0, open(file, O_RDWR | O_CREAT));
152
153 struct stat fileStat;
154 int ret;
155 ret = memset_s(&fileStat, sizeof(fileStat), 0, sizeof(fileStat));
156 TEST_ASSERT_EQUAL(0, ret);
157 RUN_AND_PRINT_ELAPSED_TIME(ret, stat(file, &fileStat));
158 TEST_ASSERT_EQUAL(0, ret);
159
160 TEST_ASSERT_EQUAL(0, unlink(file));
161 ret = memset_s(&fileStat, sizeof(fileStat), 0, sizeof(fileStat));
162 TEST_ASSERT_EQUAL(0, ret);
163 TEST_ASSERT_EQUAL(-1, stat(file, &fileStat));
164 TEST_ASSERT_EQUAL(ENOENT, errno);
165 #else
166 LOGE("no STAT_IMPLEMENTED, do not test stat() !");
167 #endif
168 }
169
TestHcFileRemove(void)170 static void TestHcFileRemove(void)
171 {
172 const char *file = TEST_FILE_NAME;
173 LOGI("begin to remove file: %s", file);
174
175 int ret;
176 RUN_AND_PRINT_ELAPSED_TIME(ret, unlink(file));
177 TEST_ASSERT_EQUAL(0, ret);
178 }
179
TestHcFile(void)180 void TestHcFile(void)
181 {
182 LOGI("test opening and closing file");
183 TestHcFileOpenAndClose();
184
185 LOGI("test removing file");
186 TestHcFileRemove();
187
188 LOGI("test reading and writing file");
189 TestHcFileReadAndWrite();
190
191 LOGI("test counting file size");
192 TestHcFileSize();
193
194 LOGI("test making directory");
195 TestHcFileMkdir();
196
197 LOGI("test getting file state");
198 TestHcFileStat();
199 }
200
201 #else // TEST_HC_FILE_OPEN_SERIES
202
203 void TestHcFile(void)
204 {
205 LOGE("no TEST_HC_FILE_OPEN_SERIES, do not test hc_file open series!");
206 }
207
208 #endif // TEST_HC_FILE_OPEN_SERIES
209
210 #ifdef __cplusplus
211 }
212 #endif
213