source: rtems/testsuites/psxtests/psx13/test.c @ 8c26e798

4.115
Last change on this file since 8c26e798 was 8c26e798, checked in by Sebastian Huber <sebastian.huber@…>, on 03/25/14 at 15:45:08

tests: Produce proper begin/end messages

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