Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2012.
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 <bsp.h>
0034 #include <pmacros.h>
0035 #include <unistd.h>
0036 #include <errno.h>
0037 #include <sys/stat.h>
0038 #include <sys/types.h>
0039 #include <fcntl.h>
0040 
0041 const char rtems_test_name[] = "PSXFCHX 1";
0042 
0043 /* forward declarations to avoid warnings */
0044 rtems_task Init(rtems_task_argument ignored);
0045 
0046 rtems_task Init(
0047   rtems_task_argument ignored
0048 )
0049 {
0050   int status = 0;
0051   int fd = 0;
0052 
0053   TEST_BEGIN();
0054 
0055   /****************************************************
0056    *                   fchdir tests
0057    ***************************************************/
0058 
0059   puts( "Init - fchdir tests" );
0060 
0061   puts( "Init - Attempt fchdir with bad file descriptor - expect EBADF" );
0062   status = fchdir( 5 );
0063   rtems_test_assert( status == -1 );
0064   rtems_test_assert( errno == EBADF );
0065 
0066   puts( "Init - Attempt fchdir with bad file descriptor - expect EBADF" );
0067   status = fchdir( 3 );
0068   rtems_test_assert( status == -1 );
0069   rtems_test_assert( errno == EBADF );
0070   
0071   puts( "Init - opening /newfile in write-mode -- OK" );
0072   fd = open( "/newfile", O_WRONLY | O_CREAT, S_IWUSR | S_IXUSR );
0073   rtems_test_assert( fd != -1 );
0074 
0075   puts( "Init - fchdir on the file descriptor - expect ENOTDIR" );
0076   status = fchdir( fd );
0077   rtems_test_assert( status == -1 );
0078   rtems_test_assert( errno == ENOTDIR );
0079 
0080   puts( "Init - closing /newfile -- OK" );
0081   status = close( fd );
0082   rtems_test_assert( status == 0 );
0083 
0084   puts( "Init - removing /newfile -- OK" );
0085   status = unlink( "/newfile" );
0086   rtems_test_assert( status == 0 );
0087 
0088   puts( "Init - opening /newfile in read-mode -- OK" );
0089   fd = open( "/newfile", O_RDONLY | O_CREAT, S_IRUSR | S_IXUSR);
0090   rtems_test_assert( fd != -1 );
0091   
0092   puts( "Init - fchdir on the file descriptor - expect ENOTDIR" );
0093   status = fchdir( fd );
0094   rtems_test_assert( status == -1 );
0095   rtems_test_assert( errno == ENOTDIR );
0096 
0097   puts( "Init - closing and removing /newfile -- OK" );
0098   status = close( fd );
0099   status |= unlink( "/newfile" );
0100   rtems_test_assert( status == 0 );
0101 
0102   puts( "Init - create directory  /tmp - RWX permissions -- OK" );
0103   status = mkdir( "/tmp", S_IRWXU );
0104   rtems_test_assert( status == 0 );
0105 
0106   puts( "Init - open the /tmp, get the file descriptor -- OK" );
0107   fd = open( "/tmp", O_RDONLY );
0108   rtems_test_assert( fd != -1 );
0109 
0110   puts( "Init - fchdir on the file descriptor -- OK" );
0111   status = fchdir( fd );
0112   rtems_test_assert( status == 0 );
0113 
0114   puts( "Init - close the file descriptor -- OK" );
0115   status = close( fd );
0116   rtems_test_assert( status == 0 );
0117 
0118   puts( "Init - remove directory /tmp -- OK" );
0119   status = rmdir( "/tmp" );
0120   rtems_test_assert( status == 0 );
0121 
0122   puts( "Init - creating directory /tmp - read permission -- OK" );
0123   status = mkdir( "/tmp", S_IRUSR );
0124   rtems_test_assert( status == 0 );
0125 
0126   puts( "Init - open the /tmp, get the file descriptor -- OK" );
0127   fd = open( "/tmp", O_RDONLY );
0128   rtems_test_assert( fd != -1 );
0129 
0130   puts( "Init - attempt fchdir on the file descriptor -- expect EACCES" );
0131   status = fchdir( fd );
0132   rtems_test_assert( status == -1);
0133   rtems_test_assert( errno == EACCES );
0134 
0135   puts( "Init - close the file descriptor -- OK" );
0136   status = close( fd );
0137   rtems_test_assert( status == 0 );
0138 
0139   puts( "Init - remove directory /tmp -- OK" );
0140   status = rmdir( "/tmp" );
0141   rtems_test_assert( status == 0 );
0142 
0143   puts( "End of fchdir tests" );
0144 
0145   /****************************************************
0146    *                   fchmod tests
0147    ***************************************************/
0148 
0149   puts( "\nInit - fchmod tests" );
0150 
0151   puts( "Init - fchmod, with a bad file descriptor - expect EBADF" );
0152   status = fchmod( 4, 0 );
0153   rtems_test_assert( status == -1 );
0154   rtems_test_assert( errno == EBADF );
0155 
0156   puts( "Init - fchmod, with an unopened file descriptor - expect EBADF" );
0157   status = fchmod( 3, 0 );
0158   rtems_test_assert( status == -1 );
0159   rtems_test_assert( errno == EBADF );
0160 
0161   puts( "Init - open new file: /newfile in read-only mode -- OK" );
0162   fd = open( "/newfile", O_RDONLY | O_CREAT, S_IRWXU );
0163   rtems_test_assert( fd != -1 );
0164   
0165   puts( "Init - fchmod, with the opened file descriptor -- OK" );
0166   status = fchmod( fd, 0 );
0167   rtems_test_assert( status == 0 );
0168 
0169   puts( "Init - close and remove /newfile" );
0170   status = close( fd );
0171   status |= unlink( "/newfile" );
0172   rtems_test_assert( status == 0 );
0173 
0174   puts( "Init - open new file: /newfile in read-write mode -- OK" );
0175   fd = open( "/newfile", O_RDWR | O_CREAT, S_IRWXU );
0176   rtems_test_assert( fd != -1 );
0177   
0178   puts( "Init - fchmod, with the opened file descriptor -- OK" );
0179   status = fchmod( fd, S_IRUSR );
0180   rtems_test_assert( status == 0 );
0181 
0182   puts( "Init - close and remove /newfile -- OK" );
0183   status = close( fd );
0184   status |= unlink( "/newfile" );
0185   rtems_test_assert( status == 0 );
0186 
0187   puts( "End of fchmod tests" );
0188 
0189   /****************************************************
0190    *                   fchown tests
0191    ***************************************************/
0192 
0193   puts( "\nInit - fchown tests" );
0194 
0195   puts( "Init - fchown, with a bad file descriptor - expect EBADF" );
0196   status = fchown( 4, 0, 0 );
0197   rtems_test_assert( status == -1 );
0198   rtems_test_assert( errno == EBADF );
0199 
0200   puts( "Init - fchown, with an unopened file descriptor - expect EBADF" );
0201   status = fchown( 3, 0, 0 );
0202   rtems_test_assert( status == -1 );
0203   rtems_test_assert( errno == EBADF );
0204 
0205   puts( "Init - open new file: /newfile in read-only mode -- OK" );
0206   fd = open( "/newfile", O_RDONLY | O_CREAT, S_IRWXU );
0207   rtems_test_assert( fd != -1 );
0208   
0209   puts( "Init - fchown, with the opened file descriptor - OK" );
0210   status = fchown( fd, 0, 0 );
0211   rtems_test_assert( status == 0 );
0212 
0213   puts( "Init - close and remove /newfile" );
0214   status = close( fd );
0215   status |= unlink( "/newfile" );
0216   rtems_test_assert( status == 0 );
0217 
0218   puts( "Init - open new file: /newfile in read-write mode -- OK" );
0219   fd = open( "/newfile", O_RDWR | O_CREAT, S_IRWXU );
0220   rtems_test_assert( fd != -1 );
0221   
0222   puts( "Init - fchown, with the opened file descriptor -- OK" );
0223   status = fchown( fd, 1, 0 );
0224   rtems_test_assert( status == 0 );
0225 
0226   puts( "Init - close and remove /newfile -- OK" );
0227   status = close( fd );
0228   status |= unlink( "/newfile" );
0229   rtems_test_assert( status == 0 );
0230 
0231   puts( "End of fchown tests" );
0232 
0233   TEST_END();
0234   rtems_test_exit( 0 );
0235 }
0236 
0237 /* configuration information */
0238 
0239 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0240 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0241 
0242 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
0243 
0244 #define CONFIGURE_MAXIMUM_TASKS 1
0245 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0246 
0247 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0248 
0249 #define CONFIGURE_INIT
0250 #include <rtems/confdefs.h>
0251 /* end of file */