source: rtems/testsuites/psxtests/psximfs01/init.c @ 9a4eca5

5
Last change on this file since 9a4eca5 was 698c2e50, checked in by Sebastian Huber <sebastian.huber@…>, on 03/25/14 at 07:06:16

tests/psxtests: Use <rtems/test.h>

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