1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * Simple test program to exercise some of the basic functionality of |
---|
5 | * POSIX Files and Directories Support. |
---|
6 | * |
---|
7 | * This test assumes that the file system is initialized with the |
---|
8 | * following directory structure: |
---|
9 | * |
---|
10 | * XXXX fill this in. |
---|
11 | * / |
---|
12 | * /dev |
---|
13 | * /dev/XXX [where XXX includes at least console] |
---|
14 | */ |
---|
15 | |
---|
16 | /* |
---|
17 | * COPYRIGHT (c) 1989-2012. |
---|
18 | * On-Line Applications Research Corporation (OAR). |
---|
19 | * |
---|
20 | * The license and distribution terms for this file may be |
---|
21 | * found in the file LICENSE in this distribution or at |
---|
22 | * http://www.rtems.org/license/LICENSE. |
---|
23 | */ |
---|
24 | |
---|
25 | #ifdef HAVE_CONFIG_H |
---|
26 | #include "config.h" |
---|
27 | #endif |
---|
28 | |
---|
29 | #include <stdio.h> |
---|
30 | |
---|
31 | #include <pmacros.h> |
---|
32 | #include <sys/types.h> |
---|
33 | #include <sys/stat.h> |
---|
34 | #include <fcntl.h> |
---|
35 | #include <unistd.h> |
---|
36 | #include <errno.h> |
---|
37 | #include <string.h> |
---|
38 | #include <inttypes.h> |
---|
39 | #include <ctype.h> |
---|
40 | #include <reent.h> |
---|
41 | #include <rtems/imfs.h> |
---|
42 | |
---|
43 | #include <rtems.h> |
---|
44 | #include <rtems/libio.h> |
---|
45 | |
---|
46 | #include "primode.h" |
---|
47 | |
---|
48 | const char rtems_test_name[] = "PSXFILE 1"; |
---|
49 | |
---|
50 | /* forward declarations to avoid warnings */ |
---|
51 | void test_case_reopen_append(void); |
---|
52 | void dump_statbuf(struct stat *buf); |
---|
53 | void stat_a_file(const char *file); |
---|
54 | int test_main(void); |
---|
55 | |
---|
56 | char test_write_buffer[ 1024 ]; |
---|
57 | rtems_filesystem_operations_table IMFS_ops_no_evalformake; |
---|
58 | rtems_filesystem_operations_table IMFS_ops_no_rename; |
---|
59 | |
---|
60 | static const char somefile[] = "somefile"; |
---|
61 | |
---|
62 | static const char somelink[] = "somelink"; |
---|
63 | |
---|
64 | /* |
---|
65 | * File test support routines. |
---|
66 | */ |
---|
67 | |
---|
68 | void test_cat( |
---|
69 | char *file, |
---|
70 | int offset_arg, |
---|
71 | int length |
---|
72 | ); |
---|
73 | |
---|
74 | void test_write( |
---|
75 | char *file, |
---|
76 | off_t offset, |
---|
77 | char *buffer |
---|
78 | ); |
---|
79 | |
---|
80 | void test_extend( |
---|
81 | char *file, |
---|
82 | off_t new_len |
---|
83 | ); |
---|
84 | |
---|
85 | /* |
---|
86 | * dump_statbuf |
---|
87 | */ |
---|
88 | void dump_statbuf( struct stat *buf ) |
---|
89 | { |
---|
90 | rtems_device_major_number major1; |
---|
91 | rtems_device_minor_number minor1; |
---|
92 | rtems_device_major_number major2; |
---|
93 | rtems_device_minor_number minor2; |
---|
94 | |
---|
95 | rtems_filesystem_split_dev_t( buf->st_dev, major1, minor1 ); |
---|
96 | rtems_filesystem_split_dev_t( buf->st_rdev, major2, minor2 ); |
---|
97 | |
---|
98 | printf( "....st_dev (0x%" PRIx32 ":0x%" PRIx32 ")\n", major1, minor1 ); |
---|
99 | printf( "....st_rdev (0x%" PRIx32 ":0x%" PRIx32 ")\n", major2, minor2 ); |
---|
100 | printf( "....st_ino %" PRIxino_t " may vary by small amount\n", buf->st_ino ); |
---|
101 | printf( "....mode = %08" PRIomode_t "\n", buf->st_mode ); |
---|
102 | printf( "....nlink = %d\n", buf->st_nlink ); |
---|
103 | |
---|
104 | printf( "....uid = %d\n", buf->st_uid ); |
---|
105 | printf( "....gid = %d\n", buf->st_gid ); |
---|
106 | |
---|
107 | printf( "....atime = %s", ctime(&buf->st_atime) ); |
---|
108 | printf( "....mtime = %s", ctime(&buf->st_mtime) ); |
---|
109 | printf( "....ctime = %s", ctime(&buf->st_ctime) ); |
---|
110 | |
---|
111 | printf( "....st_blksize %" PRIxblksize_t "\n", buf->st_blksize ); |
---|
112 | printf( "....st_blocks %" PRIxblkcnt_t "\n", buf->st_blocks ); |
---|
113 | } |
---|
114 | |
---|
115 | void stat_a_file( |
---|
116 | const char *file |
---|
117 | ) |
---|
118 | { |
---|
119 | int status; |
---|
120 | struct stat statbuf; |
---|
121 | |
---|
122 | rtems_test_assert( file ); |
---|
123 | |
---|
124 | printf( "stat( %s ) returned ", file ); |
---|
125 | |
---|
126 | status = stat( file, &statbuf ); |
---|
127 | |
---|
128 | if ( status == -1 ) { |
---|
129 | printf( ": %s\n", strerror( errno ) ); |
---|
130 | } else { |
---|
131 | puts(""); |
---|
132 | dump_statbuf( &statbuf ); |
---|
133 | } |
---|
134 | |
---|
135 | } |
---|
136 | |
---|
137 | static void test_open_directory(void) |
---|
138 | { |
---|
139 | int status; |
---|
140 | int fd; |
---|
141 | |
---|
142 | fd = open( somefile, O_CREAT, S_IRWXU ); |
---|
143 | rtems_test_assert( fd >= 0 ); |
---|
144 | |
---|
145 | status = close( fd ); |
---|
146 | rtems_test_assert( status == 0 ); |
---|
147 | |
---|
148 | #ifdef O_DIRECTORY |
---|
149 | errno = 0; |
---|
150 | fd = open( somefile, O_DIRECTORY, S_IRWXU ); |
---|
151 | rtems_test_assert( fd == -1 ); |
---|
152 | rtems_test_assert( errno == ENOTDIR ); |
---|
153 | #endif |
---|
154 | |
---|
155 | status = unlink( somefile ); |
---|
156 | rtems_test_assert( status == 0 ); |
---|
157 | |
---|
158 | errno = 0; |
---|
159 | fd = open( somefile, O_RDONLY ); |
---|
160 | rtems_test_assert( fd == -1 ); |
---|
161 | rtems_test_assert( errno == ENOENT ); |
---|
162 | } |
---|
163 | |
---|
164 | static void test_open_cloexec(void) |
---|
165 | { |
---|
166 | int status; |
---|
167 | int fd; |
---|
168 | mode_t mode; |
---|
169 | |
---|
170 | mode = O_CREAT; |
---|
171 | |
---|
172 | #ifdef O_CLOEXEC |
---|
173 | mode |= O_CLOEXEC; |
---|
174 | #endif |
---|
175 | |
---|
176 | fd = open( somefile, mode, S_IRWXU ); |
---|
177 | rtems_test_assert( fd >= 0 ); |
---|
178 | |
---|
179 | status = close( fd ); |
---|
180 | rtems_test_assert( status == 0 ); |
---|
181 | |
---|
182 | status = unlink( somefile ); |
---|
183 | rtems_test_assert( status == 0 ); |
---|
184 | |
---|
185 | errno = 0; |
---|
186 | fd = open( somefile, O_RDONLY ); |
---|
187 | rtems_test_assert( fd == -1 ); |
---|
188 | rtems_test_assert( errno == ENOENT ); |
---|
189 | } |
---|
190 | |
---|
191 | static void test_open_nofollow(void) |
---|
192 | { |
---|
193 | int status; |
---|
194 | int fd; |
---|
195 | struct stat st; |
---|
196 | |
---|
197 | fd = open( somefile, O_CREAT, S_IRWXU ); |
---|
198 | rtems_test_assert( fd >= 0 ); |
---|
199 | |
---|
200 | status = close( fd ); |
---|
201 | rtems_test_assert( status == 0 ); |
---|
202 | |
---|
203 | status = symlink( somefile, somelink ); |
---|
204 | rtems_test_assert( status == 0 ); |
---|
205 | |
---|
206 | fd = open( somelink, O_RDONLY ); |
---|
207 | rtems_test_assert( fd >= 0 ); |
---|
208 | |
---|
209 | status = fstat( fd, &st ); |
---|
210 | rtems_test_assert( status == 0 ); |
---|
211 | rtems_test_assert( S_ISREG( st.st_mode ) ); |
---|
212 | |
---|
213 | status = close( fd ); |
---|
214 | rtems_test_assert( status == 0 ); |
---|
215 | |
---|
216 | #ifdef O_NOFOLLOW |
---|
217 | fd = open( somelink, O_RDONLY | O_NOFOLLOW ); |
---|
218 | rtems_test_assert( fd >= 0 ); |
---|
219 | |
---|
220 | status = fstat( fd, &st ); |
---|
221 | rtems_test_assert( status == 0 ); |
---|
222 | rtems_test_assert( S_ISLNK( st.st_mode ) ); |
---|
223 | |
---|
224 | status = close( fd ); |
---|
225 | rtems_test_assert( status == 0 ); |
---|
226 | #endif |
---|
227 | |
---|
228 | status = unlink( somelink ); |
---|
229 | rtems_test_assert( status == 0 ); |
---|
230 | |
---|
231 | errno = 0; |
---|
232 | fd = open( somelink, O_RDONLY ); |
---|
233 | rtems_test_assert( fd == -1 ); |
---|
234 | rtems_test_assert( errno == ENOENT ); |
---|
235 | |
---|
236 | status = unlink( somefile ); |
---|
237 | rtems_test_assert( status == 0 ); |
---|
238 | |
---|
239 | errno = 0; |
---|
240 | fd = open( somefile, O_RDONLY ); |
---|
241 | rtems_test_assert( fd == -1 ); |
---|
242 | rtems_test_assert( errno == ENOENT ); |
---|
243 | } |
---|
244 | |
---|
245 | /* |
---|
246 | * Main entry point of the test |
---|
247 | */ |
---|
248 | |
---|
249 | #if defined(__rtems__) |
---|
250 | int test_main(void) |
---|
251 | #else |
---|
252 | int main( |
---|
253 | int argc, |
---|
254 | char **argv |
---|
255 | ) |
---|
256 | #endif |
---|
257 | { |
---|
258 | int status; |
---|
259 | size_t max_size; |
---|
260 | int fd; |
---|
261 | int i; |
---|
262 | struct stat buf; |
---|
263 | char buffer[128]; |
---|
264 | FILE *file; |
---|
265 | time_t atime1; |
---|
266 | time_t mtime1; |
---|
267 | time_t ctime1; |
---|
268 | time_t atime2; |
---|
269 | time_t mtime2; |
---|
270 | time_t ctime2; |
---|
271 | rtems_status_code rtems_status; |
---|
272 | rtems_time_of_day time; |
---|
273 | |
---|
274 | TEST_BEGIN(); |
---|
275 | |
---|
276 | test_open_directory(); |
---|
277 | test_open_cloexec(); |
---|
278 | test_open_nofollow(); |
---|
279 | |
---|
280 | /* |
---|
281 | * Grab the maximum size of an in-memory file. |
---|
282 | */ |
---|
283 | |
---|
284 | max_size = IMFS_MEMFILE_MAXIMUM_SIZE; |
---|
285 | |
---|
286 | build_time( &time, 12, 31, 1988, 9, 0, 0, 0 ); |
---|
287 | rtems_status = rtems_clock_set( &time ); |
---|
288 | directive_failed( rtems_status, "clock set" ); |
---|
289 | |
---|
290 | /* |
---|
291 | * Simple stat() of /dev/console. |
---|
292 | */ |
---|
293 | |
---|
294 | puts( "stat of /dev/console" ); |
---|
295 | status = stat( "/dev/console", &buf ); |
---|
296 | rtems_test_assert( !status ); |
---|
297 | |
---|
298 | dump_statbuf( &buf ); |
---|
299 | |
---|
300 | /* |
---|
301 | * Exercise mkdir() and some path evaluation. |
---|
302 | */ |
---|
303 | |
---|
304 | puts( "" ); |
---|
305 | puts( "mkdir /dev/tty" ); |
---|
306 | status = mkdir( "/dev/tty", S_IRWXU ); |
---|
307 | rtems_test_assert( !status ); |
---|
308 | |
---|
309 | puts( "" ); |
---|
310 | puts( "mkdir /usr" ); |
---|
311 | status = mkdir( "/usr", S_IRWXU ); |
---|
312 | rtems_test_assert( !status ); |
---|
313 | puts( "mkdir /etc" ); |
---|
314 | status = mkdir( "/etc", S_IRWXU ); |
---|
315 | rtems_test_assert( !status ); |
---|
316 | |
---|
317 | puts( "mkdir /tmp" ); |
---|
318 | status = mkdir( "/tmp", S_IRWXU ); |
---|
319 | rtems_test_assert( !status ); |
---|
320 | |
---|
321 | /* this tests the ".." path in path name evaluation */ |
---|
322 | puts( "mkdir /tmp/.." ); |
---|
323 | status = mkdir( "/tmp/..", S_IRWXU ); |
---|
324 | rtems_test_assert( status == -1 ); |
---|
325 | rtems_test_assert( errno == EEXIST ); |
---|
326 | |
---|
327 | /* now check out trailing separators */ |
---|
328 | puts( "mkdir /tmp/" ); |
---|
329 | status = mkdir( "/tmp/", S_IRWXU ); |
---|
330 | rtems_test_assert( status == -1 ); |
---|
331 | rtems_test_assert( errno == EEXIST ); |
---|
332 | |
---|
333 | /* try to make a directory under a non-existent subdirectory */ |
---|
334 | puts( "mkdir /j/j1" ); |
---|
335 | status = mkdir( "/j/j1", S_IRWXU ); |
---|
336 | rtems_test_assert( status == -1 ); |
---|
337 | rtems_test_assert( errno == ENOENT ); |
---|
338 | |
---|
339 | /* this tests the ability to make a directory in the current one */ |
---|
340 | puts( "mkdir tmp" ); |
---|
341 | status = mkdir( "tmp", S_IRWXU ); |
---|
342 | rtems_test_assert( status == -1 ); |
---|
343 | rtems_test_assert( errno == EEXIST ); |
---|
344 | |
---|
345 | /* |
---|
346 | * Now switch gears and exercise rmdir(). |
---|
347 | */ |
---|
348 | |
---|
349 | puts( "" ); |
---|
350 | puts( "rmdir /usr" ); |
---|
351 | status = rmdir( "/usr" ); |
---|
352 | rtems_test_assert( !status ); |
---|
353 | |
---|
354 | puts( "rmdir /dev" ); |
---|
355 | status = rmdir( "/dev" ); |
---|
356 | rtems_test_assert( status == -1 ); |
---|
357 | rtems_test_assert( errno == ENOTEMPTY); |
---|
358 | |
---|
359 | puts( "rmdir /fred" ); |
---|
360 | status = rmdir ("/fred"); |
---|
361 | rtems_test_assert (status == -1); |
---|
362 | rtems_test_assert( errno == ENOENT ); |
---|
363 | |
---|
364 | puts( "rmdir /tmp/bha" ); |
---|
365 | status = rmdir( "/tmp/bha" ); |
---|
366 | rtems_test_assert( status == -1 ); |
---|
367 | rtems_test_assert( errno == ENOENT ); |
---|
368 | |
---|
369 | puts( "mknod /dev/test_console" ); |
---|
370 | status = mknod( "/dev/test_console", S_IFCHR, 0LL ); |
---|
371 | rtems_test_assert( !status ); |
---|
372 | |
---|
373 | puts( "mknod /dev/tty/S3" ); |
---|
374 | status = mknod( "/dev/tty/S3", S_IFCHR, 0xFF00000080LL ); |
---|
375 | rtems_test_assert( !status ); |
---|
376 | |
---|
377 | puts ("mknod /etc/passwd"); |
---|
378 | status = mknod( "/etc/passwd", (S_IFREG | S_IRWXU), 0LL ); |
---|
379 | rtems_test_assert( !status ); |
---|
380 | |
---|
381 | puts( "mkdir /tmp/my_dir"); |
---|
382 | status = mkdir( "/tmp/my_dir", S_IRWXU ); |
---|
383 | rtems_test_assert( status == 0 ); |
---|
384 | |
---|
385 | puts("mkfifo /c/my_dir" ); |
---|
386 | status = mkfifo( "/c/my_dir", S_IRWXU ); |
---|
387 | rtems_test_assert( status == -1 ); |
---|
388 | |
---|
389 | /* |
---|
390 | * Try to make a directory under a file -- ERROR |
---|
391 | */ |
---|
392 | |
---|
393 | puts( "mkdir /etc/passwd/j" ); |
---|
394 | status = mkdir( "/etc/passwd/j", S_IRWXU ); |
---|
395 | rtems_test_assert( status == -1 ); |
---|
396 | rtems_test_assert( errno == ENOTDIR ); |
---|
397 | |
---|
398 | /* |
---|
399 | * Simple open failure case on non-existent file |
---|
400 | */ |
---|
401 | |
---|
402 | puts( "open /tmp/joel - should fail with ENOENT" ); |
---|
403 | fd = open( "/tmp/joel", O_RDONLY ); |
---|
404 | rtems_test_assert( fd == -1 ); |
---|
405 | rtems_test_assert( errno == ENOENT ); |
---|
406 | |
---|
407 | /* |
---|
408 | * Simple open case where the file is created. |
---|
409 | */ |
---|
410 | |
---|
411 | puts( "open /tmp/j" ); |
---|
412 | fd = open( "/tmp/j", O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO ); |
---|
413 | rtems_test_assert( fd != -1 ); |
---|
414 | printf( "open returned file descriptor %d\n", fd ); |
---|
415 | |
---|
416 | puts( "close /tmp/j" ); |
---|
417 | status = close( fd ); |
---|
418 | rtems_test_assert( !status ); |
---|
419 | |
---|
420 | puts( "close /tmp/j again" ); |
---|
421 | status = close( fd ); |
---|
422 | rtems_test_assert( status == -1 ); |
---|
423 | |
---|
424 | puts( "unlink /tmp/j" ); |
---|
425 | status = unlink( "/tmp/j" ); |
---|
426 | rtems_test_assert( !status ); |
---|
427 | |
---|
428 | puts( "unlink /tmp" ); |
---|
429 | status = unlink( "/tmp" ); |
---|
430 | rtems_test_assert( status ); |
---|
431 | |
---|
432 | /* |
---|
433 | * Simple open failure. Trying to create an existing file. |
---|
434 | */ |
---|
435 | |
---|
436 | puts("create and close /tmp/tom"); |
---|
437 | fd = open( "/tmp/tom", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO ); |
---|
438 | rtems_test_assert( fd != -1 ); |
---|
439 | status = close( fd ); |
---|
440 | rtems_test_assert( status == 0 ); |
---|
441 | |
---|
442 | puts("Attempt to recreate /tmp/tom"); |
---|
443 | fd = open( "/tmp/tom", O_CREAT | O_EXCL, S_IRWXU|S_IRWXG|S_IRWXO ); |
---|
444 | rtems_test_assert( fd == -1 ); |
---|
445 | rtems_test_assert( errno == EEXIST ); |
---|
446 | |
---|
447 | puts("create /tmp/john"); |
---|
448 | fd = open( "/tmp/john", O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO ); |
---|
449 | rtems_test_assert( fd != -1 ); |
---|
450 | status = close( fd ); |
---|
451 | rtems_test_assert( status == 0 ); |
---|
452 | |
---|
453 | /* |
---|
454 | * Open a file in read-only mode and try to truncate |
---|
455 | */ |
---|
456 | |
---|
457 | puts( "Attempt to create a file, open in read-only mode and truncate it" ); |
---|
458 | fd = open( "/tmp/bha", O_CREAT | O_RDONLY | O_TRUNC, S_IRUSR ); |
---|
459 | rtems_test_assert( fd == -1 ); |
---|
460 | rtems_test_assert( errno == EINVAL ); |
---|
461 | |
---|
462 | puts( "Exercise the reentrant version _link_r -- Expect ENOENT" ); |
---|
463 | status = _link_r( NULL, "/tmp/notexist", "/tmp/cannotexist" ); |
---|
464 | rtems_test_assert( status == -1 ); |
---|
465 | rtems_test_assert( errno == ENOENT ); |
---|
466 | |
---|
467 | puts( "Unlink /tmp/bha using the reentrant version -- OK" ); |
---|
468 | status = _unlink_r( NULL, "/tmp/bha" ); |
---|
469 | rtems_test_assert( status == 0 ); |
---|
470 | |
---|
471 | /* |
---|
472 | * Simple test case for mknod |
---|
473 | */ |
---|
474 | |
---|
475 | puts( "mknod with bad type - expect EINVAL" ); |
---|
476 | status = mknod( "/tmp/bha", 0, 0LL ); |
---|
477 | rtems_test_assert( status == -1 ); |
---|
478 | rtems_test_assert( errno == EINVAL ); |
---|
479 | |
---|
480 | /* |
---|
481 | * Read from filedes opened for write |
---|
482 | */ |
---|
483 | |
---|
484 | puts( "open /tmp/bha in write only mode -- OK" ); |
---|
485 | fd = open( "/tmp/bha", O_CREAT | O_WRONLY, S_IRWXU|S_IRWXG|S_IRWXO ); |
---|
486 | rtems_test_assert( fd != -1 ); |
---|
487 | |
---|
488 | puts( "attempt fcntl on opened file -- OK" ); |
---|
489 | status = fcntl( fd, F_SETFD, 0 ); |
---|
490 | rtems_test_assert( status == 0 ); |
---|
491 | |
---|
492 | puts( "attempt to read from /tmp/bha - expect EBADF" ); |
---|
493 | status = read( fd, buffer, 10 ); |
---|
494 | rtems_test_assert( status == -1 ); |
---|
495 | rtems_test_assert( errno == EBADF ); |
---|
496 | |
---|
497 | puts( "closing and unlinking /tmp/bha" ); |
---|
498 | status = close( fd ); |
---|
499 | status |= unlink( "/tmp/bha" ); |
---|
500 | rtems_test_assert( status == 0 ); |
---|
501 | |
---|
502 | puts( "open /tmp/bha in read only mode -- OK" ); |
---|
503 | fd = open( "/tmp/bha", O_CREAT | O_RDONLY, S_IRWXU|S_IRWXG|S_IRWXO ); |
---|
504 | rtems_test_assert( fd != -1 ); |
---|
505 | |
---|
506 | puts( "attempt to read from /tmp/bha - expect EBADF" ); |
---|
507 | status = write( fd, buffer, 10 ); |
---|
508 | rtems_test_assert( status == -1 ); |
---|
509 | rtems_test_assert( errno == EBADF ); |
---|
510 | |
---|
511 | puts( "closing and unlinking /tmp/bha" ); |
---|
512 | status = close( fd ); |
---|
513 | status |= unlink( "/tmp/bha" ); |
---|
514 | rtems_test_assert( status == 0 ); |
---|
515 | |
---|
516 | /* |
---|
517 | * Read/write from an unopened filedes |
---|
518 | */ |
---|
519 | puts( "attempt to read from an unopened filedes - expect EBADF" ); |
---|
520 | status = read( 5, buffer, 10 ); |
---|
521 | rtems_test_assert( status == -1 ); |
---|
522 | rtems_test_assert( errno == EBADF ); |
---|
523 | |
---|
524 | puts( "attempt to write to an unopened filedes - expect EBADF" ); |
---|
525 | status = write( 5, buffer, 10 ); |
---|
526 | rtems_test_assert( status == -1 ); |
---|
527 | rtems_test_assert( errno == EBADF ); |
---|
528 | |
---|
529 | /* |
---|
530 | * Test simple write to a file at offset 0 |
---|
531 | */ |
---|
532 | |
---|
533 | puts( "mknod /tmp/joel" ); |
---|
534 | status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL ); |
---|
535 | test_write( "/tmp/joel", 0, "the first write!!!\n" ); |
---|
536 | test_cat( "/tmp/joel", 0, 0 ); |
---|
537 | |
---|
538 | /* Exercise _rename_r */ |
---|
539 | |
---|
540 | /* Simple rename test */ |
---|
541 | puts( "rename /tmp/joel to /tmp/drjoel"); |
---|
542 | status = _rename_r(NULL,"/tmp/joel","/tmp/drjoel"); |
---|
543 | rtems_test_assert(status == 0); |
---|
544 | |
---|
545 | /* Simple rename test */ |
---|
546 | puts("rename /tmp/drjoel to /tmp/joel"); |
---|
547 | status = _rename_r(NULL,"/tmp/drjoel","/tmp/joel"); |
---|
548 | rtems_test_assert(status == 0); |
---|
549 | |
---|
550 | /* Invalid old path */ |
---|
551 | puts("rename /tmp/drjoel to /tmp/joel - Should result in an error \ |
---|
552 | since old path is not valid"); |
---|
553 | status = _rename_r(NULL,"/tmp/drjoel","/tmp/joel"); |
---|
554 | rtems_test_assert(status == -1); |
---|
555 | |
---|
556 | /* Invalid new path */ |
---|
557 | puts("rename /tmp/joel to /tmp/drjoel/joel - Should result in an error \ |
---|
558 | since new path is not valid"); |
---|
559 | status = _rename_r(NULL,"/tmp/joel","/tmp/drjoel/joel"); |
---|
560 | rtems_test_assert(status == -1); |
---|
561 | |
---|
562 | puts("changing dir to /tmp"); |
---|
563 | status = chdir("/tmp/"); |
---|
564 | rtems_test_assert(status == 0); |
---|
565 | |
---|
566 | puts("rename joel to drjoel"); |
---|
567 | status = _rename_r(NULL,"joel","drjoel"); |
---|
568 | rtems_test_assert(status == 0); |
---|
569 | |
---|
570 | puts("rename drjoel to joel"); |
---|
571 | status = _rename_r(NULL,"drjoel","joel"); |
---|
572 | rtems_test_assert(status == 0); |
---|
573 | |
---|
574 | /* Rename across file systems */ |
---|
575 | puts("creating directory /imfs"); |
---|
576 | status = mkdir("/imfs",0777); |
---|
577 | rtems_test_assert(status == 0); |
---|
578 | puts("creating directory /imfs/hidden_on_mount"); |
---|
579 | status = mkdir("/imfs/hidden_on_mount",0777); |
---|
580 | rtems_test_assert(status == 0); |
---|
581 | |
---|
582 | puts("mounting filesystem with IMFS_ops at /imfs"); |
---|
583 | status = mount("null", "/imfs", "imfs", RTEMS_FILESYSTEM_READ_WRITE, NULL); |
---|
584 | rtems_test_assert(status == 0); |
---|
585 | puts("creating directory /imfs/test (on newly mounted filesystem)"); |
---|
586 | status = mkdir("/imfs/test", 0777); |
---|
587 | rtems_test_assert(status == 0); |
---|
588 | |
---|
589 | puts("attempt to rename directory joel to /imfs/test/joel - should fail with EXDEV"); |
---|
590 | status = _rename_r(NULL, "joel", "/imfs/test/joel"); |
---|
591 | rtems_test_assert(status == -1); |
---|
592 | rtems_test_assert(errno == EXDEV); |
---|
593 | |
---|
594 | puts("changing dir to /"); |
---|
595 | status = chdir("/"); |
---|
596 | rtems_test_assert(status == 0); |
---|
597 | |
---|
598 | puts("attempt to rename across filesystem, with old path having a parent node"); |
---|
599 | puts("attempt to rename tmp/joel to /imfs/test/joel"); |
---|
600 | status = _rename_r(NULL, "tmp/joel", "/imfs/test/joel"); |
---|
601 | rtems_test_assert(status == -1); |
---|
602 | rtems_test_assert(errno == EXDEV); |
---|
603 | |
---|
604 | puts("End of _rename_r tests"); |
---|
605 | |
---|
606 | /* |
---|
607 | * Test simple write to a file at a non-0 offset in the first block |
---|
608 | */ |
---|
609 | |
---|
610 | status = unlink( "/tmp/joel" ); |
---|
611 | rtems_test_assert( !status ); |
---|
612 | |
---|
613 | status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL ); |
---|
614 | rtems_test_assert( !status ); |
---|
615 | |
---|
616 | test_write( "/tmp/joel", 10, "the first write!!!\n" ); |
---|
617 | test_cat( "/tmp/joel", 0, 0 ); |
---|
618 | stat_a_file( "/tmp/joel" ); |
---|
619 | |
---|
620 | /* |
---|
621 | * Test simple write to a file at a non-0 offset in the second block. Then |
---|
622 | * try to read from various offsets and lengths. |
---|
623 | */ |
---|
624 | |
---|
625 | puts("unlink /tmp/joel"); |
---|
626 | status = unlink( "/tmp/joel" ); |
---|
627 | rtems_test_assert( !status ); |
---|
628 | |
---|
629 | /* Test a failure path */ |
---|
630 | |
---|
631 | puts( "unlink /tmp/joel" ); |
---|
632 | status = unlink( "/tmp/joel" ); |
---|
633 | rtems_test_assert( status == -1 ); |
---|
634 | |
---|
635 | puts( "mknod /tmp/joel"); |
---|
636 | status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL ); |
---|
637 | rtems_test_assert( !status ); |
---|
638 | |
---|
639 | test_write( "/tmp/joel", 514, "the first write!!!\n" ); |
---|
640 | test_write( "/tmp/joel", 1, test_write_buffer ); |
---|
641 | test_write( "/tmp/joel", 63, test_write_buffer ); |
---|
642 | test_cat( "/tmp/joel", 0, 1 ); |
---|
643 | test_cat( "/tmp/joel", 1, 1 ); |
---|
644 | test_cat( "/tmp/joel", 490, 1 ); |
---|
645 | test_cat( "/tmp/joel", 512, 1 ); |
---|
646 | test_cat( "/tmp/joel", 513, 1 ); |
---|
647 | test_cat( "/tmp/joel", 514, 1 ); |
---|
648 | test_cat( "/tmp/joel", 520, 1 ); |
---|
649 | test_cat( "/tmp/joel", 1, 1024 ); |
---|
650 | |
---|
651 | /* |
---|
652 | * Read from a much longer file so we can descend into doubly and |
---|
653 | * triply indirect blocks. |
---|
654 | */ |
---|
655 | |
---|
656 | if ( max_size < (size_t) 300 * 1024 ) { |
---|
657 | test_extend( "/tmp/joel", max_size - 1 ); |
---|
658 | test_cat( "/tmp/joel", max_size / 2, 1024 ); |
---|
659 | } else { |
---|
660 | printf( "Skipping maximum file size test since max_size is %zu bytes\n", max_size ); |
---|
661 | puts("That is likely to be bigger than the available RAM on many targets." ); |
---|
662 | } |
---|
663 | |
---|
664 | stat_a_file( "/tmp/joel" ); |
---|
665 | |
---|
666 | /* |
---|
667 | * Now try to use a FILE * descriptor |
---|
668 | * |
---|
669 | * /tmp/j should not exist at this point. |
---|
670 | */ |
---|
671 | |
---|
672 | puts( "stat of /tmp/j" ); |
---|
673 | errno = 0; |
---|
674 | status = stat( "/tmp/j", &buf ); |
---|
675 | printf( "stat(/tmp/j) returned %d (errno=%d)\n", status, errno ); |
---|
676 | dump_statbuf( &buf ); |
---|
677 | |
---|
678 | puts( "fopen of /tmp/j" ); |
---|
679 | file = fopen( "/tmp/j", "w+" ); |
---|
680 | rtems_test_assert( file ); |
---|
681 | |
---|
682 | puts( "fprintf to /tmp/j" ); |
---|
683 | for (i=1 ; i<=5 ; i++) { |
---|
684 | status = fprintf( file, "This is call %d to fprintf\n", i ); |
---|
685 | rtems_test_assert( status ); |
---|
686 | printf( "(%d) %d characters written to the file\n", i, status ); |
---|
687 | } |
---|
688 | |
---|
689 | fflush( file ); |
---|
690 | |
---|
691 | status = stat( "/tmp/j", &buf ); |
---|
692 | rtems_test_assert( !status ); |
---|
693 | dump_statbuf( &buf ); |
---|
694 | atime2 = buf.st_atime; |
---|
695 | mtime2 = buf.st_mtime; |
---|
696 | ctime2 = buf.st_ctime; |
---|
697 | |
---|
698 | |
---|
699 | status = rtems_task_wake_after( rtems_clock_get_ticks_per_second() ); |
---|
700 | rewind( file ); |
---|
701 | while ( fgets(buffer, 128, file) ) |
---|
702 | printf( "%s", buffer ); |
---|
703 | |
---|
704 | /* |
---|
705 | * Verify only atime changed for a read. |
---|
706 | */ |
---|
707 | status = stat( "/tmp/j", &buf ); |
---|
708 | rtems_test_assert( !status ); |
---|
709 | dump_statbuf( &buf ); |
---|
710 | atime1 = buf.st_atime; |
---|
711 | mtime1 = buf.st_mtime; |
---|
712 | ctime1 = buf.st_ctime; |
---|
713 | rtems_test_assert( atime1 != atime2); |
---|
714 | rtems_test_assert( mtime1 == mtime2); |
---|
715 | rtems_test_assert( ctime1 == ctime2); |
---|
716 | |
---|
717 | unlink( "/tmp/joel" ); |
---|
718 | |
---|
719 | /* |
---|
720 | * Now truncate a file |
---|
721 | */ |
---|
722 | |
---|
723 | status = rtems_task_wake_after( rtems_clock_get_ticks_per_second() ); |
---|
724 | puts( "truncate /tmp/j to length of 40" ); |
---|
725 | status = truncate( "/tmp/j", 40 ); |
---|
726 | rtems_test_assert( !status ); |
---|
727 | |
---|
728 | /* |
---|
729 | * Verify truncate changed all except atime. |
---|
730 | */ |
---|
731 | status = stat( "/tmp/j", &buf ); |
---|
732 | rtems_test_assert( !status ); |
---|
733 | dump_statbuf( &buf ); |
---|
734 | atime2 = buf.st_atime; |
---|
735 | mtime2 = buf.st_mtime; |
---|
736 | ctime2 = buf.st_ctime; |
---|
737 | rtems_test_assert( atime1 == atime2); |
---|
738 | rtems_test_assert( mtime1 != mtime2); |
---|
739 | rtems_test_assert( ctime1 != ctime2); |
---|
740 | |
---|
741 | /* try to truncate the console and see what happens */ |
---|
742 | errno = 0; |
---|
743 | status = truncate( "/dev/console", 40 ); |
---|
744 | rtems_test_assert( status == 0 || ( status == -1 && errno == EINVAL ) ); |
---|
745 | |
---|
746 | puts( "truncate /tmp/j to length of 0" ); |
---|
747 | status = truncate( "/tmp/j", 0 ); |
---|
748 | rtems_test_assert( !status ); |
---|
749 | |
---|
750 | puts( "truncate /tmp to length of 0 should fail with EISDIR\n"); |
---|
751 | status = truncate( "/tmp", 0 ); |
---|
752 | rtems_test_assert( status == -1 ); |
---|
753 | printf( "%d: %s\n", errno, strerror( errno ) ); |
---|
754 | rtems_test_assert( errno == EISDIR ); |
---|
755 | |
---|
756 | status = truncate( "/tmp/fred", 10 ); |
---|
757 | rtems_test_assert( status == -1); |
---|
758 | |
---|
759 | rtems_status = rtems_io_register_name( "/dev/not_console", 0, 0 ); |
---|
760 | directive_failed( rtems_status, "io register" ); |
---|
761 | |
---|
762 | test_case_reopen_append(); |
---|
763 | |
---|
764 | TEST_END(); |
---|
765 | rtems_test_exit( 0 ); |
---|
766 | } |
---|
767 | |
---|
768 | /* |
---|
769 | * Open/Create a File and write to it |
---|
770 | * |
---|
771 | * Test case submitted by Andrew Bythell <abythell@nortelnetworks.com>. |
---|
772 | * |
---|
773 | */ |
---|
774 | |
---|
775 | void test_file (char *filename, char *mode); |
---|
776 | |
---|
777 | void test_case_reopen_append(void) |
---|
778 | { |
---|
779 | printf ("Writing First File\n"); |
---|
780 | test_file ("/one.txt", "a"); |
---|
781 | test_file ("/one.txt", "a"); |
---|
782 | |
---|
783 | /* but not the second time - this will insert junk. |
---|
784 | the number of ^@'s seems to equal the number of |
---|
785 | actual characters in the file */ |
---|
786 | |
---|
787 | printf ("Writing Second File\n"); |
---|
788 | test_file ("/two.txt", "a"); |
---|
789 | test_file ("/two.txt", "a"); |
---|
790 | |
---|
791 | test_cat( "/one.txt", 0, 1024 ); |
---|
792 | test_cat( "/two.txt", 0, 1024 ); |
---|
793 | } |
---|
794 | |
---|
795 | void test_file (char *filename, char *mode) |
---|
796 | { |
---|
797 | FILE *fp; |
---|
798 | fp = fopen (filename, mode); |
---|
799 | if (!fp) |
---|
800 | perror ("fopen"); |
---|
801 | fprintf (fp, "this is a test line\n"); |
---|
802 | if (fclose (fp)) |
---|
803 | perror ("fclose"); |
---|
804 | } |
---|