source: rtems/c/src/tests/psxtests/psxfile01/test.c @ 5b8e885d

4.104.114.84.95
Last change on this file since 5b8e885d was 0895bdb, checked in by Joel Sherrill <joel.sherrill@…>, on 11/23/98 at 18:57:48

Added tests in support of the file system infrastructure.

  • Property mode set to 100644
File size: 10.3 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-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
39char test_write_buffer[ 1024 ];
40
41/*
42 *  File test support routines.
43 */
44
45void test_cat(
46  char *file,
47  int   offset_arg,
48  int   length
49);
50
51void test_write(
52  char   *file,
53  off_t  offset,
54  char  *buffer
55);
56
57void test_extend(
58  char *file,
59  off_t new_len
60);
61
62void IMFS_dump( void );
63int IMFS_memfile_maximum_size( void );
64
65/*
66 *  dump_statbuf
67 */
68
69void 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
98void 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__)
126int test_main(void)
127#else
128int 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);
241
242  status = mknod( "/dev/test_console", S_IFCHR, 0LL );
243  assert( !status );
244  status = mknod( "/dev/tty/S3", S_IFCHR, 0xFF00000080LL );
245  assert( !status );
246  status = mknod( "/etc/passwd", (S_IFREG | S_IRWXU), 0LL );
247  assert( !status );
248
249  status = mkdir( "/tmp/my_dir", S_IRWXU );
250  assert( status == 0 );
251
252  status = mkfifo( "/c/my_dir", S_IRWXU );
253  assert( status == -1 );
254
255  /*
256   *  Try to make a directory under a file -- ERROR
257   */
258
259  puts( "mkdir /etc/passwd/j" );
260  status = mkdir( "/etc/passwd/j", S_IRWXU );
261  assert( status == -1 );
262  assert( errno == ENOTDIR );
263
264  /*
265   *  Simple open failure case on non-existent file
266   */
267
268  puts( "open /tmp/joel - should fail with ENOENT" );
269  fd = open( "/tmp/joel", O_RDONLY );
270  assert( fd == -1 );
271  assert( errno == ENOENT );
272
273  /*
274   *  Simple open case where the file is created.
275   */
276
277  puts( "open /tmp/j" );
278  fd = open( "/tmp/j", O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO );
279  assert( fd != -1 );
280
281  printf( "open returned file descriptor %d\n", fd );
282
283  puts( "close /tmp/j" );
284  status = close( fd );
285  assert( !status );
286
287  status = close( fd );
288  assert( status == -1 );
289
290  puts( "unlink /tmp/j" );
291  status = unlink( "/tmp/j" );
292  assert( !status );
293
294  status = unlink( "/tmp" );
295  assert( status );
296
297  /*
298   *  Simple open failure. Trying to create an existing file.
299   */
300
301  fd = open( "/tmp/tom", O_CREAT );
302  status = close( fd );
303  fd1 = open( "/tmp/tom", O_CREAT );
304  assert( fd1 == -1 );
305
306  fd = open( "/tmp/john", O_CREAT );
307  assert( fd != -1 );
308  status = tcdrain( fd );
309  assert( status == 0 );
310
311  /*
312   *  Test simple write to a file at offset 0
313   */
314
315  status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL );
316  test_write( "/tmp/joel", 0, "the first write!!!\n" );
317  test_cat( "/tmp/joel", 0, 0 );
318
319  /*
320   *  Test simple write to a file at a non-0 offset in the first block
321   */
322
323  status = unlink( "/tmp/joel" );
324  assert( !status );
325
326  status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL );
327  assert( !status );
328
329  test_write( "/tmp/joel", 10, "the first write!!!\n" );
330  test_cat( "/tmp/joel", 0, 0 );
331  stat_a_file( "/tmp/joel" );
332
333  /*
334   *  Test simple write to a file at a non-0 offset in the second block.  Then
335   *  try to read from various offsets and lengths.
336   */
337
338  status = unlink( "/tmp/joel" );
339  assert( !status );
340
341  /* Test a failure path */
342
343  status = unlink( "/tmp/joel" );
344  assert( status == -1 );
345
346  status = mknod( "/tmp/joel", (S_IFREG | S_IRWXU), 0LL );
347  assert( !status );
348
349  test_write( "/tmp/joel", 514, "the first write!!!\n" );
350  test_write( "/tmp/joel", 1, test_write_buffer );
351  test_write( "/tmp/joel", 63, test_write_buffer );
352  test_cat( "/tmp/joel", 0, 1 );
353  test_cat( "/tmp/joel", 1, 1 );
354  test_cat( "/tmp/joel", 490, 1 );
355  test_cat( "/tmp/joel", 512, 1 );
356  test_cat( "/tmp/joel", 513, 1 );
357  test_cat( "/tmp/joel", 514, 1 );
358  test_cat( "/tmp/joel", 520, 1 );
359  test_cat( "/tmp/joel", 1, 1024 );
360
361  /*
362   *  Read from a much longer file so we can descend into doubly and
363   *  triply indirect blocks.
364   */
365
366  test_extend( "/tmp/joel", max_size - 1 );
367  test_cat( "/tmp/joel", max_size / 2, 1024 );
368
369  stat_a_file( "/tmp/joel" );
370
371  /*
372   *  Now try to use a FILE * descriptor
373   *
374   *  /tmp/j should not exist at this point.
375   */
376
377  puts( "stat of /tmp/j" );
378  errno = 0;
379  status = stat( "/tmp/j", &buf );
380  printf( "stat(/tmp/j) returned %d (errno=%d)\n", status, errno );
381  dump_statbuf( &buf );
382
383  puts( "fopen of /tmp/j" );
384  file = fopen( "/tmp/j", "w+" );
385  assert( file );
386
387  puts( "fprintf to /tmp/j" );
388  for (i=1 ; i<=5 ; i++) {
389    status = fprintf( file, "This is call %d to fprintf\n", i );
390    assert( status );
391    printf( "(%d) %d characters written to the file\n", i, status );
392  }
393
394  fflush( file );
395
396  status = stat( "/tmp/j", &buf );
397  assert( !status );
398  dump_statbuf( &buf );
399  atime2 = buf.st_atime;
400  mtime2 = buf.st_mtime;
401  ctime2 = buf.st_ctime;
402
403
404  status = rtems_task_wake_after( 1 * TICKS_PER_SECOND );
405  rewind( file );
406  while ( fgets(buffer, 128, file) )
407    printf( buffer );
408
409  /*
410   * Verify only atime changed for a read.
411   */
412  status = stat( "/tmp/j", &buf );
413  assert( !status );
414  dump_statbuf( &buf );
415  atime1 = buf.st_atime;
416  mtime1 = buf.st_mtime;
417  ctime1 = buf.st_ctime;
418  assert( atime1 != atime2);
419  assert( mtime1 == mtime2);
420  assert( ctime1 == ctime2);
421
422  IMFS_dump();
423
424  unlink( "/tmp/joel" );
425
426  /*
427   *  Now truncate a file
428   */
429
430  status = rtems_task_wake_after( 1 * TICKS_PER_SECOND );
431  puts( "truncate /tmp/j to length of 40" );
432  status = truncate( "/tmp/j", 40 );
433  assert( !status );
434
435  /*
436   * Verify truncate changed only atime.
437   */
438  status = stat( "/tmp/j", &buf );
439  assert( !status );
440  dump_statbuf( &buf );
441  atime2 = buf.st_atime;
442  mtime2 = buf.st_mtime;
443  ctime2 = buf.st_ctime;
444  assert( atime1 != atime2);
445  assert( mtime1 == mtime2);
446  assert( ctime1 == ctime2);
447
448  IMFS_dump();
449
450  /* try to truncate the console and see what happens */
451  status = truncate( "/dev/console", 40 );
452  assert(status == -1 );
453
454  puts( "truncate /tmp/j to length of 0" );
455  status = truncate( "/tmp/j", 0 );
456  assert( !status );
457
458  puts( "truncate /tmp to length of 0 should fail with EISDIR\n");
459  status = truncate( "/tmp", 0 );
460  assert( status == -1 );
461  printf( "%d: %s\n", errno, strerror( errno ) );
462  assert( errno == EISDIR );
463
464  IMFS_dump();
465
466  status = truncate( "/tmp/fred", 10 );
467  assert( status == -1);
468
469  rtems_status = rtems_io_register_name( "/dev/console", 0, 0 );
470
471  printf( "*** END OF FILE TEST 1 ***\n" );
472  exit( 0 );
473}
474
475
Note: See TracBrowser for help on using the repository browser.