source: rtems/testsuites/psxtests/psxfile01/test.c @ 620e0329

4.104.115
Last change on this file since 620e0329 was 620e0329, checked in by Ralf Corsepius <ralf.corsepius@…>, on 11/01/09 at 07:53:31

2009-01-01 Ralf Corsépius <ralf.corsepius@…>

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