source: rtems/c/src/tests/psxtests/psxstat/test.c @ c5fe7cf6

Last change on this file since c5fe7cf6 was c5fe7cf6, checked in by Joel Sherrill <joel.sherrill@…>, on Jun 9, 2000 at 6:45:23 PM

Patch rtems-rc-4.5.0-27-cvs from Ralf Corsepius <corsepiu@…>
to remove syntactically incorrect else code that was technically
unneeded anyway.

  • Property mode set to 100644
File size: 17.7 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-1999.
6 *  On-Line Applications Research Corporation (OAR).
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *  http://www.OARcorp.com/rtems/license.html.
11 *
12 *  $Id$
13 */
14
15#include <tmacros.h>
16#include <assert.h>
17#include <sys/stat.h>
18#include <fcntl.h>
19#include <limits.h>
20#include <stdio.h>
21#include <unistd.h>
22#include <errno.h>
23#include <string.h>
24#include <rtems.h>
25#include <rtems/libio.h>
26
27#define MAXSYMLINK 5   /* There needs to be a better way of getting this. */
28
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\tst_dev     (0x%x:0x%x)\n", major1, minor1 );
196    printf(  "...st_ino     %x\n", (int) 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  st_uid = geteuid();
243  st_gid = getegid();
244
245  i = 0;
246  while ( files[i] ) {
247    printf("Change group of %s\n", files[i]);
248    chown( files[i], st_uid, (st_gid+1) );
249    stat_a_file( files[i] );
250
251    printf("Change owner of %s\n", files[i]);
252    chown( files[i], (st_uid+1), st_gid );
253    stat_a_file( files[i] );
254    i++;
255  }
256
257}
258
259
260
261/*
262 *  mknod() multiple files at a time
263 */
264
265void make_multiple_files(
266  char **files
267)
268{
269  int    i;
270  int    status;
271
272  i = 0;
273  while ( files[i] ) {
274    printf( "Making file %s\n", files[i] );
275    status = mknod( files[i], ( S_IFREG | S_IROTH|S_IWOTH ), 0LL );
276    assert( !status );
277    i++;
278  }
279  puts( "" );
280}
281
282void make_multiple_bad_files(
283  char **files
284)
285{
286  int    i;
287  int    status;
288
289  i = 0;
290  while ( files[i] ) {
291    printf( "Making file %s ", files[i] );
292    status = mknod( files[i], ( S_IFREG | S_IROTH|S_IWOTH ), 0LL );
293    assert( status );
294    printf( ": %s\n", strerror( errno ) );
295    i++;
296  }
297  puts( "" );
298}
299
300void make_multiple_links(
301  char **existing,
302  char **new
303)
304{
305  int    i;
306  int    status;
307
308  i = 0;
309  while ( new[i] && existing[i] ) {
310    printf( "Making file %s\n", new[i] );
311    status = link( existing[i], new[i] );
312    assert( !status );
313    i++;
314  }
315  puts( "" );
316
317  status = link( "fred", "bob" );
318  assert( status == -1 );
319
320  status = link( existing[1], "doug/bob" );
321  assert( status == -1 );
322}
323
324
325void make_too_many_links()
326{
327  int    i;
328  int    status;
329  char   name [20];
330
331  status = mkdir("/dummy", S_IRWXU );
332  assert( status == 0 );
333
334  for (i=1; i<= LINK_MAX; i++) {
335
336    sprintf(name,"/LinkName%d",i);
337    printf( "Making file %s\n", name );
338    status = link("/dummy" , name );
339    if( i < LINK_MAX )
340       assert( !status );
341    else
342       assert( status == -1 );
343
344  }
345}
346
347
348void make_a_symlink(
349  char *existing,
350  char *new
351)
352{
353  int    status;
354  char   buf[100];
355  int    len;
356
357  memset( buf, 0, 100 );
358
359  printf( "Making file %s\n", new );
360  status = symlink( existing, new );
361  assert( !status );
362
363  printf( "Verify with readlink\n");
364  status = readlink( new, buf, 100 );
365  len = strlen( existing );
366  assert ( status == len );
367
368  status = readlink( new, buf, 3 );
369  len = strlen( existing );
370  if (len < 3 )
371    assert( status == len );
372  else
373    assert( status == 3 );
374
375  status = strcmp( existing, buf );
376  assert( !status );
377}
378
379void make_multiple_symlinks()
380{
381 int  status;
382
383 make_a_symlink( Files[0],             SymLinks[0] );
384 make_a_symlink( Directories[0],       SymLinks[1] );
385 make_a_symlink( Links_to_dirlinks[0], SymLinks[2] );
386 make_a_symlink( "No_File",            SymLinks[3] );
387 make_a_symlink( SymLinks[1],          SymLinks[4] );
388 make_a_symlink( "//links",            "/symlinks/links"  );
389
390 stat_a_file( SymLinks[0] );
391 stat_a_file( SymLinks[1] );
392 stat_a_file( SymLinks[2] );
393 stat_a_file( SymLinks[3] );
394 stat_a_file( SymLinks[4] );
395
396 status = symlink(  "//links", "bob/frank" );
397 assert (status == -1);
398
399}
400/*
401void make_too_many_symlinks()
402{
403  int  i, status;
404  char name1[8];
405
406  for (i=1; i <= MAXSYMLINK; i++) {
407    sprintf( name1, "SymLink%d", i );
408    status = symlink( "/dummy", name1 );
409    if( i < MAXSYMLINK )
410       assert( !status );
411    else
412       assert( status == -1 );
413  }
414}
415*/
416void make_many_symlinks(
417  char  *real_file,
418  int    link_count
419)
420{
421  int  i;
422  char name1[5];
423  char name2[5];
424  char *link_file;
425
426  link_file = real_file;
427  for (i=1; i < link_count; i++) {
428    sprintf( name1, "%d", i );
429    make_a_symlink( link_file, name1 );
430    strcpy( name2, name1 );
431    link_file = name2;
432  }
433
434  for (i=1; i < link_count; i++) {
435    sprintf( name1, "%d", i );
436    stat_a_file( name1 );
437  }
438
439}
440
441/*
442 *  mkdir() multiple directories at a time
443 */
444 
445void make_multiple_directories(
446  char **files
447) 
448{
449  int    i;
450  int    status;
451
452  i = 0; 
453  while ( files[i] ) {
454    printf( "Making directory %s\n", files[i] );
455    status = mkdir( files[i], S_IRWXU );
456    assert( !status );
457    i++;
458  }
459  puts( "" );
460} 
461
462/*
463 * Cause faults.
464 */
465
466
467void Cause_faults()
468{
469  int                                   fd;
470  int                                   status;
471  char                                  longer_name[100];
472  rtems_filesystem_mount_table_entry_t *mt_entry;
473
474  /*
475   * Verify chmod with an invalid type.
476   */
477
478  printf("\n\nPass an invalid mode to chmod should fail with EPERM \n" );
479  status = chmod( Files[0], S_IFREG );
480  assert( status == -1 ); 
481  assert( errno == EPERM );
482
483  /*
484   * Try to chdir to a file.
485   */
486
487  printf("chdir to a file should fail with ENOTDIR\n");
488  status = chdir( Files[0] );
489  assert( status == -1 );
490  assert( errno == ENOTDIR );
491
492  /*
493   * Change mode to read/write on a directory.
494   * Verify directory works properly.
495   */
496
497  printf("Verify RWX permission on %s via access\n", Directories[0]);
498  status = access( Directories[0], ( R_OK | W_OK | X_OK )  );
499  assert( status == 0 );
500
501  printf( "chmod of %s to Read/Write\n", Directories[0] );
502  status = chmod( Directories[0], (S_IXGRP | S_IXOTH) );
503  assert( status == 0 );
504
505  printf( "chmod fred should fail with ENOENT\n" );
506  status = chmod( "fred", (S_IXGRP | S_IXOTH) );
507  assert( status == -1 );
508  assert( errno == ENOENT );
509
510  strcpy(longer_name, Directories[0] );
511  strcat(longer_name, "/BADNAME" );
512  printf( "Create under %s should fail with EACCES\n", Directories[0] );
513  status = mkdir( longer_name , S_IRWXU );
514  assert( status == -1 );
515  assert( errno == EACCES );
516
517  printf("chdir to %s should fail with EACCES\n", Directories[4] );
518  status = chdir( Directories[4] );
519  assert( status == -1 );
520  assert( errno == EACCES );
521
522  /*
523   * Check stat with a NULL buffer.
524   */
525
526  printf("Stat with a NULL buffer should fail with EFAULT\n");
527  status = stat( Directories[0], NULL );
528  assert( status == -1 );
529  assert( errno == EFAULT );
530
531  /*
532   * Set current to a directory with no owner permissions.
533   * Verify it works properly.
534   */
535
536  printf( "\n\nchmod of %s to Read/Write\n", Directories[0] );
537  status = chmod( Directories[0], (S_IXGRP | S_IXOTH) );
538  assert( status == 0 );
539
540  printf("mkdir %s should fail with EACCESS\n", longer_name );
541  status = mkdir( longer_name , S_IRWXU );
542  assert( status == -1 );
543  assert( errno == EACCES );
544
545  printf("\n%s Should exist ( access )\n",Directories[0] );
546  status = access( Directories[0], F_OK );
547  assert( status == 0 );
548  printf("\n%s Should have read  permission( access )\n",Directories[0] );
549  status = access( Directories[0], R_OK );
550  assert( status != 0 );
551  printf("\n%s Should have write permission( access )\n",Directories[0] );
552  status = access( Directories[0], W_OK );
553  assert( status != 0 );
554  printf("\n%s Should not have execute permission( access )\n",Directories[0] );
555  status = access( Directories[0], X_OK );
556  assert( status != 0 );
557 
558  printf("\nRestore %s to RWX\n",Directories[0] );
559  status = chmod( Directories[0], S_IRWXU );
560  assert( status == 0 );
561
562  printf("chdir to / \n");
563  status = chdir( "/" );
564  assert( status == 0 );
565
566  /*
567   * Remove one of the directories.
568   * Verify links to the removed directory still work.
569   */
570
571  printf( "Remove %s\n", Directories[5] );
572  status = rmdir( Directories[5] );
573  assert( status == 0 );
574
575  stat_a_file( Directories[5] );
576  status = access( Directories[5], F_OK );
577  assert( status != 0 );
578
579  stat_a_file( Links_to_Dirs[5] );
580  status = readlink( Links_to_Dirs[5], longer_name, 3 );
581  assert( status == -1 );
582  assert( errno == EINVAL );
583
584  stat_a_file( Links_to_dirlinks[5] );
585  printf("Chdir to %s\n", Links_to_Dirs[5] );
586  status = chdir( Links_to_Dirs[5] );
587  assert( status == 0 );
588
589  /*
590   * Verify we cannot move up from a node with no parent node.
591   */
592
593  printf("Chdir to .. should fail with ENOENT\n" );
594  status = chdir( ".." );
595  assert( status == -1 );
596  assert( errno == ENOENT );
597
598  /*
599   * Create a subdirectory under the dangling node.
600   */
601
602  printf("mkdir ../t should fail with ENOENT\n" );
603  status = mkdir( "../t" , S_IRWXU );
604  assert( status == -1 );
605  assert( errno == ENOENT );
606
607  printf("mkdir t\n");
608  status = mkdir( "t" , S_IRWXU );
609  assert( status == 0 );
610
611  printf("chdir to / \n");
612  status = chdir( "/" );
613  assert( status == 0 );
614
615  /*
616   * Check rmdir, rmnod, and unlink
617   */
618
619  printf("rmdir %s should fail with ENOTDIR\n", Links_to_Dirs[5] );
620  status = rmdir( Links_to_Dirs[5] );
621  assert( status == -1 );
622  assert( errno == ENOTDIR );
623
624  printf("unlink %s\n", Links_to_Dirs[5] );
625  status = unlink( Links_to_Dirs[5] );
626  assert( status == 0 );
627
628  printf("unlink %s should fail with ENOTEMPTY\n", Links_to_dirlinks[5] );
629  status = unlink(  Links_to_dirlinks[5] );
630  assert( status == -1 );
631  assert( errno == ENOTEMPTY );
632
633  strcpy( longer_name,  Links_to_dirlinks[5] );
634  strcat( longer_name, "/t");
635  printf("rmdir %s\n", longer_name );
636  status = rmdir( longer_name );
637  assert( status == 0 );
638
639  printf("unlink %s\n", Links_to_Dirs[5]);
640  status = unlink( Links_to_dirlinks[5] );
641  assert( status == 0 );
642
643  status = chdir( Directories[0] );
644  status = mkdir ( "my_mount_point", S_IRWXU );
645  assert( status == 0 );
646
647  printf("Attempting to mount IMFS file system at /dir1/my_mount_point \n");
648  status = mount(
649     &mt_entry,
650     &IMFS_ops,
651     RTEMS_FILESYSTEM_READ_WRITE,
652     NULL,
653     "/dir1/my_mount_point" );
654  assert( status == 0 );
655
656  printf("rmdir /dir1/my_mount_point should fail with EBUSY\n");
657  status = rmdir ("/dir1/my_mount_point" );
658  assert( status == -1 );
659  assert( errno == EBUSY );
660
661  printf( "Unmount /dir1/my_mount_point\n");
662  status = unmount( "/dir1/my_mount_point" );
663  assert( status == 0 );
664
665  /*
666   * Verify write permission is checked.
667   */
668
669  printf("chmod of %s to group and other execute\n", Files[0] );
670  status = chmod (Files[0], (S_IXGRP | S_IXOTH) );
671  assert( status == 0 );
672
673  printf("Open %s for write should fail with EACCES\n", Files[0] );
674  fd = open (Files[0], O_WRONLY); 
675  assert( fd == -1 );
676  assert( errno == EACCES );
677
678  printf("chmod of %s to User Execute and Read\n", Directories[3] );
679  status = chmod (Directories[3], (S_IXUSR | S_IRUSR) );
680  assert( status == 0 );
681  strcpy(longer_name, Directories[3] );
682  strcat(longer_name, "/NewFile" );
683  printf("Mkdir of %s should fail with EACCES\n",longer_name );
684  status = mkdir( longer_name, S_IRWXU );
685  assert( status != 0 );
686  assert( errno == EACCES );
687
688  printf(" Making too many hard links.\n" );
689  make_too_many_links( );
690
691  printf( "pass fstat a null pointer should fail with EFAULT\n");
692  status = fstat( fd, NULL );
693  assert( status == -1 ); 
694  assert( errno == EFAULT);
695
696  /*
697   * The current directory MUST be restored at the end of this test.
698   */
699
700  printf("chdir to / \n");
701  status = chdir( "/" );
702  assert( status == 0 );
703
704}
705
706void Show_Time()
707{
708  rtems_time_of_day time;
709  rtems_status_code status;
710
711  status = rtems_clock_get( RTEMS_CLOCK_GET_TOD, &time );
712  printf(">>>>Current Time: ");
713  print_time( " - rtems_clock_get - ", &time, "\n" );
714}
715
716/*
717 *  main entry point to the test
718 */
719
720#if defined(__rtems__)
721int test_main(void)
722#else
723int main(
724  int    argc,
725  char **argv
726)
727#endif
728{
729  rtems_status_code status;
730  rtems_time_of_day time;
731
732  puts( "\n\n*** STAT TEST 01 ***" );
733
734  build_time( &time, 12, 31, 1988, 9, 0, 0, 0 );
735  status = rtems_clock_set( &time );
736
737  /*
738   *  Create the files and directories for the test.
739   */
740  Show_Time();
741
742  make_multiple_directories( Directories );
743  make_multiple_files( Files );
744  make_multiple_links( Directories,    Links_to_Dirs );
745  make_multiple_links( Files,          Links_to_Files );
746
747  status = rtems_task_wake_after( 5 * TICKS_PER_SECOND );
748  make_multiple_links( Links_to_Dirs,  Links_to_dirlinks );
749  status = rtems_task_wake_after( 5 * TICKS_PER_SECOND );
750  make_multiple_links( Links_to_Files, Links_to_filelinks );
751
752  status = rtems_task_wake_after( 5 * TICKS_PER_SECOND );
753
754  /*
755   *  Now go through all the absolute path.
756   */
757
758  puts( "Doing the stat() on all the good absolute paths" );
759  stat_multiple_files( Good_absolute_paths );
760
761  /*
762   *  run through the relative paths.
763   */
764
765  puts( "\nDoing the stat() on all the good relative paths" );
766  stat_multiple_files( Good_relative_paths );
767
768  /*
769   * Change directory and releative paths are now bad.
770   */
771
772  puts("\nchdir to dev");
773  chdir("dev");
774  puts("\nstat relative paths that are now bad");
775  stat_multiple_files( Good_relative_paths );
776
777  /*
778   * Change directory to the link directory and follow links.
779   */
780
781  puts("\nchdir to ../links");
782  chdir("../links");
783  puts("Doing the stat() on good links\n");
784  stat_multiple_files( Links_to_Dirs );
785  stat_multiple_files( Links_to_Files );
786  stat_multiple_files( Links_to_dirlinks  );
787  stat_multiple_files( Links_to_filelinks );
788 
789  /*
790   * Chmod on dir1/dir1.  This allows the error path to be hit.
791   */
792
793  printf( "chmod of %s to Read/Write\n", Directories[4] );
794  chmod( Directories[4], (S_IROTH|S_IWOTH) );
795  puts( "\nDoing the stat() on all the bad paths" );
796
797  stat_multiple_files( Bad_paths );
798  make_multiple_bad_files( Bad_paths );
799
800  printf( "Return %s to RWX\n", Directories[4] );
801  chmod( Directories[4], S_IRWXU );
802
803
804  /*
805   * Check out symbolic links.
806   */
807
808  make_multiple_symlinks();
809  make_many_symlinks( "/symlinks", 10 );
810
811  status = rtems_task_wake_after( 5 * TICKS_PER_SECOND );
812  Cause_faults();
813
814  status = rtems_task_wake_after( 5 * TICKS_PER_SECOND );
815  chown_multiple_files( Files );
816
817  status = rtems_task_wake_after( 5 * TICKS_PER_SECOND );
818  chown_multiple_files( Links_to_Dirs );
819 
820  puts( "\n\n*** END OF STAT TEST 01 ***" );
821  exit(0);
822}
823
824
825
826
827
828
829
Note: See TracBrowser for help on using the repository browser.