• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "FileSystemTest.h"
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <dirent.h>
25 #include <ftw.h>
26 #include <libgen.h>
27 
28 #include <gtest/gtest.h>
29 
30 #include "utils.h"
31 #include "log.h"
32 #include "KernelConstants.h"
33 #include "libfs.h"
34 
35 using namespace testing::ext;
36 #if 0
37 /**
38  * @tc.number   SUB_KERNEL_FS_STDLIB_0100
39  * @tc.name     basic function test : mkstemp create a temporary file
40  * @tc.desc     [C- SOFTWARE -0200]
41  */
42 HWTEST_F(FileSystemTest, testMkstemp, Function | MediumTest | Level2)
43 {
44     int fd = 0;
45     char tmpFileName[]= "tmpFile_XXXXXX";
46     fd = mkstemp(tmpFileName);
47     EXPECT_NE(fd, -1) << "> mkstemp errno = " << errno;
48     WriteCloseTest(fd);
49     fd = open(tmpFileName, O_RDWR);
50     EXPECT_NE(fd, -1) << "> open errno = " << errno;
51     ReadCloseTest(fd);
52     EXPECT_NE(unlink(tmpFileName), -1) << "> unlink errno = " << errno;
53 }
54 
55 /**
56  * @tc.number   SUB_KERNEL_FS_STDLIB_0200
57  * @tc.name     basic function test : mkstemps create a temporary file
58  * @tc.desc     [C- SOFTWARE -0200]
59  */
60 HWTEST_F(FileSystemTest, testMkstemps, Function | MediumTest | Level2)
61 {
62     int fd = 0;
63     char tmpFileName[] = "tmpFile_XXXXXX";
64     fd = mkstemps(tmpFileName, 0);
65     EXPECT_NE(fd, -1) << "> mkstemp errno = " << errno;
66     WriteCloseTest(fd);
67     fd = open(tmpFileName, O_RDWR);
68     EXPECT_NE(fd, -1) << "> open errno = " << errno;
69     ReadCloseTest(fd);
70     EXPECT_NE(unlink(tmpFileName), -1) << "> unlink errno = " << errno;
71 }
72 
73 /**
74  * @tc.number   SUB_KERNEL_FS_STDLIB_0300
75  * @tc.name     basic function test : mkostemp create a temporary file with read and write
76  * @tc.desc     [C- SOFTWARE -0200]
77  */
78 HWTEST_F(FileSystemTest, testMkostemp, Function | MediumTest | Level2)
79 {
80     int fd = 0;
81     char tmpFileName[] = "tmpFile_XXXXXX";
82     fd = mkostemp(tmpFileName, O_RDWR);
83     EXPECT_NE(fd, -1) << "> mkstemp errno = " << errno;
84     WriteCloseTest(fd);
85     fd = open(tmpFileName, O_RDWR);
86     EXPECT_NE(fd, -1) << "> open errno = " << errno;
87     ReadCloseTest(fd);
88     EXPECT_NE(unlink(tmpFileName), -1) << "> unlink errno = " << errno;
89 }
90 #endif
91 /**
92  * @tc.number   SUB_KERNEL_FS_STDLIB_0400
93  * @tc.name     basic function test : mktemp create a temporary file name, mkdtemp create a directory
94  * @tc.desc     [C- SOFTWARE -0200]
95  */
96 HWTEST_F(FileSystemTest, testMktempMkdtemp, Function | MediumTest | Level2)
97 {
98     char tmpFileNamePre[] = "tmpFile_XXXXXX";
99     char tmpFileName[] = "tmpFile_XXXXXX";
100     EXPECT_NE(mktemp(tmpFileName), nullptr) << "> mktemp errno = " << errno;
101     EXPECT_STRNE(tmpFileName, tmpFileNamePre) << "> tmpFileName no change";
102 
103     char tmpDirName[] = "tmpDir_XXXXXXX";
104     EXPECT_NE(mkdtemp(tmpDirName), nullptr) << "> mktemp errno = " << errno;
105     EXPECT_NE(remove(tmpDirName), -1) << "> remove errno = " << errno;
106 }
107 
108 /**
109  * @tc.number   SUB_KERNEL_FS_STDLIB_0500
110  * @tc.name     basic function test : Use the realpath function to obtain the path.
111  * @tc.desc     [C- SOFTWARE -0200]
112  */
113 HWTEST_F(FileSystemTest, testRealpath, Function | MediumTest | Level3)
114 {
115     int fd = 0;
116     fd = creat(FILE0, 0777);
117     EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
118     EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
119 
120     // get Absolute Path
121     const char *realPathStandard = TOP_DIR "/" FILE0;
122     char *realPath = (char *)malloc(256);
123     if (realpath(FILE0, realPath) == nullptr)
124     {
125         LOG("> realpath errno == %d", errno);
126         free(realPath);
127     }
128     else
129     {
130         EXPECT_STREQ(realPath, realPathStandard);
131         LOG("> realPath = %s", realPath);
132         free(realPath);
133     }
134 }
135