FsStdioTest.cpp 16.1 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
W
wenjun 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "FileSystemTest.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <ftw.h>
#include <libgen.h>

#include <gtest/gtest.h>

#include "utils.h"
#include "log.h"
#include "KernelConstants.h"
#include "libfs.h"

using namespace testing::ext;

/**
 * @tc.number   SUB_KERNEL_FS_STDIO_0100
M
mamingshuai 已提交
39
 * @tc.name     basic function test : read and write with stream
W
wenjun 已提交
40 41
 * @tc.desc     [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
42
HWTEST_F(FileSystemTest, testFILE, Function | MediumTest | Level2)
W
wenjun 已提交
43
{
M
mamingshuai 已提交
44
    FILE *fp = nullptr;
W
wenjun 已提交
45 46
    int fd = 0;
    char writeBuf[] = "this is a file";
M
mamingshuai 已提交
47
    char readBuf[20] = {0};
W
wenjun 已提交
48 49 50 51 52 53

    // write
    fd = creat(FILE0, 0777);
    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
    fp = fopen(FILE0, "w+");
54
    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
M
mamingshuai 已提交
55 56 57 58 59
    EXPECT_EQ(fwrite(writeBuf, sizeof(writeBuf), 1, fp), 1) << "> fwrite errno = " << errno;
    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno;

    // read
    fp = fopen(FILE0, "r+");
60
    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
M
mamingshuai 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno;
    EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf;
    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno;
}

/**
 * @tc.number   SUB_KERNEL_FS_STDIO_0200
 * @tc.name     basic function test : Use the feof function to determine the end identifier of the file
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFeof, Function | MediumTest | Level3)
{
    FILE *fp = nullptr;
    int fd = 0;
    char writeBuf[] = "this is a file";
    char readBuf[20] = {0};

    // write
    fd = creat(FILE0, 0777);
    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
    WriteCloseTest(fd);

    // read
    fp = fopen(FILE0, "r+");
85
    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
M
mamingshuai 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

    EXPECT_EQ(fseeko(fp, 0, SEEK_SET), 0) << "> fseeko errno = " << errno;
    fgetc(fp);
    EXPECT_EQ(feof(fp), 0) << "> file should not be end!";      // check end
    EXPECT_EQ(fseeko(fp, 0, SEEK_SET), 0) << "> fseeko errno = " << errno;

    EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno;
    EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf;
    fgetc(fp);
    EXPECT_NE(feof(fp), 0) << "> file should be end!";          // check end
    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno;
}

/**
 * @tc.number   SUB_KERNEL_FS_STDIO_0300
 * @tc.name     basic function test : Use fseek to set the stream pointer position with SEEK_SET
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFseek, Function | MediumTest | Level3)
{
    FILE *fp = nullptr;
    int fd = 0;
    char writeBuf[] = "this is a file";
    char readBuf[20] = {0};

    // write
    fd = creat(FILE0, 0777);
    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
    WriteCloseTest(fd);

    // read
    fp = fopen(FILE0, "r+");
118
    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
M
mamingshuai 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

    fpos_t offset;
    EXPECT_EQ(fgetpos(fp, &offset), 0) << "> fgetpos errno = " << errno;

    EXPECT_EQ(fseek(fp, 2, SEEK_SET), 0) << "> fseek errno = " << errno;
    EXPECT_EQ(ftell(fp), 2) << " errno = " << errno;

    EXPECT_EQ(fsetpos(fp, &offset), 0) << "> fsetpos errno = " << errno;
    EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno;
    EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf;
    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno;
}

/**
 * @tc.number   SUB_KERNEL_FS_STDIO_0310
 * @tc.name     basic function test : Use fseek to set the stream pointer position with SEEK_CUR
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFseekSeekCur, Function | MediumTest | Level3)
{
    FILE *fp = nullptr;
    int fd = 0;
    char writeBuf[] = "this is a file";
    char readBuf[20] = {0};

    // write
    fd = creat(FILE0, 0777);
    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
    WriteCloseTest(fd);

    // read
    fp = fopen(FILE0, "r+");
151
    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
M
mamingshuai 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

    fpos_t offset;
    EXPECT_EQ(fgetpos(fp, &offset), 0) << "> fgetpos errno = " << errno;

    EXPECT_EQ(fseek(fp, 2, SEEK_CUR), 0) << "> fseek errno = " << errno;
    EXPECT_EQ(ftell(fp), 2) << " errno = " << errno;

    EXPECT_EQ(fsetpos(fp, &offset), 0) << "> fsetpos errno = " << errno;
    EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno;
    EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf;
    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno;
}

/**
 * @tc.number   SUB_KERNEL_FS_STDIO_0320
 * @tc.name     basic function test : Use fseek to set the stream pointer position with SEEK_END
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFseekSeekEnd, Function | MediumTest | Level3)
{
    FILE *fp = nullptr;
    int fd = 0;
    char writeBuf[] = "this is a file";
    char readBuf[20] = {0};

    // write
    fd = creat(FILE0, 0777);
    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
    WriteCloseTest(fd);

    // read
    fp = fopen(FILE0, "r+");
184
    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
M
mamingshuai 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

    fpos_t offset;
    EXPECT_EQ(fgetpos(fp, &offset), 0) << "> fgetpos errno = " << errno;

    EXPECT_EQ(fseek(fp, 0, SEEK_END), 0) << "> fseek errno = " << errno;
    EXPECT_EQ(ftell(fp), sizeof(writeBuf)) << " errno = " << errno;

    EXPECT_EQ(fsetpos(fp, &offset), 0) << "> fsetpos errno = " << errno;
    EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno;
    EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf;
    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno;
}

/**
 * @tc.number   SUB_KERNEL_FS_STDIO_0400
 * @tc.name     basic function test : Use fseeko to set the stream pointer position with SEEK_SET
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFseeko, Function | MediumTest | Level3)
{
    FILE *fp = nullptr;
    int fd = 0;
    char writeBuf[] = "this is a file";
    char readBuf[20] = {0};

    // write
    fd = creat(FILE0, 0777);
    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
    WriteCloseTest(fd);
W
wenjun 已提交
214 215 216

    // read
    fp = fopen(FILE0, "r+");
217
    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
M
mamingshuai 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

    EXPECT_EQ(fseeko(fp, 2, SEEK_SET), 0) << "> fseeko errno = " << errno;
    EXPECT_EQ(ftello(fp), 2) << " errno = " << errno;

    EXPECT_EQ(fseeko(fp, 0, SEEK_SET), 0) << "> fseeko errno = " << errno;
    EXPECT_EQ(ftello(fp), 0) << " errno = " << errno;
    EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno;
    EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf;
    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno;
}

/**
 * @tc.number   SUB_KERNEL_FS_STDIO_0410
 * @tc.name     basic function test : Use fseeko to set the stream pointer position with SEEK_CUR
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFseekoSeekCur, Function | MediumTest | Level3)
{
    FILE *fp = nullptr;
    int fd = 0;
    char writeBuf[] = "this is a file";
    char readBuf[20] = {0};

    // write
    fd = creat(FILE0, 0777);
    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
    WriteCloseTest(fd);

    // read
    fp = fopen(FILE0, "r+");
248
    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
M
mamingshuai 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

    EXPECT_EQ(fseeko(fp, 2, SEEK_CUR), 0) << "> fseeko errno = " << errno;
    EXPECT_EQ(ftello(fp), 2) << " errno = " << errno;

    EXPECT_EQ(fseeko(fp, 0, SEEK_SET), 0) << "> fseeko errno = " << errno;
    EXPECT_EQ(ftello(fp), 0) << " errno = " << errno;
    EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno;
    EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf;
    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno;
}

/**
 * @tc.number   SUB_KERNEL_FS_STDIO_0420
 * @tc.name     basic function test : Use fseeko to set the stream pointer position with SEEK_END
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFseekoSeekEnd, Function | MediumTest | Level3)
{
    FILE *fp = nullptr;
    int fd = 0;
    char writeBuf[] = "this is a file";
    char readBuf[20] = {0};

    // write
    fd = creat(FILE0, 0777);
    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
    WriteCloseTest(fd);

    // read
    fp = fopen(FILE0, "r+");
279
    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
M
mamingshuai 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

    EXPECT_EQ(fseeko(fp, 0, SEEK_END), 0) << "> fseeko errno = " << errno;
    EXPECT_EQ(ftello(fp), sizeof(writeBuf)) << " errno = " << errno;

    EXPECT_EQ(fseeko(fp, 0, SEEK_SET), 0) << "> fseeko errno = " << errno;
    EXPECT_EQ(ftello(fp), 0) << " errno = " << errno;
    EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno;
    EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf << "\n> readBuf = " << readBuf;
    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno;
}

void *ChildWrite(void *p)
{
    FILE *fp = (FILE*)p;
    flockfile(fp);
    size_t re0 = fwrite("this is ", sizeof("this is"), 1, fp);
    EXPECT_EQ(re0, 1) << "fwrite errno = " << errno;
    Msleep(FOURTY_MS);
    size_t re1 = fwrite("a file", sizeof("a file"), 1, fp);
    EXPECT_EQ(re1, 1) << "fwrite errno = " << errno;
    funlockfile(fp);
    LOG("> ChildWrite over");
    return nullptr;
}

/**
 * @tc.number   SUB_KERNEL_FS_STDIO_0500
 * @tc.name     basic function test : Use the funlockfile function to unlock the file
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFunlockfile, Function | MediumTest | Level3)
{
    const char filePath[] = TOP_DIR "/" DIR0 "/" DIR0_FILE0;
    char readBuf[20];
    char writeBuf[] = "this is a file";

    CreateTestFolder();
    FILE *fp = fopen(filePath, "r+");
318
    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
M
mamingshuai 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353

    pthread_t tid;
    int reInt = pthread_create(&tid, nullptr, ChildWrite, (void*)fp);
    ASSERT_EQ(reInt, 0) << "> pthread_create errno, reInt = " << reInt;

    Msleep(20);
    flockfile(fp);
    LOG("> childRead over");
    EXPECT_EQ(fseek(fp, 0, SEEK_SET), 0) << "> fseek errno = " << errno;
    EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread error";
    EXPECT_STREQ(writeBuf, readBuf);
    funlockfile(fp);

    int ret = pthread_join(tid, nullptr);
    EXPECT_EQ(ret, 0) << "pthread_join failed, errno=" << ret;
    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno;
}

/**
 * @tc.number   SUB_KERNEL_FS_STDIO_0600
 * @tc.name     basic function test : Use the fileno function to return the file descriptor of the stream, test write
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFileno, Function | MediumTest | Level2)
{
    const char filePath[] = TOP_DIR "/" FILE0;
    FILE *fp = nullptr;
    int fd = 0;

    // write
    fd = creat(filePath, 0777);
    ASSERT_NE(fd, -1) << "> creat errno = " << errno;
    ASSERT_NE(close(fd), -1) << "> close errno = " << errno;

    fp = fopen(filePath, "w+");
354
    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
M
mamingshuai 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    fd = fileno(fp);
    EXPECT_NE(fd, -1) << "> fileno errno = " << errno;
    WriteCloseTest(fd);

    // read
    fd = open(filePath, O_RDWR);
    EXPECT_NE(fd, -1) << "> open errno = " << errno;
    ReadCloseTest(fd);
}

/**
 * @tc.number   SUB_KERNEL_FS_STDIO_0610
 * @tc.name     basic function test : Use the fileno function to return the file descriptor of the stream, test read
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFileno1, Function | MediumTest | Level2)
{
    const char filePath[] = TOP_DIR "/" FILE0;
    FILE *fp = nullptr;
    int fd = 0;

    // write
    fd = creat(filePath, 0777);
    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
    WriteCloseTest(fd);

    // read
    fp = fopen(filePath, "r+");
383
    ASSERT_NE(fp, nullptr) << "> fopen errno = " << errno;
M
mamingshuai 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
    fd = fileno(fp);
    EXPECT_NE(fd, -1) << "> fileno errno = " << errno;

    ReadCloseTest(fd);
}

/**
 * @tc.number   SUB_KERNEL_FS_STDIO_0700
 * @tc.name     basic function test : Use the rename function to rename files.
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testRename, Function | MediumTest | Level3)
{
    int fd = 0;
    const char *newFileName = "FILE_NEW";
    fd = creat(FILE0, 0777);
    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;

    EXPECT_NE(rename(FILE0, newFileName), -1) << "> rename errno = " << errno;
    EXPECT_NE(unlink(newFileName), -1) << "> unlink errno = " << errno;
}

/**
 * @tc.number   SUB_KERNEL_FS_STDIO_0710
 * @tc.name     basic function test : Use the rename function to rename directories.
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testRenameDir, Function | MediumTest | Level3)
{
    const char *newDirName = "DIR_NEW";
    EXPECT_NE(mkdir(DIR0, 0777), -1) << "> mkdir errno = " << errno;

    EXPECT_NE(rename(DIR0, newDirName), -1) << "> rename errno = " << errno;
    EXPECT_NE(rmdir(newDirName), -1) << "> rmdir errno = " << errno;
}

/**
 * @tc.number   SUB_KERNEL_FS_STDIO_0800
 * @tc.name     basic function test : Use the fflush function to refresh stream
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFflush, Function | MediumTest | Level3)
{
    const char filePath[] = TOP_DIR "/" DIR0 "/" DIR0_FILE0;
    char writeBuf[] = "0123456789ABCDE";
    char readBuf[50] = {0};
    CreateTestFolder();
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "> parent: fork errno = " << errno;
    if (pid == 0) {
        int exitCode = 0;
        FILE *fp = fopen(filePath, "a+");
        if (fp == nullptr) {
            LOG("fp is null");
            exit(1);
        }
        setvbuf(fp, nullptr, _IOFBF, 1024);
        int reInt = fwrite(writeBuf, sizeof(writeBuf), 1, fp);
        if (reInt != 1) {
            LOG("fwrite return reInt = %d, errno = %d", reInt, errno);
            exitCode = 1;
        }
        Msleep(100);
        if (fflush(fp) == -1){
            LOG("fflush errno = %d", errno);
            exitCode = 1;
        }
        Msleep(100);
        if (fclose(fp) == -1){
            LOG("fclose errno = %d", errno);
            exitCode = 1;
        }
        exit(exitCode);
    }
    Msleep(50);
    FILE *fp = fopen(filePath, "r");
461
    ASSERT_NE(fp, nullptr);
M
mamingshuai 已提交
462 463 464 465 466 467
    EXPECT_EQ(fseek(fp, 0, SEEK_END), 0) << "> fseek errno = " << errno;
    EXPECT_EQ(ftell(fp), 0);
    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno;

    Msleep(100);
    fp = fopen(filePath, "r");
468
    ASSERT_NE(fp, nullptr);
M
mamingshuai 已提交
469 470 471 472 473 474 475
    EXPECT_NE(fread(readBuf, sizeof(writeBuf), 1, fp), 0) << "> fread errno = " << errno;
    EXPECT_STREQ(readBuf, writeBuf);
    LOG("> readBuf  = %s", readBuf);
    EXPECT_NE(fclose(fp), -1) << "> fclose errno =" << errno;

    Msleep(100);
    AssertProcExitedOK(pid);
W
wenjun 已提交
476
}