Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 2014
0005  *  Andre Marques <andre.lousa.marques at gmail.com>
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 "fstest.h"
0034 #include "fs_config.h"
0035 #include "fstest_support.h"
0036 #include <tmacros.h>
0037 
0038 #include <sys/stat.h>
0039 #include <fcntl.h>
0040 #include <stdio.h>
0041 #include <string.h>
0042 #include <unistd.h>
0043 #include <errno.h>
0044 #include <limits.h>
0045 
0046 const char rtems_test_name[] = "FSRENAME " FILESYSTEM;
0047 const RTEMS_TEST_STATE rtems_test_state = TEST_STATE;
0048 
0049 static void rename_file_twice_test (void)
0050 {
0051   const char *name01 = "name01";
0052   const char *name02 = "name02";
0053   const char *name03 = "name03";
0054   mode_t      mode;
0055   int         fd;
0056   int         status;
0057 
0058   puts ("\nRename file twice\n");
0059 
0060   mode = S_IRWXU | S_IRWXG | S_IRWXO;
0061   fd = creat (name01, mode);
0062   rtems_test_assert (fd >= 0);
0063   status = close (fd);
0064   rtems_test_assert (status == 0);
0065 
0066   EXPECT_EQUAL (0, rename, name01, name02);
0067   EXPECT_EQUAL (0, rename, name02, name03);
0068 
0069   errno = 0;
0070   EXPECT_EQUAL (-1, unlink, name01);
0071   rtems_test_assert (errno == ENOENT);
0072   EXPECT_EQUAL (-1, unlink, name02);
0073   rtems_test_assert (errno == ENOENT);
0074   EXPECT_EQUAL (0, unlink, name03);
0075 }
0076 
0077 static void rename_opened_file_test (void)
0078 {
0079   const char *name01 = "name01.txt";
0080   const char *name02 = "name02.txt";
0081   const char *message = "Test file content";
0082   mode_t      mode;
0083   int         fd1;
0084   int         fd2;
0085   int         result;
0086   char        buffer[16];
0087 
0088   puts ("\nRename opened file and open a new file\n");
0089 
0090   mode = S_IRWXU | S_IRWXG | S_IRWXO;
0091   fd1 = open (name01, O_RDWR | O_CREAT, mode);
0092   rtems_test_assert (fd1 >= 0);
0093   result = write (fd1, message, strlen (message));
0094   rtems_test_assert (result == strlen (message));
0095 
0096   EXPECT_EQUAL (0, rename, name01, name02);
0097 
0098   fd2 = open (name01, O_RDWR | O_CREAT, mode);
0099   rtems_test_assert (fd1 >= 0);
0100   EXPECT_EQUAL (0, read, fd2, buffer, sizeof (buffer));
0101 
0102   result = close (fd1);
0103   rtems_test_assert (result == 0);
0104 
0105   result = close (fd2);
0106   rtems_test_assert (result == 0);
0107 
0108   EXPECT_EQUAL (0, unlink, name01);
0109   EXPECT_EQUAL (0, unlink, name02);
0110 }
0111 
0112 static void directory_test (void)
0113 {
0114   int fd;
0115   int status;
0116   int rv;
0117 
0118   const char *name02 = "name02";
0119 
0120   const char *dir01 = "dir01";
0121   const char *dir02 = "dir02";
0122 
0123   char path01[30];
0124 
0125   mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
0126   const char *wd = __func__;
0127 
0128   /*
0129    * Create a new directory and change the current directory to this
0130    */
0131 
0132   status = mkdir (wd, mode);
0133   rtems_test_assert (status == 0);
0134   status = chdir (wd);
0135   rtems_test_assert (status == 0);
0136 
0137   /*
0138    * The new argument points to an empty directory and
0139    * the old argument points to a non empty directory.
0140    */
0141 
0142   puts ("\nRename directory with non empty directory\n");
0143 
0144   status = mkdir (dir01, mode);
0145   rtems_test_assert (status == 0);
0146 
0147   status = mkdir (dir02, mode);
0148   rtems_test_assert (status == 0);
0149 
0150   rv = snprintf (path01, sizeof(path01), "%s/%s", dir02, name02);
0151   rtems_test_assert (rv < sizeof(path01));
0152   fd = creat (path01, mode);
0153   rtems_test_assert (fd >= 0);
0154   status = close (fd);
0155   rtems_test_assert (status == 0);
0156 
0157   EXPECT_EQUAL (-1, rename, dir01, dir02);
0158 
0159   puts("Testing errno for EEXIST or ENOTEMPTY");
0160 
0161   if (errno == EEXIST || errno == ENOTEMPTY) {
0162     FS_PASS ();
0163   } else {
0164     FS_FAIL ();
0165   }
0166 
0167   /*
0168    * Clear directory
0169    */
0170 
0171   EXPECT_EQUAL (0, unlink, path01);
0172   EXPECT_EQUAL (0, rmdir, dir01);
0173   EXPECT_EQUAL (0, rmdir, dir02);
0174 
0175   /*
0176    * Go back to parent directory
0177    */
0178 
0179   status = chdir ("..");
0180   rtems_test_assert (status == 0);
0181 
0182   /*
0183    * Remove test directory
0184    */
0185 
0186   status = rmdir (wd);
0187   rtems_test_assert (status == 0);
0188 }
0189 
0190 static void arg_test (void)
0191 {
0192   int fd;
0193   int status;
0194   int rv;
0195 
0196   const char *name01 = "name01";
0197   const char *name02 = "name02";
0198 
0199   const char *dir01 = "dir01";
0200   const char *dir02 = "dir02";
0201 
0202   mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
0203   const char *wd = __func__;
0204   
0205   char path01[20];
0206 
0207   /*
0208    * Create a new directory and change the current directory to this
0209    */
0210 
0211   status = mkdir (wd, mode);
0212   rtems_test_assert (status == 0);
0213   status = chdir (wd);
0214   rtems_test_assert (status == 0);
0215 
0216   /*
0217    * The new argument points to a non existant file and
0218    * the old argument points to a file.
0219    */
0220 
0221   puts ("\nRename file with non existant file\n");
0222 
0223   fd = creat (name01, mode);
0224   rtems_test_assert (fd >= 0);
0225   status = close (fd);
0226   rtems_test_assert (status == 0);
0227 
0228   EXPECT_EQUAL (0, rename, name01, name02);
0229 
0230   /*
0231    * Clear directory
0232    */
0233 
0234   EXPECT_EQUAL (-1, unlink, name01);
0235   EXPECT_EQUAL (0, unlink, name02);
0236 
0237   /*
0238    * The new argument points to a non existant file and
0239    * the old argument points to a file where a component of the
0240    * filepath does not exist.
0241    */
0242 
0243   puts ("\nRename file with non existant filepath\n");
0244 
0245   status = mkdir (dir01, mode);
0246   rtems_test_assert (status == 0);
0247 
0248   rv = snprintf (path01, sizeof(path01), "%s/%s/%s", dir01, name01, name02);
0249   rtems_test_assert (rv < sizeof(path01));
0250   EXPECT_ERROR (ENOENT, rename, path01, name01);
0251 
0252   /*
0253    * Clear directory
0254    */
0255 
0256   EXPECT_EQUAL (-1, unlink, name01);
0257   EXPECT_EQUAL (0, rmdir, dir01);
0258 
0259   /*
0260    * The new argument points to a non existant directory and
0261    * the old argument points to a directory.
0262    */
0263 
0264   puts ("\nRename directory with non existant directory\n");
0265 
0266   status = mkdir (dir01, mode);
0267   rtems_test_assert (status == 0);
0268 
0269   EXPECT_EQUAL (0, rename, dir01, dir02);
0270 
0271   /*
0272    * Clear directory
0273    */
0274 
0275   EXPECT_EQUAL (-1, rmdir, dir01);
0276   EXPECT_EQUAL (0, rmdir, dir02);
0277 
0278   /*
0279    * Go back to parent directory
0280    */
0281 
0282   status = chdir ("..");
0283   rtems_test_assert (status == 0);
0284 
0285   /*
0286    * Remove test directory
0287    */
0288 
0289   status = rmdir (wd);
0290   rtems_test_assert (status == 0);
0291 }
0292 
0293 static void filesystem_test (void)
0294 {
0295   int fd;
0296   int status;
0297   int rv;
0298 
0299   const char *name01 = "name01";
0300   const char *name02 = "name02";
0301 
0302   char path01[20];
0303 
0304   mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
0305   const char *wd = __func__;
0306 
0307   /*
0308    * Create a new directory and change the current directory to this
0309    */
0310 
0311   status = mkdir (wd, mode);
0312   rtems_test_assert (status == 0);
0313   status = chdir (wd);
0314   rtems_test_assert (status == 0);
0315 
0316   /*
0317    * The new argument points to a file on another instance of the filesystem and
0318    * the old argument points to a file on the base filesystem.
0319    */
0320 
0321   puts ("\nRename files across different filesystems\n");
0322 
0323   rv = chroot ("/");
0324   rtems_test_assert (rv == 0);
0325 
0326   fd = creat (name01, mode);
0327   rtems_test_assert (fd >= 0);
0328   status = close (fd);
0329   rtems_test_assert (status == 0);
0330 
0331   rv = snprintf (path01, sizeof(path01), "%s/%s", BASE_FOR_TEST, name02);
0332   rtems_test_assert (rv < sizeof(path01));
0333   EXPECT_ERROR (EXDEV, rename, name01, path01);
0334 
0335   EXPECT_EQUAL (-1, unlink, path01);
0336   EXPECT_EQUAL (0, unlink, name01);
0337 
0338   rv = chroot (BASE_FOR_TEST);
0339   rtems_test_assert (rv == 0);
0340 
0341   /*
0342    * Go back to parent directory
0343    */
0344 
0345   status = chdir ("..");
0346   rtems_test_assert (status == 0);
0347 
0348   /*
0349    * Remove test directory
0350    */
0351 
0352   status = rmdir (wd);
0353   rtems_test_assert (status == 0);
0354 }
0355 
0356 void test (void)
0357 {
0358   rename_file_twice_test ();
0359   directory_test ();
0360   rename_opened_file_test ();
0361   arg_test ();
0362   filesystem_test ();
0363 }