source: rtems/testsuites/psxtests/psx13/test.c @ 161f54b4

4.104.11
Last change on this file since 161f54b4 was 161f54b4, checked in by Joel Sherrill <joel.sherrill@…>, on Dec 28, 2009 at 4:43:10 PM

2009-12-28 Shrikant Gaikwad <n3oo3n@…>

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