[0895bdb] | 1 | /* |
---|
| 2 | * Simple test program to exercise some of the basic functionality of |
---|
| 3 | * POSIX Files and Directories Support. |
---|
| 4 | * |
---|
| 5 | * This test assumes that the file system is initialized with the |
---|
| 6 | * following directory structure: |
---|
| 7 | * |
---|
| 8 | * XXXX fill this in. |
---|
| 9 | * / |
---|
| 10 | * /dev |
---|
| 11 | * /dev/XXX [where XXX includes at least console] |
---|
| 12 | * |
---|
| 13 | * COPYRIGHT (c) 1989-1998. |
---|
| 14 | * On-Line Applications Research Corporation (OAR). |
---|
| 15 | * Copyright assigned to U.S. Government, 1994. |
---|
| 16 | * |
---|
| 17 | * The license and distribution terms for this file may be |
---|
| 18 | * found in the file LICENSE in this distribution or at |
---|
| 19 | * http://www.OARcorp.com/rtems/license.html. |
---|
| 20 | * |
---|
| 21 | * $Id$ |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #include <stdio.h> |
---|
| 25 | |
---|
| 26 | #include <tmacros.h> |
---|
| 27 | #include <sys/types.h> |
---|
| 28 | #include <sys/stat.h> |
---|
| 29 | #include <fcntl.h> |
---|
| 30 | #include <unistd.h> |
---|
| 31 | #include <errno.h> |
---|
| 32 | #include <string.h> |
---|
| 33 | #include <ctype.h> |
---|
| 34 | |
---|
| 35 | #include <assert.h> |
---|
| 36 | #include <rtems.h> |
---|
| 37 | #include <rtems/libio.h> |
---|
| 38 | |
---|
| 39 | char test_write_buffer[ 1024 ]; |
---|
| 40 | |
---|
| 41 | /* |
---|
| 42 | * File test support routines. |
---|
| 43 | */ |
---|
| 44 | |
---|
| 45 | void test_cat( |
---|
| 46 | char *file, |
---|
| 47 | int offset_arg, |
---|
| 48 | int length |
---|
| 49 | ); |
---|
| 50 | |
---|
| 51 | void test_write( |
---|
| 52 | char *file, |
---|
| 53 | off_t offset, |
---|
| 54 | char *buffer |
---|
| 55 | ); |
---|
| 56 | |
---|
| 57 | void test_extend( |
---|
| 58 | char *file, |
---|
| 59 | off_t new_len |
---|
| 60 | ); |
---|
| 61 | |
---|
| 62 | void IMFS_dump( void ); |
---|
| 63 | int IMFS_memfile_maximum_size( void ); |
---|
| 64 | |
---|
| 65 | /* |
---|
| 66 | * dump_statbuf |
---|
| 67 | */ |
---|
| 68 | |
---|
| 69 | void dump_statbuf( struct stat *buf ) |
---|
| 70 | { |
---|
| 71 | int major1; |
---|
| 72 | int minor1; |
---|
| 73 | int major2; |
---|
| 74 | int minor2; |
---|
| 75 | |
---|
| 76 | rtems_filesystem_split_dev_t( buf->st_dev, major1, minor1 ); |
---|
| 77 | rtems_filesystem_split_dev_t( buf->st_rdev, major2, minor2 ); |
---|
| 78 | |
---|
| 79 | printf( " st_dev (0x%x:0x%x)\n", major1, minor1 ); |
---|
| 80 | printf( " st_ino %x\n", buf->st_ino ); |
---|
| 81 | printf( " mode = %08o\n", buf->st_mode ); |
---|
| 82 | printf( " nlink = %d\n", buf->st_nlink ); |
---|
| 83 | |
---|
| 84 | printf( " uid = %d\n", buf->st_uid ); |
---|
| 85 | printf( " gid = %d\n", buf->st_gid ); |
---|
| 86 | |
---|
| 87 | printf( " atime = %s", ctime(&buf->st_atime) ); |
---|
| 88 | printf( " mtime = %s", ctime(&buf->st_mtime) ); |
---|
| 89 | printf( " ctime = %s", ctime(&buf->st_ctime) ); |
---|
| 90 | |
---|
| 91 | #if defined(__svr4__) && !defined(__PPC__) && !defined(__sun__) |
---|
| 92 | printf( " st_blksize %x\n", buf.st_blksize ); |
---|
| 93 | printf( " st_blocks %x\n", buf.st_blocks ); |
---|
| 94 | #endif |
---|
| 95 | |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | void stat_a_file( |
---|
| 99 | const char *file |
---|
| 100 | ) |
---|
| 101 | { |
---|
| 102 | int status; |
---|
| 103 | struct stat statbuf; |
---|
| 104 | |
---|
| 105 | assert( file ); |
---|
| 106 | |
---|
| 107 | printf( "stat( %s ) returned ", file ); |
---|
| 108 | fflush( stdout ); |
---|
| 109 | |
---|
| 110 | status = stat( file, &statbuf ); |
---|
| 111 | |
---|
| 112 | if ( status == -1 ) { |
---|
| 113 | printf( ": %s\n", strerror( errno ) ); |
---|
| 114 | } else { |
---|
| 115 | dump_statbuf( &statbuf ); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | /* |
---|
| 122 | * Main entry point of the test |
---|
| 123 | */ |
---|
| 124 | |
---|
| 125 | #if defined(__rtems__) |
---|
| 126 | int test_main(void) |
---|
| 127 | #else |
---|
| 128 | int main( |
---|
| 129 | int argc, |
---|
| 130 | char **argv |
---|
| 131 | ) |
---|
| 132 | #endif |
---|
| 133 | { |
---|
| 134 | int status; |
---|
| 135 | int max_size; |
---|
| 136 | int fd, fd1; |
---|
| 137 | int i; |
---|
| 138 | struct stat buf; |
---|
| 139 | char buffer[128]; |
---|
| 140 | FILE *file; |
---|
| 141 | time_t atime1; |
---|
| 142 | time_t mtime1; |
---|
| 143 | time_t ctime1; |
---|
| 144 | time_t atime2; |
---|
| 145 | time_t mtime2; |
---|
| 146 | time_t ctime2; |
---|
| 147 | rtems_status_code rtems_status; |
---|
| 148 | rtems_time_of_day time; |
---|
| 149 | |
---|
| 150 | printf( "\n\n*** FILE TEST 1 ***\n" ); |
---|
| 151 | |
---|
| 152 | /* |
---|
| 153 | * Grab the maximum size of an in-memory file. |
---|
| 154 | */ |
---|
| 155 | |
---|
| 156 | max_size = IMFS_memfile_maximum_size(); |
---|
| 157 | |
---|
| 158 | build_time( &time, 12, 31, 1988, 9, 0, 0, 0 ); |
---|
| 159 | rtems_status = rtems_clock_set( &time ); |
---|
| 160 | |
---|
| 161 | /* |
---|
| 162 | * Dump an empty file system |
---|
| 163 | */ |
---|
| 164 | |
---|
| 165 | IMFS_dump(); |
---|
| 166 | |
---|
| 167 | /* |
---|
| 168 | * Simple stat() of /dev/console. |
---|
| 169 | */ |
---|
| 170 | |
---|
| 171 | puts( "stat of /dev/console" ); |
---|
| 172 | status = stat( "/dev/console", &buf ); |
---|
| 173 | assert( !status ); |
---|
| 174 | |
---|
| 175 | dump_statbuf( &buf ); |
---|
| 176 | |
---|
| 177 | /* |
---|
| 178 | * Exercise mkdir() and some path evaluation. |
---|
| 179 | */ |
---|
| 180 | |
---|
| 181 | puts( "" ); |
---|
| 182 | puts( "mkdir /dev/tty" ); |
---|
| 183 | status = mkdir( "/dev/tty", S_IRWXU ); |
---|
| 184 | assert( !status ); |
---|
| 185 | |
---|
| 186 | puts( "" ); |
---|
| 187 | puts( "mkdir /usr" ); |
---|
| 188 | status = mkdir( "/usr", S_IRWXU ); |
---|
| 189 | assert( !status ); |
---|
| 190 | puts( "mkdir /etc" ); |
---|
| 191 | status = mkdir( "/etc", S_IRWXU ); |
---|
| 192 | assert( !status ); |
---|
| 193 | |
---|
| 194 | puts( "mkdir /tmp" ); |
---|
| 195 | status = mkdir( "/tmp", S_IRWXU ); |
---|
| 196 | assert( !status ); |
---|
| 197 | |
---|
| 198 | /* this tests the ".." path in path name evaluation */ |
---|
| 199 | puts( "mkdir /tmp/.." ); |
---|
| 200 | status = mkdir( "/tmp/..", S_IRWXU ); |
---|
| 201 | assert( status == -1 ); |
---|
| 202 | assert( errno == EEXIST ); |
---|
| 203 | |
---|
| 204 | /* now check out trailing separators */ |
---|
| 205 | puts( "mkdir /tmp/" ); |
---|
| 206 | status = mkdir( "/tmp/", S_IRWXU ); |
---|
| 207 | assert( status == -1 ); |
---|
| 208 | assert( errno == EEXIST ); |
---|
| 209 | |
---|
| 210 | /* try to make a directory under a non-existent subdirectory */ |
---|
| 211 | puts( "mkdir /j/j1" ); |
---|
| 212 | status = mkdir( "/j/j1", S_IRWXU ); |
---|
| 213 | assert( status == -1 ); |
---|
| 214 | assert( errno == ENOENT ); |
---|
| 215 | |
---|
| 216 | /* this tests the ability to make a directory in the current one */ |
---|
| 217 | puts( "mkdir tmp" ); |
---|
| 218 | status = mkdir( "tmp", S_IRWXU ); |
---|
| 219 | assert( status == -1 ); |
---|
| 220 | assert( errno == EEXIST ); |
---|
| 221 | |
---|
| 222 | /* test rtems_filesystem_evaluate_path by sending NULL path */ |
---|
| 223 | status = chdir( NULL ); |
---|
| 224 | assert( status == -1 ); |
---|
| 225 | |
---|
| 226 | /* |
---|
| 227 | * Now switch gears and exercise rmdir(). |
---|
| 228 | */ |
---|
| 229 | |
---|
| 230 | puts( "" ); |
---|
| 231 | puts( "rmdir /usr" ); |
---|
| 232 | status = rmdir( "/usr" ); |
---|
| 233 | assert( !status ); |
---|
| 234 | |
---|
| 235 | status = rmdir( "/dev" ); |
---|
| 236 | assert( status == -1 ); |
---|
| 237 | assert( errno == ENOTEMPTY); |
---|
| 238 | |
---|
| 239 | status = rmdir ("/fred"); |
---|
| 240 | assert (status == -1); |
---|
[c2f9b97] | 241 | assert( errno == ENOENT ); |
---|
[0895bdb] | 242 | |
---|
| 243 | status = mknod( "/dev/test_console", S_IFCHR, 0LL ); |
---|
| 244 | assert( !status ); |
---|
| 245 | status = mknod( "/dev/tty/S3", S_IFCHR, 0xFF00000080LL ); |
---|
| 246 | assert( !status ); |
---|
| 247 | status = mknod( "/etc/passwd", (S_IFREG | S_IRWXU), 0LL ); |
---|
| 248 | assert( !status ); |
---|
| 249 | |
---|
| 250 | status = mkdir( "/tmp/my_dir", S_IRWXU ); |
---|
| 251 | assert( status == 0 ); |
---|
| 252 | |
---|
| 253 | status = mkfifo( "/c/my_dir", S_IRWXU ); |
---|
| 254 | assert( status == -1 ); |
---|
| 255 | |
---|
| 256 | /* |
---|
| 257 | * Try to make a directory under a file -- ERROR |
---|
| 258 | */ |
---|
| 259 | |
---|
| 260 | puts( "mkdir /etc/passwd/j" ); |
---|
| 261 | status = mkdir( "/etc/passwd/j", S_IRWXU ); |
---|
| 262 | assert( status == -1 ); |
---|
| 263 | assert( errno == ENOTDIR ); |
---|
| 264 | |
---|
| 265 | /* |
---|
| 266 | * Simple open failure case on non-existent file |
---|
| 267 | */ |
---|
| 268 | |
---|
| 269 | puts( "open /tmp/joel - should fail with ENOENT" ); |
---|
| 270 | fd = open( "/tmp/joel", O_RDONLY ); |
---|
| 271 | assert( fd == -1 ); |
---|
| 272 | assert( errno == ENOENT ); |
---|
| 273 | |
---|
| 274 | /* |
---|
| 275 | * Simple open case where the file is created. |
---|
| 276 | */ |
---|
| 277 | |
---|
| 278 | puts( "open /tmp/j" ); |
---|
| 279 | fd = open( "/tmp/j", O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO ); |
---|
| 280 | assert( fd != -1 ); |
---|
| 281 | |
---|
| 282 | printf( "open returned file descriptor %d\n", fd ); |
---|
| 283 | |
---|
| 284 | puts( "close /tmp/j" ); |
---|
| 285 | status = close( fd ); |
---|
| 286 | assert( !status ); |
---|
| 287 | |
---|
| 288 | status = close( fd ); |
---|
| 289 | assert( status == -1 ); |
---|
| 290 | |
---|
| 291 | puts( "unlink /tmp/j" ); |
---|
| 292 | status = unlink( "/tmp/j" ); |
---|
| 293 | assert( !status ); |
---|
| 294 | |
---|
| 295 | status = unlink( "/tmp" ); |
---|
| 296 | assert( status ); |
---|
| 297 | |
---|
| 298 | /* |
---|
| 299 | * Simple open failure. Trying to create an existing file. |
---|
| 300 | */ |
---|
| 301 | |
---|
| 302 | fd = open( "/tmp/tom", O_CREAT ); |
---|
| 303 | status = close( fd ); |
---|
| 304 | fd1 = open( "/tmp/tom", O_CREAT ); |
---|
| 305 | assert( fd1 == -1 ); |
---|
| 306 | |
---|
| 307 | fd = open( "/tmp/john", O_CREAT ); |
---|
| 308 | assert( fd != -1 ); |
---|
| 309 | status = tcdrain( fd ); |
---|
| 310 | assert( status == 0 ); |
---|
| 311 | |
---|
| 312 | /* |
---|
| 313 | * Test simple write to a file at offset 0 |
---|
| 314 | */ |
---|
| 315 | |
---|
| 316 | status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL ); |
---|
| 317 | test_write( "/tmp/joel", 0, "the first write!!!\n" ); |
---|
| 318 | test_cat( "/tmp/joel", 0, 0 ); |
---|
| 319 | |
---|
| 320 | /* |
---|
| 321 | * Test simple write to a file at a non-0 offset in the first block |
---|
| 322 | */ |
---|
| 323 | |
---|
| 324 | status = unlink( "/tmp/joel" ); |
---|
| 325 | assert( !status ); |
---|
| 326 | |
---|
| 327 | status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL ); |
---|
| 328 | assert( !status ); |
---|
| 329 | |
---|
| 330 | test_write( "/tmp/joel", 10, "the first write!!!\n" ); |
---|
| 331 | test_cat( "/tmp/joel", 0, 0 ); |
---|
| 332 | stat_a_file( "/tmp/joel" ); |
---|
| 333 | |
---|
| 334 | /* |
---|
| 335 | * Test simple write to a file at a non-0 offset in the second block. Then |
---|
| 336 | * try to read from various offsets and lengths. |
---|
| 337 | */ |
---|
| 338 | |
---|
| 339 | status = unlink( "/tmp/joel" ); |
---|
| 340 | assert( !status ); |
---|
| 341 | |
---|
| 342 | /* Test a failure path */ |
---|
| 343 | |
---|
| 344 | status = unlink( "/tmp/joel" ); |
---|
| 345 | assert( status == -1 ); |
---|
| 346 | |
---|
| 347 | status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL ); |
---|
| 348 | assert( !status ); |
---|
| 349 | |
---|
| 350 | test_write( "/tmp/joel", 514, "the first write!!!\n" ); |
---|
| 351 | test_write( "/tmp/joel", 1, test_write_buffer ); |
---|
| 352 | test_write( "/tmp/joel", 63, test_write_buffer ); |
---|
| 353 | test_cat( "/tmp/joel", 0, 1 ); |
---|
| 354 | test_cat( "/tmp/joel", 1, 1 ); |
---|
| 355 | test_cat( "/tmp/joel", 490, 1 ); |
---|
| 356 | test_cat( "/tmp/joel", 512, 1 ); |
---|
| 357 | test_cat( "/tmp/joel", 513, 1 ); |
---|
| 358 | test_cat( "/tmp/joel", 514, 1 ); |
---|
| 359 | test_cat( "/tmp/joel", 520, 1 ); |
---|
| 360 | test_cat( "/tmp/joel", 1, 1024 ); |
---|
| 361 | |
---|
| 362 | /* |
---|
| 363 | * Read from a much longer file so we can descend into doubly and |
---|
| 364 | * triply indirect blocks. |
---|
| 365 | */ |
---|
| 366 | |
---|
| 367 | test_extend( "/tmp/joel", max_size - 1 ); |
---|
| 368 | test_cat( "/tmp/joel", max_size / 2, 1024 ); |
---|
| 369 | |
---|
| 370 | stat_a_file( "/tmp/joel" ); |
---|
| 371 | |
---|
| 372 | /* |
---|
| 373 | * Now try to use a FILE * descriptor |
---|
| 374 | * |
---|
| 375 | * /tmp/j should not exist at this point. |
---|
| 376 | */ |
---|
| 377 | |
---|
| 378 | puts( "stat of /tmp/j" ); |
---|
| 379 | errno = 0; |
---|
| 380 | status = stat( "/tmp/j", &buf ); |
---|
| 381 | printf( "stat(/tmp/j) returned %d (errno=%d)\n", status, errno ); |
---|
| 382 | dump_statbuf( &buf ); |
---|
| 383 | |
---|
| 384 | puts( "fopen of /tmp/j" ); |
---|
| 385 | file = fopen( "/tmp/j", "w+" ); |
---|
| 386 | assert( file ); |
---|
| 387 | |
---|
| 388 | puts( "fprintf to /tmp/j" ); |
---|
| 389 | for (i=1 ; i<=5 ; i++) { |
---|
| 390 | status = fprintf( file, "This is call %d to fprintf\n", i ); |
---|
| 391 | assert( status ); |
---|
| 392 | printf( "(%d) %d characters written to the file\n", i, status ); |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | fflush( file ); |
---|
| 396 | |
---|
| 397 | status = stat( "/tmp/j", &buf ); |
---|
| 398 | assert( !status ); |
---|
| 399 | dump_statbuf( &buf ); |
---|
| 400 | atime2 = buf.st_atime; |
---|
| 401 | mtime2 = buf.st_mtime; |
---|
| 402 | ctime2 = buf.st_ctime; |
---|
| 403 | |
---|
| 404 | |
---|
| 405 | status = rtems_task_wake_after( 1 * TICKS_PER_SECOND ); |
---|
| 406 | rewind( file ); |
---|
| 407 | while ( fgets(buffer, 128, file) ) |
---|
| 408 | printf( buffer ); |
---|
| 409 | |
---|
| 410 | /* |
---|
| 411 | * Verify only atime changed for a read. |
---|
| 412 | */ |
---|
| 413 | status = stat( "/tmp/j", &buf ); |
---|
| 414 | assert( !status ); |
---|
| 415 | dump_statbuf( &buf ); |
---|
| 416 | atime1 = buf.st_atime; |
---|
| 417 | mtime1 = buf.st_mtime; |
---|
| 418 | ctime1 = buf.st_ctime; |
---|
| 419 | assert( atime1 != atime2); |
---|
| 420 | assert( mtime1 == mtime2); |
---|
| 421 | assert( ctime1 == ctime2); |
---|
| 422 | |
---|
| 423 | IMFS_dump(); |
---|
| 424 | |
---|
| 425 | unlink( "/tmp/joel" ); |
---|
| 426 | |
---|
| 427 | /* |
---|
| 428 | * Now truncate a file |
---|
| 429 | */ |
---|
| 430 | |
---|
| 431 | status = rtems_task_wake_after( 1 * TICKS_PER_SECOND ); |
---|
| 432 | puts( "truncate /tmp/j to length of 40" ); |
---|
| 433 | status = truncate( "/tmp/j", 40 ); |
---|
| 434 | assert( !status ); |
---|
| 435 | |
---|
| 436 | /* |
---|
| 437 | * Verify truncate changed only atime. |
---|
| 438 | */ |
---|
| 439 | status = stat( "/tmp/j", &buf ); |
---|
| 440 | assert( !status ); |
---|
| 441 | dump_statbuf( &buf ); |
---|
| 442 | atime2 = buf.st_atime; |
---|
| 443 | mtime2 = buf.st_mtime; |
---|
| 444 | ctime2 = buf.st_ctime; |
---|
| 445 | assert( atime1 != atime2); |
---|
| 446 | assert( mtime1 == mtime2); |
---|
| 447 | assert( ctime1 == ctime2); |
---|
| 448 | |
---|
| 449 | IMFS_dump(); |
---|
| 450 | |
---|
| 451 | /* try to truncate the console and see what happens */ |
---|
| 452 | status = truncate( "/dev/console", 40 ); |
---|
| 453 | assert(status == -1 ); |
---|
| 454 | |
---|
| 455 | puts( "truncate /tmp/j to length of 0" ); |
---|
| 456 | status = truncate( "/tmp/j", 0 ); |
---|
| 457 | assert( !status ); |
---|
| 458 | |
---|
| 459 | puts( "truncate /tmp to length of 0 should fail with EISDIR\n"); |
---|
| 460 | status = truncate( "/tmp", 0 ); |
---|
| 461 | assert( status == -1 ); |
---|
| 462 | printf( "%d: %s\n", errno, strerror( errno ) ); |
---|
| 463 | assert( errno == EISDIR ); |
---|
| 464 | |
---|
| 465 | IMFS_dump(); |
---|
| 466 | |
---|
| 467 | status = truncate( "/tmp/fred", 10 ); |
---|
| 468 | assert( status == -1); |
---|
| 469 | |
---|
| 470 | rtems_status = rtems_io_register_name( "/dev/console", 0, 0 ); |
---|
| 471 | |
---|
| 472 | printf( "*** END OF FILE TEST 1 ***\n" ); |
---|
| 473 | exit( 0 ); |
---|
| 474 | } |
---|
| 475 | |
---|
| 476 | |
---|