source: rtems/testsuites/psxtests/psximfs01/init.c @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 5.4 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2012.
3 *  On-Line Applications Research Corporation (OAR).
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 <tmacros.h>
15#include "test_support.h"
16
17#include <unistd.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <fcntl.h>
21#include <errno.h>
22#include <rtems/libcsupport.h>
23
24/* forward declarations to avoid warnings */
25rtems_task Init(rtems_task_argument argument);
26void open_it(bool readOnly, bool create);
27void write_helper(void);
28void read_helper(void);
29void truncate_helper(void);
30void extend_helper(int eno);
31void close_it(void);
32void unlink_it(void);
33
34int TestFd;
35uint8_t Buffer[256];
36ssize_t TotalWritten;
37
38#define FILE_NAME "biggie"
39
40void open_it(bool readOnly, bool create)
41{
42  int flag = 0;
43
44  if ( readOnly )
45    flag |= O_RDONLY;
46  else {
47    if ( create )
48      flag |= O_CREAT;
49    flag |= O_RDWR;
50  }
51
52  /* open the file */
53  puts( "open(" FILE_NAME ") - OK " );
54  TestFd = open( FILE_NAME, flag, 0777 );
55  rtems_test_assert( TestFd != -1 );
56}
57
58void write_helper(void)
59{
60  ssize_t written;
61
62  TotalWritten = 0;
63  puts( "write(" FILE_NAME ") - OK " );
64  do {
65    written = write( TestFd, Buffer, sizeof(Buffer) );
66    if ( written == -1 ) {
67      if ( errno == EFBIG ) {
68        printf( "Total written = %zd\n", TotalWritten );
69        return;
70      }
71      fprintf(
72        stderr,
73        "Unable to create largest IMFS file (error=%s)\n",
74        strerror(errno)
75      );
76      rtems_test_exit(0);
77    }
78    TotalWritten += written;
79  } while (1);
80 
81}
82
83void read_helper(void)
84{
85  uint8_t ch;
86  ssize_t sc;
87  int     i=0;
88
89  puts( "read(" FILE_NAME ") - OK " );
90  do {
91    sc = read( TestFd, &ch, sizeof(ch) );
92    if ( sc == 1 ) {
93      if ( ch != (i%256) ) {
94        fprintf(
95          stderr,
96          "MISMATCH 0x%02x != 0x%02x at offset %d\n",
97          ch,
98          i % 256,
99          i
100        );
101        rtems_test_exit(0);
102      }
103      i++;
104    } else if ( sc != 0 ) {
105      fprintf(
106        stderr,
107        "ERROR - at offset %d - returned %zd and error=%s\n",
108        i,
109        sc,
110        strerror( errno )
111      );
112      rtems_test_exit(0);
113    }
114  } while ( sc > 0 );
115
116  if ( i == TotalWritten ) {
117    puts( "File correctly read until EOF returned\n" );
118  }
119}
120
121void truncate_helper(void)
122{
123  off_t position;
124  off_t new;
125  off_t sc;
126  int   rc;
127
128  position = lseek( TestFd, 0, SEEK_END );
129  printf( "Seek to end .. returned %d\n", (int) position );
130  rtems_test_assert( position == TotalWritten );
131
132  puts( "lseek/ftruncate loop.." );
133  new = position;
134  do {
135    sc = lseek( TestFd, new, SEEK_SET );
136    rtems_test_assert( sc == new );
137
138    rc = ftruncate( TestFd, new );
139    if ( rc != 0 ) {
140      fprintf(
141        stderr,
142        "ERROR - at offset %d - returned %d and error=%s\n",
143        (int) new,
144        rc,
145        strerror( errno )
146      );
147    }
148    rtems_test_assert( rc == 0 );
149    --new;
150  } while (new > 0);
151}
152
153void extend_helper(int eno)
154{
155  off_t position;
156  off_t new;
157  off_t sc;
158  int   rc;
159
160  position = lseek( TestFd, 0, SEEK_END );
161  printf( "Seek to end .. returned %d\n", (int) position );
162
163  /*
164   * test case to ftruncate a file to a length > its size
165   */
166
167  rc = ftruncate( TestFd, 2 );
168  rtems_test_assert( rc == 0 );
169
170  puts( "lseek/ftruncate loop.." );
171  new = position;
172  do {
173    sc = lseek( TestFd, new, SEEK_SET );
174    rtems_test_assert( sc == new );
175
176    rc = ftruncate( TestFd, new );
177    if ( rc != 0 ) {
178      if( errno != eno ) {
179        fprintf(
180          stderr,
181          "ERROR - at offset %d - returned %d and error=%s\n",
182          (int) new,
183          rc,
184          strerror( errno )
185        );
186        break;
187      }
188      else {
189        break;
190      }
191    }
192    rtems_test_assert( rc == 0 );
193    ++new;
194  } while ( 1 );
195}
196
197void close_it(void)
198{
199  int rc;
200
201  puts( "close(" FILE_NAME ") - OK " );
202  rc = close( TestFd );
203  rtems_test_assert( rc == 0 );
204}
205
206void unlink_it(void)
207{
208  int rc;
209
210  puts( "unlink(" FILE_NAME ") - OK" );
211  rc = unlink( FILE_NAME );
212  rtems_test_assert( rc == 0 );
213}
214
215rtems_task Init(
216  rtems_task_argument argument
217)
218{
219  int i;
220  void *alloc_ptr = (void *)0;
221  off_t position;
222  off_t new_position;
223  char buf [1];
224  ssize_t n;
225
226  puts( "\n\n*** TEST IMFS 01 ***" );
227
228  for (i=0 ; i<sizeof(Buffer) ; i++ )
229    Buffer[i] = (uint8_t) i;
230
231  open_it(false, true);
232  write_helper();
233  close_it();
234
235  puts( "" );
236
237  open_it(true, false);
238  read_helper();
239  close_it();
240 
241  open_it(false, false);
242  truncate_helper();
243
244  /*
245   * Allocate the heap, so that extend cannot be successful
246   */
247  alloc_ptr = malloc( malloc_free_space() - 4 );
248
249  extend_helper(ENOSPC);
250
251  /*
252   * free the allocated heap memory
253   */
254  free(alloc_ptr);
255
256  extend_helper(EFBIG);
257  position = lseek( TestFd , 0, SEEK_END );
258  new_position = lseek( TestFd, position + 2, SEEK_SET );
259  rtems_test_assert( new_position == position + 2 );
260
261  n = write( TestFd, buf, sizeof(buf) );
262  rtems_test_assert( n == -1 );
263  rtems_test_assert( errno == EFBIG );
264
265  close_it();
266  unlink_it();
267
268  puts( "*** END OF TEST IMFS 01 ***" );
269
270  rtems_test_exit(0);
271}
272
273/* configuration information */
274
275#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
276#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
277
278#define CONFIGURE_MAXIMUM_TASKS             1
279#define CONFIGURE_IMFS_MEMFILE_BYTES_PER_BLOCK 16
280#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
281#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
282
283#define CONFIGURE_INIT
284
285#include <rtems/confdefs.h>
286/* end of file */
Note: See TracBrowser for help on using the repository browser.