source: rtems/testsuites/psxtests/psxstat/test.c @ 0895bdb

4.104.114.84.95
Last change on this file since 0895bdb 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: 17.8 KB
Line 
1/*
2 *  This test exercises stat() via fstat() and generates as many of the
3 *  path evaluation cases as possible.
4 *
5 *  COPYRIGHT (c) 1989-1998.
6 *  On-Line Applications Research Corporation (OAR).
7 *  Copyright assigned to U.S. Government, 1994.
8 *
9 *  The license and distribution terms for this file may be
10 *  found in the file LICENSE in this distribution or at
11 *  http://www.OARcorp.com/rtems/license.html.
12 *
13 *  $Id$
14 */
15
16#include <tmacros.h>
17#include <assert.h>
18#include <sys/stat.h>
19#include <fcntl.h>
20#include <limits.h>
21#include <stdio.h>
22#include <unistd.h>
23#include <errno.h>
24#include <string.h>
25#include <rtems.h>
26#include <rtems/libio.h>
27
28#define MAXSYMLINK 5   /* There needs to be a better way of getting this. */
29
30/*
31 *  List of files which should exist.
32 */
33
34char *Files[] = {
35  "////dir1/\\//file1\\\\//",
36  "/dir1/file2",
37  "/dir1/file3",
38  "/dir1/file4",
39  "/dir1/dir1/file1",
40  "../../..//dir1/./././dir1/ file1",
41  "main.c",
42  0
43};
44
45/*
46 *  List of directories which should exist.
47 */
48
49char *Directories[] = {
50  "/dir1",
51  "/dir2",
52  "/dir3",
53  "/dir4",
54  "/dir1/dir1",
55  "/./././dir1/ dir1",
56  "/./././links", 
57  "///dir1/dir1/../../dir1/../symlinks/////",
58  0
59};
60
61char *Links_to_Dirs[]= {
62  "dir1/dir1/../../links/dir1",
63  "links/dir2",
64  "links/dir3",
65  "links/dir4",
66  "links/dir1_dir1",
67  "links/dir1_ dir1",
68  "links/../links/../links/links",
69  0
70};
71
72char *Links_to_Files[]= {
73  "links/dir1_file1",
74  "links/dir1_file2",
75  "links/dir1_file3",
76  "links/dir1_file4",
77  "links/dir1_dir1_f1",
78  "links/dir1_dir1 f1",
79  0
80};
81
82char *Links_to_dirlinks[]= {
83  "links/links/links/links_dir1",
84  "links//links_dir2",
85  "links//links_dir3",
86  "links//links_dir4",
87  "links//links_dir1_d1",
88  "links//links_dir1 d1",
89  "links//links_links",
90  0
91};
92
93char *Links_to_filelinks[]= {
94  "links///links_d1_file1",
95  "links///links_d1_file2",
96  "links///links_d1_file3",
97  "links///links_d1_file4",
98  "links///links_d1_d1_f1",
99  "links///links_r1_d1 f1",
100  0
101};
102
103char *SymLinks[]= {
104  "/symlinks/a_file_symlink",
105  "/symlinks/a_dir_symlink",
106  "/symlinks/a_link_symlink",
107  "../symlinks/no_file",
108  "/symlinks/a_dir_symlink/a_file_symlink",
109  0
110};
111
112/*
113 *  List of absolute paths to stat.
114 */
115
116char *Good_absolute_paths[] = {
117  "/dev",
118  "////dir1/\\//file1\\\\//",
119  "/dir1/\\\\/file2",
120  "/dir1/file3/////\\\\\\",
121  "/dir1/file4",
122  "/dir1/dir1/file1",
123  "/dir1/dir1/ file1",
124  "/dir1",
125  "/dir2//////\\",
126  "/dir3",
127  "/dir4",
128  "/dir1/dir1",
129  "/dir1/ dir1///\\\\",
130  "/\\/\\/\\/\\/\\/\\/links\\/\\/\\/\\/\\/\\",
131  0
132};
133
134
135char *Bad_paths[] = {
136  "/links/ENAMETOOLONG___",
137  "/dir1/file4/NOTADIR",
138  "/dir1/dir1/EACCES__",
139  0
140};
141
142/*
143 *  List of relative paths to stat.
144 */
145
146char *Good_relative_paths[] = {
147  "dev",
148  "dir1/\\//file1\\\\//",
149  "dir1/\\\\/file2",
150  "dir1/file3/////\\\\\\",
151  "dir1/file4",
152  "dir1/dir1/file1",
153  "dir1/dir1/ file1",
154  "dir1",
155  "dir2//////\\",
156  "dir3",
157  "dir4",
158  "dir1/dir1",
159  "dir1/ dir1///\\\\",
160  "main.c",
161  0
162};
163
164/*
165 *  Do a stat on a single file and report the status.
166 */
167
168void stat_a_file(
169  const char *file
170)
171{
172  int         status;
173  struct stat statbuf;
174  int         major1;
175  int         minor1;
176  int         major2;
177  int         minor2;
178
179
180  assert( file );
181
182  printf( "stat( %s ) returned ", file );
183  fflush( stdout );
184
185  status = stat( file, &statbuf );
186
187  if ( status == -1 ) {
188    printf( ": %s\n", strerror( errno ) );
189  } else {
190
191    rtems_filesystem_split_dev_t( statbuf.st_dev, major1, minor1 );
192    rtems_filesystem_split_dev_t( statbuf.st_rdev, major2, minor2 );
193
194
195    printf("\n   st_dev     (0x%x:0x%x)\n", major1, minor1 );
196    printf(  "   st_ino     %x\n", statbuf.st_ino );
197    printf(  "   st_mode    %o\n", statbuf.st_mode );
198    printf(  "   st_nlink   %x\n", statbuf.st_nlink );
199    printf(  "   st_uid     %d\n", statbuf.st_uid );
200    printf(  "   st_gid     %d\n", statbuf.st_gid );
201    printf(  "   st_rdev    (0x%x:0x%x)\n", major2, minor2 );
202    printf(  "   st_size    %d\n",(unsigned int) statbuf.st_size );
203    printf(  "   st_atime   %s", ctime( &statbuf.st_atime ) );
204    printf(  "   st_mtime   %s", ctime( &statbuf.st_mtime ) );
205    printf(  "   st_ctime   %s", ctime( &statbuf.st_ctime ) );
206#if defined(__svr4__) && !defined(__PPC__) && !defined(__sun__)
207    printf(  "   st_blksize %x\n", statbuf.st_blksize );
208    printf(  "   st_blocks  %x\n", statbuf.st_blocks );
209#endif
210
211  }
212}
213
214/*
215 *  stat() multiple files at a time
216 */
217
218void stat_multiple_files(
219  char **files
220)
221{
222  int    i;
223
224  i = 0;
225  while ( files[i] ) {
226    stat_a_file( files[i] );
227    i++;
228  }
229}
230
231/*
232 *  chown() multiple files at a time
233 */
234void chown_multiple_files(
235  char **files
236)
237{
238  int    i;
239  uid_t  st_uid;
240  gid_t  st_gid;
241
242#if defined(RTEMS_POSIX_API)
243  st_uid = geteuid();
244  st_gid = getegid();
245#else
246  st_uid = 100;
247  st_gid = 0;
248#endif
249
250  i = 0;
251  while ( files[i] ) {
252    printf("Change group of %s\n", files[i]);
253    chown( files[i], st_uid, (st_gid+1) );
254    stat_a_file( files[i] );
255
256    printf("Change owner of %s\n", files[i]);
257    chown( files[i], (st_uid+1), st_gid );
258    stat_a_file( files[i] );
259    i++;
260  }
261
262}
263
264
265
266/*
267 *  mknod() multiple files at a time
268 */
269
270void make_multiple_files(
271  char **files
272)
273{
274  int    i;
275  int    status;
276
277  i = 0;
278  while ( files[i] ) {
279    printf( "Making file %s\n", files[i] );
280    status = mknod( files[i], ( S_IFREG | S_IROTH|S_IWOTH ), 0LL );
281    assert( !status );
282    i++;
283  }
284  puts( "" );
285}
286
287void make_multiple_bad_files(
288  char **files
289)
290{
291  int    i;
292  int    status;
293
294  i = 0;
295  while ( files[i] ) {
296    printf( "Making file %s ", files[i] );
297    status = mknod( files[i], ( S_IFREG | S_IROTH|S_IWOTH ), 0LL );
298    assert( status );
299    printf( ": %s\n", strerror( errno ) );
300    i++;
301  }
302  puts( "" );
303}
304
305void make_multiple_links(
306  char **existing,
307  char **new
308)
309{
310  int    i;
311  int    status;
312
313  i = 0;
314  while ( new[i] && existing[i] ) {
315    printf( "Making file %s\n", new[i] );
316    status = link( existing[i], new[i] );
317    assert( !status );
318    i++;
319  }
320  puts( "" );
321
322  status = link( "fred", "bob" );
323  assert( status == -1 );
324
325  status = link( existing[1], "doug/bob" );
326  assert( status == -1 );
327}
328
329
330void make_too_many_links()
331{
332  int    i;
333  int    status;
334  char   name [20];
335
336  status = mkdir("/dummy", S_IRWXU );
337  assert( status == 0 );
338
339  for (i=1; i<= LINK_MAX; i++) {
340
341    sprintf(name,"/LinkName%d",i);
342    printf( "Making file %s\n", name );
343    status = link("/dummy" , name );
344    if( i < LINK_MAX )
345       assert( !status );
346    else
347       assert( status == -1 );
348
349  }
350}
351
352
353void make_a_symlink(
354  char *existing,
355  char *new
356)
357{
358  int    status;
359  char   buf[100];
360  int    len;
361
362  memset( buf, 0, 100 );
363
364  printf( "Making file %s\n", new );
365  status = symlink( existing, new );
366  assert( !status );
367
368  printf( "Verify with readlink\n");
369  status = readlink( new, buf, 100 );
370  len = strlen( existing );
371  assert ( status == len );
372
373  status = readlink( new, buf, 3 );
374  len = strlen( existing );
375  if (len < 3 )
376    assert( status == len );
377  else
378    assert( status == 3 );
379
380  status = strcmp( existing, buf );
381  assert( !status );
382}
383
384void make_multiple_symlinks()
385{
386 int  status;
387
388 make_a_symlink( Files[0],             SymLinks[0] );
389 make_a_symlink( Directories[0],       SymLinks[1] );
390 make_a_symlink( Links_to_dirlinks[0], SymLinks[2] );
391 make_a_symlink( "No_File",            SymLinks[3] );
392 make_a_symlink( SymLinks[1],          SymLinks[4] );
393 make_a_symlink( "//links",            "/symlinks/links"  );
394
395 stat_a_file( SymLinks[0] );
396 stat_a_file( SymLinks[1] );
397 stat_a_file( SymLinks[2] );
398 stat_a_file( SymLinks[3] );
399 stat_a_file( SymLinks[4] );
400
401 status = symlink(  "//links", "bob/frank" );
402 assert (status == -1);
403
404}
405/*
406void make_too_many_symlinks()
407{
408  int  i, status;
409  char name1[8];
410
411  for (i=1; i <= MAXSYMLINK; i++) {
412    sprintf( name1, "SymLink%d", i );
413    status = symlink( "/dummy", name1 );
414    if( i < MAXSYMLINK )
415       assert( !status );
416    else
417       assert( status == -1 );
418  }
419}
420*/
421void make_many_symlinks(
422  char  *real_file,
423  int    link_count
424)
425{
426  int  i;
427  char name1[5];
428  char name2[5];
429  char *link_file;
430
431  link_file = real_file;
432  for (i=1; i < link_count; i++) {
433    sprintf( name1, "%d", i );
434    make_a_symlink( link_file, name1 );
435    strcpy( name2, name1 );
436    link_file = name2;
437  }
438
439  for (i=1; i < link_count; i++) {
440    sprintf( name1, "%d", i );
441    stat_a_file( name1 );
442  }
443
444}
445
446/*
447 *  mkdir() multiple directories at a time
448 */
449 
450void make_multiple_directories(
451  char **files
452)
453{
454  int    i;
455  int    status;
456
457  i = 0;
458  while ( files[i] ) {
459    printf( "Making directory %s\n", files[i] );
460    status = mkdir( files[i], S_IRWXU );
461    assert( !status );
462    i++;
463  }
464  puts( "" );
465
466
467/*
468 * Cause faults.
469 */
470
471
472void Cause_faults()
473{
474  int                                   fd;
475  int                                   status;
476  char                                  longer_name[100];
477  rtems_filesystem_mount_table_entry_t *mt_entry;
478
479  /*
480   * Verify chmod with an invalid type.
481   */
482
483  printf("\n\nPass an invalid mode to chmod should fail with EPERM \n" );
484  status = chmod( Files[0], S_IFREG );
485  assert( status == -1 );
486  assert( errno == EPERM );
487
488  /*
489   * Try to chdir to a file.
490   */
491
492  printf("chdir to a file should fail with ENOTDIR\n");
493  status = chdir( Files[0] );
494  assert( status == -1 );
495  assert( errno == ENOTDIR );
496
497  /*
498   * Change mode to read/write on a directory.
499   * Verify directory works properly.
500   */
501
502  printf("Verify RWX permission on %s via access\n", Directories[0]);
503  status = access( Directories[0], ( R_OK | W_OK | X_OK )  );
504  assert( status == 0 );
505
506  printf( "chmod of %s to Read/Write\n", Directories[0] );
507  status = chmod( Directories[0], (S_IXGRP | S_IXOTH) );
508  assert( status == 0 );
509
510  printf( "chmod fred should fail with ENOENT\n" );
511  status = chmod( "fred", (S_IXGRP | S_IXOTH) );
512  assert( status == -1 );
513  assert( errno == ENOENT );
514
515  strcpy(longer_name, Directories[0] );
516  strcat(longer_name, "/BADNAME" );
517  printf( "Create under %s should fail with EACCES\n", Directories[0] );
518  status = mkdir( longer_name , S_IRWXU );
519  assert( status == -1 );
520  assert( errno == EACCES );
521
522  printf("chdir to %s should fail with EACCES\n", Directories[4] );
523  status = chdir( Directories[4] );
524  assert( status == -1 );
525  assert( errno == EACCES );
526
527  /*
528   * Check stat with a NULL buffer.
529   */
530
531  printf("Stat with a NULL buffer should fail with EFAULT\n");
532  status = stat( Directories[0], NULL );
533  assert( status == -1 );
534  assert( errno == EFAULT );
535
536  /*
537   * Set current to a directory with no owner permissions.
538   * Verify it works properly.
539   */
540
541  printf( "\n\nchmod of %s to Read/Write\n", Directories[0] );
542  status = chmod( Directories[0], (S_IXGRP | S_IXOTH) );
543  assert( status == 0 );
544
545  printf("mkdir %s should fail with EACCESS\n", longer_name );
546  status = mkdir( longer_name , S_IRWXU );
547  assert( status == -1 );
548  assert( errno == EACCES );
549
550  printf("\n%s Should exist ( access )\n",Directories[0] );
551  status = access( Directories[0], F_OK );
552  assert( status == 0 );
553  printf("\n%s Should have read  permission( access )\n",Directories[0] );
554  status = access( Directories[0], R_OK );
555  assert( status != 0 );
556  printf("\n%s Should have write permission( access )\n",Directories[0] );
557  status = access( Directories[0], W_OK );
558  assert( status != 0 );
559  printf("\n%s Should not have execute permission( access )\n",Directories[0] );
560  status = access( Directories[0], X_OK );
561  assert( status != 0 );
562 
563  printf("\nRestore %s to RWX\n",Directories[0] );
564  status = chmod( Directories[0], S_IRWXU );
565  assert( status == 0 );
566
567  printf("chdir to / \n");
568  status = chdir( "/" );
569  assert( status == 0 );
570
571  /*
572   * Remove one of the directories.
573   * Verify links to the removed directory still work.
574   */
575
576  printf( "Remove %s\n", Directories[5] );
577  status = rmdir( Directories[5] );
578  assert( status == 0 );
579
580  stat_a_file( Directories[5] );
581  status = access( Directories[5], F_OK );
582  assert( status != 0 );
583
584  stat_a_file( Links_to_Dirs[5] );
585  status = readlink( Links_to_Dirs[5], longer_name, 3 );
586  assert( status == -1 );
587  assert( errno == EINVAL );
588
589  stat_a_file( Links_to_dirlinks[5] );
590  printf("Chdir to %s\n", Links_to_Dirs[5] );
591  status = chdir( Links_to_Dirs[5] );
592  assert( status == 0 );
593
594  /*
595   * Verify we cannot move up from a node with no parent node.
596   */
597
598  printf("Chdir to .. should fail with ENOENT\n" );
599  status = chdir( ".." );
600  assert( status == -1 );
601  assert( errno == ENOENT );
602
603  /*
604   * Create a subdirectory under the dangling node.
605   */
606
607  printf("mkdir ../t should fail with ENOENT\n" );
608  status = mkdir( "../t" , S_IRWXU );
609  assert( status == -1 );
610  assert( errno == ENOENT );
611
612  printf("mkdir t\n");
613  status = mkdir( "t" , S_IRWXU );
614  assert( status == 0 );
615
616  printf("chdir to / \n");
617  status = chdir( "/" );
618  assert( status == 0 );
619
620  /*
621   * Check rmdir, rmnod, and unlink
622   */
623
624  printf("rmdir %s should fail with ENOTDIR\n", Links_to_Dirs[5] );
625  status = rmdir( Links_to_Dirs[5] );
626  assert( status == -1 );
627  assert( errno == ENOTDIR );
628
629  printf("unlink %s\n", Links_to_Dirs[5] );
630  status = unlink( Links_to_Dirs[5] );
631  assert( status == 0 );
632
633  printf("unlink %s should fail with ENOTEMPTY\n", Links_to_dirlinks[5] );
634  status = unlink(  Links_to_dirlinks[5] );
635  assert( status == -1 );
636  assert( errno == ENOTEMPTY );
637
638  strcpy( longer_name,  Links_to_dirlinks[5] );
639  strcat( longer_name, "/t");
640  printf("rmdir %s\n", longer_name );
641  status = rmdir( longer_name );
642  assert( status == 0 );
643
644  printf("unlink %s\n", Links_to_Dirs[5]);
645  status = unlink( Links_to_dirlinks[5] );
646  assert( status == 0 );
647
648  status = chdir( Directories[0] );
649  status = mkdir ( "my_mount_point", S_IRWXU );
650  assert( status == 0 );
651
652  printf("Attempting to mount IMFS file system at /dir1/my_mount_point \n");
653  status = mount(
654     &mt_entry,
655     &IMFS_ops,
656     "RW",
657     NULL,
658     "/dir1/my_mount_point" );
659  assert( status == 0 );
660
661  printf("rmdir /dir1/my_mount_point should fail with EBUSY\n");
662  status = rmdir ("/dir1/my_mount_point" );
663  assert( status == -1 );
664  assert( errno == EBUSY );
665
666  printf( "Unmount /dir1/my_mount_point\n");
667  status = unmount( "/dir1/my_mount_point" );
668  assert( status == 0 );
669
670  /*
671   * Verify write permission is checked.
672   */
673
674  printf("chmod of %s to group and other execute\n", Files[0] );
675  status = chmod (Files[0], (S_IXGRP | S_IXOTH) );
676  assert( status == 0 );
677
678  printf("Open %s for write should fail with EACCES\n", Files[0] );
679  fd = open (Files[0], O_WRONLY);
680  assert( fd == -1 );
681  assert( errno == EACCES );
682
683  printf("chmod of %s to User Execute and Read\n", Directories[3] );
684  status = chmod (Directories[3], (S_IXUSR | S_IRUSR) );
685  assert( status == 0 );
686  strcpy(longer_name, Directories[3] );
687  strcat(longer_name, "/NewFile" );
688  printf("Mkdir of %s should fail with EACCES\n",longer_name );
689  status = mkdir( longer_name, S_IRWXU );
690  assert( status != 0 );
691  assert( errno == EACCES );
692
693  printf(" Making too many hard links.\n" );
694  make_too_many_links( );
695
696  printf( "pass fstat a null pointer should fail with EFAULT\n");
697  status = fstat( fd, NULL );
698  assert( status == -1 );
699  assert( errno == EFAULT);
700
701  /*
702   * The current directory MUST be restored at the end of this test.
703   */
704
705  printf("chdir to / \n");
706  status = chdir( "/" );
707  assert( status == 0 );
708
709}
710
711void Show_Time()
712{
713  rtems_time_of_day time;
714  rtems_status_code status;
715
716  status = rtems_clock_get( RTEMS_CLOCK_GET_TOD, &time );
717  printf(">>>>Current Time: ");
718  print_time( " - rtems_clock_get - ", &time, "\n" );
719}
720
721/*
722 *  main entry point to the test
723 */
724
725#if defined(__rtems__)
726int test_main(void)
727#else
728int main(
729  int    argc,
730  char **argv
731)
732#endif
733{
734  rtems_status_code status;
735  rtems_time_of_day time;
736
737  puts( "\n\n*** STAT TEST 01 ***" );
738
739  build_time( &time, 12, 31, 1988, 9, 0, 0, 0 );
740  status = rtems_clock_set( &time );
741
742  /*
743   *  Create the files and directories for the test.
744   */
745  Show_Time();
746
747  make_multiple_directories( Directories );
748  make_multiple_files( Files );
749  make_multiple_links( Directories,    Links_to_Dirs );
750  make_multiple_links( Files,          Links_to_Files );
751
752  status = rtems_task_wake_after( 5 * TICKS_PER_SECOND );
753  make_multiple_links( Links_to_Dirs,  Links_to_dirlinks );
754  status = rtems_task_wake_after( 5 * TICKS_PER_SECOND );
755  make_multiple_links( Links_to_Files, Links_to_filelinks );
756
757  status = rtems_task_wake_after( 5 * TICKS_PER_SECOND );
758
759  /*
760   *  Now go through all the absolute path.
761   */
762
763  puts( "Doing the stat() on all the good absolute paths" );
764  stat_multiple_files( Good_absolute_paths );
765
766  /*
767   *  run through the relative paths.
768   */
769
770  puts( "\nDoing the stat() on all the good relative paths" );
771  stat_multiple_files( Good_relative_paths );
772
773  /*
774   * Change directory and releative paths are now bad.
775   */
776
777  puts("\nchdir to dev");
778  chdir("dev");
779  puts("\nstat relative paths that are now bad");
780  stat_multiple_files( Good_relative_paths );
781
782  /*
783   * Change directory to the link directory and follow links.
784   */
785
786  puts("\nchdir to ../links");
787  chdir("../links");
788  puts("Doing the stat() on good links\n");
789  stat_multiple_files( Links_to_Dirs );
790  stat_multiple_files( Links_to_Files );
791  stat_multiple_files( Links_to_dirlinks  );
792  stat_multiple_files( Links_to_filelinks );
793 
794  /*
795   * Chmod on dir1/dir1.  This allows the error path to be hit.
796   */
797
798  printf( "chmod of %s to Read/Write\n", Directories[4] );
799  chmod( Directories[4], (S_IROTH|S_IWOTH) );
800  puts( "\nDoing the stat() on all the bad paths" );
801
802  stat_multiple_files( Bad_paths );
803  make_multiple_bad_files( Bad_paths );
804
805  printf( "Return %s to RWX\n", Directories[4] );
806  chmod( Directories[4], S_IRWXU );
807
808
809  /*
810   * Check out symbolic links.
811   */
812
813  make_multiple_symlinks();
814  make_many_symlinks( "/symlinks", 10 );
815
816  status = rtems_task_wake_after( 5 * TICKS_PER_SECOND );
817  Cause_faults();
818
819  status = rtems_task_wake_after( 5 * TICKS_PER_SECOND );
820  chown_multiple_files( Files );
821
822  status = rtems_task_wake_after( 5 * TICKS_PER_SECOND );
823  chown_multiple_files( Links_to_Dirs );
824 
825  puts( "\n\n*** END OF STAT TEST 01 ***" );
826  exit(0);
827}
828
829
830
831
832
833
834
Note: See TracBrowser for help on using the repository browser.