FsFcntlTest.cpp 12.6 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
 * 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>
M
mamingshuai 已提交
22
#include <sys/statvfs.h>
W
wenjun 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#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;

M
mamingshuai 已提交
38
#if defined(LITE_FS_NFS) || defined(LITE_FS_VFAT)
W
wenjun 已提交
39 40
/**
 * @tc.number   SUB_KERNEL_FS_FCNTL_0100
M
mamingshuai 已提交
41
 * @tc.name     basic function test : Test fcntl, use F_DUPFD to copy
W
wenjun 已提交
42 43
 * @tc.desc     [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
44
HWTEST_F(FileSystemTest, testFcntl, Function | MediumTest | Level3)
W
wenjun 已提交
45 46 47 48 49 50 51 52
{
    int fd = 0;
    EXPECT_NE((fd = open(FILE0, O_CREAT | O_RDWR, 0777)), -1) << "> open faild errno = " << errno;
    WriteCloseTest(fd);

    // F_DUPFD
    int fdNew = 0;
    EXPECT_NE((fd = open(FILE0, O_RDONLY, 0777)), -1) << "> open faild errno = " << errno;
M
mamingshuai 已提交
53 54 55 56 57 58 59
    // openharmony not support F_DUPFD, after support change to NE
    EXPECT_EQ((fdNew = fcntl(fd, F_DUPFD)), -1) << "> fcntl errno = " << errno;
    if (fdNew != -1) {
        ReadCloseTest(fdNew);
    }
    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
}
W
wenjun 已提交
60
#endif
M
mamingshuai 已提交
61 62 63 64 65 66 67 68 69 70 71 72

/**
 * @tc.number   SUB_KERNEL_FS_FCNTL_0110
 * @tc.name     basic function test : Test fcntl with F_GETFL
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFcntlFgetfl, Function | MediumTest | Level3)
{
    int fd = 0;
    EXPECT_NE((fd = open(FILE0, O_CREAT | O_RDWR, 0777)), -1) << "> open faild errno = " << errno;
    WriteCloseTest(fd);

W
wenjun 已提交
73 74 75 76 77 78 79
    int flagReadWrite = GetRandom(3) - 1;       // 0 is O_RDONLY, 1 is O_WRONLY, 2 is O_RDWR
    EXPECT_NE((fd = open(FILE0, flagReadWrite, 0777)), -1) << "> open faild errno = " << errno;

    // F_GETFL
    int flagGet = 0;
    EXPECT_NE((flagGet = fcntl(fd, F_GETFL)), -1) << "> fcntl errno = " << errno;
    EXPECT_EQ(flagReadWrite, (flagGet & O_ACCMODE)) << "> flagReadWrite != (flagGet & O_ACCMODE)";
M
mamingshuai 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
}

/**
 * @tc.number   SUB_KERNEL_FS_FCNTL_0120
 * @tc.name     basic function test : Test fcntl with F_SETFL
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFcntlFsetfl, Function | MediumTest | Level3)
{
    int fd = 0;
    EXPECT_NE((fd = open(FILE0, O_CREAT | O_RDWR, 0777)), -1) << "> open faild errno = " << errno;
    WriteCloseTest(fd);

    int flagReadWrite = GetRandom(3) - 1;       // 0 is O_RDONLY, 1 is O_WRONLY, 2 is O_RDWR
    EXPECT_NE((fd = open(FILE0, flagReadWrite, 0777)), -1) << "> open faild errno = " << errno;
W
wenjun 已提交
96 97

    // F_SETFL
M
mamingshuai 已提交
98
    int flagSet = fcntl(fd, F_GETFL) | O_APPEND | O_NONBLOCK;
W
wenjun 已提交
99 100
    EXPECT_NE(fcntl(fd, F_SETFL, flagSet), -1) << "> fcntl errno = " << errno;

M
mamingshuai 已提交
101 102 103
    // use F_GETFL check
    int flagGet = fcntl(fd, F_GETFL);
    EXPECT_NE(flagGet, -1) << "> fcntl errno = " << errno;
W
wenjun 已提交
104 105 106 107 108 109 110
    EXPECT_EQ(flagGet & O_APPEND, O_APPEND) << "> O_APPEND faild";
    EXPECT_EQ(flagGet & O_NONBLOCK, O_NONBLOCK) << "> O_NONBLOCK faild";
    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
}

/**
 * @tc.number   SUB_KERNEL_FS_FCNTL_0200
M
mamingshuai 已提交
111
 * @tc.name     basic function test : test lseek with SEEK_CUR
W
wenjun 已提交
112 113
 * @tc.desc     [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
114
HWTEST_F(FileSystemTest, testLseek, Function | MediumTest | Level3)
W
wenjun 已提交
115 116 117 118
{
    int reLseek;
    int fd = 0;
    char writeBuf[100];
M
mamingshuai 已提交
119 120 121
    for (int i = 0; i < 100; i++) {
        writeBuf[i] = '1';
    }
W
wenjun 已提交
122 123 124 125

    fd = open(FILE0, O_CREAT | O_RDWR, 0777);
    EXPECT_NE(fd, -1) << "> open faild errno = " << errno;
    EXPECT_NE(write(fd, writeBuf, 20), -1) << "> write errno = " << errno;
M
mamingshuai 已提交
126 127

    // reLseek 0 -> 20
W
wenjun 已提交
128 129 130 131
    reLseek = lseek(fd, 0, SEEK_CUR);
    EXPECT_NE(reLseek, -1) << "> lseek errno = " << errno;
    EXPECT_EQ(reLseek, 20) << "> reLseek = " << reLseek;

M
mamingshuai 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    // reLseek 20 -> 40
    EXPECT_NE(write(fd, writeBuf, 20), -1) << "> write errno = " << errno;
    reLseek = lseek(fd, 0, SEEK_CUR);
    EXPECT_NE(reLseek, -1) << "> lseek errno = " << errno;
    EXPECT_EQ(reLseek, 40) << "> reLseek = " << reLseek;

    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
}

/**
 * @tc.number   SUB_KERNEL_FS_FCNTL_0210
 * @tc.name     basic function test : test lseek wioth SEEK_SET
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testLseekSeekSet, Function | MediumTest | Level3)
{
    int reLseek;
    int fd = 0;
    char writeBuf[100];
    for (int i = 0; i < 100; i++) {
        writeBuf[i] = '1';
    }

    fd = open(FILE0, O_CREAT | O_RDWR, 0777);
    EXPECT_NE(fd, -1) << "> open faild errno = " << errno;
    EXPECT_NE(write(fd, writeBuf, 20), -1) << "> write errno = " << errno;

    // reLseek 20 -> 0
    reLseek = lseek(fd, 0, SEEK_SET);
    EXPECT_NE(reLseek, -1) << "> lseek errno = " << errno;
    EXPECT_EQ(reLseek, 0) << "> reLseek = " << reLseek;

    // reLseek 0 -> 10
W
wenjun 已提交
165 166 167 168
    reLseek = lseek(fd, 10, SEEK_SET);
    EXPECT_NE(reLseek, -1) << "> lseek errno = " << errno;
    EXPECT_EQ(reLseek, 10) << "> reLseek = " << reLseek;

M
mamingshuai 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
}

/**
 * @tc.number   SUB_KERNEL_FS_FCNTL_0220
 * @tc.name     basic function test : test lseek wioth SEEK_END
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testLseekSeekEnd, Function | MediumTest | Level3)
{
    int reLseek;
    int fd = 0;
    char writeBuf[100];
    for (int i = 0; i < 100; i++) {
        writeBuf[i] = '1';
    }

    fd = open(FILE0, O_CREAT | O_RDWR, 0777);
    EXPECT_NE(fd, -1) << "> open faild errno = " << errno;
    EXPECT_NE(write(fd, writeBuf, 20), -1) << "> write errno = " << errno;
W
wenjun 已提交
189 190 191 192 193 194

    // reLseek 20 -> 10
    reLseek = lseek(fd, -10, SEEK_END);
    EXPECT_NE(reLseek, -1) << "> lseek errno = " << errno;
    EXPECT_EQ(reLseek, 10) << "> reLseek = " << reLseek;

M
mamingshuai 已提交
195 196
    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
}
W
wenjun 已提交
197

M
mamingshuai 已提交
198 199 200 201 202 203 204 205 206
#if defined(LITE_FS_VFAT)
/**
 * @tc.number   SUB_KERNEL_FS_FCNTL_0300
 * @tc.name     basic function test : Use the fallocate function to pre-allocate large space to the file
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFallocate, Function | MediumTest | Level2)
{
    char writeBuf[] = "123456789ABCDE";
W
wenjun 已提交
207

M
mamingshuai 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    unsigned int file_size;
    file_size = GetRandom(1024) * 102400 + 20;   // >20
    LOG("> GetRandom = %u", file_size);

    struct statvfs vfsBufOld;
    statvfs(".", &vfsBufOld);        // old system info.
    LOG("> vfsBufOld.f_bfree = %lu", vfsBufOld.f_bfree);

    int fd = open(FILE0, O_RDWR | O_CREAT | O_EXCL, 0777);
    ASSERT_NE(fd, -1) << "> open faild errno = " << errno;
    EXPECT_NE(write(fd, writeBuf, sizeof(writeBuf)), -1) << "> write errno = " << errno;

    int reInt = fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, file_size);
    EXPECT_EQ(reInt, 0) << "> fallocate errno = " << errno;
    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;

    // expect not changed
    struct stat statbuf;
    EXPECT_NE(stat(FILE0, &statbuf), -1) << "> fstat errno = " << errno;
    EXPECT_TRUE(statbuf.st_size == sizeof(writeBuf));

    struct statvfs vfsBufNew;
    statvfs(".", &vfsBufNew);        // new system info.
    LOG("> vfsBufNew.f_bfree = %lu", vfsBufNew.f_bfree);
W
wenjun 已提交
232

N
nanxiansen 已提交
233 234
    int ret = strcmp(TOP_DIR, "sdcard");
    if(ret = 0) {
M
mamingshuai 已提交
235 236 237 238 239
    int bNumExpect = file_size / vfsBufNew.f_bsize;
    int bNum = vfsBufOld.f_bfree - vfsBufNew.f_bfree;
    LOG("> bNumExpect = %d", bNumExpect);
    LOG("> bNum = %d", bNum);
    EXPECT_GE(bNum, bNumExpect);
N
nanxiansen 已提交
240
    }
M
mamingshuai 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
}
#endif

#if defined(LITE_FS_VFAT)
/**
 * @tc.number   SUB_KERNEL_FS_FCNTL_0310
 * @tc.name     basic function test : Use the fallocate function to pre-allocate a small space to the file,
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFallocateSmallSize, Function | MediumTest | Level2)
{
    char writeBuf[] = "123456789ABCDE";

    unsigned int file_size;
    file_size = GetRandom(1024) + 20;   // >20
    LOG("> GetRandom = %u", file_size);

    struct statvfs vfsBufOld;
    statvfs(".", &vfsBufOld);        // old system info.
    LOG("> vfsBufOld.f_bfree = %lu", vfsBufOld.f_bfree);

    int fd = open(FILE0, O_RDWR | O_CREAT | O_EXCL, 0777);
    ASSERT_NE(fd, -1) << "> open faild errno = " << errno;
    EXPECT_NE(write(fd, writeBuf, sizeof(writeBuf)), -1) << "> write errno = " << errno;

    int reInt = fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, file_size);
    EXPECT_EQ(reInt, 0) << "> fallocate errno = " << errno;
W
wenjun 已提交
268
    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
M
mamingshuai 已提交
269 270 271 272 273 274 275 276 277 278

    // expect not changed
    struct stat statbuf;
    EXPECT_NE(stat(FILE0, &statbuf), -1) << "> fstat errno = " << errno;
    EXPECT_TRUE(statbuf.st_size == sizeof(writeBuf));

    struct statvfs vfsBufNew;
    statvfs(".", &vfsBufNew);        // new system info.
    LOG("> vfsBufNew.f_bfree = %lu", vfsBufNew.f_bfree);

N
nanxiansen 已提交
279 280
    int ret = strcmp(TOP_DIR, "sdcard");
    if (ret = 0) {
M
mamingshuai 已提交
281 282 283 284 285
    int bNumExpect = file_size / vfsBufNew.f_bsize;
    int bNum = vfsBufOld.f_bfree - vfsBufNew.f_bfree;
    LOG("> bNumExpect = %d", bNumExpect);
    LOG("> bNum = %d", bNum);
    EXPECT_GE(bNum, bNumExpect);
N
nanxiansen 已提交
286
    }
W
wenjun 已提交
287 288
}
#endif
M
mamingshuai 已提交
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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

/**
 * @tc.number   SUB_KERNEL_FS_FCNTL_0400
 * @tc.name     basic function test : test open with O_RDWR O_CREAT O_EXCL when the file does not exist
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testOpen, Function | MediumTest | Level2)
{
    int fd = open(FILE0, O_RDWR | O_CREAT | O_EXCL, 0777);
    EXPECT_NE(fd, -1) << "> open faild errno = " << errno;
    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
}

/**
 * @tc.number   SUB_KERNEL_FS_FCNTL_0410
 * @tc.name     basic function test : test open with error number EEXIST
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testOpenEexist, Function | MediumTest | Level2)
{
    int fd = 0;
    fd = creat(FILE0, 0777);
    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;

    fd = open(FILE0, O_RDWR | O_CREAT | O_EXCL, 0777);
    EXPECT_EQ(fd, -1) << "> Should open failed";
    EXPECT_EQ(errno, EEXIST);
    close(fd);
}

/**
 * @tc.number   SUB_KERNEL_FS_FCNTL_0420
 * @tc.name     basic function test : test open with error number EISDIR
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testOpenEisdir, Function | MediumTest | Level2)
{
    int fd = 0;
    CreateTestFolder();

    fd = open(DIR0, O_RDWR, 0777);
    EXPECT_EQ(fd, -1) << "> Should open failed";
    EXPECT_EQ(errno, EISDIR);
C
chuaizhzh 已提交
333
    close(fd);
M
mamingshuai 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
}

/**
 * @tc.number   SUB_KERNEL_FS_FCNTL_0430
 * @tc.name     basic function test : test open with error number ENOENT
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testOpenEnoent, Function | MediumTest | Level2)
{
    int fd = 0;

    fd = open(FILE0, O_RDWR, 0777);
    EXPECT_EQ(fd, -1) << "> Should open failed";
    EXPECT_EQ(errno, ENOENT);
    close(fd);
}

/**
 * @tc.number   SUB_KERNEL_FS_FCNTL_0440
 * @tc.name     basic function test : test open with error number ENAMETOOLONG
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testOpenEnametoolong, Function | MediumTest | Level2)
{
    int fd = 0;
    const char *fileName = "12345678901234567890123456789012345678901234567890\
                            12345678901234567890123456789012345678901234567890\
                            12345678901234567890123456789012345678901234567890\
                            12345678901234567890123456789012345678901234567890\
                            12345678901234567890123456789012345678901234567890\
                            12345678901234567890123456789012345678901234567890\
                            12345678901234567890123456789012345678901234567890\
                            12345678901234567890123456789012345678901234567890";

    fd = open(fileName, O_RDWR | O_CREAT | O_EXCL, 0777);
    EXPECT_EQ(fd, -1) << "> Should open failed";
    EXPECT_EQ(errno, ENAMETOOLONG);
    close(fd);
}