FsStatTest.cpp 13.2 KB
Newer Older
M
mamingshuai 已提交
1 2 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 118 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 151 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 184 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 214 215 216 217 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 248 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 279 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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * 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;

#ifndef COMMERCIAL
/**
 * @tc.number   SUB_KERNEL_FS_STAT_0100
 * @tc.name     basic function test : umask set and get file mode creation mask
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testUmask, Function | MediumTest | Level2)
{
    // set mode
    mode_t maskNew = GetRandom(077);
    mode_t maskPre = umask(maskNew);
    LOG("> maskPre = %d", maskPre);
    LOG("> maskNew = %d", maskNew);
    EXPECT_EQ(umask(maskPre), maskNew) << "> umask error";
}
#endif

/**
 * @tc.number   SUB_KERNEL_FS_STAT_0200
 * @tc.name     basic function test : Run the stat function to obtain the file status.
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testStat, Function | MediumTest | Level3)
{
    int fd = 0;
    mode_t mode = 0777;
    const char *filePath = TOP_DIR "/" FILE0;
    struct stat buf = {0};
    char writeBuf[] = "this is a file";

    fd = open(FILE0, O_CREAT | O_RDWR, mode);
    EXPECT_NE(fd, -1) << "> open faild errno = " << errno;
    WriteCloseTest(fd);

    EXPECT_NE(stat(filePath, &buf), -1) << "> fstat errno = " << errno;
    LOG("> buf.st_dev = %lu", buf.st_dev);                          // IDs of device on which file resides
    LOG("> buf.st_ino = %lu", buf.st_ino);                          // I-node number of file
    EXPECT_EQ(buf.st_rdev, 0) << "> buf.st_rdev not expect";        // IDs for device special files
    EXPECT_EQ(buf.st_size, sizeof(writeBuf)) << "> buf.st_size = " << buf.st_size;
    LOG("> buf.st_atim = %lds,%ldns", buf.st_atim.tv_sec, buf.st_atim.tv_nsec);     // time for last file access
    LOG("> buf.st_mtim = %lds,%ldns", buf.st_mtim.tv_sec, buf.st_mtim.tv_nsec);     // time for last file modification
    LOG("> buf.st_ctim = %lds,%ldns", buf.st_ctim.tv_sec, buf.st_ctim.tv_nsec);     // time for last file status change
}

#if defined(LITE_FS_JFFS2)
#ifndef COMMERCIAL
/**
 * @tc.number   SUB_KERNEL_FS_STAT_0210
 * @tc.name     basic function test : Run the stat function to test limt.
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testStatLimt, Function | MediumTest | Level3)
{
    int fd = 0;
    mode_t mode = 0777;
    const char *filePath = TOP_DIR "/" FILE0;
    struct stat buf;

    // set mode
    mode_t maskNew = GetRandom(077);
    mode_t maskPre = umask(maskNew);
    mode = 0700 + GetRandom(077);
    LOG("> maskPre = %d", maskPre);
    LOG("> maskNew = %d", maskNew);
    LOG("> mode = %d", mode);

    fd = open(FILE0, O_CREAT | O_RDWR, mode);
    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
    EXPECT_NE(stat(filePath, &buf), -1) << "> fstat errno = " << errno;

    EXPECT_NE((buf.st_mode & S_IFREG), 0) << "> check file type faild";
    EXPECT_EQ((buf.st_mode & 0777), (mode & (~maskNew))) << "> check file permission faild";
    EXPECT_EQ(buf.st_nlink, 1) << "> buf.st_nlink not expect";                      // Number of (hard) links to file
    EXPECT_EQ(buf.st_uid, getuid()) << "> The UIDs are different";                  // uid
    EXPECT_EQ(buf.st_gid, getgid()) << "> The GIDs are different";                  // giu
    umask(maskPre);
}
#endif
#endif

/**
 * @tc.number   SUB_KERNEL_FS_STAT_0300
 * @tc.name     basic function test : Run the lstat function to obtain the file status.
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testLstat, Function | MediumTest | Level3)
{
    int fd = 0;
    mode_t mode = 0777;
    const char *filePath = TOP_DIR "/" FILE0;
    struct stat buf = {0};
    char writeBuf[] = "this is a file";

    fd = open(filePath, O_CREAT | O_RDWR, mode);
    EXPECT_NE(fd, -1) << "> open faild errno = " << errno;
    WriteCloseTest(fd);

    EXPECT_NE(lstat(filePath, &buf), -1) << "> fstat errno = " << errno;
    LOG("> buf.st_dev = %lu", buf.st_dev);                          // IDs of device on which file resides
    LOG("> buf.st_ino = %lu", buf.st_ino);                          // I-node number of file
    EXPECT_EQ(buf.st_rdev, 0) << "> buf.st_rdev not expect";        // IDs for device special files
    EXPECT_EQ(buf.st_size, sizeof(writeBuf)) << "> buf.st_size = " << buf.st_size;

    LOG("> buf.st_atim = %lds,%ldns", buf.st_atim.tv_sec, buf.st_atim.tv_nsec);     // time for last file access
    LOG("> buf.st_mtim = %lds,%ldns", buf.st_mtim.tv_sec, buf.st_mtim.tv_nsec);     // time for last file modification
    LOG("> buf.st_ctim = %lds,%ldns", buf.st_ctim.tv_sec, buf.st_ctim.tv_nsec);     // time for last file status change
}

#if defined(LITE_FS_JFFS2)
#ifndef COMMERCIAL
/**
 * @tc.number   SUB_KERNEL_FS_STAT_0310
 * @tc.name     basic function test : Run the lstat function to test limt.
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testLstatLimt, Function | MediumTest | Level3)
{
    int fd = 0;
    mode_t mode = 0777;
    const char *filePath = TOP_DIR "/" FILE0;
    struct stat buf;

    // set mode
    mode_t maskNew = GetRandom(077);
    mode_t maskPre = umask(maskNew);
    mode = 0700 + GetRandom(077);
    LOG("> maskPre = %d", maskPre);
    LOG("> maskNew = %d", maskNew);
    LOG("> mode = %d", mode);

    fd = open(filePath, O_CREAT | O_RDWR, mode);
    EXPECT_NE(fd, -1) << "> open faild errno = " << errno;
    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;

    EXPECT_NE(lstat(filePath, &buf), -1) << "> fstat errno = " << errno;
    EXPECT_NE((buf.st_mode & S_IFREG), 0) << "> check file type faild";
    EXPECT_EQ((buf.st_mode & 0777), (mode & (~maskNew))) << "> check file permission faild";
    EXPECT_EQ(buf.st_nlink, 1) << "> buf.st_nlink not expect";                      // Number of (hard) links to file
    EXPECT_EQ(buf.st_uid, getuid()) << "> The UIDs are different";                  // uid
    EXPECT_EQ(buf.st_gid, getgid()) << "> The GIDs are different";                  // giu
    umask(maskPre);
}
#endif
#endif

/**
 * @tc.number   SUB_KERNEL_FS_STAT_0400
 * @tc.name     basic function test : Run the fstat function to obtain the file status.
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFstat, Function | MediumTest | Level3)
{
    int fd = 0;
    mode_t mode = 0777;
    struct stat buf = {0};
    char writeBuf[] = "this is a file";

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

    EXPECT_NE(fstat(fd, &buf), -1) << "> fstat errno = " << errno;
    LOG("> buf.st_dev = %lu", buf.st_dev);                          // IDs of device on which file resides
    LOG("> buf.st_ino = %lu", buf.st_ino);                          // I-node number of file
    EXPECT_EQ(buf.st_rdev, 0) << "> buf.st_rdev not expect";        // IDs for device special files
    EXPECT_EQ(buf.st_size, sizeof(writeBuf)) << "> buf.st_size = " << buf.st_size;
    LOG("> buf.st_atim = %lds,%ldns", buf.st_atim.tv_sec, buf.st_atim.tv_nsec);     // time for last file access
    LOG("> buf.st_mtim = %lds,%ldns", buf.st_mtim.tv_sec, buf.st_mtim.tv_nsec);     // time for last file modification
    LOG("> buf.st_ctim = %lds,%ldns", buf.st_ctim.tv_sec, buf.st_ctim.tv_nsec);     // time for last file status change
    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
}

#if defined(LITE_FS_JFFS2)
#ifndef COMMERCIAL
/**
 * @tc.number   SUB_KERNEL_FS_STAT_0410
 * @tc.name     basic function test : Run the fstat function to test limt
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testFstatLimt, Function | MediumTest | Level3)
{
    int fd = 0;
    mode_t mode = 0777;
    struct stat buf;

    // set mode
    mode_t maskNew = GetRandom(077);
    mode_t maskPre = umask(maskNew);
    mode = 0700 + GetRandom(077);
    LOG("> maskPre = %d", maskPre);
    LOG("> maskNew = %d", maskNew);
    LOG("> mode = %d", mode);

    fd = open(FILE0, O_CREAT | O_RDWR, mode);
    EXPECT_NE(fd, -1) << "> open faild errno = " << errno;

    EXPECT_NE(fstat(fd, &buf), -1) << "> fstat errno = " << errno;
    EXPECT_NE((buf.st_mode & S_IFREG), 0) << "> check file type faild";
    EXPECT_EQ((buf.st_mode & 0777), (mode & (~maskNew))) << "> check file permission faild";
    EXPECT_EQ(buf.st_nlink, 1) << "> buf.st_nlink not expect";                      // Number of (hard) links to file
    EXPECT_EQ(buf.st_uid, getuid()) << "> The UIDs are different";                  // uid
    EXPECT_EQ(buf.st_gid, getgid()) << "> The GIDs are different";                  // giu

    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
    umask(maskPre);
}
#endif
#endif

/**
 * @tc.number   SUB_KERNEL_FS_STAT_0500
 * @tc.name     basic function test : Run the {mkdirat} function to create a directory.
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testMkdirat, Function | MediumTest | Level0)
{
    const char *pathName = TOP_DIR "/" DIR0;
    EXPECT_NE(mkdirat(AT_FDCWD, pathName, 0777), -1) << "> creat faild errno = " << errno;
    EXPECT_EQ(access(pathName, F_OK), 0) << "> access F_OK errno = " << errno;
    EXPECT_EQ(remove(pathName), 0) << "> remove errno = " << errno;
    EXPECT_NE(access(pathName, F_OK), 0) << "> access F_OK expect faild but success";
}

/**
 * @tc.number   SUB_KERNEL_FS_STAT_0510
 * @tc.name     basic function test : test mkdirat with error number EINVAL
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testMkdiratEinval, Function | MediumTest | Level3)
{
    EXPECT_EQ(mkdirat(AT_FDCWD, nullptr, 0777), -1) << "> should be error";
    EXPECT_EQ(errno, EINVAL);
}

/**
 * @tc.number   SUB_KERNEL_FS_STAT_0520
 * @tc.name     basic function test : test mkdirat with error number ENAMETOOLONG
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testMkdiratEnametoolong, Function | MediumTest | Level3)
{
    const char *pathName = "12345678901234567890123456789012345678901234567890\
                            12345678901234567890123456789012345678901234567890\
                            12345678901234567890123456789012345678901234567890\
                            12345678901234567890123456789012345678901234567890\
                            12345678901234567890123456789012345678901234567890\
                            12345678901234567890123456789012345678901234567890\
                            12345678901234567890123456789012345678901234567890\
                            12345678901234567890123456789012345678901234567890";
    EXPECT_EQ(mkdirat(AT_FDCWD, pathName, 0777), -1) << "> should be error";
    EXPECT_EQ(errno, ENAMETOOLONG);
}

/**
 * @tc.number   SUB_KERNEL_FS_STAT_0530
 * @tc.name     basic function test : test mkdirat with error number ENOENT
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testMkdiratEnoent, Function | MediumTest | Level3)
{
    const char *pathName = TOP_DIR "/NoExist/NoExist";
    EXPECT_EQ(mkdirat(AT_FDCWD, pathName, 0777), -1) << "> should be error";
    EXPECT_EQ(errno, ENOENT);
}

/**
 * @tc.number   SUB_KERNEL_FS_STAT_0540
 * @tc.name     basic function test : test mkdirat with error number EEXIST
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testMkdiratEexist, Function | MediumTest | Level3)
{
    const char *pathName = TOP_DIR "/" DIR0;
    EXPECT_NE(mkdirat(AT_FDCWD, pathName, 0777), -1) << "> creat faild errno = " << errno;
    EXPECT_EQ(access(pathName, F_OK), 0) << "> access F_OK errno = " << errno;
    EXPECT_EQ(mkdirat(AT_FDCWD, pathName, 0777), -1) << "> should be error";
    EXPECT_EQ(errno, EEXIST);
}

#if defined(LITE_FS_JFFS2)
/**
 * @tc.number   SUB_KERNEL_FS_STAT_0600
 * @tc.name     basic function test : use chmod function to change mode
 * @tc.desc     [C- SOFTWARE -0200]
 */
HWTEST_F(FileSystemTest, testChmod, Function | MediumTest | Level3)
{
    struct stat buf;
    mode_t maskPre = umask(0);
    const char *fileName = TOP_DIR "/" FILE0;
    int fd = creat(FILE0, 0777);
    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;

    mode_t mode = 0666;
    EXPECT_EQ(chmod(fileName, mode), 0);
    EXPECT_NE(stat(fileName, &buf), -1) << "> fstat errno = " << errno;
    EXPECT_EQ((buf.st_mode & 0777), mode) << "> check file permission faild";
    umask(maskPre);
}
#endif