summaryrefslogtreecommitdiffstats
path: root/testsuites/psxtests/psximfs01/init.c
blob: c567eee10fe6c94fbf023c9aec6f6edda7ade920 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 *  COPYRIGHT (c) 1989-2010.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id$
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <tmacros.h>
#include "test_support.h"

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <rtems/libcsupport.h>

int TestFd;
uint8_t Buffer[256];
ssize_t TotalWritten;

#define FILE_NAME "biggie"

void open_it(bool readOnly, bool create)
{
  int flag = 0;

  if ( readOnly )
    flag |= O_RDONLY;
  else {
    if ( create )
      flag |= O_CREAT;
    flag |= O_RDWR;
  }

  /* open the file */
  puts( "open(" FILE_NAME ") - OK " );
  TestFd = open( FILE_NAME, flag, 0777 );
  rtems_test_assert( TestFd != -1 );
}

void write_helper(void)
{
  ssize_t written;

  TotalWritten = 0;
  puts( "write(" FILE_NAME ") - OK " );
  do {
    written = write( TestFd, Buffer, sizeof(Buffer) );
    if ( written == -1 ) {
      if ( errno == ENOSPC ) {
        printf( "Total written = %zd\n", TotalWritten );
        return;
      }
      fprintf(
        stderr,
        "Unable to create largest IMFS file (error=%s)\n",
        strerror(errno)
      );
      rtems_test_exit(0);
    }
    TotalWritten += written;
  } while (1);
  
}

void read_helper(void)
{
  uint8_t ch;
  ssize_t sc;
  int     i=0;

  puts( "read(" FILE_NAME ") - OK " );
  do {
    sc = read( TestFd, &ch, sizeof(ch) );
    if ( sc == 1 ) {
      if ( ch != (i%256) ) {
        fprintf(
          stderr,
          "MISMATCH 0x%02x != 0x%02x at offset %d\n",
          ch,
          i % 256,
          i
        );
        rtems_test_exit(0);
      }
      i++;
      continue;
    }
    /* Unsure if ENOSPC is the write error to be returned */
    if ( errno == ENOSPC && i == TotalWritten ) {
      puts( "File correctly read until ENOSPC returned\n" );
      return;
    }
    fprintf(
      stderr,
      "ERROR - at offset %d - returned %zd and error=%s\n",
      i,
      sc,
      strerror( errno )
    );
    rtems_test_exit(0);
  } while (1);
}

void truncate_helper(void)
{
  off_t position;
  off_t new;
  off_t sc;
  int   rc;

  position = lseek( TestFd, 0, SEEK_END );
  printf( "Seek to end .. returned %d\n", (int) position );
  rtems_test_assert( position == TotalWritten );

  puts( "lseek/ftruncate loop.." );
  new = position;
  do {
    sc = lseek( TestFd, new, SEEK_SET );
    rtems_test_assert( sc == new );

    rc = ftruncate( TestFd, new );
    if ( rc != 0 ) {
      fprintf(
	stderr,
	"ERROR - at offset %d - returned %d and error=%s\n",
	(int) new,
	rc,
	strerror( errno )
      );
    }
    rtems_test_assert( rc == 0 );
    --new;
  } while (new > 0);
}

void extend_helper(void)
{
  off_t position;
  off_t new;
  off_t sc;
  int   rc;

  position = lseek( TestFd, 0, SEEK_END );
  printf( "Seek to end .. returned %d\n", (int) position );

  /* 
   * test case to ftruncate a file to a length > its size 
   */

  rc = ftruncate( TestFd, 2 );
  rtems_test_assert( rc == 0 );

  puts( "lseek/ftruncate loop.." );
  new = position;
  do {
    sc = lseek( TestFd, new, SEEK_SET );
    if( sc == -1 ) {
      if( errno == ENOSPC ) {
	break;
      }
      else {
	rtems_test_assert( 0 );
      }
    }

    rc = ftruncate( TestFd, new );
    if ( rc != 0 ) {
      if( errno != ENOSPC ) {
	fprintf(
	  stderr,
	  "ERROR - at offset %d - returned %d and error=%s\n",
	  (int) new,
	  rc,
	  strerror( errno )
        );
	break;
      }
      else {
	break;
      }
    }
    rtems_test_assert( rc == 0 );
    ++new;
  } while ( 1 );
}

void close_it(void)
{
  int rc;

  puts( "close(" FILE_NAME ") - OK " );
  rc = close( TestFd );
  rtems_test_assert( rc == 0 );
}

void unlink_it(void)
{
  int rc;

  puts( "unlink(" FILE_NAME ") - OK" );
  rc = unlink( FILE_NAME );
  rtems_test_assert( rc == 0 );
}

rtems_task Init(
  rtems_task_argument argument
)
{
  int i;
  void *alloc_ptr = (void *)0;
  int position = 0;
  int status = 0;

  puts( "\n\n*** TEST IMFS 01 ***" );

  for (i=0 ; i<sizeof(Buffer) ; i++ )
    Buffer[i] = (uint8_t) i;

  open_it(false, true);
  write_helper();
  close_it();

  puts( "" );

  open_it(true, false);
  read_helper();
  close_it();
  
  open_it(false, false);
  truncate_helper();

  /*
   * Allocate the heap, so that extend cannot be successful
   */
  alloc_ptr = malloc( malloc_free_space() - 4 );

  extend_helper();

  /* 
   * free the allocated heap memory
   */
  free(alloc_ptr);

  extend_helper();
  position = lseek( TestFd , 0, SEEK_END );
  status = lseek( TestFd, position+2, SEEK_SET );
  rtems_test_assert( status == -1 );
  rtems_test_assert( errno == ENOSPC );

  close_it();
  unlink_it();

  puts( "*** END OF TEST IMFS 01 ***" );

  rtems_test_exit(0);
}

/* configuration information */

#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER

#define CONFIGURE_MAXIMUM_TASKS             1
#define CONFIGURE_IMFS_MEMFILE_BYTES_PER_BLOCK 16
#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT

#include <rtems/confdefs.h>
/* end of file */