• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "It_vfs_jffs.h"
33 #include <fcntl.h>
34 #include "sys/stat.h"
35 #include "string.h"
36 
testcase8(VOID)37 static UINT32 testcase8(VOID)
38 {
39     struct stat buf;
40     char pathname[200] = FILEPATH_775;
41     int ret = 0;
42     int fd = 0;
43 
44     /* omit to create test file dynamicly,use prepared test files in /storage instand. */
45     #if 0
46     errno = 0;
47     sprintf(pathname, "%s%s", __func__, ".tmp");
48     #endif
49 
50     errno = 0;
51     fd = open(pathname, O_CREAT, 0777);
52     TEST_PRINT("[INFO]%s:%d,%s,fd=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, fd, errno, strerror(errno));
53     ICUNIT_GOTO_NOT_EQUAL(fd, -1, fd, OUT);
54 
55     errno = 0;
56     ret = fstatat(fd, FILEPATH_RELATIVE, &buf, 0);
57     TEST_PRINT("[INFO]%s:%d,%s,ret=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, ret, errno, strerror(errno));
58     ICUNIT_GOTO_EQUAL(ret, -1, ret, OUT);
59     ICUNIT_GOTO_EQUAL(errno, ENOTDIR, errno, OUT);
60 
61     return LOS_OK;
62 OUT:
63     return LOS_NOK;
64 }
65 
testcase7(VOID)66 static UINT32 testcase7(VOID)
67 {
68     struct stat buf;
69     char pathname[] = FILEPATH_NOACCESS;
70     char dirname[] = DIRPATH_775;
71     DIR *dir = NULL;
72     int ret = 0;
73     int fd = 0;
74 
75     errno = 0;
76     dir = opendir(dirname);
77     fd = dirfd(dir);
78     TEST_PRINT("[INFO]%s:%d,%s,fd=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, fd, errno, strerror(errno));
79     ICUNIT_GOTO_NOT_EQUAL(fd, -1, fd, OUT);
80 
81     errno = 0;
82     ret = fstatat(fd, pathname, &buf, 0);
83     TEST_PRINT("[INFO]%s:%d,%s,ret=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, ret, errno, strerror(errno));
84     ICUNIT_GOTO_EQUAL(ret, -1, ret, OUT);
85     ICUNIT_GOTO_EQUAL(errno, EACCES, errno, OUT);
86 
87     return LOS_OK;
88 OUT:
89     return LOS_NOK;
90 }
91 
testcase6(VOID)92 static UINT32 testcase6(VOID)
93 {
94     struct stat buf;
95     /* let the pathname more than 4096 characters,to generate the ENAMETOOLONG errno. */
96     char pathname[] = PATHNAME_ENAMETOOLONG;
97     int ret = 0;
98     char dirname[] = DIRPATH_775;
99     DIR *dir = NULL;
100     int fd = 0;
101 
102     errno = 0;
103     dir = opendir(dirname);
104     fd = dirfd(dir);
105     TEST_PRINT("[INFO]%s:%d,%s,fd=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, fd, errno, strerror(errno));
106     ICUNIT_GOTO_NOT_EQUAL(fd, -1, fd, OUT);
107 
108     errno = 0;
109     ret = fstatat(fd, pathname, &buf, 0);
110     TEST_PRINT("[INFO]%s:%d,%s,ret=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, ret, errno, strerror(errno));
111     ICUNIT_GOTO_EQUAL(ret, -1, ret, OUT);
112     ICUNIT_GOTO_EQUAL(errno, ENAMETOOLONG, errno, OUT);
113 
114     return LOS_OK;
115 OUT:
116     return LOS_NOK;
117 }
118 
testcase5(VOID)119 static UINT32 testcase5(VOID)
120 {
121     struct stat buf;
122     char pathname[] = FILEPATH_ENOENT;
123     int ret = 0;
124 
125     errno = 0;
126     ret = fstatat(FD_EBADF, pathname, &buf, 0);
127     TEST_PRINT("[INFO]%s:%d,%s,ret=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, ret, errno, strerror(errno));
128     ICUNIT_GOTO_EQUAL(ret, -1, ret, OUT);
129     ICUNIT_GOTO_EQUAL(errno, ENOENT, errno, OUT);
130 
131     return LOS_OK;
132 OUT:
133     return LOS_NOK;
134 }
135 
testcase3(VOID)136 static UINT32 testcase3(VOID)
137 {
138     struct stat buf;
139     char pathname[] = "";
140     int ret = 0;
141 
142     errno = 0;
143     ret = fstatat(AT_FDCWD, pathname, &buf, 0);
144     TEST_PRINT("[INFO]%s:%d,%s,ret=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, ret, errno, strerror(errno));
145     ICUNIT_GOTO_EQUAL(ret, -1, ret, OUT);
146     ICUNIT_GOTO_EQUAL(errno, ENOENT, errno, OUT);
147 
148     return LOS_OK;
149 OUT:
150     return LOS_NOK;
151 }
152 
testcase2(VOID)153 static UINT32 testcase2(VOID)
154 {
155     struct stat buf;
156     char pathname[] = FILEPATH_ENOENT;
157     int ret = 0;
158 
159     errno = 0;
160     ret = fstatat(AT_FDCWD, pathname, &buf, 0);
161     TEST_PRINT("[INFO]%s:%d,%s,ret=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, ret, errno, strerror(errno));
162     ICUNIT_GOTO_EQUAL(ret, -1, ret, OUT);
163     ICUNIT_GOTO_EQUAL(errno, ENOENT, errno, OUT);
164 
165     return LOS_OK;
166 OUT:
167     return LOS_NOK;
168 }
169 
testcase1(VOID)170 static UINT32 testcase1(VOID)
171 {
172     struct stat buf;
173     char pathname[] = FILEPATH_000;
174     int ret = 0;
175 
176     errno = 0;
177     ret = fstatat(1, pathname, &buf, 0);
178     TEST_PRINT("[INFO]%s:%d,%s,ret=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, ret, errno, strerror(errno));
179     ICUNIT_GOTO_EQUAL(ret, -1, ret, OUT);
180     ICUNIT_GOTO_EQUAL(errno, EACCES, errno, OUT);
181 
182     return LOS_OK;
183 OUT:
184     return LOS_NOK;
185 }
186 
testcase(VOID)187 static UINT32 testcase(VOID)
188 {
189     testcase8(); /* CASE:fd is no a dirfd */
190     /* testcase7(); omitted as program can not create file with no access privilege. */
191     testcase6();
192     testcase5();
193     testcase3();
194     testcase2();
195     /* testcase1(); CASE:no access */
196 
197     return LOS_OK;
198 }
199 
IO_TEST_FSTATAT_002(void)200 VOID IO_TEST_FSTATAT_002(void)
201 {
202     TEST_ADD_CASE(__FUNCTION__, testcase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION);
203 }
204