Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:30

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2011.
0005  *  On-Line Applications Research Corporation (OAR).
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032 
0033 #include <sys/stat.h>
0034 #include <limits.h>
0035 #include <fcntl.h>
0036 #include <errno.h>
0037 #include <stdio.h>
0038 #include <stdint.h>
0039 #include <stdlib.h>
0040 #include <string.h>
0041 #include <unistd.h>
0042 
0043 #include "fstest.h"
0044 #include "fs_config.h"
0045 #include <tmacros.h>
0046 
0047 const char rtems_test_name[] = "FSERROR " FILESYSTEM;
0048 const RTEMS_TEST_STATE rtems_test_state = TEST_STATE;
0049 
0050 static void open_mkdir_error (void)
0051 {
0052   int fd;
0053   int status;
0054   char *name01 = "name01";
0055   char *name02 = "name02";
0056   char *name03 = "name03";
0057 
0058   char name[20];
0059 
0060   mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
0061 
0062   const char *wd = __func__;
0063 
0064   /*
0065    * Create a new directory and change the current directory to  this
0066    */
0067   status = mkdir (wd, mode);
0068   rtems_test_assert (status == 0);
0069   status = chdir (wd);
0070   rtems_test_assert (status == 0);
0071 
0072 
0073   /*
0074    * Create a file and a directory for test.
0075    */
0076   fd = creat (name01, mode);
0077   status = close (fd);
0078   rtems_test_assert (status == 0);
0079   status = mkdir (name02, mode);
0080   rtems_test_assert (status == 0);
0081 
0082   /*
0083    * O_CREAT and O_EXCL are set, and the named file exists.
0084    */
0085   EXPECT_ERROR (EEXIST, open, name01, O_CREAT | O_EXCL);
0086 
0087   EXPECT_ERROR (EEXIST, mkdir, name01, mode);
0088   /*
0089    * The named file is a directory
0090    * and oflag includes O_WRONLY or O_RDWR.
0091    */
0092   EXPECT_ERROR (EISDIR, open, name02, O_WRONLY);
0093   EXPECT_ERROR (EISDIR, open, name02, O_RDWR);
0094 
0095   /*
0096    * O_CREAT is not set and the named file does not exist
0097    * or O_CREAT is set and either the path prefix does not exist or
0098    * the path argument points to an empty string.
0099    */
0100 
0101   sprintf (name, "%s/%s", name03, name02);
0102   EXPECT_ERROR (ENOENT, open, name, O_WRONLY);
0103   EXPECT_ERROR (ENOENT, open, "", O_WRONLY);
0104   EXPECT_ERROR (ENOENT, open, name03, O_WRONLY);
0105 
0106   EXPECT_ERROR (ENOENT, mkdir, name, mode);
0107   EXPECT_ERROR (ENOENT, mkdir, "", mode);
0108 
0109   /*
0110    * A component of the path prefix is not a directory.
0111    */
0112 
0113   sprintf (name, "%s/%s", name01, name02);
0114   EXPECT_ERROR (ENOTDIR, open, name, O_WRONLY);
0115 
0116   EXPECT_ERROR (ENOTDIR, mkdir, name, mode);
0117   /*
0118    * The fildes argument is not a valid file descriptor.
0119    */
0120   EXPECT_ERROR (EBADF, close, -1);
0121   EXPECT_ERROR (EBADF, close, 100);
0122 
0123   /*
0124    * Go back to parent directory
0125    */
0126   status = chdir ("..");
0127   rtems_test_assert (status == 0);
0128 
0129 }
0130 
0131 static void rename_error (void)
0132 {
0133 
0134   int fd;
0135   int status;
0136   char *name01 = "name01";
0137   char *name02 = "name02";
0138   char *name03 = "name03";
0139 
0140   char name[20];
0141 
0142   mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
0143   const char *wd = __func__;
0144 
0145   /*
0146    *Create a new directory and change the current directory to  this
0147    */
0148 
0149   status = mkdir (wd, mode);
0150   rtems_test_assert (status == 0);
0151   status = chdir (wd);
0152   rtems_test_assert (status == 0);
0153 
0154 
0155   /*
0156    * Create a new directory and a new directory in it
0157    */
0158 
0159   status = mkdir (name01, mode);
0160   rtems_test_assert (status == 0);
0161   status = mkdir (name02, mode);
0162   rtems_test_assert (status == 0);
0163   sprintf (name, "%s/%s", name01, name03);
0164   status = mkdir (name, mode);
0165   rtems_test_assert (status == 0);
0166 
0167   /*
0168    * The link named by new is a directory that is
0169    *  not an empty directory.
0170    */
0171   status = rename (name02, name01);
0172   rtems_test_assert (status != 0);
0173   rtems_test_assert (errno == EEXIST || errno == ENOTEMPTY);
0174   /*
0175    * The new argument points to a directory and
0176    *  the old argument points to a file that is not a directory.
0177    */
0178   fd = creat (name03, mode);
0179   status = close (fd);
0180   rtems_test_assert (status == 0);
0181 
0182   /*
0183    * The link named by old does not name an existing file,
0184    *    or either old or new points to an empty string.
0185    */
0186   EXPECT_ERROR (ENOENT, rename, name01, "");
0187 
0188   /*
0189    * A component of either path prefix is not a directory;
0190    * or the old argument names a directory and new argument names
0191    *  a non-directory file.
0192    */
0193 
0194   sprintf (name, "%s/%s", name03, name01);
0195   EXPECT_ERROR (ENOTDIR, rename, name03, name);
0196 
0197   /*
0198    * Go back to parent directory
0199    */
0200   status = chdir ("..");
0201   rtems_test_assert (status == 0);
0202 }
0203 
0204 static void truncate_error (void)
0205 {
0206 
0207   int fd;
0208   int status;
0209   char *file = "name";
0210 
0211   mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
0212 
0213   const char *wd = __func__;
0214 
0215   /*
0216    *Create a new directory and change the current directory to  this
0217    */
0218   status = mkdir (wd, mode);
0219   rtems_test_assert (status == 0);
0220   status = chdir (wd);
0221   rtems_test_assert (status == 0);
0222   /*
0223    * Create a file
0224    */
0225   fd = creat (file, mode);
0226   status = close (fd);
0227   rtems_test_assert (status == 0);
0228 
0229 
0230   /*
0231    * The length argument was less than 0.
0232    */
0233   EXPECT_ERROR (EINVAL, truncate, file, -1);
0234 
0235   /*
0236    * Go back to parent directory
0237    */
0238   status = chdir ("..");
0239   rtems_test_assert (status == 0);
0240 }
0241 
0242 
0243 static void rmdir_unlink_error (void)
0244 {
0245   int status;
0246   int fd;
0247   mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
0248   char *nonexistence = "name04";
0249 
0250   const char *wd = __func__;
0251 
0252   /*
0253    * Create a new directory and change the current directory to  this
0254    */
0255   status = mkdir (wd, mode);
0256   rtems_test_assert (status == 0);
0257   status = chdir (wd);
0258   rtems_test_assert (status == 0);
0259 
0260   /*
0261    * Create a new directory and a file in it for test
0262    */
0263   status = mkdir ("tmp", mode);
0264   rtems_test_assert (status == 0);
0265   fd = creat ("tmp/file", mode);
0266   status = close (fd);
0267   rtems_test_assert (status == 0);
0268 
0269 
0270 
0271   /*
0272    * The path argument names a directory that is not an empty directory,
0273    * or there are hard links to the directory other than
0274    * dot or a single entry in dot-dot.
0275    */
0276   EXPECT_ERROR (ENOTEMPTY, rmdir, "tmp");
0277 
0278 
0279   /*
0280    * The path argument contains a last component that is dot.
0281    */
0282 
0283 
0284   EXPECT_ERROR (EINVAL, rmdir, ".");
0285   EXPECT_ERROR (EINVAL, rmdir, "tmp/.");
0286 
0287   /*
0288    * A component of path does not name an existing file,
0289    * or the path argument names a nonexistent directory or
0290    * points to an empty string
0291    */
0292   EXPECT_ERROR (ENOENT, rmdir, "");
0293   EXPECT_ERROR (ENOENT, rmdir, nonexistence);
0294 
0295   EXPECT_ERROR (ENOENT, unlink, "");
0296   EXPECT_ERROR (ENOENT, unlink, nonexistence);
0297 
0298   /*
0299    * component of path is not a directory.
0300    */
0301 
0302   EXPECT_ERROR (ENOTDIR, rmdir, "tmp/file");
0303 
0304   EXPECT_ERROR (ENOTDIR, unlink, "tmp/file/dir");
0305   /*
0306    * Go back to parent directory
0307    */
0308   status = chdir ("..");
0309   rtems_test_assert (status == 0);
0310 
0311 
0312 }
0313 
0314 static void rdwr_error (void)
0315 {
0316 
0317   int status;
0318   int fd;
0319   char *file01 = "name01";
0320   char *databuf = "test";
0321   char *readbuf[10];
0322   int shift = sizeof(off_t) * 8 - 1;
0323   off_t one = 1;
0324   off_t tiny = one << shift;
0325   off_t huge = tiny - 1;
0326   off_t off;
0327 
0328   mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
0329   const char *wd = __func__;
0330 
0331   /*
0332    * Create a new directory and change the current directory to  this
0333    */
0334   status = mkdir (wd, mode);
0335   rtems_test_assert (status == 0);
0336   status = chdir (wd);
0337   rtems_test_assert (status == 0);
0338 
0339   fd = open (file01, O_WRONLY | O_CREAT, mode);
0340 
0341   /*
0342    * The fildes argument is not a valid file descriptor open for reading.
0343    */
0344 
0345   EXPECT_ERROR (EBADF, read, fd, readbuf, 10);
0346   EXPECT_ERROR (EBADF, read, 100, readbuf, 10);
0347 
0348   status = close (fd);
0349   rtems_test_assert (status == 0);
0350   /*
0351    * The fildes argument is not a valid file descriptor open for writing.
0352    */
0353   fd = open (file01, O_RDONLY, mode);
0354 
0355   EXPECT_ERROR (EBADF, write, fd, databuf, 10);
0356 
0357   status = close (fd);
0358   rtems_test_assert (status == 0);
0359 
0360   EXPECT_ERROR (EBADF, write, fd, readbuf, 10);
0361 
0362   /*
0363    * The whence argument is not a proper value,
0364    * or the resulting file offset would be negative for a regular file,
0365    * block special file, or directory.
0366    */
0367 
0368   fd = open (file01, O_RDWR, mode);
0369 
0370   EXPECT_ERROR (EINVAL, lseek, fd, -100, SEEK_END);
0371   EXPECT_ERROR (EINVAL, lseek, fd, -100, SEEK_CUR);
0372   EXPECT_ERROR (EINVAL, lseek, fd, -100, SEEK_SET);
0373 
0374   status = ftruncate (fd, 1);
0375   rtems_test_assert (status == 0);
0376   EXPECT_ERROR (EOVERFLOW, lseek, fd, huge, SEEK_END);
0377 
0378   off = lseek (fd, 1, SEEK_SET);
0379   rtems_test_assert (off == 1);
0380   EXPECT_ERROR (EOVERFLOW, lseek, fd, huge, SEEK_CUR);
0381 
0382   status = close (fd);
0383   rtems_test_assert (status == 0);
0384 
0385   EXPECT_ERROR (EBADF, lseek, fd, 0, SEEK_SET);
0386 
0387   /*
0388    * Go back to parent directory
0389    */
0390   status = chdir ("..");
0391   rtems_test_assert (status == 0);
0392 
0393 }
0394 
0395 void test (void)
0396 {
0397 
0398   open_mkdir_error ();
0399   rename_error ();
0400   truncate_error ();
0401   rmdir_unlink_error ();
0402   rdwr_error ();
0403 }