home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / syscalls / open / open.c.foo < prev    next >
Encoding:
Text File  |  1990-05-31  |  4.9 KB  |  213 lines

  1. /*
  2.  * openTest.c --
  3.  *    Test of the open system call. 
  4.  *
  5.  *
  6.  *
  7.  */
  8.  
  9. #include <sprite.h>
  10. #include <stdio.h>
  11. #include <sys/types.h>
  12. #include <sys/time.h>
  13. #include <sys/file.h>
  14. #include <sys/stat.h>
  15.  
  16.  
  17. void
  18. main()
  19. {
  20.  
  21.     openTest();
  22.     exit((errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
  23. }
  24.  
  25.  
  26.  
  27. static void
  28. openTest()
  29. {
  30.     char tempfile[MAXPATHLEN];
  31.     char longname[2 * MAXPATHLEN];
  32.     int fd, fd2;
  33.     struct stat statb;
  34.  
  35.     /*
  36.      * Test bad pathname argument.
  37.      */
  38.     if ((fd = open(-1, 0, 0)) != -1) {
  39.     fprintf(stderr, "open(-1, 0, 0) succeeded!\n");
  40.     if (fstat(fd, &statb) < 0) {
  41.         fprintf(stderr, "but fstat failed: %s", strerror(errno));
  42.     } else {
  43.         fprintf(stderr, 
  44.         "-1 found file <%d,%d>\n", statb.st_dev, statb.st_ino);
  45.     }
  46.     ++errors;
  47.     close(fd);
  48.     } else {
  49.     checkErrno(EFAULT, "open(-1, 0, 0)");
  50.     }
  51.     if ((fd = open(NULL, 0, 0)) != -1) {
  52.     fprintf(stderr, "open(NULL, 0, 0) succeeded!\n");
  53.     if (fstat(fd, &statb) < 0) {
  54.         fprintf(stderr, "but fstat failed: %s\n", strerror(errno));
  55.     } else {
  56.         fprintf(stderr,
  57.         "0 found file <%d,%d>\n", statb.st_dev, statb.st_ino);
  58.     }   
  59.     ++errors;
  60.     close(fd);
  61.     } else {
  62.     checkErrno(EFAULT, "open(NULL, 0, 0)");
  63.     }
  64.  
  65.     /*
  66.      * Test too long a pathname.
  67.      */
  68.     memset(longname, 'x', sizeof(longname);
  69.     if (open(longname, O_RDWR|O_CREAT, 0666) != -1) {
  70.     (void) fprintf(stderr,
  71.         "ERROR: open(tooLongName, 0, 0) succeeded!\n");
  72.     ++errors;
  73.     } else if (errno != ENAMETOOLONG) {
  74.     (void) fprintf(stderr, "Bad errno (%d): %s\n",
  75.         errno, strerror(errno));
  76.     ++errors;
  77.     }
  78.  
  79.     /*
  80.      *  Do various permission checks.  This checks against violations
  81.      *    of the following conditions:
  82.      *        Exclusive open
  83.      *        read permission
  84.      *        write permission
  85.      *        busy file
  86.      *        writing a directory
  87.      */
  88.     if (tmpnam(tempfile) != tempfile) {
  89.     (void) fprintf(stderr, "tmpnam() returned an incorrect value.\n");
  90.     ++errors;
  91.     return;
  92.     }
  93.  
  94.     /*
  95.      * Test open-unlink-close
  96.      */
  97.     if ((fd = open(tempfile, O_CREAT, 0444)) < 0) {
  98.     (void) fprintf("Can't create %s: %s\n", tempfile, strerror(errno));
  99.     ++errors;
  100.     } else {
  101.     if ((fd2 = open(name, O_RDONLY)) >= 0) {
  102.         close(fd2);
  103.     } else {
  104.         perror("Can't open readable file");
  105.         ++errors;
  106.     }
  107.     if (unlink(name)) {
  108.         (void) fprintf(stderr, "unlink failed: %s\n", strerror(errno));
  109.         ++errors;
  110.     }
  111.     if (close(fd)) {
  112.         (void) fprintf(stderr, "close failed: %s\n", strerror(errno));
  113.         ++errors;
  114.     }
  115.     }
  116.  
  117.     /*
  118.      * Test permission checking.
  119.      */
  120.     if ((fd = open(tempfile, O_CREAT, 0)) < 0) {
  121.     (void) fprintf("Can't create %s: %s\n", tempfile, strerror(errno));
  122.     ++errors;
  123.     } else {
  124.     if ((fd2 = open(name, O_CREAT|O_EXCL, 0)) >= 0) {
  125.         printf("ERROR: exclusive open of existing file succeeded!\n");
  126.         if (chmod(name, 0) < 0) {
  127.         perror("Can't chmod file");
  128.         }
  129.         close(fd2);
  130.     } else {
  131.         checkErrno(EEXIST, "Exclusive open of existing file");
  132.     }
  133.     if (open(name, O_RDONLY) != -1) {
  134.         printf("ERROR: opened with no read permission!\n");
  135.     } else {
  136.         checkErrno(EACCES, "Open for reading with no read permission\n");
  137.     }
  138.     chmod(name, 0444);
  139.     if (open(name, O_WRONLY) != -1) {
  140.         printf("ERROR: opened with no write permission!\n");
  141.     } else {
  142.         checkErrno(EACCES, "Open for writing with no write permission\n");
  143.     }
  144.     if (open("/bin/csh", O_WRONLY) >= 0) {
  145.         fprintf(stderr, "opened executing program (csh) for writing!\n");
  146.     }
  147.     if (open(".", O_WRONLY) != -1) {
  148.         fprintf(stderr, "ERROR: opened directory for writing!\n");
  149.     } else {
  150.         checkErrno(EISDIR, "open directory for writing");
  151.     }
  152.     if (unlink(name) < 0) {
  153.         (void) fprintf(stderr, "Can't unlink %s: %s\n",
  154.         tempname, strerror(errno));
  155.         ++errors;
  156.     }
  157.  
  158.     /*
  159.      * Test closing closed file.
  160.      */
  161.     if (close(fd) != 0) {
  162.         (void) fprintf(stderr, "Close failed: %s\n", strerror(errno));
  163.         ++errors;
  164.     }
  165.     if (close(fd) != -1)  {
  166.         (void) fprintf(stderr, "close of closed file: %s",
  167.         strerror(errno));
  168.         ++errors;
  169.     } else if (errno != EBADF) {
  170.         fprintf(stderr, "Incorrect errno (%d): %s\n",
  171.         errno, strerror(errno));
  172.         ++errors;
  173.     }
  174.     }
  175.  
  176.     /*
  177.      *  Try to open a file using a pathname that contains a
  178.      *  character with the high-order bit set.
  179.      */
  180.     if (tmpnam(tempfile) != tempfile) {
  181.     (void) fprintf(stderr, "tmpnam() returned an incorrect value.\n");
  182.     ++errors;
  183.     return;
  184.     }
  185.     if ((fd = open(tempfile, O_CREAT|O_RDWR, 0666) != -1) {
  186.     (void) fprintf(stderr, "Created file with high order bit set.\n");
  187.     unlink(tempfile);
  188.     ++errors;
  189.     } else {
  190.     checkErrno(EINVAL,
  191.         "Open of filename containing char with high order bit set");
  192.     }
  193.     return;
  194. }
  195.  
  196. static void
  197. checkErrno(correct_errno, errstring)
  198.     int correct_errno;
  199.     char *errstring;
  200. {
  201.  
  202.     if (errno != correct_errno) {
  203.     (void) fprintf(stderr, "%s\n", errstring);
  204.     (void) fprintf(stderr, "Incorrect errno (%d):  %s\n",
  205.         errno, strerror(errno));
  206.     (void) fprintf(stderr, "Should be (%d):  %s\n",
  207.         correct_errno, strerror(correct_errno));
  208.     ++errors;
  209.     }
  210.     return;
  211. }
  212.  
  213.