source: rtems/testsuites/psxtests/psxfile01/test.c @ 042a442

4.104.115
Last change on this file since 042a442 was 042a442, checked in by Chris Johns <chrisj@…>, on 06/02/10 at 00:50:37

2010-06-02 Chris Johns <chrisj@…>

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