source: rtems/testsuites/psxtests/psxfile01/test.c @ 514a3fe

4.104.115
Last change on this file since 514a3fe was 514a3fe, checked in by Joel Sherrill <joel.sherrill@…>, on May 17, 2010 at 5:56:13 PM

2010-05-17 Bharath Suri <bharath.s.jois@…>

  • psxfile01/test.c, psxfile01/psxfile01.scn: This file now exercises the _rename_r in libcsupport. For now, it also provides two fsmount_me_h handlers to enable certain error checking paths
  • Property mode set to 100644
File size: 17.7 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 *  File test support routines.
45 */
46
47void test_cat(
48  char *file,
49  int   offset_arg,
50  int   length
51);
52
53void test_write(
54  char   *file,
55  off_t  offset,
56  char  *buffer
57);
58
59void test_extend(
60  char *file,
61  off_t new_len
62);
63
64void IMFS_dump( void );
65int IMFS_memfile_maximum_size( void );
66
67/*
68 *  dump_statbuf
69 */
70
71void dump_statbuf( struct stat *buf )
72{
73  int         major1;
74  int         minor1;
75  int         major2;
76  int         minor2;
77
78  rtems_filesystem_split_dev_t( buf->st_dev, major1, minor1 );
79  rtems_filesystem_split_dev_t( buf->st_rdev, major2, minor2 );
80
81  printf( "....st_dev     (0x%x:0x%x)\n", major1, minor1 );
82  printf( "....st_ino     %" PRIxino_t "  may vary by small amount\n", buf->st_ino );
83  printf( "....mode  = %08o\n", (unsigned int) buf->st_mode );
84  printf( "....nlink = %d\n", buf->st_nlink );
85
86  printf( "....uid = %d\n", buf->st_uid );
87  printf( "....gid = %d\n", buf->st_gid );
88
89  printf( "....atime = %s", ctime(&buf->st_atime) );
90  printf( "....mtime = %s", ctime(&buf->st_mtime) );
91  printf( "....ctime = %s", ctime(&buf->st_ctime) );
92
93  printf( "....st_blksize %" PRIxblksize_t "\n", buf->st_blksize );
94  printf( "....st_blocks  %" PRIxblkcnt_t "\n", buf->st_blocks );
95}
96
97void stat_a_file(
98  const char *file
99)
100{
101  int         status;
102  struct stat statbuf;
103
104  rtems_test_assert( file );
105
106  printf( "stat( %s ) returned ", file );
107  fflush( stdout );
108
109  status = stat( file, &statbuf );
110
111  if ( status == -1 ) {
112    printf( ": %s\n", strerror( errno ) );
113  } else {
114    puts("");
115    dump_statbuf( &statbuf );
116  }
117
118}
119
120int no_evalformake_IMFS_initialize(
121  rtems_filesystem_mount_table_entry_t *temp_mt_entry
122)
123{
124   return IMFS_initialize_support(
125     temp_mt_entry,
126     &IMFS_ops_no_evalformake,
127     &IMFS_memfile_handlers,
128     &IMFS_directory_handlers
129   );
130}
131
132int no_rename_IMFS_initialize(
133  rtems_filesystem_mount_table_entry_t *temp_mt_entry
134)
135{
136   return IMFS_initialize_support(
137     temp_mt_entry,
138     &IMFS_ops_no_rename,
139     &IMFS_memfile_handlers,
140     &IMFS_directory_handlers
141   );
142}
143
144
145/*
146 *  Main entry point of the test
147 */
148
149#if defined(__rtems__)
150int test_main(void)
151#else
152int main(
153  int argc,
154  char **argv
155)
156#endif
157{
158  int               status;
159  size_t            max_size;
160  int               fd;
161  int               i;
162  struct stat       buf;
163  char              buffer[128];
164  FILE             *file;
165  time_t            atime1;
166  time_t            mtime1;
167  time_t            ctime1;
168  time_t            atime2;
169  time_t            mtime2;
170  time_t            ctime2;
171  rtems_status_code rtems_status;
172  rtems_time_of_day time;
173  rtems_filesystem_mount_table_entry_t *mt_entry;
174
175  IMFS_ops_no_evalformake = IMFS_ops;
176  IMFS_ops_no_rename = IMFS_ops;
177
178  IMFS_ops_no_evalformake.fsmount_me_h = no_evalformake_IMFS_initialize;
179  IMFS_ops_no_evalformake.evalformake_h = NULL;
180
181  IMFS_ops_no_rename.fsmount_me_h = no_rename_IMFS_initialize;
182  IMFS_ops_no_rename.rename_h = NULL;
183
184
185  printf( "\n\n*** FILE TEST 1 ***\n" );
186
187  /*
188   *  Grab the maximum size of an in-memory file.
189   */
190
191  max_size = IMFS_memfile_maximum_size();
192
193  build_time( &time, 12, 31, 1988, 9, 0, 0, 0 );
194  rtems_status = rtems_clock_set( &time );
195
196  /*
197   *  Dump an empty file system
198   */
199
200  IMFS_dump();
201
202  /*
203   *  Simple stat() of /dev/console.
204   */
205
206  puts( "stat of /dev/console" );
207  status = stat( "/dev/console", &buf );
208  rtems_test_assert( !status );
209
210  dump_statbuf( &buf );
211
212  /*
213   *  Exercise mkdir() and some path evaluation.
214   */
215
216  puts( "" );
217  puts( "mkdir /dev/tty" );
218  status = mkdir( "/dev/tty", S_IRWXU );
219  rtems_test_assert( !status );
220
221  puts( "" );
222  puts( "mkdir /usr" );
223  status = mkdir( "/usr", S_IRWXU );
224  rtems_test_assert( !status );
225  puts( "mkdir /etc" );
226  status = mkdir( "/etc", S_IRWXU );
227  rtems_test_assert( !status );
228
229  puts( "mkdir /tmp" );
230  status = mkdir( "/tmp", S_IRWXU );
231  rtems_test_assert( !status );
232
233  /* this tests the ".." path in path name evaluation */
234  puts( "mkdir /tmp/.." );
235  status = mkdir( "/tmp/..", S_IRWXU );
236  rtems_test_assert( status == -1 );
237  rtems_test_assert( errno == EEXIST );
238
239  /* now check out trailing separators */
240  puts( "mkdir /tmp/" );
241  status = mkdir( "/tmp/", S_IRWXU );
242  rtems_test_assert( status == -1 );
243  rtems_test_assert( errno == EEXIST );
244
245  /* try to make a directory under a non-existent subdirectory */
246  puts( "mkdir /j/j1" );
247  status = mkdir( "/j/j1", S_IRWXU );
248  rtems_test_assert( status == -1 );
249  rtems_test_assert( errno == ENOENT );
250
251  /* this tests the ability to make a directory in the current one */
252  puts( "mkdir tmp" );
253  status = mkdir( "tmp", S_IRWXU );
254  rtems_test_assert( status == -1 );
255  rtems_test_assert( errno == EEXIST );
256
257  /* test rtems_filesystem_evaluate_path by sending NULL path */
258  status = chdir( NULL );
259  rtems_test_assert( status == -1 );
260  rtems_test_assert( errno == EFAULT );
261
262  /*
263   *  Now switch gears and exercise rmdir().
264   */
265
266  puts( "" );
267  puts( "rmdir /usr" );
268  status = rmdir( "/usr" );
269  rtems_test_assert( !status );
270
271  puts( "rmdir /dev" );
272  status = rmdir( "/dev" );
273  rtems_test_assert( status == -1 );
274  rtems_test_assert( errno ==  ENOTEMPTY);
275
276  puts( "rmdir /fred" );
277  status = rmdir ("/fred");
278  rtems_test_assert (status == -1);
279  rtems_test_assert( errno == ENOENT );
280
281  puts( "mknod /dev/test_console" );
282  status = mknod( "/dev/test_console", S_IFCHR, 0LL );
283  rtems_test_assert( !status );
284
285  puts( "mknod /dev/tty/S3" );
286  status = mknod( "/dev/tty/S3", S_IFCHR, 0xFF00000080LL );
287  rtems_test_assert( !status );
288
289  puts ("mknod /etc/passwd");
290  status = mknod( "/etc/passwd", (S_IFREG | S_IRWXU), 0LL );
291  rtems_test_assert( !status );
292
293  puts( "mkdir /tmp/my_dir");
294  status = mkdir( "/tmp/my_dir", S_IRWXU );
295  rtems_test_assert( status == 0 );
296
297  puts("mkfifo /c/my_dir" );
298  status = mkfifo( "/c/my_dir", S_IRWXU );
299  rtems_test_assert( status == -1 );
300
301  /*
302   *  Try to make a directory under a file -- ERROR
303   */
304
305  puts( "mkdir /etc/passwd/j" );
306  status = mkdir( "/etc/passwd/j", S_IRWXU );
307  rtems_test_assert( status == -1 );
308  rtems_test_assert( errno == ENOTDIR );
309
310  /*
311   *  Simple open failure case on non-existent file
312   */
313
314  puts( "open /tmp/joel - should fail with ENOENT" );
315  fd = open( "/tmp/joel", O_RDONLY );
316  rtems_test_assert( fd == -1 );
317  rtems_test_assert( errno == ENOENT );
318
319  /*
320   *  Simple open case where the file is created.
321   */
322
323  puts( "open /tmp/j" );
324  fd = open( "/tmp/j", O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO );
325  rtems_test_assert( fd != -1 );
326  printf( "open returned file descriptor %d\n", fd );
327
328  puts( "close /tmp/j" );
329  status = close( fd );
330  rtems_test_assert( !status );
331
332  puts( "close /tmp/j again" );
333  status = close( fd );
334  rtems_test_assert( status == -1 );
335
336  puts( "unlink /tmp/j" );
337  status = unlink( "/tmp/j" );
338  rtems_test_assert( !status );
339
340  puts( "unlink /tmp" );
341  status = unlink( "/tmp" );
342  rtems_test_assert( status );
343
344  /*
345   *  Simple open failure. Trying to create an existing file.
346   */
347
348  puts("create and close /tmp/tom");
349  fd = open( "/tmp/tom", O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO );
350  rtems_test_assert( fd != -1 );
351  status = close( fd );
352  rtems_test_assert( status == 0 );
353
354  puts("Attempt to recreate /tmp/tom");
355  fd = open( "/tmp/tom", O_CREAT | O_EXCL, S_IRWXU|S_IRWXG|S_IRWXO );
356  rtems_test_assert( fd == -1 );
357  rtems_test_assert( errno == EEXIST );
358
359  puts("create /tmp/john");
360  fd = open( "/tmp/john", O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO );
361  rtems_test_assert( fd != -1 );
362
363  puts("tcdrain /tmp/john" );
364  status = tcdrain( fd );
365  rtems_test_assert( status == 0 );
366
367  /*
368   *  Test simple write to a file at offset 0
369   */
370
371  puts( "mknod /tmp/joel" );
372  status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL );
373  test_write( "/tmp/joel", 0, "the first write!!!\n" );
374  test_cat( "/tmp/joel", 0, 0 );
375
376  /* Exercise _rename_r */
377
378  /* Simple rename test */
379  puts( "rename /tmp/joel to /tmp/drjoel");
380  status = _rename_r(NULL,"/tmp/joel","/tmp/drjoel");
381  rtems_test_assert(status == 0);
382
383  /* Simple rename test */
384  puts("rename /tmp/drjoel to /tmp/joel");
385  status = _rename_r(NULL,"/tmp/drjoel","/tmp/joel");
386  rtems_test_assert(status == 0);
387
388  /* Invalid old path */
389  puts("rename /tmp/drjoel to /tmp/joel - Should result in an error \
390since old path is not valid");
391  status = _rename_r(NULL,"/tmp/drjoel","/tmp/joel");
392  rtems_test_assert(status == -1);
393
394  /* Invalid new path */
395  puts("rename /tmp/joel to /tmp/drjoel/joel - Should result in an error \
396since new path is not valid");
397  status = _rename_r(NULL,"/tmp/joel","/tmp/drjoel/joel");
398  rtems_test_assert(status == -1);
399
400  puts("changing dir to /tmp");
401  status = chdir("/tmp/");
402  rtems_test_assert(status == 0);
403
404  puts("rename joel to drjoel");
405  status = _rename_r(NULL,"joel","drjoel");
406  rtems_test_assert(status == 0);
407
408  puts("rename drjoel to joel");
409  status = _rename_r(NULL,"drjoel","joel");
410  rtems_test_assert(status == 0);
411
412  /* Rename across file systems */
413  puts("creating directory /imfs");
414  status = mkdir("/imfs",0777);
415  rtems_test_assert(status == 0);
416  puts("creating directory /imfs/hidden_on_mount");
417  status = mkdir("/imfs/hidden_on_mount",0777);
418  rtems_test_assert(status == 0);
419
420  puts("mounting filesystem with IMFS_ops at /imfs");
421  status = mount(&mt_entry, &IMFS_ops, 
422                 RTEMS_FILESYSTEM_READ_WRITE, 
423                 NULL, "/imfs");
424  rtems_test_assert(status == 0);
425  rtems_test_assert(mt_entry != NULL);
426  puts("creating directory /imfs/test (on newly mounted filesystem)");
427  status = mkdir("/imfs/test", 0777);
428  rtems_test_assert(status == 0);
429
430  puts("attempt to rename directory joel to /imfs/test/joel - should fail with EXDEV");
431  status = _rename_r(NULL, "joel", "/imfs/test/joel");
432  rtems_test_assert(status == -1);
433  rtems_test_assert(errno == EXDEV);
434
435  puts("changing dir to /");
436  status = chdir("/");
437  rtems_test_assert(status == 0);
438
439  puts("attempt to rename across filesystem, with old path having a parent node");
440  puts("attempt to rename tmp/joel to /imfs/test/joel");
441  status = _rename_r(NULL, "tmp/joel", "/imfs/test/joel");
442  rtems_test_assert(status == -1);
443  rtems_test_assert(errno == EXDEV);
444
445  puts("Unmounting /imfs");
446  status = unmount("/imfs");
447  rtems_test_assert(status == 0);
448
449  puts("Mounting filesystem @ /imfs with no support for evalformake");
450 
451  status = mount(&mt_entry, &IMFS_ops_no_evalformake, 
452                 RTEMS_FILESYSTEM_READ_WRITE, 
453                 NULL, "/imfs");
454  rtems_test_assert(status == 0);
455  rtems_test_assert(mt_entry != NULL);
456
457  puts("change directory to /imfs");
458  status = chdir("/imfs");
459  rtems_test_assert(status == 0);
460
461  puts("exercise _rename_r, with target on /imfs - expected ENOTSUP");
462  puts("attempt to rename /tmp/joel to joel");
463  status = _rename_r(NULL, "/tmp/joel", "joel");
464  rtems_test_assert(status == -1);
465  rtems_test_assert(errno == ENOTSUP);
466
467  puts("change directory to /");
468  status = chdir("/");
469  rtems_test_assert(status == 0);
470 
471  status = unmount("/imfs");
472  rtems_test_assert(status == 0);
473
474
475  puts("Mounting filesystem @ /imfs with no support for rename");
476  status = mount(&mt_entry, &IMFS_ops_no_rename, 
477                 RTEMS_FILESYSTEM_READ_WRITE, 
478                 NULL, "/imfs");
479  rtems_test_assert(status == 0);
480  rtems_test_assert(mt_entry != NULL);
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.