Back to home page

LXR

 
 

    


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

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 <tmacros.h>
0034 #include "test_support.h"
0035 
0036 #include <unistd.h>
0037 #include <sys/types.h>
0038 #include <sys/stat.h>
0039 #include <fcntl.h>
0040 #include <errno.h>
0041 #include <rtems/libcsupport.h>
0042 
0043 const char rtems_test_name[] = "PSXIMFS 1";
0044 
0045 /* forward declarations to avoid warnings */
0046 rtems_task Init(rtems_task_argument argument);
0047 void open_it(bool readOnly, bool create);
0048 void write_helper(void);
0049 void read_helper(void);
0050 void truncate_helper(void);
0051 void extend_helper(int eno);
0052 void close_it(void);
0053 void unlink_it(void);
0054 
0055 int TestFd;
0056 uint8_t Buffer[256];
0057 ssize_t TotalWritten;
0058 
0059 #define FILE_NAME "biggie"
0060 
0061 void open_it(bool readOnly, bool create)
0062 {
0063   int flag = 0;
0064 
0065   if ( readOnly )
0066     flag |= O_RDONLY;
0067   else {
0068     if ( create )
0069       flag |= O_CREAT;
0070     flag |= O_RDWR;
0071   }
0072 
0073   /* open the file */
0074   puts( "open(" FILE_NAME ") - OK " );
0075   TestFd = open( FILE_NAME, flag, 0777 );
0076   rtems_test_assert( TestFd != -1 );
0077 }
0078 
0079 void write_helper(void)
0080 {
0081   ssize_t written;
0082 
0083   TotalWritten = 0;
0084   puts( "write(" FILE_NAME ") - OK " );
0085   do {
0086     written = write( TestFd, Buffer, sizeof(Buffer) );
0087     if ( written == -1 ) {
0088       if ( errno == EFBIG ) {
0089         printf( "Total written = %zd\n", TotalWritten );
0090         return;
0091       }
0092       printf(
0093         "Unable to create largest IMFS file (error=%s)\n",
0094         strerror(errno)
0095       );
0096       rtems_test_exit(0);
0097     }
0098     TotalWritten += written;
0099   } while (1);
0100 
0101 }
0102 
0103 void read_helper(void)
0104 {
0105   uint8_t ch;
0106   ssize_t sc;
0107   int     i=0;
0108 
0109   puts( "read(" FILE_NAME ") - OK " );
0110   do {
0111     sc = read( TestFd, &ch, sizeof(ch) );
0112     if ( sc == 1 ) {
0113       if ( ch != (i%256) ) {
0114         printf(
0115           "MISMATCH 0x%02x != 0x%02x at offset %d\n",
0116           ch,
0117           i % 256,
0118           i
0119         );
0120         rtems_test_exit(0);
0121       }
0122       i++;
0123     } else if ( sc != 0 ) {
0124       printf(
0125         "ERROR - at offset %d - returned %zd and error=%s\n",
0126         i,
0127         sc,
0128         strerror( errno )
0129       );
0130       rtems_test_exit(0);
0131     }
0132   } while ( sc > 0 );
0133 
0134   if ( i == TotalWritten ) {
0135     puts( "File correctly read until EOF returned\n" );
0136   }
0137 }
0138 
0139 void truncate_helper(void)
0140 {
0141   off_t position;
0142   off_t new;
0143   off_t sc;
0144   int   rc;
0145 
0146   position = lseek( TestFd, 0, SEEK_END );
0147   printf( "Seek to end .. returned %d\n", (int) position );
0148   rtems_test_assert( position == TotalWritten );
0149 
0150   puts( "lseek/ftruncate loop.." );
0151   new = position;
0152   do {
0153     sc = lseek( TestFd, new, SEEK_SET );
0154     rtems_test_assert( sc == new );
0155 
0156     rc = ftruncate( TestFd, new );
0157     if ( rc != 0 ) {
0158       printf(
0159         "ERROR - at offset %d - returned %d and error=%s\n",
0160         (int) new,
0161         rc,
0162         strerror( errno )
0163       );
0164     }
0165     rtems_test_assert( rc == 0 );
0166     --new;
0167   } while (new > 0);
0168 }
0169 
0170 void extend_helper(int eno)
0171 {
0172   off_t position;
0173   off_t new;
0174   off_t sc;
0175   int   rc;
0176 
0177   position = lseek( TestFd, 0, SEEK_END );
0178   printf( "Seek to end .. returned %d\n", (int) position );
0179 
0180   /*
0181    * test case to ftruncate a file to a length > its size
0182    */
0183 
0184   rc = ftruncate( TestFd, 2 );
0185   rtems_test_assert( rc == 0 );
0186 
0187   puts( "lseek/ftruncate loop.." );
0188   new = position;
0189   do {
0190     sc = lseek( TestFd, new, SEEK_SET );
0191     rtems_test_assert( sc == new );
0192 
0193     rc = ftruncate( TestFd, new );
0194     if ( rc != 0 ) {
0195       if( errno != eno ) {
0196         printf(
0197           "ERROR - at offset %d - returned %d and error=%s\n",
0198           (int) new,
0199           rc,
0200           strerror( errno )
0201         );
0202     break;
0203       }
0204       else {
0205     break;
0206       }
0207     }
0208     rtems_test_assert( rc == 0 );
0209     ++new;
0210   } while ( 1 );
0211 }
0212 
0213 void close_it(void)
0214 {
0215   int rc;
0216 
0217   puts( "close(" FILE_NAME ") - OK " );
0218   rc = close( TestFd );
0219   rtems_test_assert( rc == 0 );
0220 }
0221 
0222 void unlink_it(void)
0223 {
0224   int rc;
0225 
0226   puts( "unlink(" FILE_NAME ") - OK" );
0227   rc = unlink( FILE_NAME );
0228   rtems_test_assert( rc == 0 );
0229 }
0230 
0231 rtems_task Init(
0232   rtems_task_argument argument
0233 )
0234 {
0235   int i;
0236   void *alloc_ptr = (void *)0;
0237   off_t position;
0238   off_t new_position;
0239   char buf [1];
0240   ssize_t n;
0241 
0242   TEST_BEGIN();
0243 
0244   for (i=0 ; i<sizeof(Buffer) ; i++ )
0245     Buffer[i] = (uint8_t) i;
0246 
0247   open_it(false, true);
0248   write_helper();
0249   close_it();
0250 
0251   puts( "" );
0252 
0253   open_it(true, false);
0254   read_helper();
0255   close_it();
0256 
0257   open_it(false, false);
0258   truncate_helper();
0259 
0260   /*
0261    * Allocate the heap, so that extend cannot be successful
0262    */
0263   alloc_ptr = malloc( malloc_free_space() - 4 );
0264 
0265   extend_helper(ENOSPC);
0266 
0267   /*
0268    * free the allocated heap memory
0269    */
0270   free(alloc_ptr);
0271 
0272   extend_helper(EFBIG);
0273   position = lseek( TestFd , 0, SEEK_END );
0274   new_position = lseek( TestFd, position + 2, SEEK_SET );
0275   rtems_test_assert( new_position == position + 2 );
0276 
0277   n = write( TestFd, buf, sizeof(buf) );
0278   rtems_test_assert( n == -1 );
0279   rtems_test_assert( errno == EFBIG );
0280 
0281   close_it();
0282   unlink_it();
0283 
0284   TEST_END();
0285 
0286   rtems_test_exit(0);
0287 }
0288 
0289 /* configuration information */
0290 
0291 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0292 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0293 
0294 #define CONFIGURE_MAXIMUM_TASKS             1
0295 #define CONFIGURE_IMFS_MEMFILE_BYTES_PER_BLOCK 16
0296 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
0297 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0298 
0299 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0300 
0301 #define CONFIGURE_INIT
0302 
0303 #include <rtems/confdefs.h>
0304 /* end of file */