1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2012. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.org/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | #ifdef HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <bsp.h> |
---|
15 | #include <pmacros.h> |
---|
16 | #include <unistd.h> |
---|
17 | #include <errno.h> |
---|
18 | #include <sys/stat.h> |
---|
19 | #include <sys/types.h> |
---|
20 | #include <fcntl.h> |
---|
21 | |
---|
22 | const char rtems_test_name[] = "PSXFCHX 1"; |
---|
23 | |
---|
24 | /* forward declarations to avoid warnings */ |
---|
25 | rtems_task Init(rtems_task_argument ignored); |
---|
26 | |
---|
27 | rtems_task Init( |
---|
28 | rtems_task_argument ignored |
---|
29 | ) |
---|
30 | { |
---|
31 | int status = 0; |
---|
32 | int fd = 0; |
---|
33 | |
---|
34 | TEST_BEGIN(); |
---|
35 | |
---|
36 | /**************************************************** |
---|
37 | * fchdir tests |
---|
38 | ***************************************************/ |
---|
39 | |
---|
40 | puts( "Init - fchdir tests" ); |
---|
41 | |
---|
42 | puts( "Init - Attempt fchdir with bad file descriptor - expect EBADF" ); |
---|
43 | status = fchdir( 5 ); |
---|
44 | rtems_test_assert( status == -1 ); |
---|
45 | rtems_test_assert( errno == EBADF ); |
---|
46 | |
---|
47 | puts( "Init - Attempt fchdir with bad file descriptor - expect EBADF" ); |
---|
48 | status = fchdir( 3 ); |
---|
49 | rtems_test_assert( status == -1 ); |
---|
50 | rtems_test_assert( errno == EBADF ); |
---|
51 | |
---|
52 | puts( "Init - opening /newfile in write-mode -- OK" ); |
---|
53 | fd = open( "/newfile", O_WRONLY | O_CREAT, S_IWUSR | S_IXUSR ); |
---|
54 | rtems_test_assert( fd != -1 ); |
---|
55 | |
---|
56 | puts( "Init - fchdir on the file descriptor - expect ENOTDIR" ); |
---|
57 | status = fchdir( fd ); |
---|
58 | rtems_test_assert( status == -1 ); |
---|
59 | rtems_test_assert( errno == ENOTDIR ); |
---|
60 | |
---|
61 | puts( "Init - closing /newfile -- OK" ); |
---|
62 | status = close( fd ); |
---|
63 | rtems_test_assert( status == 0 ); |
---|
64 | |
---|
65 | puts( "Init - removing /newfile -- OK" ); |
---|
66 | status = unlink( "/newfile" ); |
---|
67 | rtems_test_assert( status == 0 ); |
---|
68 | |
---|
69 | puts( "Init - opening /newfile in read-mode -- OK" ); |
---|
70 | fd = open( "/newfile", O_RDONLY | O_CREAT, S_IRUSR | S_IXUSR); |
---|
71 | rtems_test_assert( fd != -1 ); |
---|
72 | |
---|
73 | puts( "Init - fchdir on the file descriptor - expect ENOTDIR" ); |
---|
74 | status = fchdir( fd ); |
---|
75 | rtems_test_assert( status == -1 ); |
---|
76 | rtems_test_assert( errno == ENOTDIR ); |
---|
77 | |
---|
78 | puts( "Init - closing and removing /newfile -- OK" ); |
---|
79 | status = close( fd ); |
---|
80 | status |= unlink( "/newfile" ); |
---|
81 | rtems_test_assert( status == 0 ); |
---|
82 | |
---|
83 | puts( "Init - create directory /tmp - RWX permissions -- OK" ); |
---|
84 | status = mkdir( "/tmp", S_IRWXU ); |
---|
85 | rtems_test_assert( status == 0 ); |
---|
86 | |
---|
87 | puts( "Init - open the /tmp, get the file descriptor -- OK" ); |
---|
88 | fd = open( "/tmp", O_RDONLY ); |
---|
89 | rtems_test_assert( fd != -1 ); |
---|
90 | |
---|
91 | puts( "Init - fchdir on the file descriptor -- OK" ); |
---|
92 | status = fchdir( fd ); |
---|
93 | rtems_test_assert( status == 0 ); |
---|
94 | |
---|
95 | puts( "Init - close the file descriptor -- OK" ); |
---|
96 | status = close( fd ); |
---|
97 | rtems_test_assert( status == 0 ); |
---|
98 | |
---|
99 | puts( "Init - remove directory /tmp -- OK" ); |
---|
100 | status = rmdir( "/tmp" ); |
---|
101 | rtems_test_assert( status == 0 ); |
---|
102 | |
---|
103 | puts( "Init - creating directory /tmp - read permission -- OK" ); |
---|
104 | status = mkdir( "/tmp", S_IRUSR ); |
---|
105 | rtems_test_assert( status == 0 ); |
---|
106 | |
---|
107 | puts( "Init - open the /tmp, get the file descriptor -- OK" ); |
---|
108 | fd = open( "/tmp", O_RDONLY ); |
---|
109 | rtems_test_assert( fd != -1 ); |
---|
110 | |
---|
111 | puts( "Init - attempt fchdir on the file descriptor -- expect EACCES" ); |
---|
112 | status = fchdir( fd ); |
---|
113 | rtems_test_assert( status == -1); |
---|
114 | rtems_test_assert( errno == EACCES ); |
---|
115 | |
---|
116 | puts( "Init - close the file descriptor -- OK" ); |
---|
117 | status = close( fd ); |
---|
118 | rtems_test_assert( status == 0 ); |
---|
119 | |
---|
120 | puts( "Init - remove directory /tmp -- OK" ); |
---|
121 | status = rmdir( "/tmp" ); |
---|
122 | rtems_test_assert( status == 0 ); |
---|
123 | |
---|
124 | puts( "End of fchdir tests" ); |
---|
125 | |
---|
126 | /**************************************************** |
---|
127 | * fchmod tests |
---|
128 | ***************************************************/ |
---|
129 | |
---|
130 | puts( "\nInit - fchmod tests" ); |
---|
131 | |
---|
132 | puts( "Init - fchmod, with a bad file descriptor - expect EBADF" ); |
---|
133 | status = fchmod( 4, 0 ); |
---|
134 | rtems_test_assert( status == -1 ); |
---|
135 | rtems_test_assert( errno == EBADF ); |
---|
136 | |
---|
137 | puts( "Init - fchmod, with an unopened file descriptor - expect EBADF" ); |
---|
138 | status = fchmod( 3, 0 ); |
---|
139 | rtems_test_assert( status == -1 ); |
---|
140 | rtems_test_assert( errno == EBADF ); |
---|
141 | |
---|
142 | puts( "Init - open new file: /newfile in read-only mode -- OK" ); |
---|
143 | fd = open( "/newfile", O_RDONLY | O_CREAT, S_IRWXU ); |
---|
144 | rtems_test_assert( fd != -1 ); |
---|
145 | |
---|
146 | puts( "Init - fchmod, with the opened file descriptor -- OK" ); |
---|
147 | status = fchmod( fd, 0 ); |
---|
148 | rtems_test_assert( status == 0 ); |
---|
149 | |
---|
150 | puts( "Init - close and remove /newfile" ); |
---|
151 | status = close( fd ); |
---|
152 | status |= unlink( "/newfile" ); |
---|
153 | rtems_test_assert( status == 0 ); |
---|
154 | |
---|
155 | puts( "Init - open new file: /newfile in read-write mode -- OK" ); |
---|
156 | fd = open( "/newfile", O_RDWR | O_CREAT, S_IRWXU ); |
---|
157 | rtems_test_assert( fd != -1 ); |
---|
158 | |
---|
159 | puts( "Init - fchmod, with the opened file descriptor -- OK" ); |
---|
160 | status = fchmod( fd, S_IRUSR ); |
---|
161 | rtems_test_assert( status == 0 ); |
---|
162 | |
---|
163 | puts( "Init - close and remove /newfile -- OK" ); |
---|
164 | status = close( fd ); |
---|
165 | status |= unlink( "/newfile" ); |
---|
166 | rtems_test_assert( status == 0 ); |
---|
167 | |
---|
168 | puts( "End of fchmod tests" ); |
---|
169 | |
---|
170 | /**************************************************** |
---|
171 | * fchown tests |
---|
172 | ***************************************************/ |
---|
173 | |
---|
174 | puts( "\nInit - fchown tests" ); |
---|
175 | |
---|
176 | puts( "Init - fchown, with a bad file descriptor - expect EBADF" ); |
---|
177 | status = fchown( 4, 0, 0 ); |
---|
178 | rtems_test_assert( status == -1 ); |
---|
179 | rtems_test_assert( errno == EBADF ); |
---|
180 | |
---|
181 | puts( "Init - fchown, with an unopened file descriptor - expect EBADF" ); |
---|
182 | status = fchown( 3, 0, 0 ); |
---|
183 | rtems_test_assert( status == -1 ); |
---|
184 | rtems_test_assert( errno == EBADF ); |
---|
185 | |
---|
186 | puts( "Init - open new file: /newfile in read-only mode -- OK" ); |
---|
187 | fd = open( "/newfile", O_RDONLY | O_CREAT, S_IRWXU ); |
---|
188 | rtems_test_assert( fd != -1 ); |
---|
189 | |
---|
190 | puts( "Init - fchown, with the opened file descriptor - OK" ); |
---|
191 | status = fchown( fd, 0, 0 ); |
---|
192 | rtems_test_assert( status == 0 ); |
---|
193 | |
---|
194 | puts( "Init - close and remove /newfile" ); |
---|
195 | status = close( fd ); |
---|
196 | status |= unlink( "/newfile" ); |
---|
197 | rtems_test_assert( status == 0 ); |
---|
198 | |
---|
199 | puts( "Init - open new file: /newfile in read-write mode -- OK" ); |
---|
200 | fd = open( "/newfile", O_RDWR | O_CREAT, S_IRWXU ); |
---|
201 | rtems_test_assert( fd != -1 ); |
---|
202 | |
---|
203 | puts( "Init - fchown, with the opened file descriptor -- OK" ); |
---|
204 | status = fchown( fd, 1, 0 ); |
---|
205 | rtems_test_assert( status == 0 ); |
---|
206 | |
---|
207 | puts( "Init - close and remove /newfile -- OK" ); |
---|
208 | status = close( fd ); |
---|
209 | status |= unlink( "/newfile" ); |
---|
210 | rtems_test_assert( status == 0 ); |
---|
211 | |
---|
212 | puts( "End of fchown tests" ); |
---|
213 | |
---|
214 | TEST_END(); |
---|
215 | rtems_test_exit( 0 ); |
---|
216 | } |
---|
217 | |
---|
218 | /* configuration information */ |
---|
219 | |
---|
220 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
221 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
222 | |
---|
223 | #define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM |
---|
224 | #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4 |
---|
225 | |
---|
226 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
227 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
228 | |
---|
229 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
230 | |
---|
231 | #define CONFIGURE_INIT |
---|
232 | #include <rtems/confdefs.h> |
---|
233 | /* end of file */ |
---|