OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rtems-20020807/] [c/] [src/] [tests/] [psxtests/] [psx13/] [test.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1026 ivang
/*
2
 *  Psx13
3
 *  Chris Bond (working under Jennifer's account)
4
 *
5
 *  This test exercises the following routines:
6
 *
7
 *     device_lseek - test implemented
8
 *     dup          - test implemented
9
 *     dup2         - test implemented
10
 *     fdatasync    - test implemented
11
 *     fsync        - test implemented
12
 *     pathconf     - test implemented
13
 *     fpathconf    - test implemented
14
 *     pipe         - test implemented
15
 *     umask        - test implemented
16
 *     utime        - test implemented
17
 *
18
 *  COPYRIGHT (c) 1989-1999.
19
 *  On-Line Applications Research Corporation (OAR).
20
 *
21
 *  The license and distribution terms for this file may be
22
 *  found in the file LICENSE in this distribution or at
23
 *  http://www.OARcorp.com/rtems/license.html.
24
 *
25
 *  test.c,v 1.5 2002/08/02 00:53:21 joel Exp
26
 */
27
 
28
#include <rtems.h>
29
#include <rtems/libio.h>
30
#include <fcntl.h>
31
#include <unistd.h>
32
#include <errno.h>
33
#include <utime.h>
34
 
35
#include <stdio.h>
36
 
37
#include <pmacros.h>
38
 
39
/*-------------------------------------------------------------------
40
 * InitFiles function
41
 *
42
 * Initializes the three files to be used in the test.
43
 *
44
 * arguments: none
45
 * assumptions: fopen, fprintf, fwrite, FILE are available
46
 * actions: creates testfile1, a text file with 'a'..'z' listed 4 times.
47
 *          creates testfile2, a text file with 'a'..'z' listed 4 times.
48
 *          creates testfile3, a binary file with 0..9 listed 4 times.
49
 * returns: TRUE if files opened successfully.
50
 *          FALSE if fail on file open for write.
51
 *
52
 * ------------------------------------------------------------------
53
 */
54
 
55
int InitFiles (void) {
56
 
57
  int count;
58
  FILE *fp1, *fp2, *fp3;
59
  char letter;
60
  int number;
61
  int retval;
62
 
63
  fp1 = fopen("testfile1.tst", "wt");
64
  fp2 = fopen("testfile2.tst", "wt");
65
  fp3 = fopen("testfile4.tst", "wb");
66
 
67
  if ((fp1 != NULL) && (fp2 != NULL) && (fp3 !=NULL)) {
68
 
69
    letter = 'a';
70
 
71
    for (count=0 ; count<(26*4); ++count) {
72
      fprintf (fp1, "%c", letter);
73
      fprintf (fp2, "%c", letter);
74
 
75
      ++letter;
76
      if (letter > 'z')
77
        letter = 'a';
78
    }
79
 
80
    number = 0;
81
 
82
    for (count = 0; count <40; ++count) {
83
 
84
      fwrite (&number, 1, sizeof(int), fp3);
85
 
86
      ++number;
87
      if (number > 9)
88
        number = 0;
89
    }
90
 
91
    fclose(fp1);
92
    fclose(fp2);
93
    fclose(fp3);
94
 
95
    retval = TRUE;
96
  }
97
 
98
  else
99
    retval = FALSE;
100
 
101
  /* assert (retval == TRUE);*/
102
 
103
  return (retval);
104
}
105
 
106
/* ---------------------------------------------------------------
107
 * DeviceLSeekTest function
108
 *
109
 * Hits the device_lseek code by lseeking on the console.
110
 *
111
 * arguments: none
112
 * assumptions: lseek available
113
 * actions: hits lseek with some dummy arguments.
114
 * returns: value of return from lseek.
115
 *
116
 * ---------------------------------------------------------------
117
 */
118
 
119
int DeviceLSeekTest (void) {
120
 
121
  int error = -1, retval = FALSE;
122
 
123
  int fd = open ("/dev/console", O_RDONLY);
124
 
125
  error = lseek(fd, 5, SEEK_SET);
126
 
127
  if (error == 0)
128
    retval = TRUE;
129
  else
130
    retval = FALSE;
131
 
132
  /* assert (retval == TRUE);*/
133
 
134
  return (retval);
135
 
136
}
137
 
138
/* ---------------------------------------------------------------
139
 * DupTest function
140
 *
141
 * Hits the dup code.
142
 *
143
 * arguments: none
144
 * assumptions: dup, open, close, fcntl available.
145
 * actions: Gets a file descriptor(fd1) for test file1.
146
 *          dups fd1 to fd2.
147
 *          sets fd1 to append mode
148
 *          checks fd2 to ensure it's in append mode, also.
149
 * returns: success if fd2 is indeed a copy of fd1.
150
 *
151
 * ---------------------------------------------------------------
152
 */
153
 
154
int DupTest(void) {
155
 
156
  int fd1, fd2;
157
 
158
  int flags = 0, retval = FALSE;
159
 
160
  fd1 = open ("testfile1.tst", O_RDONLY);
161
  fd2 = dup(fd1);
162
 
163
  if (fd2 != -1) {
164
 
165
    fcntl(fd1, F_SETFL, O_APPEND);
166
    flags = fcntl(fd2, F_GETFL);
167
 
168
    close (fd1);
169
 
170
    flags = (flags & O_APPEND);
171
 
172
    retval = (flags == O_APPEND);
173
  }
174
 
175
  else
176
    retval = FALSE;
177
 
178
  /* assert (retval == TRUE);*/
179
 
180
  return (retval);
181
 
182
}
183
 
184
/* ---------------------------------------------------------------
185
 * Dup2Test function
186
 *
187
 * Hits the dup2 code.
188
 *
189
 * arguments: none
190
 * assumptions: dup, dup2, open, close, fcntl available.
191
 * actions: Gets a file descriptor(fd1) for test file1.
192
 *          dups fd1 to fd2.
193
 *          sets fd1 to append mode
194
 *          checks fd2 to ensure it's in append mode, also.
195
 *          sets fd1 to invalid value, fd2 to valid, tries to dup2.
196
 *          sets fd2 to invalid value, fd1 to valid tries to dup2.
197
 * returns: success if fd2 is a copy of fd1, and invalid fd1 or fd2 produce errors.
198
 *
199
 * ---------------------------------------------------------------
200
 */
201
 
202
int Dup2Test(void) {
203
 
204
  int fd1, fd2;
205
 
206
  int flags = 0, retval = FALSE;
207
 
208
  int error = 0;
209
 
210
  fd1 = open ("testfile1.tst", O_RDONLY);
211
  fd2 = open ("testfile2.tst", O_RDONLY);
212
  error = dup2(fd1, fd2);
213
 
214
  /* make sure dup2 works if both fd1 and fd2 are valid file descriptors. */
215
 
216
  if (error != -1) {
217
 
218
    fcntl(fd1, F_SETFL, O_APPEND);
219
    flags = fcntl(fd1, F_GETFL);
220
 
221
    flags = (flags & O_APPEND);
222
    retval = (flags == O_APPEND);
223
  }
224
 
225
  else {
226
    retval = FALSE;
227
    close(fd2);
228
  }
229
 
230
  if (retval == TRUE) {
231
 
232
    /* make sure dup2 fails correctly if one or the other arguments are invalid. */
233
    /* this assumes -1 is an invalid value for a file descriptor!!! (POSIX book, p.135) */
234
 
235
    fd1 = -1;
236
 
237
    if (dup2 (fd1, fd2) != -1)
238
      retval = FALSE;
239
    else {
240
      fd1 = dup(fd2);
241
      fd2 = -1;
242
 
243
      if (dup2(fd1, fd2) != -1)
244
        retval = FALSE;
245
    }
246
  }
247
 
248
  close (fd1);
249
 
250
  /* assert (retval == TRUE);*/
251
 
252
  return (retval);
253
 
254
}
255
 
256
/* ---------------------------------------------------------------
257
 * FDataSyncTest function
258
 *
259
 * Hits the fdatasync code. Does NOT test the functionality of the
260
 * underlying fdatasync entry in the IMFS op table.
261
 *
262
 * arguments: none
263
 * assumptions: open, close, fdatasync functions available.
264
 * actions: attempts to fdatasync a file descriptor flagged as read-only.
265
 *          attempts to fdatasync an invalid file descriptor (-1).
266
 *          attempts to fdatasync a perfectly valid fd opened as RDWR
267
 *
268
 * returns: TRUE if attempt to fdatasync invalid and read-only filed escriptor fail, and fdatasync succeeds on valid fd.
269
 *          FALSE otherwise.
270
 *
271
 * ---------------------------------------------------------------
272
 */
273
 
274
int FDataSyncTest(void) {
275
 
276
  int fd = -1;
277
  int error = 0, retval = TRUE;
278
 
279
  /* Try it with a RD_ONLY file. */
280
 
281
  fd = open ("testfile1.tst", O_RDONLY);
282
 
283
  error = fdatasync(fd);
284
  if ((error == -1) && (errno == EINVAL))
285
    retval = TRUE;
286
  else
287
    retval = FALSE;
288
 
289
  close (fd);
290
 
291
  if (retval == TRUE) {
292
 
293
    /* Try it with a bad file descriptor */
294
 
295
    fd = -1;
296
 
297
    error = fdatasync(fd);
298
    if ((errno == EBADF) && (error == -1))
299
      retval = TRUE;
300
    else
301
      retval = FALSE;
302
  }
303
 
304
  /* Okay - now the success case... */
305
 
306
  if (retval == TRUE) {
307
    fd = open ("testfile1.tst", O_RDWR);
308
    error = fdatasync(fd);
309
 
310
    if (error == 0)
311
      retval = TRUE;
312
    else
313
      retval = FALSE;
314
 
315
    close (fd);
316
 
317
  }
318
 
319
  /* assert (retval == TRUE);*/
320
 
321
  return (retval);
322
}
323
 
324
/* ---------------------------------------------------------------
325
 * UMaskTest function
326
 *
327
 * Hits the umask code.
328
 *
329
 * arguments: none
330
 * assumptions: umask function available.
331
 * actions: set umask to 0ctal 23.
332
 *          set umask to Octal 22, retrieve the old value.
333
 *
334
 * returns: TRUE if old value is 23,
335
 *          FALSE otherwise.
336
 *
337
 * ---------------------------------------------------------------
338
 */
339
 
340
int UMaskTest (void) {
341
 
342
  int error = 0, retval = FALSE;
343
 
344
  umask (023);
345
  error = umask(022);
346
 
347
  if (error == 023)
348
    retval = TRUE;
349
  else
350
    retval = FALSE;
351
 
352
  /* assert (retval == TRUE);*/
353
 
354
  return(retval);
355
 
356
}
357
 
358
/* ---------------------------------------------------------------
359
 * UTimeTest function
360
 *
361
 * Hits the utime code. Does NOT test the functionality of the underlying utime
362
 * entry in the IMFS op table.
363
 *
364
 * arguments: none
365
 * assumptions: utime function available.
366
 * actions: set utime for an invalid filename.
367
 *          set utime for a valid filename.
368
 *
369
 * returns: TRUE if time on valid file is set correctly and utime failed on an invaid filename.
370
 *          FALSE otherwise.
371
 *
372
 * ---------------------------------------------------------------
373
 */
374
 
375
int UTimeTest (void) {
376
 
377
  int error = 0, retval = FALSE;
378
  struct utimbuf time;
379
  struct stat fstat;
380
 
381
  /* First, an invalid filename. */
382
  error = utime("!This is an =invalid p@thname!!! :)", NULL);
383
 
384
  if (error == -1)
385
    retval = TRUE;
386
  else
387
    retval = FALSE;
388
 
389
  /* Now, the success test. */
390
  if (retval == TRUE) {
391
 
392
    time.actime  = 12345;
393
    time.modtime = 54321;
394
 
395
    error = utime("testfile1.tst", &time);
396
 
397
    if (error == 0) {
398
 
399
      /* But, did it set the time? */
400
      stat ("testfile1.tst", &fstat);
401
 
402
      if ((fstat.st_atime == 12345) && (fstat.st_mtime == 54321 ))
403
        retval = TRUE;
404
      else
405
        retval = FALSE;
406
    }
407
 
408
    else
409
      retval = FALSE;
410
  }
411
 
412
  /* assert (retval == TRUE);*/
413
 
414
  return (retval);
415
 
416
}
417
 
418
/* ---------------------------------------------------------------
419
 * PipeTest function
420
 *
421
 * Hits the pipe code.
422
 *
423
 * arguments: none
424
 * assumptions: pipe function available.
425
 * actions: call pipe.
426
 *
427
 * returns: TRUE if pipe retuens ENOSYS,
428
 *          FALSE otherwise.
429
 *
430
 * ---------------------------------------------------------------
431
 */
432
 
433
int PipeTest (void) {
434
 
435
  int error = 0, retval = FALSE;
436
  int fd[2];
437
 
438
  error = pipe(fd);
439
 
440
  if ((error == -1) && (errno == ENOSYS))
441
    retval = TRUE;
442
  else
443
    retval = FALSE;
444
 
445
  /* assert (retval == TRUE);*/
446
 
447
  return(retval);
448
 
449
}
450
 
451
/* ---------------------------------------------------------------
452
 * PathConfTest function
453
 *
454
 * Hits the pathconf code.
455
 *
456
 * arguments: none
457
 * assumptions: pathconf function available.
458
 * actions: Try to pathconf a bad filename.
459
 *          Try to pathconf a good filename.
460
 *
461
 * returns: TRUE if pathconf fails on bad file, succeeds on good file.
462
 *          FALSE otherwise.
463
 *
464
 * ---------------------------------------------------------------
465
 */
466
 
467
int PathConfTest (void) {
468
 
469
  int error = 0, retval = FALSE;
470
 
471
  error = pathconf("thisfiledoesnotexist", _PC_LINK_MAX);
472
 
473
  if (error == -1) {
474
    error = pathconf("testfile1.tst", _PC_LINK_MAX);
475
 
476
    if (error != -1)
477
      retval = TRUE;
478
    else
479
      retval = FALSE;
480
  }
481
 
482
  else
483
    retval = FALSE;
484
 
485
  /* assert (retval == TRUE);*/
486
 
487
  return(retval);
488
 
489
}
490
 
491
/* ---------------------------------------------------------------
492
 * FPathConfTest function
493
 *
494
 * Hits the fpathconf code.
495
 *
496
 * arguments: none
497
 * assumptions: fpathconf function available.
498
 * actions: Call fpathconf with all arguments, plus an invalid.
499
 *
500
 * returns: TRUE always.
501
 *
502
 * ---------------------------------------------------------------
503
 */
504
 
505
int FPathConfTest (void) {
506
 
507
  int error = 0, retval = TRUE;
508
 
509
  int fd  = -1;
510
 
511
  error = fpathconf(fd, _PC_LINK_MAX);
512
 
513
  if (error == -1) {
514
    fd = open("testfile1.tst", O_RDWR);
515
 
516
    error = fpathconf(fd, _PC_LINK_MAX);
517
    error = fpathconf(fd, _PC_MAX_CANON);
518
    error = fpathconf(fd, _PC_MAX_INPUT);
519
    error = fpathconf(fd, _PC_NAME_MAX);
520
    error = fpathconf(fd, _PC_PATH_MAX);
521
    error = fpathconf(fd, _PC_PIPE_BUF);
522
    error = fpathconf(fd, _PC_CHOWN_RESTRICTED);
523
    error = fpathconf(fd, _PC_NO_TRUNC);
524
    error = fpathconf(fd, _PC_VDISABLE);
525
    error = fpathconf(fd, _PC_ASYNC_IO);
526
    error = fpathconf(fd, _PC_PRIO_IO);
527
    error = fpathconf(fd, _PC_SYNC_IO);
528
    error = fpathconf(fd, 255);
529
 
530
    retval = TRUE;
531
  }
532
 
533
  else
534
    retval = FALSE;
535
 
536
  /* assert (retval == TRUE);*/
537
 
538
  return(retval);
539
 
540
}
541
 
542
/* ---------------------------------------------------------------
543
 * FSyncTest function
544
 *
545
 * Hits the fsync code.
546
 *
547
 * arguments: none
548
 * assumptions: open, fsync functions available.
549
 * actions: open test file,
550
 *          try to fsync it.
551
 *
552
 * returns: TRUE if fsync doesn't return -1,
553
 *          FALSE otherwise.
554
 *
555
 * ---------------------------------------------------------------
556
 */
557
 
558
int FSyncTest (void) {
559
 
560
  int error = 0, retval = FALSE;
561
  int fd = -1;
562
 
563
  fd = open("testfile1.tst", O_RDWR);
564
 
565
  if (fd != -1) {
566
 
567
    error = fsync(fd);
568
 
569
    if (error != -1)
570
      retval = TRUE;
571
    else
572
      retval = FALSE;
573
 
574
    close(fd);
575
  }
576
 
577
  else
578
    retval = FALSE;
579
 
580
  /* assert (retval == TRUE);*/
581
 
582
  return(retval);
583
 
584
}
585
 
586
/* ---------------------------------------------------------------
587
 * Main function
588
 *
589
 *  main entry point to the test
590
 *
591
 * ---------------------------------------------------------------
592
 */
593
 
594
#if defined(__rtems__)
595
int test_main(void)
596
#else
597
int main(
598
  int    argc,
599
  char **argv
600
)
601
#endif
602
{
603
  puts( "*** POSIX TEST 13 ***" );
604
 
605
  if (InitFiles() == TRUE) {
606
    printf ("\nFiles initialized successfully.\n");
607
 
608
    printf ("Testing device_lseek()... ");
609
    if (DeviceLSeekTest() == TRUE)
610
      printf ("Success.\n");
611
    else
612
      printf ("Failed!!!\n");
613
 
614
    printf ("Testing dup()............ ");
615
    if (DupTest() == TRUE)
616
      printf ("Success.\n");
617
    else
618
      printf ("Failed!!!\n");
619
 
620
    printf ("Testing dup2()........... ");
621
    if (Dup2Test() == TRUE)
622
      printf ("Success.\n");
623
    else
624
      printf ("Failed!!!\n");
625
 
626
    printf ("Testing fdatasync()...... ");
627
    if (FDataSyncTest() == TRUE)
628
      printf ("Success.\n");
629
    else
630
      printf ("Failed!!!\n");
631
 
632
    printf ("Testing umask().......... ");
633
    if (UMaskTest() == TRUE)
634
      printf ("Success.\n");
635
    else
636
      printf ("Failed!!!\n");
637
 
638
   printf ("Testing utime().......... ");
639
    if (UTimeTest() == TRUE)
640
      printf ("Success.\n");
641
    else
642
      printf ("Failed!!!\n");
643
 
644
   printf ("Testing pipe()........... ");
645
    if (PipeTest() == TRUE)
646
      printf ("Success.\n");
647
    else
648
      printf ("Failed!!!\n");
649
 
650
   printf ("Testing fsync().......... ");
651
    if (FSyncTest() == TRUE)
652
      printf ("Success.\n");
653
    else
654
      printf ("Failed!!!\n");
655
 
656
   printf ("Testing pathconf()....... ");
657
    if (PathConfTest() == TRUE)
658
      printf ("Success.\n");
659
    else
660
      printf ("Failed!!!\n");
661
 
662
   printf ("Testing fpathconf()...... ");
663
    if (FPathConfTest() == TRUE)
664
      printf ("Success.\n");
665
    else
666
      printf ("Failed!!!\n");
667
  }
668
 
669
 
670
  else
671
    printf ("\n\nError opening files for write!!!!\n");
672
 
673
  printf( "\n\n*** END OF TEST PSX13 ***" );
674
  rtems_test_exit(0);
675
}
676
 
677
 
678
 
679
 
680
 
681
 
682
 
683
 
684
 
685
 
686
 
687
 
688
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.