1f6603c60Sopenharmony_ci/* 2f6603c60Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License. 5f6603c60Sopenharmony_ci * You may obtain a copy of the License at 6f6603c60Sopenharmony_ci * 7f6603c60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f6603c60Sopenharmony_ci * 9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and 13f6603c60Sopenharmony_ci * limitations under the License. 14f6603c60Sopenharmony_ci */ 15f6603c60Sopenharmony_ci#include "FileSystemTest.h" 16f6603c60Sopenharmony_ci#include <stdio.h> 17f6603c60Sopenharmony_ci#include <string.h> 18f6603c60Sopenharmony_ci#include <stdlib.h> 19f6603c60Sopenharmony_ci 20f6603c60Sopenharmony_ci#include <sys/stat.h> 21f6603c60Sopenharmony_ci#include <sys/types.h> 22f6603c60Sopenharmony_ci#include <fcntl.h> 23f6603c60Sopenharmony_ci#include <unistd.h> 24f6603c60Sopenharmony_ci#include <dirent.h> 25f6603c60Sopenharmony_ci#include <ftw.h> 26f6603c60Sopenharmony_ci#include <libgen.h> 27f6603c60Sopenharmony_ci 28f6603c60Sopenharmony_ci#include <gtest/gtest.h> 29f6603c60Sopenharmony_ci 30f6603c60Sopenharmony_ci#include "utils.h" 31f6603c60Sopenharmony_ci#include "log.h" 32f6603c60Sopenharmony_ci#include "KernelConstants.h" 33f6603c60Sopenharmony_ci#include "libfs.h" 34f6603c60Sopenharmony_ci 35f6603c60Sopenharmony_ciusing namespace testing::ext; 36f6603c60Sopenharmony_ci 37f6603c60Sopenharmony_ci#ifndef COMMERCIAL 38f6603c60Sopenharmony_ci/** 39f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_FS_STAT_0100 40f6603c60Sopenharmony_ci * @tc.name basic function test : umask set and get file mode creation mask 41f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 42f6603c60Sopenharmony_ci */ 43f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testUmask, Function | MediumTest | Level2) 44f6603c60Sopenharmony_ci{ 45f6603c60Sopenharmony_ci // set mode 46f6603c60Sopenharmony_ci mode_t maskNew = GetRandom(077); 47f6603c60Sopenharmony_ci mode_t maskPre = umask(maskNew); 48f6603c60Sopenharmony_ci LOG("> maskPre = %d", maskPre); 49f6603c60Sopenharmony_ci LOG("> maskNew = %d", maskNew); 50f6603c60Sopenharmony_ci EXPECT_EQ(umask(maskPre), maskNew) << "> umask error"; 51f6603c60Sopenharmony_ci} 52f6603c60Sopenharmony_ci#endif 53f6603c60Sopenharmony_ci#if defined(LITE_FS_JFFS2) 54f6603c60Sopenharmony_ci#ifndef COMMERCIAL 55f6603c60Sopenharmony_ci/** 56f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_FS_STAT_0210 57f6603c60Sopenharmony_ci * @tc.name basic function test : Run the stat function to test limt. 58f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 59f6603c60Sopenharmony_ci */ 60f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testStatLimt, Function | MediumTest | Level3) 61f6603c60Sopenharmony_ci{ 62f6603c60Sopenharmony_ci int fd = 0; 63f6603c60Sopenharmony_ci mode_t mode = 0777; 64f6603c60Sopenharmony_ci const char *filePath = TOP_DIR "/" FILE0; 65f6603c60Sopenharmony_ci struct stat buf; 66f6603c60Sopenharmony_ci 67f6603c60Sopenharmony_ci // set mode 68f6603c60Sopenharmony_ci mode_t maskNew = GetRandom(077); 69f6603c60Sopenharmony_ci mode_t maskPre = umask(maskNew); 70f6603c60Sopenharmony_ci mode = 0700 + GetRandom(077); 71f6603c60Sopenharmony_ci LOG("> maskPre = %d", maskPre); 72f6603c60Sopenharmony_ci LOG("> maskNew = %d", maskNew); 73f6603c60Sopenharmony_ci LOG("> mode = %d", mode); 74f6603c60Sopenharmony_ci 75f6603c60Sopenharmony_ci fd = open(FILE0, O_CREAT | O_RDWR, mode); 76f6603c60Sopenharmony_ci EXPECT_NE(close(fd), -1) << "> close errno = " << errno; 77f6603c60Sopenharmony_ci EXPECT_NE(stat(filePath, &buf), -1) << "> fstat errno = " << errno; 78f6603c60Sopenharmony_ci 79f6603c60Sopenharmony_ci EXPECT_NE((buf.st_mode & S_IFREG), 0) << "> check file type faild"; 80f6603c60Sopenharmony_ci EXPECT_EQ((buf.st_mode & 0777), (mode & (~maskNew))) << "> check file permission faild"; 81f6603c60Sopenharmony_ci EXPECT_EQ(buf.st_nlink, 1) << "> buf.st_nlink not expect"; // Number of (hard) links to file 82f6603c60Sopenharmony_ci EXPECT_EQ(buf.st_uid, getuid()) << "> The UIDs are different"; // uid 83f6603c60Sopenharmony_ci EXPECT_EQ(buf.st_gid, getgid()) << "> The GIDs are different"; // giu 84f6603c60Sopenharmony_ci umask(maskPre); 85f6603c60Sopenharmony_ci} 86f6603c60Sopenharmony_ci#endif 87f6603c60Sopenharmony_ci#endif 88f6603c60Sopenharmony_ci#if defined(LITE_FS_JFFS2) 89f6603c60Sopenharmony_ci#ifndef COMMERCIAL 90f6603c60Sopenharmony_ci/** 91f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_FS_STAT_0310 92f6603c60Sopenharmony_ci * @tc.name basic function test : Run the lstat function to test limt. 93f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 94f6603c60Sopenharmony_ci */ 95f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testLstatLimt, Function | MediumTest | Level3) 96f6603c60Sopenharmony_ci{ 97f6603c60Sopenharmony_ci int fd = 0; 98f6603c60Sopenharmony_ci mode_t mode = 0777; 99f6603c60Sopenharmony_ci const char *filePath = TOP_DIR "/" FILE0; 100f6603c60Sopenharmony_ci struct stat buf; 101f6603c60Sopenharmony_ci 102f6603c60Sopenharmony_ci // set mode 103f6603c60Sopenharmony_ci mode_t maskNew = GetRandom(077); 104f6603c60Sopenharmony_ci mode_t maskPre = umask(maskNew); 105f6603c60Sopenharmony_ci mode = 0700 + GetRandom(077); 106f6603c60Sopenharmony_ci LOG("> maskPre = %d", maskPre); 107f6603c60Sopenharmony_ci LOG("> maskNew = %d", maskNew); 108f6603c60Sopenharmony_ci LOG("> mode = %d", mode); 109f6603c60Sopenharmony_ci 110f6603c60Sopenharmony_ci fd = open(filePath, O_CREAT | O_RDWR, mode); 111f6603c60Sopenharmony_ci EXPECT_NE(fd, -1) << "> open faild errno = " << errno; 112f6603c60Sopenharmony_ci EXPECT_NE(close(fd), -1) << "> close errno = " << errno; 113f6603c60Sopenharmony_ci 114f6603c60Sopenharmony_ci EXPECT_NE(lstat(filePath, &buf), -1) << "> fstat errno = " << errno; 115f6603c60Sopenharmony_ci EXPECT_NE((buf.st_mode & S_IFREG), 0) << "> check file type faild"; 116f6603c60Sopenharmony_ci EXPECT_EQ((buf.st_mode & 0777), (mode & (~maskNew))) << "> check file permission faild"; 117f6603c60Sopenharmony_ci EXPECT_EQ(buf.st_nlink, 1) << "> buf.st_nlink not expect"; // Number of (hard) links to file 118f6603c60Sopenharmony_ci EXPECT_EQ(buf.st_uid, getuid()) << "> The UIDs are different"; // uid 119f6603c60Sopenharmony_ci EXPECT_EQ(buf.st_gid, getgid()) << "> The GIDs are different"; // giu 120f6603c60Sopenharmony_ci umask(maskPre); 121f6603c60Sopenharmony_ci} 122f6603c60Sopenharmony_ci#endif 123f6603c60Sopenharmony_ci#endif 124f6603c60Sopenharmony_ci#if defined(LITE_FS_JFFS2) 125f6603c60Sopenharmony_ci#ifndef COMMERCIAL 126f6603c60Sopenharmony_ci/** 127f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_FS_STAT_0410 128f6603c60Sopenharmony_ci * @tc.name basic function test : Run the fstat function to test limt 129f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 130f6603c60Sopenharmony_ci */ 131f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testFstatLimt, Function | MediumTest | Level3) 132f6603c60Sopenharmony_ci{ 133f6603c60Sopenharmony_ci int fd = 0; 134f6603c60Sopenharmony_ci mode_t mode = 0777; 135f6603c60Sopenharmony_ci struct stat buf; 136f6603c60Sopenharmony_ci 137f6603c60Sopenharmony_ci // set mode 138f6603c60Sopenharmony_ci mode_t maskNew = GetRandom(077); 139f6603c60Sopenharmony_ci mode_t maskPre = umask(maskNew); 140f6603c60Sopenharmony_ci mode = 0700 + GetRandom(077); 141f6603c60Sopenharmony_ci LOG("> maskPre = %d", maskPre); 142f6603c60Sopenharmony_ci LOG("> maskNew = %d", maskNew); 143f6603c60Sopenharmony_ci LOG("> mode = %d", mode); 144f6603c60Sopenharmony_ci 145f6603c60Sopenharmony_ci fd = open(FILE0, O_CREAT | O_RDWR, mode); 146f6603c60Sopenharmony_ci EXPECT_NE(fd, -1) << "> open faild errno = " << errno; 147f6603c60Sopenharmony_ci 148f6603c60Sopenharmony_ci EXPECT_NE(fstat(fd, &buf), -1) << "> fstat errno = " << errno; 149f6603c60Sopenharmony_ci EXPECT_NE((buf.st_mode & S_IFREG), 0) << "> check file type faild"; 150f6603c60Sopenharmony_ci EXPECT_EQ((buf.st_mode & 0777), (mode & (~maskNew))) << "> check file permission faild"; 151f6603c60Sopenharmony_ci EXPECT_EQ(buf.st_nlink, 1) << "> buf.st_nlink not expect"; // Number of (hard) links to file 152f6603c60Sopenharmony_ci EXPECT_EQ(buf.st_uid, getuid()) << "> The UIDs are different"; // uid 153f6603c60Sopenharmony_ci EXPECT_EQ(buf.st_gid, getgid()) << "> The GIDs are different"; // giu 154f6603c60Sopenharmony_ci 155f6603c60Sopenharmony_ci EXPECT_NE(close(fd), -1) << "> close errno = " << errno; 156f6603c60Sopenharmony_ci umask(maskPre); 157f6603c60Sopenharmony_ci} 158f6603c60Sopenharmony_ci#endif 159f6603c60Sopenharmony_ci#endif 160f6603c60Sopenharmony_ci 161f6603c60Sopenharmony_ci/** 162f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_FS_STAT_0500 163f6603c60Sopenharmony_ci * @tc.name basic function test : Run the {mkdirat} function to create a directory. 164f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 165f6603c60Sopenharmony_ci */ 166f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testMkdirat, Function | MediumTest | Level0) 167f6603c60Sopenharmony_ci{ 168f6603c60Sopenharmony_ci const char *pathName = TOP_DIR "/" DIR0; 169f6603c60Sopenharmony_ci EXPECT_NE(mkdirat(AT_FDCWD, pathName, 0777), -1) << "> creat faild errno = " << errno; 170f6603c60Sopenharmony_ci EXPECT_EQ(access(pathName, F_OK), 0) << "> access F_OK errno = " << errno; 171f6603c60Sopenharmony_ci EXPECT_EQ(remove(pathName), 0) << "> remove errno = " << errno; 172f6603c60Sopenharmony_ci EXPECT_NE(access(pathName, F_OK), 0) << "> access F_OK expect faild but success"; 173f6603c60Sopenharmony_ci} 174f6603c60Sopenharmony_ci 175f6603c60Sopenharmony_ci/** 176f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_FS_STAT_0510 177f6603c60Sopenharmony_ci * @tc.name basic function test : test mkdirat with error number EINVAL 178f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 179f6603c60Sopenharmony_ci */ 180f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testMkdiratEinval, Function | MediumTest | Level3) 181f6603c60Sopenharmony_ci{ 182f6603c60Sopenharmony_ci EXPECT_EQ(mkdirat(AT_FDCWD, nullptr, 0777), -1) << "> should be error"; 183f6603c60Sopenharmony_ci EXPECT_EQ(errno, EINVAL); 184f6603c60Sopenharmony_ci} 185f6603c60Sopenharmony_ci 186f6603c60Sopenharmony_ci/** 187f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_FS_STAT_0520 188f6603c60Sopenharmony_ci * @tc.name basic function test : test mkdirat with error number ENAMETOOLONG 189f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 190f6603c60Sopenharmony_ci */ 191f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testMkdiratEnametoolong, Function | MediumTest | Level3) 192f6603c60Sopenharmony_ci{ 193f6603c60Sopenharmony_ci const char *pathName = "12345678901234567890123456789012345678901234567890\ 194f6603c60Sopenharmony_ci 12345678901234567890123456789012345678901234567890\ 195f6603c60Sopenharmony_ci 12345678901234567890123456789012345678901234567890\ 196f6603c60Sopenharmony_ci 12345678901234567890123456789012345678901234567890\ 197f6603c60Sopenharmony_ci 12345678901234567890123456789012345678901234567890\ 198f6603c60Sopenharmony_ci 12345678901234567890123456789012345678901234567890\ 199f6603c60Sopenharmony_ci 12345678901234567890123456789012345678901234567890\ 200f6603c60Sopenharmony_ci 12345678901234567890123456789012345678901234567890"; 201f6603c60Sopenharmony_ci EXPECT_EQ(mkdirat(AT_FDCWD, pathName, 0777), -1) << "> should be error"; 202f6603c60Sopenharmony_ci EXPECT_EQ(errno, ENAMETOOLONG); 203f6603c60Sopenharmony_ci} 204f6603c60Sopenharmony_ci 205f6603c60Sopenharmony_ci/** 206f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_FS_STAT_0530 207f6603c60Sopenharmony_ci * @tc.name basic function test : test mkdirat with error number ENOENT 208f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 209f6603c60Sopenharmony_ci */ 210f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testMkdiratEnoent, Function | MediumTest | Level3) 211f6603c60Sopenharmony_ci{ 212f6603c60Sopenharmony_ci const char *pathName = TOP_DIR "/NoExist/NoExist"; 213f6603c60Sopenharmony_ci EXPECT_EQ(mkdirat(AT_FDCWD, pathName, 0777), -1) << "> should be error"; 214f6603c60Sopenharmony_ci EXPECT_EQ(errno, ENOENT); 215f6603c60Sopenharmony_ci} 216f6603c60Sopenharmony_ci 217f6603c60Sopenharmony_ci/** 218f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_FS_STAT_0540 219f6603c60Sopenharmony_ci * @tc.name basic function test : test mkdirat with error number EEXIST 220f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 221f6603c60Sopenharmony_ci */ 222f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testMkdiratEexist, Function | MediumTest | Level3) 223f6603c60Sopenharmony_ci{ 224f6603c60Sopenharmony_ci const char *pathName = TOP_DIR "/" DIR0; 225f6603c60Sopenharmony_ci EXPECT_NE(mkdirat(AT_FDCWD, pathName, 0777), -1) << "> creat faild errno = " << errno; 226f6603c60Sopenharmony_ci EXPECT_EQ(access(pathName, F_OK), 0) << "> access F_OK errno = " << errno; 227f6603c60Sopenharmony_ci EXPECT_EQ(mkdirat(AT_FDCWD, pathName, 0777), -1) << "> should be error"; 228f6603c60Sopenharmony_ci EXPECT_EQ(errno, EEXIST); 229f6603c60Sopenharmony_ci} 230f6603c60Sopenharmony_ci 231f6603c60Sopenharmony_ci#if defined(LITE_FS_JFFS2) 232f6603c60Sopenharmony_ci/** 233f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_FS_STAT_0600 234f6603c60Sopenharmony_ci * @tc.name basic function test : use chmod function to change mode 235f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 236f6603c60Sopenharmony_ci */ 237f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testChmod, Function | MediumTest | Level3) 238f6603c60Sopenharmony_ci{ 239f6603c60Sopenharmony_ci struct stat buf; 240f6603c60Sopenharmony_ci mode_t maskPre = umask(0); 241f6603c60Sopenharmony_ci const char *fileName = TOP_DIR "/" FILE0; 242f6603c60Sopenharmony_ci int fd = creat(FILE0, 0777); 243f6603c60Sopenharmony_ci EXPECT_NE(fd, -1) << "> creat faild errno = " << errno; 244f6603c60Sopenharmony_ci EXPECT_NE(close(fd), -1) << "> close errno = " << errno; 245f6603c60Sopenharmony_ci 246f6603c60Sopenharmony_ci mode_t mode = 0666; 247f6603c60Sopenharmony_ci EXPECT_EQ(chmod(fileName, mode), 0); 248f6603c60Sopenharmony_ci EXPECT_NE(stat(fileName, &buf), -1) << "> fstat errno = " << errno; 249f6603c60Sopenharmony_ci EXPECT_EQ((buf.st_mode & 0777), mode) << "> check file permission faild"; 250f6603c60Sopenharmony_ci umask(maskPre); 251f6603c60Sopenharmony_ci} 252f6603c60Sopenharmony_ci#endif 253