test3.c

Go to the documentation of this file.
00001 /* test 3 - library routines rather than system calls */
00002 
00003 #include <sys/types.h>
00004 #include <sys/utsname.h>
00005 #include <errno.h>
00006 #include <fcntl.h>
00007 #include <limits.h>
00008 #include <signal.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include <unistd.h>
00012 #include <stdio.h>
00013 
00014 #define ITERATIONS 10
00015 #define MAX_ERROR 4
00016 #define SIZE 64
00017 
00018 int errct, subtest;
00019 char el_weirdo[] = "\n\t\\\e@@!!##\e\e\n\n";
00020 
00021 _PROTOTYPE(int main, (int argc, char *argv []));
00022 _PROTOTYPE(void test3a, (void));
00023 _PROTOTYPE(void test3b, (void));
00024 _PROTOTYPE(void test3c, (void));
00025 _PROTOTYPE(void test3d, (void));
00026 _PROTOTYPE(void test3e, (void));
00027 _PROTOTYPE(void quit, (void));
00028 _PROTOTYPE(void e, (int n));
00029 
00030 int main(argc, argv)
00031 int argc;
00032 char *argv[];
00033 {
00034   int i, m = 0xFFFF;
00035 
00036   sync();
00037   if (geteuid() == 0 || getuid() == 0) {
00038         printf("Test  3 cannot run as root; test aborted\n");
00039         exit(1);
00040   }
00041 
00042   if (argc == 2) m = atoi(argv[1]);
00043 
00044   printf("Test  3 ");
00045   fflush(stdout);               /* have to flush for child's benefit */
00046 
00047   system("rm -rf DIR_03; mkdir DIR_03");
00048   chdir("DIR_03");
00049 
00050   for (i = 0; i < ITERATIONS; i++) {
00051         if (m & 0001) test3a();
00052         if (m & 0002) test3b();
00053         if (m & 0004) test3c();
00054         if (m & 0010) test3d();
00055         if (m & 0020) test3e();
00056   }
00057   quit();
00058   return(-1);                   /* impossible */
00059 
00060 }
00061 
00062 void test3a()
00063 {
00064 /* Signal set manipulation. */
00065 
00066   sigset_t s, s1;
00067 
00068   subtest = 1;
00069   errno = -1000;                /* None of these calls set errno. */
00070   if (sigemptyset(&s) != 0) e(1);
00071   if (sigemptyset(&s1) != 0) e(2);
00072   if (sigaddset(&s, SIGABRT) != 0) e(3);
00073   if (sigaddset(&s, SIGALRM) != 0) e(4);
00074   if (sigaddset(&s, SIGFPE ) != 0) e(5);
00075   if (sigaddset(&s, SIGHUP ) != 0) e(6);
00076   if (sigaddset(&s, SIGILL ) != 0) e(7);
00077   if (sigaddset(&s, SIGINT ) != 0) e(8);
00078   if (sigaddset(&s, SIGKILL) != 0) e(9);
00079   if (sigaddset(&s, SIGPIPE) != 0) e(10);
00080   if (sigaddset(&s, SIGQUIT) != 0) e(11);
00081   if (sigaddset(&s, SIGSEGV) != 0) e(12);
00082   if (sigaddset(&s, SIGTERM) != 0) e(13);
00083   if (sigaddset(&s, SIGUSR1) != 0) e(14);
00084   if (sigaddset(&s, SIGUSR2) != 0) e(15);
00085   
00086   if (sigismember(&s, SIGABRT) != 1) e(16);
00087   if (sigismember(&s, SIGALRM) != 1) e(17);
00088   if (sigismember(&s, SIGFPE ) != 1) e(18);
00089   if (sigismember(&s, SIGHUP ) != 1) e(19);
00090   if (sigismember(&s, SIGILL ) != 1) e(20);
00091   if (sigismember(&s, SIGINT ) != 1) e(21);
00092   if (sigismember(&s, SIGKILL) != 1) e(22);
00093   if (sigismember(&s, SIGPIPE) != 1) e(23);
00094   if (sigismember(&s, SIGQUIT) != 1) e(24);
00095   if (sigismember(&s, SIGSEGV) != 1) e(25);
00096   if (sigismember(&s, SIGTERM) != 1) e(26);
00097   if (sigismember(&s, SIGUSR1) != 1) e(27);
00098   if (sigismember(&s, SIGUSR2) != 1) e(28);
00099   
00100   if (sigdelset(&s, SIGABRT) != 0) e(29);
00101   if (sigdelset(&s, SIGALRM) != 0) e(30);
00102   if (sigdelset(&s, SIGFPE ) != 0) e(31);
00103   if (sigdelset(&s, SIGHUP ) != 0) e(32);
00104   if (sigdelset(&s, SIGILL ) != 0) e(33);
00105   if (sigdelset(&s, SIGINT ) != 0) e(34);
00106   if (sigdelset(&s, SIGKILL) != 0) e(35);
00107   if (sigdelset(&s, SIGPIPE) != 0) e(36);
00108   if (sigdelset(&s, SIGQUIT) != 0) e(37);
00109   if (sigdelset(&s, SIGSEGV) != 0) e(38);
00110   if (sigdelset(&s, SIGTERM) != 0) e(39);
00111   if (sigdelset(&s, SIGUSR1) != 0) e(40);
00112   if (sigdelset(&s, SIGUSR2) != 0) e(41);
00113   
00114   if (s != s1) e(42);
00115 
00116   if (sigaddset(&s, SIGILL) != 0) e(43);
00117   if (s == s1) e(44);  
00118 
00119   if (sigfillset(&s) != 0) e(45);
00120   if (sigismember(&s, SIGABRT) != 1) e(46);
00121   if (sigismember(&s, SIGALRM) != 1) e(47);
00122   if (sigismember(&s, SIGFPE ) != 1) e(48);
00123   if (sigismember(&s, SIGHUP ) != 1) e(49);
00124   if (sigismember(&s, SIGILL ) != 1) e(50);
00125   if (sigismember(&s, SIGINT ) != 1) e(51);
00126   if (sigismember(&s, SIGKILL) != 1) e(52);
00127   if (sigismember(&s, SIGPIPE) != 1) e(53);
00128   if (sigismember(&s, SIGQUIT) != 1) e(54);
00129   if (sigismember(&s, SIGSEGV) != 1) e(55);
00130   if (sigismember(&s, SIGTERM) != 1) e(56);
00131   if (sigismember(&s, SIGUSR1) != 1) e(57);
00132   if (sigismember(&s, SIGUSR2) != 1) e(58);
00133 
00134   /* Test error returns. */
00135   if (sigaddset(&s, -1) != -1) e(59);
00136   if (sigaddset(&s, -1) != -1) e(60);
00137   if (sigismember(&s, -1) != -1) e(61);
00138   if (sigaddset(&s, 10000) != -1) e(62);
00139   if (sigaddset(&s, 10000) != -1) e(63);
00140   if (sigismember(&s, 10000) != -1) e(64);
00141 
00142 }
00143 
00144 void test3b()
00145 {
00146 /* Test uname. */
00147 
00148   struct utsname u;             /* contains all kinds of system ids */
00149 
00150   subtest = 2;
00151 #if 0
00152   errno = -2000;                /* None of these calls set errno. */
00153   if (uname(&u) != 0) e(1);
00154   if (strcmp(u.sysname, "MINIX") != 0
00155         && strcmp(u.sysname, "Minix") != 0) e(2);    /* only one defined */
00156 #endif
00157 }
00158 
00159 void test3c()
00160 {
00161 /* Test getenv.  Asume HOME, PATH, and LOGNAME exist (not strictly required).*/
00162 
00163   char *p, name[SIZE];
00164 
00165   subtest = 3;
00166   errno = -3000;                /* None of these calls set errno. */
00167   if ( (p = getenv("HOME")) == NULL) e(1);
00168   if (*p != '/') e(2);          /* path must be absolute */
00169   if ( (p = getenv("PATH")) == NULL) e(3);
00170   if ( (p = getenv("LOGNAME")) == NULL) e(5);
00171   strcpy(name, p);              /* save it, since getlogin might wipe it out */
00172   p = getlogin();
00173   if (strcmp(p, name) != 0) e(6);
00174 
00175   /* The following test could fail in a legal POSIX system.  However, if it
00176    * does, you deserve it to fail.
00177    */
00178   if (getenv(el_weirdo) != NULL) e(7);
00179 }
00180 
00181 void test3d()
00182 {
00183 /* Test ctermid, ttyname, and isatty. */
00184 
00185   int fd;
00186   char *p, name[L_ctermid];
00187 
00188   subtest = 4;
00189   errno = -4000;                /* None of these calls set errno. */
00190 
00191   /* Test ctermid first. */
00192   if ( (p = ctermid(name)) == NULL) e(1);
00193   if (strcmp(p, name) != 0) e(2);
00194   if (strncmp(p, "/dev/tty", 8) != 0) e(3);     /* MINIX convention */
00195   
00196   if ( (p = ttyname(0)) == NULL) e(4);
00197   if (strncmp(p, "/dev/tty", 8) != 0 && strcmp(p, "/dev/console") != 0) e(5);
00198   if ( (p = ttyname(3)) != NULL) e(6);
00199   if (ttyname(5000) != NULL) e(7);
00200   if ( (fd = creat("T3a", 0777)) < 0) e(8);
00201   if (ttyname(fd) != NULL) e(9);
00202 
00203   if (isatty(0) != 1) e(10);
00204   if (isatty(3) != 0) e(11);
00205   if (isatty(fd) != 0) e(12);
00206   if (close(fd) != 0) e(13);
00207   if (ttyname(fd) != NULL) e(14);
00208 }
00209 
00210 void test3e()
00211 {
00212 /* Test ctermid, ttyname, and isatty. */
00213 
00214   subtest = 5;
00215   errno = -5000;                /* None of these calls set errno. */
00216 
00217   if (sysconf(_SC_ARG_MAX) < _POSIX_ARG_MAX) e(1);
00218   if (sysconf(_SC_CHILD_MAX) < _POSIX_CHILD_MAX) e(2);
00219   if (sysconf(_SC_NGROUPS_MAX) < 0) e(3);
00220   if (sysconf(_SC_OPEN_MAX) < _POSIX_OPEN_MAX) e(4);
00221 
00222   /* The rest are MINIX specific */
00223   if (sysconf(_SC_JOB_CONTROL) >= 0) e(5);      /* no job control! */
00224 }
00225 
00226 void quit()
00227 {
00228   chdir("..");
00229   system("rm -rf DIR*");
00230 
00231   if (errct == 0) {
00232         printf("ok\n");
00233         exit(0);
00234   } else {
00235         printf("%d errors\n", errct);
00236         exit(4);
00237   }
00238 }
00239 
00240 void e(n)
00241 int n;
00242 {
00243   int err_num = errno;          /* save errno in case printf clobbers it */
00244 
00245   printf("Subtest %d,  error %d  errno=%d  ", subtest, n, errno);
00246   errno = err_num;              /* restore errno, just in case */
00247   perror("");
00248   if (errct++ > MAX_ERROR) {
00249         printf("Test aborted.  Too many errors: ");
00250         chdir("..");
00251         system("rm -rf DIR*");
00252         exit(1);
00253   }
00254 }

Generated on Fri Apr 14 22:57:34 2006 for minix by  doxygen 1.4.6