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

4.115
Last change on this file since cafefbf was cafefbf, checked in by Ralf Corsepius <ralf.corsepius@…>, on 02/22/11 at 09:47:36

Add HAVE_CONFIG_H.

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