source: rtems/testsuites/psxtests/psxfile01/test.c @ 947015f

4.115
Last change on this file since 947015f was 947015f, checked in by Joel Sherrill <joel.sherrill@…>, on 07/15/10 at 13:59:25

2010-07-15 Bharath Suri <bharath.s.jois@…>

PR 1617/testing

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