summaryrefslogtreecommitdiffstats
path: root/testsuites/psxtests/psx13/test.c
blob: e98b03a6f57ce6be60e142974ea1c86bfc276353 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*  SPDX-License-Identifier: BSD-2-Clause */

/**
 *  @file
 *
 *  @brief This tests various file system functions.
 *
 *  This test exercises the following routines:
 *
 *     - lseek()
 *     - dup()
 *     - dup2()
 *     - fdatasync()
 *     - fsync()
 *     - pathconf()
 *     - fpathconf()
 *     - umask()
 *     - utime()
 *     - utimes()
 *     - sync()
 */

/*
 *  COPYRIGHT (c) 1989-2009, 2021.
 *  On-Line Applications Research Corporation (OAR).
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

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

#include <rtems.h>
#include <rtems/libio.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <utime.h>
#include <tmacros.h>

#include <stdio.h>
#include <unistd.h>

#include <pmacros.h>

const char rtems_test_name[] = "PSX 13";

/**
 * @brief Initializes the three files to be used for the test.
 */
static void InitFiles( void )
{
  int count;
  int rv;
  FILE *fp1, *fp2, *fp3;
  char letter;
  int number;

  fp1 = fopen( "testfile1.tst", "wt" );
  rtems_test_assert( fp1 != NULL );

  fp2 = fopen( "testfile2.tst", "wt" );
  rtems_test_assert( fp2 != NULL );

  fp3 = fopen( "testfile4.tst", "wb" );
  rtems_test_assert( fp3 != NULL );

  letter = 'a';

  for( count = 0 ; count < (26*4); ++count) {
    fprintf( fp1, "%c", letter );
    fprintf( fp2, "%c", letter );

    ++letter;
    if( letter > 'z' )
      letter = 'a';
  }

  number = 0;

  for( count = 0; count < 40; ++count ) {
    fwrite( &number, 1, sizeof(int), fp3 );

    ++number;
    if( number > 9 )
      number = 0;
  }

  rv = fclose( fp1 );
  rtems_test_assert( rv != EOF );

  rv = fclose( fp2 );
  rtems_test_assert( rv != EOF );

  rv = fclose( fp3 );
  rtems_test_assert( rv != EOF );
}

/**
 * @brief Exercises lseek() by lseeking on the console.
 */
static void DeviceLSeekTest( void )
{
  int rv;
  int fd;

  fd = open( "/dev/console", O_RDONLY );
  rtems_test_assert( fd != -1 );

  rv = lseek( fd, 5, SEEK_SET );
  rtems_test_assert( rv == -1 );
  rtems_test_assert( errno == ESPIPE );

  rv = close( fd );
  rtems_test_assert( rv == 0 );
}

/**
 * @brief Exercises dup().
 */
static void DupTest( void )
{
  int fd1, fd2;
  int flags;
  int rv;

  fd1 = open( "testfile1.tst", O_RDONLY );
  rtems_test_assert( fd1 != -1 );

  fd2 = dup( fd1 );
  rtems_test_assert( fd2 != -1 );

  rv = fcntl( fd1, F_SETFL, O_APPEND );
  rtems_test_assert( rv != -1 );

  flags = fcntl( fd2, F_GETFL ) & O_APPEND;
  rtems_test_assert( flags == 0 );

  rv = close( fd1 );
  rtems_test_assert( rv == 0 );

  rv = close( fd2 );
  rtems_test_assert( rv == 0 );
}

/**
 * @brief Exercises dup2().
 */
static void Dup2Test( void )
{
  int fd1, fd2;
  int flags;
  int rv;

  fd1 = open( "testfile1.tst", O_RDONLY );
  rtems_test_assert( fd1 != -1 );

  fd2 = open( "testfile2.tst", O_RDONLY );
  rtems_test_assert( fd2 != -1 );

  /* make sure dup2 works if both fd1 and fd2 are valid file descriptors. */
  rv = dup2( fd1, fd2 );
  rtems_test_assert( rv != -1 );

  rv = fcntl( fd1, F_SETFL, O_APPEND );
  rtems_test_assert( rv != -1 );

  flags = fcntl( fd1, F_GETFL ) & O_APPEND;
  rtems_test_assert( flags == O_APPEND );

  /* make sure dup2 fails correctly if one or the other arguments are invalid. */
  /* this assumes -1 is an invalid value for a file descriptor!!! (POSIX book, p.135) */
  rv = close( fd1 );
  rtems_test_assert( rv == 0 );

  fd1 = -1;

  rv = dup2( fd1, fd2 );
  rtems_test_assert( rv == -1 );

  fd1 = dup( fd2 );
  fd2 = -1;

  rv = dup2( fd1, fd2 );
  rtems_test_assert( rv == -1 );

  rv = close( fd1 );
  rtems_test_assert( rv == 0 );
}

/**
 * @brief Exercises fdatasync(). Does NOT test the functionality of the
 *        underlying fdatasync entry in the IMFS op table.
 */
static void FDataSyncTest( void )
{
  int fd;
  int rv;

  /* Try it with a RD_ONLY file. */
  fd = open( "testfile1.tst", O_RDONLY );
  rtems_test_assert( fd != -1 );

  rv = fdatasync( fd );
  rtems_test_assert( rv == -1 );
  rtems_test_assert( errno == EBADF );

  rv = close(fd);
  rtems_test_assert( rv == 0 );

  /* Try it with a bad file descriptor */
  fd = -1;

  rv = fdatasync( fd );
  rtems_test_assert( rv == -1 );
  rtems_test_assert( errno == EBADF );

  /* Okay - now the success case... */
  fd = open( "testfile1.tst", O_RDWR );
  rv = fdatasync( fd );
  rtems_test_assert( rv == 0 );

  rv = close( fd );
  rtems_test_assert( rv == 0 );
}

/**
 * @brief Exercises umask().
 */
static void UMaskTest( void )
{
  mode_t rv;

  (void) umask( 023 );

  rv = umask( 022 );
  rtems_test_assert( rv == 023 );
}

/**
 * @brief Exercises utime(). Does not test the functionality of the
 *        underlying utime entry in the IMFS op table.
 */
static void UTimeTest( void )
{
  int rv;
  struct utimbuf time;
  struct stat fstat;

  /* First, an invalid filename. */
  rv = utime( "!This is an =invalid p@thname!!! :)", NULL );
  rtems_test_assert( rv == -1 );
  rtems_test_assert( errno == ENOENT );

  /* Now, the success test. */
  time.actime  = 12345;
  time.modtime = 54321;

  rv = utime( "testfile1.tst", &time );
  rtems_test_assert( rv == 0 );

  /* But, did it set the time? */
  rv = stat( "testfile1.tst", &fstat );
  rtems_test_assert( rv == 0 );
  rtems_test_assert( fstat.st_atime == 12345 );
  rtems_test_assert( fstat.st_mtime == 54321 );

  rv = utime( "testfile1.tst", NULL );
  rtems_test_assert( rv == 0 );
}

/**
 * @brief Exercises utimes(). Does NOT test the functionality of the
 *        underlying utime entry in the IMFS op table.
 */
static void UTimesTest( void )
{
  int rv;
  struct timeval time[2];
  struct stat fstat;

  /* First, an invalid filename. */
  rv = utimes( "!This is an =invalid p@thname!!! : )", NULL);
  rtems_test_assert( rv == -1 );
  rtems_test_assert( errno == ENOENT );

  /* Now, the success test. */
  time[0].tv_sec = 12345;
  time[1].tv_sec = 54321;

  rv = utimes( "testfile1.tst", (struct timeval *)&time );
  rtems_test_assert( rv == 0 );

  /* But, did it set the time? */
  rv = stat( "testfile1.tst", &fstat );
  rtems_test_assert( rv == 0 );
  rtems_test_assert( fstat.st_atime == 12345 );
  rtems_test_assert( fstat.st_mtime == 54321 );
}

/**
 * @brief Exercises pathconf().
 */
static void PathConfTest( void )
{
  int rv;

  rv = pathconf( "thisfiledoesnotexist", _PC_LINK_MAX );
  rtems_test_assert( rv == -1 );

  rv = pathconf( "testfile1.tst", _PC_LINK_MAX );
  rtems_test_assert( rv != -1 );
}

/**
 * @brief Exercises fpathconf().
 */
static void FPathConfTest( void )
{
  int rv;
  int fd;

  fd = -1;
  rv = fpathconf( fd, _PC_LINK_MAX );
  rtems_test_assert( rv == -1 );

  fd = open( "testfile1.tst", O_RDWR );
  rtems_test_assert( fd != -1 );

  rv = fpathconf( fd, _PC_LINK_MAX );
  rtems_test_assert( rv != -1 );

  rv = fpathconf( fd, _PC_MAX_CANON );
  rtems_test_assert( rv != -1 );

  rv = fpathconf( fd, _PC_MAX_INPUT );
  rtems_test_assert( rv != -1 );

  rv = fpathconf( fd, _PC_NAME_MAX );
  rtems_test_assert( rv != -1 );

  rv = fpathconf( fd, _PC_PATH_MAX );
  rtems_test_assert( rv != -1 );

  rv = fpathconf( fd, _PC_PIPE_BUF );
  rtems_test_assert( rv != -1 );

  rv = fpathconf( fd, _PC_CHOWN_RESTRICTED );
  rtems_test_assert( rv != -1 );

  rv = fpathconf( fd, _PC_NO_TRUNC );
  rtems_test_assert( rv != -1 );

  rv = fpathconf( fd, _PC_VDISABLE );
  rtems_test_assert( rv != -1 );

  rv = fpathconf( fd, _PC_ASYNC_IO );
  rtems_test_assert( rv != -1 );

  rv = fpathconf( fd, _PC_PRIO_IO );
  rtems_test_assert( rv != -1 );

  rv = fpathconf( fd, _PC_SYNC_IO );
  rtems_test_assert( rv != -1 );

  rv = fpathconf( fd, 255 );
  rtems_test_assert( rv == -1 );

  rv = close( fd );
  rtems_test_assert( rv == 0 );

  fd = open( "testfile1.tst", O_WRONLY );
  rtems_test_assert( rv != -1 );

  rv = fpathconf( fd, _PC_LINK_MAX );
  rtems_test_assert( rv != -1 );

  rv = close( fd );
  rtems_test_assert( rv == 0 );
}

/**
 * @brief Exercises fsync().
 */
static void FSyncTest( void )
{
  int rv;
  int fd;

  fd = open( "testfile1.tst", O_RDWR );
  rtems_test_assert( fd != -1 );

  rv = fsync(fd);
  rtems_test_assert( rv != -1 );

  rv = close( fd );
  rtems_test_assert( rv == 0 );
}

/**
 * @brief Exercises sync().
 */
static void SyncTest( void )
{
  sync();
}

/**
 * @brief The main entry point to the test.
 */
int test_main( void );
int test_main( void )
{
  TEST_BEGIN();

  InitFiles();

  DeviceLSeekTest();
  DupTest();
  Dup2Test();
  FDataSyncTest();
  UMaskTest();
  UTimeTest();
  UTimesTest();
  FSyncTest();
  PathConfTest();
  FPathConfTest();
  SyncTest();

  TEST_END();

  rtems_test_exit( 0 );
}