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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [tests/] [psxtests/] [psx13/] [test.c] - Blame information for rev 609

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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