source: rtems/testsuites/fstests/fsrename/test.c @ 27d240e0

4.11
Last change on this file since 27d240e0 was 27d240e0, checked in by Andre Marques <andre.lousa.marques@…>, on Feb 22, 2014 at 9:57:34 PM

New fstest to check rename POSIX conformance

This patch is a newer version of the test presented on

http://www.rtems.org/pipermail/rtems-devel/2014-February/005318.html

Unchecked error cases:

  • EIO (physical error)
  • ENOSPC (no space left in the new filepath)
  • EROFS (already covered on testsuites/fstests/fsrofs01)

Untested functionality:

  • File system lock during rename() operation
  • If after rename() the link count of a file becomes 0 it should be

removed and the space ocupied by the file shall be freed and no longer
accessible (the function statvfs() gives "not implemented" on the imfs
file system, so this is postponed for now)

  • Property mode set to 100644
File size: 25.2 KB
Line 
1/*
2 *  COPYRIGHT (c) 2014
3 *  Andre Marques <andre.lousa.marques at gmail.com>
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.org/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11  #include "config.h"
12#endif
13
14#include "fstest.h"
15#include "fs_config.h"
16#include "pmacros.h"
17
18#include <sys/stat.h>
19#include <fcntl.h>
20#include <stdio.h>
21#include <string.h>
22#include <unistd.h>
23#include <errno.h>
24#include <limits.h>
25
26const char rtems_test_name[] = "FSRENAME " FILESYSTEM;
27
28void test_initialize_filesystem (void);
29void test_shutdown_filesystem (void);
30
31static void symbolic_link_test (void)
32{
33  int fd;
34  int status;
35
36  const char *name01 = "name01";
37  const char *name02 = "name02";
38
39  const char *symlink01 = "slink01";
40  const char *symlink02 = "slink02";
41
42  char path01[20];
43
44  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
45  const char *wd = __func__;
46
47  struct stat statbuf;
48
49  /*
50   * Create a new directory and change the current directory to this
51   */
52
53  status = mkdir (wd, mode);
54  rtems_test_assert (status == 0);
55  status = chdir (wd);
56  rtems_test_assert (status == 0);
57
58  /*
59   * The new argument points to a file and
60   * the old argument points to a symbolic link to another file.
61   */
62
63  puts ("\nOld is a simbolic link and rename operates on the simbolic link itself\n");
64
65  fd = creat (name01, mode);
66  rtems_test_assert (fd >= 0);
67  status = close (fd);
68  rtems_test_assert (status == 0);
69
70  fd = creat (name02, mode);
71  rtems_test_assert (fd >= 0);
72  status = close (fd);
73  rtems_test_assert (status == 0);
74
75  status = symlink (name01, symlink01);
76  rtems_test_assert (status == 0);
77
78  EXPECT_EQUAL (0, rename, symlink01, name02);
79  EXPECT_EQUAL (0, lstat, name02, &statbuf);
80
81  puts ("Testing if name02 is now a symlink");
82
83  if(S_ISLNK(statbuf.st_mode) != 0)
84    FS_PASS ();
85  else
86    FS_FAIL ();
87
88  /*
89   * Clear directory
90   */
91
92  EXPECT_EQUAL (0, unlink, name01);
93  EXPECT_EQUAL (0, unlink, name02);
94  EXPECT_EQUAL (-1, unlink, symlink01);
95
96  /*
97   * The new argument points to a symbolic link to another file and
98   * the old argument points to a file.
99   */
100
101  puts ("\nNew is a simbolic link and rename operates on the simbolic link itself\n");
102
103  fd = creat (name01, mode);
104  rtems_test_assert (fd >= 0);
105  status = close (fd);
106  rtems_test_assert (status == 0);
107
108  fd = creat (name02, mode);
109  rtems_test_assert (fd >= 0);
110  status = close (fd);
111  rtems_test_assert (status == 0);
112
113  status = symlink (name01, symlink01);
114  rtems_test_assert (status == 0);
115
116  EXPECT_EQUAL (0, rename, name02, symlink01);
117  EXPECT_EQUAL (0, lstat, symlink01, &statbuf);
118
119  puts ("Testing that symlink01 is not a symlink");
120
121  if(S_ISLNK(statbuf.st_mode) == 0)
122    FS_PASS ();
123  else
124    FS_FAIL ();
125
126  /*
127   * Clear directory
128   */
129
130  EXPECT_EQUAL (0, unlink, name01);
131  EXPECT_EQUAL (-1, unlink, name02);
132  EXPECT_EQUAL (0, unlink, symlink01);
133
134  /*
135   * The new argument points to a file inside a directory and
136   * the old argument points to a file which filepath contains
137   * a symbolic link loop.
138   */
139
140  puts ("\nTesting with symbolic link loop's\n");
141
142  status = symlink (symlink01, symlink02);
143  rtems_test_assert (status == 0);
144
145  status = symlink (symlink02, symlink01);
146  rtems_test_assert (status == 0);
147
148  sprintf (path01, "%s/test", symlink01);
149  EXPECT_ERROR (ELOOP, rename, path01, name01);
150
151  sprintf (path01, "%s/test", symlink02);
152  EXPECT_ERROR (ELOOP, rename, path01, name01);
153
154  /*
155   * Clear directory
156   */
157
158  EXPECT_EQUAL (-1, unlink, name01);
159  EXPECT_EQUAL (0, unlink, symlink01);
160  EXPECT_EQUAL (0, unlink, symlink02);
161
162  /*
163   * The new argument points to a file, which filepath contains
164   * a symbolic link loop, and
165   * the old argument points to a file inside a directory
166   */
167
168  fd = creat (name01, mode);
169  rtems_test_assert (fd >= 0);
170  status = close (fd);
171  rtems_test_assert (status == 0);
172
173  status = symlink (symlink01, symlink02);
174  rtems_test_assert (status == 0);
175
176  status = symlink (symlink02, symlink01);
177  rtems_test_assert (status == 0);
178
179  sprintf (path01, "%s/test", symlink01);
180  EXPECT_ERROR (ELOOP, rename, name01, path01);
181
182  sprintf (path01, "%s/test", symlink02);
183  EXPECT_ERROR (ELOOP, rename, name01, path01);
184
185  /*
186   * Clear directory
187   */
188
189  EXPECT_EQUAL (0, unlink, name01);
190  EXPECT_EQUAL (0, unlink, symlink01);
191  EXPECT_EQUAL (0, unlink, symlink02);
192
193  /*
194   * Go back to parent directory
195   */
196
197  status = chdir ("..");
198  rtems_test_assert (status == 0);
199
200  /*
201   * Remove test directory
202   */
203
204  status = rmdir (wd);
205  rtems_test_assert (status == 0);
206}
207
208static void same_file_test (void)
209{
210  int fd;
211  int status;
212
213  const char *name01 = "name01";
214  const char *name02 = "name02";
215
216  const char *dir01 = "dir01";
217
218  char path01[30];
219
220  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
221  const char *wd = __func__;
222
223  /*
224   * Create a new directory and change the current directory to this
225   */
226
227  status = mkdir (wd, mode);
228  rtems_test_assert (status == 0);
229  status = chdir (wd);
230  rtems_test_assert (status == 0);
231
232  /*
233   * The new argument points to a file and
234   * the old argument points to the same file on the same directory.
235   */
236
237  puts ("\nRename file with itself\n");
238
239  fd = creat (name01, mode);
240  rtems_test_assert (fd >= 0);
241  status = close (fd);
242  rtems_test_assert (status == 0);
243
244  EXPECT_EQUAL (0, rename, name01, name01);
245
246  /*
247   * Clear directory
248   */
249
250  EXPECT_EQUAL (0, unlink, name01);
251
252  /*
253   * The new argument points to a file and
254   * the old argument points to the same file from another directory.
255   */
256
257  puts ("\nRename file with itself through a hard link in another directory\n");
258
259  fd = creat (name01, mode);
260  rtems_test_assert (fd >= 0);
261  status = close (fd);
262  rtems_test_assert (status == 0);
263
264  status = mkdir (dir01, mode);
265  rtems_test_assert (status == 0);
266
267  sprintf (path01, "%s/%s", dir01, name02);
268  status = link (name01, path01);
269  rtems_test_assert (status == 0);
270
271  EXPECT_EQUAL (0, rename, name01, path01);
272
273  /*
274   * Clear directory
275   */
276
277  EXPECT_EQUAL (0, unlink, name01);
278  EXPECT_EQUAL (0, unlink, path01);
279  EXPECT_EQUAL (0, rmdir, dir01);
280
281  /*
282   * Go back to parent directory
283   */
284
285  status = chdir ("..");
286  rtems_test_assert (status == 0);
287
288  /*
289   * Remove test directory
290   */
291
292  status = rmdir (wd);
293  rtems_test_assert (status == 0);
294}
295
296static void directory_test (void)
297{
298  int fd;
299  int status;
300  int i;
301
302  const char *name01 = "name01";
303  const char *name02 = "name02";
304
305  const char *dir01 = "dir01";
306  const char *dir02 = "dir02";
307
308  char path01[30];
309
310  char link_name[10];
311
312  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
313  const char *wd = __func__;
314
315  struct stat statbuf;
316
317  long LINK_MAX_val;
318
319  /*
320   * Create a new directory and change the current directory to this
321   */
322
323  status = mkdir (wd, mode);
324  rtems_test_assert (status == 0);
325  status = chdir (wd);
326  rtems_test_assert (status == 0);
327
328  /*
329   * The new argument points to a file and
330   * the old argument points to a directory.
331   */
332
333  puts ("\nRename directory with file\n");
334
335  fd = creat (name01, mode);
336  rtems_test_assert (fd >= 0);
337  status = close (fd);
338  rtems_test_assert (status == 0);
339
340  status = mkdir (dir01, mode);
341  rtems_test_assert (status == 0);
342
343  EXPECT_ERROR (ENOTDIR, rename, dir01, name01);
344
345  /*
346   * Clear directory
347   */
348
349  EXPECT_EQUAL (0, unlink, name01);
350  EXPECT_EQUAL (0, rmdir, dir01);
351
352  /*
353   * The new argument points to a directory and
354   * the old argument points to a file.
355   */
356
357  puts ("\nRename file with directory\n");
358
359  fd = creat (name01, mode);
360  rtems_test_assert (fd >= 0);
361  status = close (fd);
362  rtems_test_assert (status == 0);
363
364  status = mkdir (dir01, mode);
365  rtems_test_assert (status == 0);
366
367  EXPECT_ERROR (EISDIR, rename, name01, dir01);
368
369  /*
370   * Clear directory
371   */
372
373  EXPECT_EQUAL (0, unlink, name01);
374  EXPECT_EQUAL (0, rmdir, dir01);
375
376  /*
377   * The new argument points to an empty directory and
378   * the old argument points to an ancestor directory of new.
379   */
380
381  puts ("\nRename directory with ancestor directory\n");
382
383  status = mkdir (dir02, mode);
384  rtems_test_assert (status == 0);
385
386  sprintf (path01, "%s/%s", dir02, dir01);
387  status = mkdir (path01, mode);
388  rtems_test_assert (status == 0);
389
390  EXPECT_ERROR (EINVAL, rename, dir02, path01);
391
392  /*
393   * Clear directory
394   */
395
396  EXPECT_EQUAL (0, rmdir, path01);
397  EXPECT_EQUAL (0, rmdir, dir02);
398
399  /*
400   * The new argument points to an empty directory and
401   * the old argument points to a non empty directory.
402   */
403
404  puts ("\nRename directory with non empty directory\n");
405
406  status = mkdir (dir01, mode);
407  rtems_test_assert (status == 0);
408
409  status = mkdir (dir02, mode);
410  rtems_test_assert (status == 0);
411
412  sprintf (path01, "%s/%s", dir02, name02);
413  fd = creat (path01, mode);
414  rtems_test_assert (fd >= 0);
415  status = close (fd);
416  rtems_test_assert (status == 0);
417
418  EXPECT_EQUAL (-1, rename, dir01, dir02);
419
420  puts("Testing errno for EEXIST or ENOTEMPTY");
421
422  if(errno == EEXIST || errno == ENOTEMPTY)
423    FS_PASS ();
424  else
425    FS_FAIL ();
426
427  /*
428   * Clear directory
429   */
430
431  EXPECT_EQUAL (0, unlink, path01);
432  EXPECT_EQUAL (0, rmdir, dir01);
433  EXPECT_EQUAL (0, rmdir, dir02);
434
435  /*
436   * The new argument points to an empty directory and
437   * the old argument points to other empty directory.
438   */
439
440  puts ("\nRename empty directory with another empty directory\n");
441
442  status = mkdir (dir01, mode);
443  rtems_test_assert (status == 0);
444
445  status = mkdir (dir02, mode);
446  rtems_test_assert (status == 0);
447
448  EXPECT_EQUAL (0, rename, dir01, dir02);
449
450  /*
451   * Clear directory
452   */
453
454  EXPECT_EQUAL (-1, rmdir, dir01);
455  EXPECT_EQUAL (0, rmdir, dir02);
456
457  /*
458   * The new argument points to a non existant directory and
459   * the old argument points to an existant directory at LINK_MAX.
460   */
461
462  status = mkdir (dir01, mode);
463  rtems_test_assert (status == 0);
464
465  LINK_MAX_val = pathconf (dir01, _PC_LINK_MAX);
466  rtems_test_assert (LINK_MAX_val >= 0);
467
468  status = stat (dir01, &statbuf);
469  rtems_test_assert (status == 0);
470
471  for(i = statbuf.st_nlink; i < LINK_MAX_val; i++)
472  {
473    sprintf (link_name, "%s/%d", dir01, i);
474
475    status = mkdir (link_name, mode);
476    rtems_test_assert (status == 0);
477  }
478
479  status = mkdir (dir02, mode);
480  rtems_test_assert (status == 0);
481
482  sprintf (path01, "%s/%s", dir01, dir01);
483  EXPECT_ERROR (EMLINK, rename, dir02, path01);
484
485  /*
486   * Clear directory
487   */
488
489  for(i = statbuf.st_nlink; i < LINK_MAX_val; i++)
490  {
491    sprintf (link_name, "%s/%d", dir01, i);
492
493    status = rmdir (link_name);
494    rtems_test_assert (status == 0);
495  }
496
497  EXPECT_EQUAL (-1, rmdir, path01);
498  EXPECT_EQUAL (0, rmdir, dir02);
499  EXPECT_EQUAL (0, rmdir, dir01);
500
501  /*
502   * The new argument points to a file and
503   * the old argument points to another file on a directory with S_ISVTX.
504   */
505
506  puts ("\nRename files within directories protected with S_ISVTX\n");
507
508  status = mkdir (dir01, mode | S_ISVTX);
509  rtems_test_assert (status == 0);
510
511  sprintf (path01, "%s/%s", dir01, name01);
512  fd = creat (path01, mode);
513  rtems_test_assert (fd >= 0);
514  status = close (fd);
515  rtems_test_assert (status == 0);
516
517  fd = creat (name02, mode);
518  rtems_test_assert (fd >= 0);
519  status = close (fd);
520  rtems_test_assert (status == 0);
521
522  status = chown (path01, 65534, -1);
523  rtems_test_assert (status == 0);
524
525  status = chown (dir01, 65534, -1);
526  rtems_test_assert (status == 0);
527
528  EXPECT_EQUAL (-1, rename, path01, name02);
529
530  puts("Testing errno for EPERM or EACCES");
531
532  if(errno == EPERM || errno == EACCES)
533    FS_PASS ();
534  else
535    FS_FAIL ();
536
537  /*
538   * Clear directory
539   */
540
541  EXPECT_EQUAL (0, unlink, path01);
542  EXPECT_EQUAL (0, unlink, name02);
543  EXPECT_EQUAL (0, rmdir, dir01);
544
545  /*
546   * The new argument points to a file on a directory with S_ISVTX and
547   * the old argument points to a file outside that directory.
548   */
549
550  status = mkdir (dir01, mode | S_ISVTX);
551  rtems_test_assert (status == 0);
552
553  sprintf (path01, "%s/%s", dir01, name01);
554  fd = creat (path01, mode);
555  rtems_test_assert (fd >= 0);
556  status = close (fd);
557  rtems_test_assert (status == 0);
558
559  fd = creat (name02, mode);
560  rtems_test_assert (fd >= 0);
561  status = close (fd);
562  rtems_test_assert (status == 0);
563
564  status = chown (path01, 65534, -1);
565  rtems_test_assert (status == 0);
566
567  status = chown (dir01, 65534, -1);
568  rtems_test_assert (status == 0);
569
570  EXPECT_EQUAL (-1, rename, name02, path01);
571
572  puts("Testing errno for EPERM or EACCES");
573
574  if(errno == EPERM || errno == EACCES)
575    FS_PASS ();
576  else
577    FS_FAIL ();
578
579  /*
580   * Clear directory
581   */
582
583  EXPECT_EQUAL (0, unlink, path01);
584  EXPECT_EQUAL (0, unlink, name02);
585  EXPECT_EQUAL (0, rmdir, dir01);
586
587  /*
588   * Go back to parent directory
589   */
590
591  status = chdir ("..");
592  rtems_test_assert (status == 0);
593
594  /*
595   * Remove test directory
596   */
597
598  status = rmdir (wd);
599  rtems_test_assert (status == 0);
600}
601
602static void arg_test (void)
603{
604  int fd;
605  int status;
606  int i;
607
608  const char *name01 = "name01";
609  const char *name02 = "name02";
610
611  const char *dir01 = "dir01";
612  const char *dir02 = "dir02";
613
614  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
615  const char *wd = __func__;
616
617  char filename[NAME_MAX + 1];
618  char path01[20];
619
620  /*
621   * Create a new directory and change the current directory to this
622   */
623
624  status = mkdir (wd, mode);
625  rtems_test_assert (status == 0);
626  status = chdir (wd);
627  rtems_test_assert (status == 0);
628
629  /*
630   * The new argument points to a non existant file and
631   * the old argument points to a file.
632   */
633
634  puts ("\nRename file with non existant file\n");
635
636  fd = creat (name01, mode);
637  rtems_test_assert (fd >= 0);
638  status = close (fd);
639  rtems_test_assert (status == 0);
640
641  EXPECT_EQUAL (0, rename, name01, name02);
642
643  /*
644   * Clear directory
645   */
646
647  EXPECT_EQUAL (-1, unlink, name01);
648  EXPECT_EQUAL (0, unlink, name02);
649
650  /*
651   * The new argument points to a file and
652   * the old argument points to a non existant file.
653   */
654
655  fd = creat (name01, mode);
656  rtems_test_assert (fd >= 0);
657  status = close (fd);
658  rtems_test_assert (status == 0);
659
660  EXPECT_ERROR (ENOENT, rename, name02, name01);
661
662  /*
663   * Clear directory
664   */
665
666  EXPECT_EQUAL (0, unlink, name01);
667  EXPECT_EQUAL (-1, unlink, name02);
668
669  /*
670   * The new argument points to a non existant file and
671   * the old argument points to a file where a component of the
672   * filepath does not exist.
673   */
674
675  puts ("\nRename file with non existant filepath\n");
676
677  status = mkdir (dir01, mode);
678  rtems_test_assert (status == 0);
679
680  sprintf (path01, "%s/%s/%s", dir01, name01, name02);
681  EXPECT_ERROR (ENOENT, rename, path01, name01);
682
683  /*
684   * Clear directory
685   */
686
687  EXPECT_EQUAL (-1, unlink, name01);
688  EXPECT_EQUAL (0, rmdir, dir01);
689
690  /*
691   * The new argument points to a non existant directory and
692   * the old argument points to a directory.
693   */
694
695  puts ("\nRename directory with non existant directory\n");
696
697  status = mkdir (dir01, mode);
698  rtems_test_assert (status == 0);
699
700  EXPECT_EQUAL (0, rename, dir01, dir02);
701
702  /*
703   * Clear directory
704   */
705
706  EXPECT_EQUAL (-1, rmdir, dir01);
707  EXPECT_EQUAL (0, rmdir, dir02);
708
709  /*
710   * The new argument is a name bigger than NAME_MAX and
711   * the old argument points to a file.
712   */
713
714  puts ("\nRename file with a name size exceeding NAME_MAX\n");
715
716  fd = creat (name01, mode);
717  rtems_test_assert (fd >= 0);
718  status = close (fd);
719  rtems_test_assert (status == 0);
720
721  /* Generate string with NAME_MAX + 1 length */
722
723  for(i = 0; i < NAME_MAX + 1; i++)
724    strcat(filename, "a");
725
726  EXPECT_ERROR (ENAMETOOLONG, rename, name01, filename);
727
728  /*
729   * Clear directory
730   */
731
732  EXPECT_EQUAL (0, unlink, name01);
733  EXPECT_EQUAL (-1, unlink, filename);
734
735  /*
736   * Go back to parent directory
737   */
738
739  status = chdir ("..");
740  rtems_test_assert (status == 0);
741
742  /*
743   * Remove test directory
744   */
745
746  status = rmdir (wd);
747  rtems_test_assert (status == 0);
748}
749
750static void arg_format_test (void)
751{
752  int fd;
753  int status;
754  const char *name01 = "name01";
755
756  const char *dir01 = "dir01";
757
758  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
759  const char *wd = __func__;
760
761  /*
762   * Create a new directory and change the current directory to this
763   */
764
765  status = mkdir (wd, mode);
766  rtems_test_assert (status == 0);
767  status = chdir (wd);
768  rtems_test_assert (status == 0);
769
770  /*
771   * The new argument points to a directory and
772   * the old argument points to current directory.
773   */
774
775  puts ("\nRename directory with current directory\n");
776
777  status = mkdir (dir01, mode);
778  rtems_test_assert (status == 0);
779
780  EXPECT_EQUAL (-1, rename, "." , dir01);
781
782  puts("Testing errno for EINVAL or EBUSY");
783
784  if(errno == EINVAL || errno == EBUSY)
785    FS_PASS ();
786  else
787    FS_FAIL ();
788
789  /*
790   * The new argument points to current directory and
791   * the old argument points to a directory.
792   */
793
794  EXPECT_EQUAL (-1, rename, dir01, ".");
795
796  puts("Testing errno for EINVAL or EBUSY");
797
798  if(errno == EINVAL || errno == EBUSY)
799    FS_PASS ();
800  else
801    FS_FAIL ();
802
803  /*
804   * The new argument points to a directory and
805   * the old argument points to previous directory.
806   */
807
808  puts ("\nRename directory with previous directory\n");
809
810  EXPECT_EQUAL (-1, rename, ".." , dir01);
811
812  puts("Testing errno for EINVAL or EBUSY");
813
814  if(errno == EINVAL || errno == EBUSY)
815    FS_PASS ();
816  else
817    FS_FAIL ();
818
819  /*
820   * The new argument points to previous directory and
821   * the old argument points to a directory.
822   */
823
824  EXPECT_EQUAL (-1, rename, dir01, "..");
825
826  puts("Testing errno for EINVAL or EBUSY");
827
828  if(errno == EINVAL || errno == EBUSY)
829    FS_PASS ();
830  else
831    FS_FAIL ();
832
833  /*
834   * Clear directory
835   */
836
837  EXPECT_EQUAL (0, rmdir, dir01);
838
839  /*
840   * The new argument points to a file and
841   * the old argument is an empty string.
842   */
843
844  puts("\nTesting empty filepaths\n");
845
846  fd = creat (name01, mode);
847  rtems_test_assert (fd >= 0);
848  status = close (fd);
849  rtems_test_assert (status == 0);
850
851  EXPECT_ERROR (ENOENT, rename, name01, "");
852
853  /*
854   * Clear directory
855   */
856
857  EXPECT_EQUAL (0, unlink, name01);
858
859  /*
860   * The new argument is an empty string and
861   * the old argument points to a file.
862   */
863
864  fd = creat (name01, mode);
865  rtems_test_assert (fd >= 0);
866  status = close (fd);
867  rtems_test_assert (status == 0);
868
869  EXPECT_ERROR (ENOENT, rename, "", name01);
870
871  /*
872   * Clear directory
873   */
874
875  EXPECT_EQUAL (0, unlink, name01);
876
877  /*
878   * Go back to parent directory
879   */
880
881  status = chdir ("..");
882  rtems_test_assert (status == 0);
883
884  /*
885   * Remove test directory
886   */
887
888  status = rmdir (wd);
889  rtems_test_assert (status == 0);
890}
891
892static void write_permission_test (void)
893{
894  int fd;
895  int status;
896
897  const char *name01 = "name01";
898  const char *name02 = "name02";
899
900  const char *dir01 = "dir01";
901  const char *dir02 = "dir02";
902
903  char path01[20];
904
905  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
906  mode_t no_write_access = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP
907                         | S_IROTH | S_IXOTH;
908
909  const char *wd = __func__;
910
911  /*
912   * Create a new directory and change the current directory to this
913   */
914
915  status = mkdir (wd, mode);
916  rtems_test_assert (status == 0);
917  status = chdir (wd);
918  rtems_test_assert (status == 0);
919
920  /*
921   * The new argument points to a file and
922   * the old argument points to another file,
923   * both inside a directory with no write permission.
924   */
925
926  puts ("\nRename two files on a directory with no write permission \n");
927
928  status = mkdir (dir01, mode);
929  rtems_test_assert (status == 0);
930
931  status = chdir (dir01);
932  rtems_test_assert (status == 0);
933
934  fd = creat (name01, mode);
935  rtems_test_assert (fd >= 0);
936  status = close (fd);
937  rtems_test_assert (status == 0);
938
939  fd = creat (name02, mode);
940  rtems_test_assert (fd >= 0);
941  status = close (fd);
942  rtems_test_assert (status == 0);
943
944  status = chmod (".", no_write_access);
945  rtems_test_assert (status == 0);
946
947  EXPECT_ERROR (EACCES, rename, name01 , name02);
948
949  status = chdir ("..");
950  rtems_test_assert (status == 0);
951
952  /*
953   * The new argument points to a file in a directory with no write access and
954   * the old argument points to another file on a directory with write access.
955   */
956
957  puts ("\nRename file between two directories, with and without write access\n");
958
959  status = mkdir (dir02, mode);
960  rtems_test_assert (status == 0);
961
962  status = chdir (dir02);
963  rtems_test_assert (status == 0);
964
965  fd = creat (name01, mode);
966  rtems_test_assert (fd >= 0);
967  status = close (fd);
968  rtems_test_assert (status == 0);
969
970  sprintf (path01, "../%s/%s", dir01, name02);
971  EXPECT_ERROR (EACCES, rename, name01, path01);
972
973  /*
974   * The new argument points to a file in a directory with write access and
975   * the old argument points to another file on a directory without write access.
976   */
977
978  EXPECT_ERROR (EACCES, rename, path01, name01);
979
980  /*
981   * Clear directory
982   */
983
984  EXPECT_EQUAL (0, unlink, name01);
985
986  sprintf (path01, "../%s", dir01);
987  status = chmod (path01, mode);
988  rtems_test_assert (status == 0);
989
990  sprintf (path01, "../%s/%s", dir01, name01);
991  EXPECT_EQUAL (0, unlink, path01);
992
993  sprintf (path01, "../%s/%s", dir01, name02);
994  EXPECT_EQUAL (0, unlink, path01);
995
996  status = chdir ("..");
997  rtems_test_assert (status == 0);
998
999  EXPECT_EQUAL (0, rmdir, dir01);
1000  EXPECT_EQUAL (0, rmdir, dir02);
1001
1002  /*
1003   * Go back to parent directory
1004   */
1005
1006  status = chdir ("..");
1007  rtems_test_assert (status == 0);
1008
1009  /*
1010   * Remove test directory
1011   */
1012
1013  status = rmdir (wd);
1014  rtems_test_assert (status == 0);
1015}
1016
1017static void search_permission_test (void)
1018{
1019  int fd;
1020  int status;
1021
1022  const char *name01 = "name01";
1023  const char *name02 = "name02";
1024
1025  const char *dir01 = "dir01";
1026  const char *dir02 = "dir02";
1027
1028  char path01[20];
1029  char path02[20];
1030
1031  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
1032  mode_t no_execute_access = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
1033                           | S_IROTH | S_IWOTH;
1034
1035  const char *wd = __func__;
1036
1037  /*
1038   * Create a new directory and change the current directory to this
1039   */
1040
1041  status = mkdir (wd, mode);
1042  rtems_test_assert (status == 0);
1043  status = chdir (wd);
1044  rtems_test_assert (status == 0);
1045
1046  /*
1047   * The new argument points to a file and
1048   * the old argument points to another file,
1049   * both inside a directory with no execute permission.
1050   */
1051
1052  puts ("\nRename two files on a directory with no execute permission \n");
1053
1054  status = mkdir (dir01, mode);
1055  rtems_test_assert (status == 0);
1056
1057  sprintf (path01, "%s/%s", dir01, name01);
1058  fd = creat (path01, mode);
1059  rtems_test_assert (fd >= 0);
1060  status = close (fd);
1061  rtems_test_assert (status == 0);
1062
1063  sprintf (path02, "%s/%s", dir01, name02);
1064  fd = creat (path02, mode);
1065  rtems_test_assert (fd >= 0);
1066  status = close (fd);
1067  rtems_test_assert (status == 0);
1068
1069  status = chmod (dir01, no_execute_access);
1070  rtems_test_assert (status == 0);
1071
1072  EXPECT_ERROR (EACCES, rename, path01 , path02);
1073
1074  /*
1075   * The new argument points to a file in a directory with no execute access and
1076   * the old argument points to another file on a directory with execute access.
1077   */
1078
1079  puts ("\nRename file between two directories, with and without execute access\n");
1080
1081  status = mkdir (dir02, mode);
1082  rtems_test_assert (status == 0);
1083
1084  status = chdir (dir02);
1085  rtems_test_assert (status == 0);
1086
1087  fd = creat (name01, mode);
1088  rtems_test_assert (fd >= 0);
1089  status = close (fd);
1090  rtems_test_assert (status == 0);
1091
1092  status = chdir ("..");
1093  rtems_test_assert (status == 0);
1094
1095  sprintf (path01, "%s/%s", dir02, name01);
1096  EXPECT_ERROR (EACCES, rename, path01, path02);
1097
1098  /*
1099   * The new argument points to a file in a directory with execute access and
1100   * the old argument points to another file on a directory without execute access.
1101   */
1102
1103  EXPECT_ERROR (EACCES, rename, path02, path01);
1104
1105  /*
1106   * Clear directory
1107   */
1108
1109  EXPECT_EQUAL (0, unlink, path01);
1110
1111  status = chmod (dir01, mode);
1112  rtems_test_assert (status == 0);
1113
1114  sprintf (path01, "%s/%s", dir01, name01);
1115  EXPECT_EQUAL (0, unlink, path01);
1116  EXPECT_EQUAL (0, unlink, path02);
1117  EXPECT_EQUAL (0, rmdir, dir01);
1118  EXPECT_EQUAL (0, rmdir, dir02);
1119
1120  /*
1121   * Go back to parent directory
1122   */
1123
1124  status = chdir ("..");
1125  rtems_test_assert (status == 0);
1126
1127  /*
1128   * Remove test directory
1129   */
1130
1131  status = rmdir (wd);
1132  rtems_test_assert (status == 0);
1133}
1134
1135static void filesystem_test (void)
1136{
1137  int fd;
1138  int status;
1139
1140  const char *name01 = "name01";
1141  const char *name02 = "name02";
1142
1143  char path01[20];
1144
1145  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
1146  const char *wd = __func__;
1147
1148  /*
1149   * Create a new directory and change the current directory to this
1150   */
1151
1152  status = mkdir (wd, mode);
1153  rtems_test_assert (status == 0);
1154  status = chdir (wd);
1155  rtems_test_assert (status == 0);
1156
1157  /*
1158   * The new argument points to a file on another instance of the filesystem and
1159   * the old argument points to a file on the base filesystem.
1160   */
1161
1162  puts ("\nRename files across diferent filesystems\n");
1163
1164  test_initialize_filesystem ();
1165
1166  fd = creat (name01, mode);
1167  rtems_test_assert (fd >= 0);
1168  status = close (fd);
1169  rtems_test_assert (status == 0);
1170
1171  sprintf (path01, "%s/%s", BASE_FOR_TEST, name02);
1172  EXPECT_ERROR (EXDEV, rename, name01, path01);
1173
1174  /*
1175   * Clear directory
1176   */
1177
1178  test_shutdown_filesystem ();
1179
1180  EXPECT_EQUAL (-1, unlink, path01);
1181  EXPECT_EQUAL (0, unlink, name01);
1182
1183  /*
1184   * Go back to parent directory
1185   */
1186
1187  status = chdir ("..");
1188  rtems_test_assert (status == 0);
1189
1190  /*
1191   * Remove test directory
1192   */
1193
1194  status = rmdir (wd);
1195  rtems_test_assert (status == 0);
1196}
1197
1198void test (void)
1199{
1200  symbolic_link_test ();
1201  same_file_test ();
1202  directory_test ();
1203  arg_test ();
1204  arg_format_test ();
1205  write_permission_test ();
1206  search_permission_test ();
1207  filesystem_test ();
1208}
Note: See TracBrowser for help on using the repository browser.