1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2020-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 <sys/statvfs.h>
23f6603c60Sopenharmony_ci#include <sys/statfs.h>
24f6603c60Sopenharmony_ci#include <sys/mount.h>
25f6603c60Sopenharmony_ci#include <wchar.h>
26f6603c60Sopenharmony_ci#include <fcntl.h>
27f6603c60Sopenharmony_ci#include <unistd.h>
28f6603c60Sopenharmony_ci#include <dirent.h>
29f6603c60Sopenharmony_ci#include <ftw.h>
30f6603c60Sopenharmony_ci#include <libgen.h>
31f6603c60Sopenharmony_ci#include <glob.h>
32f6603c60Sopenharmony_ci
33f6603c60Sopenharmony_ci#include <gtest/gtest.h>
34f6603c60Sopenharmony_ci
35f6603c60Sopenharmony_ci#include "utils.h"
36f6603c60Sopenharmony_ci#include "log.h"
37f6603c60Sopenharmony_ci#include "KernelConstants.h"
38f6603c60Sopenharmony_ci#include "libfs.h"
39f6603c60Sopenharmony_ci
40f6603c60Sopenharmony_ciusing namespace testing::ext;
41f6603c60Sopenharmony_ci
42f6603c60Sopenharmony_ci/**
43f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_FS_OTHER_0100
44f6603c60Sopenharmony_ci * @tc.name     basic function test : get dirname
45f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
46f6603c60Sopenharmony_ci */
47f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testPath, Function | MediumTest | Level1)
48f6603c60Sopenharmony_ci{
49f6603c60Sopenharmony_ci    int fd = 0;
50f6603c60Sopenharmony_ci    fd = creat(FILE0, 0777);
51f6603c60Sopenharmony_ci    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
52f6603c60Sopenharmony_ci    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
53f6603c60Sopenharmony_ci
54f6603c60Sopenharmony_ci    // get dir
55f6603c60Sopenharmony_ci    char *workDir = dirname((char*)FILE0);
56f6603c60Sopenharmony_ci    ASSERT_NE(workDir, nullptr) << "> dirname errno = " << errno;
57f6603c60Sopenharmony_ci    EXPECT_STREQ(".", workDir);
58f6603c60Sopenharmony_ci    LOG("> workDir = %s", workDir);
59f6603c60Sopenharmony_ci}
60f6603c60Sopenharmony_ci
61f6603c60Sopenharmony_ci/**
62f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_FS_OTHER_0110
63f6603c60Sopenharmony_ci * @tc.name     basic function test : get current dir name
64f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
65f6603c60Sopenharmony_ci */
66f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testGetCurrentDirName, Function | MediumTest | Level1)
67f6603c60Sopenharmony_ci{
68f6603c60Sopenharmony_ci    int fd = 0;
69f6603c60Sopenharmony_ci    fd = creat(FILE0, 0777);
70f6603c60Sopenharmony_ci    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
71f6603c60Sopenharmony_ci    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
72f6603c60Sopenharmony_ci
73f6603c60Sopenharmony_ci    // get current dir name
74f6603c60Sopenharmony_ci    const char *currentDirStandard = TOP_DIR;
75f6603c60Sopenharmony_ci    char *currentDir = get_current_dir_name();
76f6603c60Sopenharmony_ci    ASSERT_NE(currentDir, nullptr);
77f6603c60Sopenharmony_ci    EXPECT_STREQ(currentDir, currentDirStandard);
78f6603c60Sopenharmony_ci    LOG("> currentDir = %s", currentDir);
79f6603c60Sopenharmony_ci}
80f6603c60Sopenharmony_ci
81f6603c60Sopenharmony_ci/**
82f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_FS_OTHER_0120
83f6603c60Sopenharmony_ci * @tc.name     basic function test : get basename
84f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
85f6603c60Sopenharmony_ci */
86f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testBasename, Function | MediumTest | Level1)
87f6603c60Sopenharmony_ci{
88f6603c60Sopenharmony_ci    int fd = 0;
89f6603c60Sopenharmony_ci    fd = creat(FILE0, 0777);
90f6603c60Sopenharmony_ci    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
91f6603c60Sopenharmony_ci    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
92f6603c60Sopenharmony_ci
93f6603c60Sopenharmony_ci    // get file name
94f6603c60Sopenharmony_ci    char *desName = basename((char*)FILE0);
95f6603c60Sopenharmony_ci    ASSERT_NE(desName, nullptr) << "> basename errno = " << errno;
96f6603c60Sopenharmony_ci    EXPECT_STREQ(desName, FILE0);
97f6603c60Sopenharmony_ci    LOG("> desName = %s", desName);
98f6603c60Sopenharmony_ci}
99f6603c60Sopenharmony_ci
100f6603c60Sopenharmony_ciint FtwCheckDirTree(const char *path, const struct stat *sb, int flag)
101f6603c60Sopenharmony_ci{
102f6603c60Sopenharmony_ci    const char file0[] = DIR0 "/" DIR0_FILE0;
103f6603c60Sopenharmony_ci    const char file1[] = DIR0 "/" DIR0_DIR1 "/" DIR0_DIR1_FILE0;
104f6603c60Sopenharmony_ci    const char dir0[] = DIR0;
105f6603c60Sopenharmony_ci    const char dir1[] = DIR0 "/" DIR0_DIR0;
106f6603c60Sopenharmony_ci    const char dir2[] = DIR0 "/" DIR0_DIR1;
107f6603c60Sopenharmony_ci    const char dir3[] = DIR0 "/" DIR0_DIR1"/" DIR0_DIR1_DIR0;
108f6603c60Sopenharmony_ci    if (flag == FTW_F) {
109f6603c60Sopenharmony_ci        if (strncmp(path, file0, sizeof(file0)) == 0) {
110f6603c60Sopenharmony_ci            LOG("> File %s", file0);
111f6603c60Sopenharmony_ci        } else if (strncmp(path, file1, sizeof(file1)) == 0) {
112f6603c60Sopenharmony_ci            LOG("> File %s", file1);
113f6603c60Sopenharmony_ci        } else {
114f6603c60Sopenharmony_ci            LOG("> File error %s", path);
115f6603c60Sopenharmony_ci            return -1;
116f6603c60Sopenharmony_ci        }
117f6603c60Sopenharmony_ci    } else if (flag == FTW_D) {
118f6603c60Sopenharmony_ci        if (strncmp(path, dir0, sizeof(dir0)) == 0) {
119f6603c60Sopenharmony_ci            LOG("> Dir  %s", DIR0);
120f6603c60Sopenharmony_ci        } else if (strncmp(path, dir1, sizeof(dir1)) == 0) {
121f6603c60Sopenharmony_ci            LOG("> Dir  %s", dir1);
122f6603c60Sopenharmony_ci        } else if (strncmp(path, dir2, sizeof(dir2)) == 0) {
123f6603c60Sopenharmony_ci            LOG("> Dir  %s", dir2);
124f6603c60Sopenharmony_ci        } else if (strncmp(path, dir3, sizeof(dir3)) == 0) {
125f6603c60Sopenharmony_ci            LOG("> Dir  %s", dir3);
126f6603c60Sopenharmony_ci        } else {
127f6603c60Sopenharmony_ci            LOG("> File error  %s", path);
128f6603c60Sopenharmony_ci            return -1;
129f6603c60Sopenharmony_ci        }
130f6603c60Sopenharmony_ci    }
131f6603c60Sopenharmony_ci    return 0;
132f6603c60Sopenharmony_ci}
133f6603c60Sopenharmony_ci
134f6603c60Sopenharmony_ci/**
135f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_FS_OTHER_0200
136f6603c60Sopenharmony_ci * @tc.name     basic function test : use ftw check file tree
137f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
138f6603c60Sopenharmony_ci */
139f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testFtw, Function | MediumTest | Level3)
140f6603c60Sopenharmony_ci{
141f6603c60Sopenharmony_ci    CreateTestFolder();
142f6603c60Sopenharmony_ci    EXPECT_EQ(ftw(DIR0, FtwCheckDirTree, 100), 0) << "> ftw error";
143f6603c60Sopenharmony_ci}
144f6603c60Sopenharmony_ci
145f6603c60Sopenharmony_ciint NftwCheckDirTree(const char *path, const struct stat *sb, int flag, struct FTW *s)
146f6603c60Sopenharmony_ci{
147f6603c60Sopenharmony_ci    const char file0[] = DIR0 "/" DIR0_FILE0;
148f6603c60Sopenharmony_ci    const char file1[] = DIR0 "/" DIR0_DIR1 "/" DIR0_DIR1_FILE0;
149f6603c60Sopenharmony_ci    const char dir0[] = DIR0;
150f6603c60Sopenharmony_ci    const char dir1[] = DIR0 "/" DIR0_DIR0;
151f6603c60Sopenharmony_ci    const char dir2[] = DIR0 "/" DIR0_DIR1;
152f6603c60Sopenharmony_ci    const char dir3[] = DIR0 "/" DIR0_DIR1"/" DIR0_DIR1_DIR0;
153f6603c60Sopenharmony_ci    if (flag == FTW_F) {
154f6603c60Sopenharmony_ci        if (strncmp(path, file0, sizeof(file0)) == 0) {
155f6603c60Sopenharmony_ci            LOG("> File %s", file0);
156f6603c60Sopenharmony_ci        } else if (strncmp(path, file1, sizeof(file1)) == 0) {
157f6603c60Sopenharmony_ci            LOG("> File %s", file1);
158f6603c60Sopenharmony_ci        } else {
159f6603c60Sopenharmony_ci            LOG("> File %s", path);
160f6603c60Sopenharmony_ci            return -1;
161f6603c60Sopenharmony_ci        }
162f6603c60Sopenharmony_ci    } else if (flag == FTW_D) {
163f6603c60Sopenharmony_ci        if (strncmp(path, dir0, sizeof(dir0)) == 0) {
164f6603c60Sopenharmony_ci            LOG("> Dir  %s", DIR0);
165f6603c60Sopenharmony_ci        } else if (strncmp(path, dir1, sizeof(dir1)) == 0) {
166f6603c60Sopenharmony_ci            LOG("> Dir  %s", dir1);
167f6603c60Sopenharmony_ci        } else if (strncmp(path, dir2, sizeof(dir2)) == 0) {
168f6603c60Sopenharmony_ci            LOG("> Dir  %s", dir2);
169f6603c60Sopenharmony_ci        } else if (strncmp(path, dir3, sizeof(dir3)) == 0) {
170f6603c60Sopenharmony_ci            LOG("> Dir  %s", dir3);
171f6603c60Sopenharmony_ci        } else {
172f6603c60Sopenharmony_ci            LOG("> File %s", path);
173f6603c60Sopenharmony_ci            return -1;
174f6603c60Sopenharmony_ci        }
175f6603c60Sopenharmony_ci    }
176f6603c60Sopenharmony_ci    return 0;
177f6603c60Sopenharmony_ci}
178f6603c60Sopenharmony_ci
179f6603c60Sopenharmony_ci/**
180f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_FS_OTHER_0210
181f6603c60Sopenharmony_ci * @tc.name     basic function test : use nftw check file tree
182f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
183f6603c60Sopenharmony_ci */
184f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testNftw, Function | MediumTest | Level3)
185f6603c60Sopenharmony_ci{
186f6603c60Sopenharmony_ci    CreateTestFolder();
187f6603c60Sopenharmony_ci    EXPECT_EQ(nftw(DIR0, NftwCheckDirTree, 100, 0), 0) << "> ftw error";
188f6603c60Sopenharmony_ci}
189f6603c60Sopenharmony_ci
190f6603c60Sopenharmony_ci/**
191f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_FS_OTHER_0300
192f6603c60Sopenharmony_ci * @tc.name     basic function test : use statvfs and statfs check file system information.
193f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
194f6603c60Sopenharmony_ci */
195f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testStatvfs, Function | MediumTest | Level2)
196f6603c60Sopenharmony_ci{
197f6603c60Sopenharmony_ci    struct statvfs vfsBuf = {0};
198f6603c60Sopenharmony_ci    struct statfs fsBuf = {0};
199f6603c60Sopenharmony_ci
200f6603c60Sopenharmony_ci    statvfs(".", &vfsBuf);
201f6603c60Sopenharmony_ci    LOG("vfsBuf.f_bsize = %lu", vfsBuf.f_bsize);        // File system block size.
202f6603c60Sopenharmony_ci    LOG("vfsBuf.f_frsize = %lu", vfsBuf.f_frsize);      // Fundamental file system block size.
203f6603c60Sopenharmony_ci    LOG("vfsBuf.f_blocks = %lu", vfsBuf.f_blocks);      // Total number of blocks on file system in units of f_frsize.
204f6603c60Sopenharmony_ci    LOG("vfsBuf.f_bfree = %lu", vfsBuf.f_bfree);        // Total number of free blocks.
205f6603c60Sopenharmony_ci    LOG("vfsBuf.f_bavail = %lu", vfsBuf.f_bavail);      // Number of free blocks available to non-privileged process.
206f6603c60Sopenharmony_ci    LOG("vfsBuf.f_files = %lu", vfsBuf.f_files);        // Total number of file serial numbers.
207f6603c60Sopenharmony_ci    LOG("vfsBuf.f_ffree = %lu", vfsBuf.f_ffree);        // Total number of free file serial numbers.
208f6603c60Sopenharmony_ci    LOG("vfsBuf.f_favail = %lu", vfsBuf.f_favail);      // Number of i-nodes available to unprivileged process.
209f6603c60Sopenharmony_ci    LOG("vfsBuf.f_fsid = %lu", vfsBuf.f_fsid);          // File system ID.
210f6603c60Sopenharmony_ci    LOG("vfsBuf.f_flag = %lu", vfsBuf.f_flag);          // Bit mask of f_flag values.
211f6603c60Sopenharmony_ci    LOG("vfsBuf.f_namemax = %lu", vfsBuf.f_namemax);    // Maximum filename length.
212f6603c60Sopenharmony_ci
213f6603c60Sopenharmony_ci    statfs(".", &fsBuf);
214f6603c60Sopenharmony_ci    LOG("fsBuf.f_type = %lu", fsBuf.f_type);            // Type of filesystem.
215f6603c60Sopenharmony_ci    LOG("fsBuf.f_bsize = %lu", fsBuf.f_bsize);          // Optimal transfer block size.
216f6603c60Sopenharmony_ci    LOG("fsBuf.f_blocks = %lu", fsBuf.f_blocks);        // Total data blocks in filesystem.
217f6603c60Sopenharmony_ci    LOG("fsBuf.f_bfree = %lu", fsBuf.f_bfree);          // Total data blocks in filesystem.
218f6603c60Sopenharmony_ci    LOG("fsBuf.f_bavail = %lu", fsBuf.f_bavail);        // Free blocks available to unprivileged user.
219f6603c60Sopenharmony_ci    LOG("fsBuf.f_files = %lu", fsBuf.f_files);          // Total file nodes in filesystem.
220f6603c60Sopenharmony_ci    LOG("fsBuf.f_ffree = %lu", fsBuf.f_ffree);          // Free file nodes in filesystem.
221f6603c60Sopenharmony_ci    LOG("fsBuf.f_fsid.__val[0] = %d", fsBuf.f_fsid.__val[0]);   // Filesystem ID.
222f6603c60Sopenharmony_ci    LOG("fsBuf.f_fsid.__val[1] = %d", fsBuf.f_fsid.__val[1]);   // Filesystem ID.
223f6603c60Sopenharmony_ci    LOG("fsBuf.f_namelen = %ld", fsBuf.f_namelen);      // Maximum length of filenames.
224f6603c60Sopenharmony_ci
225f6603c60Sopenharmony_ci    EXPECT_EQ(vfsBuf.f_bsize, fsBuf.f_bsize);
226f6603c60Sopenharmony_ci    EXPECT_EQ(vfsBuf.f_blocks, fsBuf.f_blocks);
227f6603c60Sopenharmony_ci    EXPECT_EQ(vfsBuf.f_files, fsBuf.f_files);
228f6603c60Sopenharmony_ci    EXPECT_EQ(vfsBuf.f_ffree, fsBuf.f_ffree);
229f6603c60Sopenharmony_ci    EXPECT_EQ(vfsBuf.f_namemax, fsBuf.f_namelen);
230f6603c60Sopenharmony_ci}
231f6603c60Sopenharmony_ci
232f6603c60Sopenharmony_ci/**
233f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_FS_OTHER_0400
234f6603c60Sopenharmony_ci * @tc.name     basic function test : Use glob function and globfree function for path generation and release
235f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
236f6603c60Sopenharmony_ci */
237f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testGlob, Function | MediumTest | Level3)
238f6603c60Sopenharmony_ci{
239f6603c60Sopenharmony_ci    glob_t buf;
240f6603c60Sopenharmony_ci    CreateTestFolder();
241f6603c60Sopenharmony_ci    EXPECT_EQ(glob(TOP_DIR "/" DIR0 "/*", GLOB_ERR, NULL, &buf), 0) << "> glod errno = " << errno;
242f6603c60Sopenharmony_ci    if (buf.gl_pathc == 3) {
243f6603c60Sopenharmony_ci        EXPECT_STREQ(buf.gl_pathv[0], TOP_DIR "/" DIR0 "/" DIR0_DIR0);
244f6603c60Sopenharmony_ci        EXPECT_STREQ(buf.gl_pathv[1], TOP_DIR "/" DIR0 "/" DIR0_DIR1);
245f6603c60Sopenharmony_ci        EXPECT_STREQ(buf.gl_pathv[2], TOP_DIR "/" DIR0 "/" DIR0_FILE0);
246f6603c60Sopenharmony_ci    } else {
247f6603c60Sopenharmony_ci        ADD_FAILURE();
248f6603c60Sopenharmony_ci    }
249f6603c60Sopenharmony_ci    globfree(&buf);
250f6603c60Sopenharmony_ci}
251