1 /* 2 * Copyright (c) 2020-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 #include "FileSystemTest.h" 16 #include <stdio.h> 17 #include <string.h> 18 #include <stdlib.h> 19 20 #include <sys/stat.h> 21 #include <sys/types.h> 22 #include <fcntl.h> 23 #include <unistd.h> 24 #include <dirent.h> 25 #include <ftw.h> 26 #include <libgen.h> 27 28 #include <gtest/gtest.h> 29 30 #include "utils.h" 31 #include "log.h" 32 #include "KernelConstants.h" 33 #include "libfs.h" 34 35 using namespace testing::ext; 36 #if 0 37 /** 38 * @tc.number SUB_KERNEL_FS_STDIO_0100 39 * @tc.name basic function test : read and write with stream 40 * @tc.desc [C- SOFTWARE -0200] 41 */ 42 HWTEST_F(FileSystemTest, testFILE, Function | MediumTest | Level2) 43 { 44 FILE *fp = nullptr; 45 int fd = 0; 46 char writeBuf[] = "this is a file"; 47 char readBuf[20] = {0}; 48 49 // write 50 fd = creat(FILE0, 0777); 51 EXPECT_NE(fd, -1) << "> creat faild errno = " << errno; 52 EXPECT_NE(close(fd), -1) << "> close errno = " << errno; 53 fp = fopen(FILE0, "w+"); 54 ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno; 55 EXPECT_EQ(fwrite(writeBuf, sizeof(writeBuf), 1, fp), 1) << "> fwrite errno = " << errno; 56 EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno; 57 58 // read 59 fp = fopen(FILE0, "r+"); 60 ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno; 61 EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno; 62 EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf; 63 EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno; 64 } 65 66 /** 67 * @tc.number SUB_KERNEL_FS_STDIO_0200 68 * @tc.name basic function test : Use the feof function to determine the end identifier of the file 69 * @tc.desc [C- SOFTWARE -0200] 70 */ 71 HWTEST_F(FileSystemTest, testFeof, Function | MediumTest | Level3) 72 { 73 FILE *fp = nullptr; 74 int fd = 0; 75 char writeBuf[] = "this is a file"; 76 char readBuf[20] = {0}; 77 78 // write 79 fd = creat(FILE0, 0777); 80 EXPECT_NE(fd, -1) << "> creat faild errno = " << errno; 81 WriteCloseTest(fd); 82 83 // read 84 fp = fopen(FILE0, "r+"); 85 ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno; 86 87 EXPECT_EQ(fseeko(fp, 0, SEEK_SET), 0) << "> fseeko errno = " << errno; 88 fgetc(fp); 89 EXPECT_EQ(feof(fp), 0) << "> file should not be end!"; // check end 90 EXPECT_EQ(fseeko(fp, 0, SEEK_SET), 0) << "> fseeko errno = " << errno; 91 92 EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno; 93 EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf; 94 fgetc(fp); 95 EXPECT_NE(feof(fp), 0) << "> file should be end!"; // check end 96 EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno; 97 } 98 99 /** 100 * @tc.number SUB_KERNEL_FS_STDIO_0300 101 * @tc.name basic function test : Use fseek to set the stream pointer position with SEEK_SET 102 * @tc.desc [C- SOFTWARE -0200] 103 */ 104 HWTEST_F(FileSystemTest, testFseek, Function | MediumTest | Level3) 105 { 106 FILE *fp = nullptr; 107 int fd = 0; 108 char writeBuf[] = "this is a file"; 109 char readBuf[20] = {0}; 110 111 // write 112 fd = creat(FILE0, 0777); 113 EXPECT_NE(fd, -1) << "> creat faild errno = " << errno; 114 WriteCloseTest(fd); 115 116 // read 117 fp = fopen(FILE0, "r+"); 118 ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno; 119 120 fpos_t offset; 121 EXPECT_EQ(fgetpos(fp, &offset), 0) << "> fgetpos errno = " << errno; 122 123 EXPECT_EQ(fseek(fp, 2, SEEK_SET), 0) << "> fseek errno = " << errno; 124 EXPECT_EQ(ftell(fp), 2) << " errno = " << errno; 125 126 EXPECT_EQ(fsetpos(fp, &offset), 0) << "> fsetpos errno = " << errno; 127 EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno; 128 EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf; 129 EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno; 130 } 131 132 /** 133 * @tc.number SUB_KERNEL_FS_STDIO_0310 134 * @tc.name basic function test : Use fseek to set the stream pointer position with SEEK_CUR 135 * @tc.desc [C- SOFTWARE -0200] 136 */ 137 HWTEST_F(FileSystemTest, testFseekSeekCur, Function | MediumTest | Level3) 138 { 139 FILE *fp = nullptr; 140 int fd = 0; 141 char writeBuf[] = "this is a file"; 142 char readBuf[20] = {0}; 143 144 // write 145 fd = creat(FILE0, 0777); 146 EXPECT_NE(fd, -1) << "> creat faild errno = " << errno; 147 WriteCloseTest(fd); 148 149 // read 150 fp = fopen(FILE0, "r+"); 151 ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno; 152 153 fpos_t offset; 154 EXPECT_EQ(fgetpos(fp, &offset), 0) << "> fgetpos errno = " << errno; 155 156 EXPECT_EQ(fseek(fp, 2, SEEK_CUR), 0) << "> fseek errno = " << errno; 157 EXPECT_EQ(ftell(fp), 2) << " errno = " << errno; 158 159 EXPECT_EQ(fsetpos(fp, &offset), 0) << "> fsetpos errno = " << errno; 160 EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno; 161 EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf; 162 EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno; 163 } 164 165 /** 166 * @tc.number SUB_KERNEL_FS_STDIO_0320 167 * @tc.name basic function test : Use fseek to set the stream pointer position with SEEK_END 168 * @tc.desc [C- SOFTWARE -0200] 169 */ 170 HWTEST_F(FileSystemTest, testFseekSeekEnd, Function | MediumTest | Level3) 171 { 172 FILE *fp = nullptr; 173 int fd = 0; 174 char writeBuf[] = "this is a file"; 175 char readBuf[20] = {0}; 176 177 // write 178 fd = creat(FILE0, 0777); 179 EXPECT_NE(fd, -1) << "> creat faild errno = " << errno; 180 WriteCloseTest(fd); 181 182 // read 183 fp = fopen(FILE0, "r+"); 184 ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno; 185 186 fpos_t offset; 187 EXPECT_EQ(fgetpos(fp, &offset), 0) << "> fgetpos errno = " << errno; 188 189 EXPECT_EQ(fseek(fp, 0, SEEK_END), 0) << "> fseek errno = " << errno; 190 EXPECT_EQ(ftell(fp), sizeof(writeBuf)) << " errno = " << errno; 191 192 EXPECT_EQ(fsetpos(fp, &offset), 0) << "> fsetpos errno = " << errno; 193 EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno; 194 EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf; 195 EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno; 196 } 197 198 /** 199 * @tc.number SUB_KERNEL_FS_STDIO_0400 200 * @tc.name basic function test : Use fseeko to set the stream pointer position with SEEK_SET 201 * @tc.desc [C- SOFTWARE -0200] 202 */ 203 HWTEST_F(FileSystemTest, testFseeko, Function | MediumTest | Level3) 204 { 205 FILE *fp = nullptr; 206 int fd = 0; 207 char writeBuf[] = "this is a file"; 208 char readBuf[20] = {0}; 209 210 // write 211 fd = creat(FILE0, 0777); 212 EXPECT_NE(fd, -1) << "> creat faild errno = " << errno; 213 WriteCloseTest(fd); 214 215 // read 216 fp = fopen(FILE0, "r+"); 217 ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno; 218 219 EXPECT_EQ(fseeko(fp, 2, SEEK_SET), 0) << "> fseeko errno = " << errno; 220 EXPECT_EQ(ftello(fp), 2) << " errno = " << errno; 221 222 EXPECT_EQ(fseeko(fp, 0, SEEK_SET), 0) << "> fseeko errno = " << errno; 223 EXPECT_EQ(ftello(fp), 0) << " errno = " << errno; 224 EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno; 225 EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf; 226 EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno; 227 } 228 229 /** 230 * @tc.number SUB_KERNEL_FS_STDIO_0410 231 * @tc.name basic function test : Use fseeko to set the stream pointer position with SEEK_CUR 232 * @tc.desc [C- SOFTWARE -0200] 233 */ 234 HWTEST_F(FileSystemTest, testFseekoSeekCur, Function | MediumTest | Level3) 235 { 236 FILE *fp = nullptr; 237 int fd = 0; 238 char writeBuf[] = "this is a file"; 239 char readBuf[20] = {0}; 240 241 // write 242 fd = creat(FILE0, 0777); 243 EXPECT_NE(fd, -1) << "> creat faild errno = " << errno; 244 WriteCloseTest(fd); 245 246 // read 247 fp = fopen(FILE0, "r+"); 248 ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno; 249 250 EXPECT_EQ(fseeko(fp, 2, SEEK_CUR), 0) << "> fseeko errno = " << errno; 251 EXPECT_EQ(ftello(fp), 2) << " errno = " << errno; 252 253 EXPECT_EQ(fseeko(fp, 0, SEEK_SET), 0) << "> fseeko errno = " << errno; 254 EXPECT_EQ(ftello(fp), 0) << " errno = " << errno; 255 EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno; 256 EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf; 257 EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno; 258 } 259 260 /** 261 * @tc.number SUB_KERNEL_FS_STDIO_0420 262 * @tc.name basic function test : Use fseeko to set the stream pointer position with SEEK_END 263 * @tc.desc [C- SOFTWARE -0200] 264 */ 265 HWTEST_F(FileSystemTest, testFseekoSeekEnd, Function | MediumTest | Level3) 266 { 267 FILE *fp = nullptr; 268 int fd = 0; 269 char writeBuf[] = "this is a file"; 270 char readBuf[20] = {0}; 271 272 // write 273 fd = creat(FILE0, 0777); 274 EXPECT_NE(fd, -1) << "> creat faild errno = " << errno; 275 WriteCloseTest(fd); 276 277 // read 278 fp = fopen(FILE0, "r+"); 279 ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno; 280 281 EXPECT_EQ(fseeko(fp, 0, SEEK_END), 0) << "> fseeko errno = " << errno; 282 EXPECT_EQ(ftello(fp), sizeof(writeBuf)) << " errno = " << errno; 283 284 EXPECT_EQ(fseeko(fp, 0, SEEK_SET), 0) << "> fseeko errno = " << errno; 285 EXPECT_EQ(ftello(fp), 0) << " errno = " << errno; 286 EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno; 287 EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf; 288 EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno; 289 } 290 291 void *ChildWrite(void *p) 292 { 293 FILE *fp = (FILE*)p; 294 flockfile(fp); 295 size_t re0 = fwrite("this is ", sizeof("this is"), 1, fp); 296 EXPECT_EQ(re0, 1) << "fwrite errno = " << errno; 297 Msleep(FOURTY_MS); 298 size_t re1 = fwrite("a file", sizeof("a file"), 1, fp); 299 EXPECT_EQ(re1, 1) << "fwrite errno = " << errno; 300 funlockfile(fp); 301 LOG("> ChildWrite over"); 302 return nullptr; 303 } 304 305 /** 306 * @tc.number SUB_KERNEL_FS_STDIO_0500 307 * @tc.name basic function test : Use the funlockfile function to unlock the file 308 * @tc.desc [C- SOFTWARE -0200] 309 */ 310 HWTEST_F(FileSystemTest, testFunlockfile, Function | MediumTest | Level3) 311 { 312 const char filePath[] = TOP_DIR "/" DIR0 "/" DIR0_FILE0; 313 char readBuf[20]; 314 char writeBuf[] = "this is a file"; 315 316 CreateTestFolder(); 317 FILE *fp = fopen(filePath, "r+"); 318 ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno; 319 320 pthread_t tid; 321 int reInt = pthread_create(&tid, nullptr, ChildWrite, (void*)fp); 322 ASSERT_EQ(reInt, 0) << "> pthread_create errno, reInt = " << reInt; 323 324 Msleep(20); 325 flockfile(fp); 326 LOG("> childRead over"); 327 EXPECT_EQ(fseek(fp, 0, SEEK_SET), 0) << "> fseek errno = " << errno; 328 EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread error"; 329 EXPECT_STREQ(writeBuf, readBuf); 330 funlockfile(fp); 331 332 int ret = pthread_join(tid, nullptr); 333 EXPECT_EQ(ret, 0) << "pthread_join failed, errno=" << ret; 334 EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno; 335 } 336 337 /** 338 * @tc.number SUB_KERNEL_FS_STDIO_0600 339 * @tc.name basic function test : Use the fileno function to return the file descriptor of the stream, test write 340 * @tc.desc [C- SOFTWARE -0200] 341 */ 342 HWTEST_F(FileSystemTest, testFileno, Function | MediumTest | Level2) 343 { 344 const char filePath[] = TOP_DIR "/" FILE0; 345 FILE *fp = nullptr; 346 int fd = 0; 347 348 // write 349 fd = creat(filePath, 0777); 350 ASSERT_NE(fd, -1) << "> creat errno = " << errno; 351 ASSERT_NE(close(fd), -1) << "> close errno = " << errno; 352 353 fp = fopen(filePath, "w+"); 354 ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno; 355 fd = fileno(fp); 356 EXPECT_NE(fd, -1) << "> fileno errno = " << errno; 357 WriteCloseTest(fd); 358 359 // read 360 fd = open(filePath, O_RDWR); 361 EXPECT_NE(fd, -1) << "> open errno = " << errno; 362 ReadCloseTest(fd); 363 } 364 365 /** 366 * @tc.number SUB_KERNEL_FS_STDIO_0610 367 * @tc.name basic function test : Use the fileno function to return the file descriptor of the stream, test read 368 * @tc.desc [C- SOFTWARE -0200] 369 */ 370 HWTEST_F(FileSystemTest, testFileno1, Function | MediumTest | Level2) 371 { 372 const char filePath[] = TOP_DIR "/" FILE0; 373 FILE *fp = nullptr; 374 int fd = 0; 375 376 // write 377 fd = creat(filePath, 0777); 378 EXPECT_NE(fd, -1) << "> creat faild errno = " << errno; 379 WriteCloseTest(fd); 380 381 // read 382 fp = fopen(filePath, "r+"); 383 ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno; 384 fd = fileno(fp); 385 EXPECT_NE(fd, -1) << "> fileno errno = " << errno; 386 387 ReadCloseTest(fd); 388 } 389 #endif 390 /** 391 * @tc.number SUB_KERNEL_FS_STDIO_0700 392 * @tc.name basic function test : Use the rename function to rename files. 393 * @tc.desc [C- SOFTWARE -0200] 394 */ 395 HWTEST_F(FileSystemTest, testRename, Function | MediumTest | Level3) 396 { 397 int fd = 0; 398 const char *newFileName = "FILE_NEW"; 399 fd = creat(FILE0, 0777); 400 EXPECT_NE(fd, -1) << "> creat faild errno = " << errno; 401 EXPECT_NE(close(fd), -1) << "> close errno = " << errno; 402 403 EXPECT_NE(rename(FILE0, newFileName), -1) << "> rename errno = " << errno; 404 EXPECT_NE(unlink(newFileName), -1) << "> unlink errno = " << errno; 405 } 406 407 /** 408 * @tc.number SUB_KERNEL_FS_STDIO_0710 409 * @tc.name basic function test : Use the rename function to rename directories. 410 * @tc.desc [C- SOFTWARE -0200] 411 */ 412 HWTEST_F(FileSystemTest, testRenameDir, Function | MediumTest | Level3) 413 { 414 const char *newDirName = "DIR_NEW"; 415 EXPECT_NE(mkdir(DIR0, 0777), -1) << "> mkdir errno = " << errno; 416 417 EXPECT_NE(rename(DIR0, newDirName), -1) << "> rename errno = " << errno; 418 EXPECT_NE(rmdir(newDirName), -1) << "> rmdir errno = " << errno; 419 } 420 #if 0 421 /** 422 * @tc.number SUB_KERNEL_FS_STDIO_0800 423 * @tc.name basic function test : Use the fflush function to refresh stream 424 * @tc.desc [C- SOFTWARE -0200] 425 */ 426 HWTEST_F(FileSystemTest, testFflush, Function | MediumTest | Level3) 427 { 428 const char filePath[] = TOP_DIR "/" DIR0 "/" DIR0_FILE0; 429 char writeBuf[] = "0123456789ABCDE"; 430 char readBuf[50] = {0}; 431 CreateTestFolder(); 432 pid_t pid = fork(); 433 ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno; 434 if (pid == 0) { 435 int exitCode = 0; 436 FILE *fp = fopen(filePath, "a+"); 437 if (fp == nullptr) { 438 LOG("fp is null"); 439 exit(1); 440 } 441 setvbuf(fp, nullptr, _IOFBF, 1024); 442 int reInt = fwrite(writeBuf, sizeof(writeBuf), 1, fp); 443 if (reInt != 1) { 444 LOG("fwrite return reInt = %d, errno = %d", reInt, errno); 445 exitCode = 1; 446 } 447 Msleep(100); 448 if (fflush(fp) == -1){ 449 LOG("fflush errno = %d", errno); 450 exitCode = 1; 451 } 452 Msleep(100); 453 if (fclose(fp) == -1){ 454 LOG("fclose errno = %d", errno); 455 exitCode = 1; 456 } 457 exit(exitCode); 458 } 459 Msleep(50); 460 FILE *fp = fopen(filePath, "r"); 461 ASSERT_NE(fp, nullptr); 462 EXPECT_EQ(fseek(fp, 0, SEEK_END), 0) << "> fseek errno = " << errno; 463 EXPECT_EQ(ftell(fp), 0); 464 EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno; 465 466 Msleep(100); 467 fp = fopen(filePath, "r"); 468 ASSERT_NE(fp, nullptr); 469 EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno; 470 EXPECT_STREQ(readBuf, writeBuf); 471 LOG("> readBuf = %s", readBuf); 472 EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno; 473 474 Msleep(100); 475 AssertProcExitedOK(pid); 476 } 477 #endif 478