vm: don't force physical addresses to be nonzero.
[minix.git] / test / test6.c
blob90f3839e1f290bae4bee6049bbd14edcd05c26a7
1 /* test 6 */
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <limits.h>
8 #include <signal.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
13 #define MAX_ERROR 4
15 int errct;
16 int subtest = 1;
17 int zilch[5000];
18 char curdir[PATH_MAX];
20 _PROTOTYPE(int main, (int argc, char *argv []));
21 _PROTOTYPE(void test6a, (void));
22 _PROTOTYPE(void test6b, (void));
23 _PROTOTYPE(void test6c, (void));
24 _PROTOTYPE(void e, (int n));
25 _PROTOTYPE(void quit, (void));
27 int main(argc, argv)
28 int argc;
29 char *argv[];
31 char buffer[PATH_MAX + 1];
32 int i, m = 0xFFFF;
34 sync();
35 if (geteuid() == 0 || getuid() == 0) {
36 realpath(argv[0], buffer);
37 execl("/usr/bin/su", "/usr/bin/su", "-", "ast", "-c", buffer, NULL);
38 printf("Test 6 cannot run as root; test aborted\n");
39 exit(1);
42 if (argc == 2) m = atoi(argv[1]);
44 printf("Test 6 ");
45 fflush(stdout);
47 getcwd(curdir, PATH_MAX);
48 system("rm -rf DIR_06; mkdir DIR_06");
49 chdir("DIR_06");
51 for (i = 0; i < 70; i++) {
52 if (m & 00001) test6a();
53 if (m & 00002) test6b();
54 if (m & 00004) test6c();
57 quit();
58 return(-1); /* impossible */
61 void test6a()
63 /* Test sbrk() and brk(). */
65 char *addr, *addr2, *addr3;
66 int i, del, click, click2;
68 subtest = 1;
69 addr = sbrk(0);
70 addr = sbrk(0); /* force break to a click boundary */
71 for (i = 0; i < 10; i++) sbrk(7 * i);
72 for (i = 0; i < 10; i++) sbrk(-7 * i);
73 if (sbrk(0) != addr) e(1);
74 sbrk(30);
75 if (brk(addr) != 0) e(2);
76 if (sbrk(0) != addr) e(3);
78 del = 0;
79 do {
80 del++;
81 brk(addr + del);
82 addr2 = sbrk(0);
83 } while (addr2 == addr);
84 click = addr2 - addr;
85 sbrk(-1);
86 if (sbrk(0) != addr) e(4);
87 brk(addr);
88 if (sbrk(0) != addr) e(5);
90 del = 0;
91 do {
92 del++;
93 brk(addr - del);
94 addr3 = sbrk(0);
95 } while (addr3 == addr);
96 click2 = addr - addr3;
97 sbrk(1);
98 if (sbrk(0) != addr) e(6);
99 brk(addr);
100 if (sbrk(0) != addr) e(8);
101 if (click != click2) e(9);
103 brk(addr + 2 * click);
104 if (sbrk(0) != addr + 2 * click) e(10);
105 sbrk(3 * click);
106 if (sbrk(0) != addr + 5 * click) e(11);
107 sbrk(-5 * click);
108 if (sbrk(0) != addr) e(12);
111 void test6b()
113 int i, err;
115 subtest = 2;
116 signal(SIGQUIT, SIG_IGN);
117 err = 0;
118 for (i = 0; i < 5000; i++)
119 if (zilch[i] != 0) err++;
120 if (err > 0) e(1);
121 kill(getpid(), SIGQUIT);
124 void test6c()
126 /* Test mknod, chdir, chmod, chown, access. */
128 int i, j;
129 struct stat s;
131 subtest = 3;
132 if (getuid() != 0) return;
133 for (j = 0; j < 2; j++) {
134 umask(0);
136 if (chdir("/") < 0) e(1);
137 if (mknod("dir", 040700, 0) < 0) e(2);
138 if (link("/", "/dir/..") < 0) e(3);
139 if (mknod("T3a", 0777, 0) < 0) e(4);
140 if (mknod("/dir/T3b", 0777, 0) < 0) e(5);
141 if (mknod("dir/T3c", 0777, 0) < 0) e(6);
142 if ((i = open("/dir/T3b", 0)) < 0) e(7);
143 if (close(i) < 0) e(8);
144 if ((i = open("dir/T3c", O_RDONLY)) < 0) e(9);
145 if (close(i) < 0) e(10);
146 if (chdir("dir") < 0) e(11);
147 if ((i = open("T3b", 0)) < 0) e(12);
148 if (close(i) < 0) e(13);
149 if ((i = open("../T3a", O_RDONLY)) < 0) e(14);
150 if (close(i) < 0) e(15);
151 if ((i = open("../dir/../dir/../dir/../dir/../dir/T3c", O_RDONLY)) < 0)
152 e(16);
153 if (close(i) < 0) e(17);
155 if (chmod("../dir/../dir/../dir/../dir/../T3a", 0123) < 0) e(18);
156 if (stat("../dir/../dir/../dir/../T3a", &s) < 0) e(19);
157 if ((s.st_mode & 077777) != 0123) e(20);
158 if (chmod("../dir/../dir/../T3a", 0456) < 0) e(21);
159 if (stat("../T3a", &s) < 0) e(22);
160 if ((s.st_mode & 077777) != 0456) e(23);
161 if (chown("../dir/../dir/../T3a", 20, 30) < 0) e(24);
162 if (stat("../T3a", &s) < 0) e(25);
163 if (s.st_uid != 20) e(26);
164 if (s.st_gid != 30) e(27);
166 if ((i = open("/T3c", O_RDONLY)) >= 0) e(28);
167 if ((i = open("/T3a", O_RDONLY)) < 0) e(29);
168 if (close(i) < 0) e(30);
170 if (access("/T3a", 4) < 0) e(31);
171 if (access("/dir/T3b", 4) < 0) e(32);
172 if (access("/dir/T3d", 4) >= 0) e(33);
174 if (unlink("T3b") < 0) e(34);
175 if (unlink("T3c") < 0) e(35);
176 if (unlink("..") < 0) e(36);
177 if (chdir("/") < 0) e(37);
178 if (unlink("dir") < 0) e(38);
179 if (unlink("/T3a") < 0) e(39);
184 void e(n)
185 int n;
188 int err_num = errno; /* save errno in case printf clobbers it */
190 printf("Subtest %d, error %d errno=%d ", subtest, n, errno);
191 errno = err_num; /* restore errno, just in case */
192 perror("");
193 if (errct++ > MAX_ERROR) {
194 printf("Too many errors; test aborted\n");
195 chdir("..");
196 system("rm -rf DIR*");
197 exit(1);
201 void quit()
204 chdir("..");
205 system("rm -rf DIR*");
207 chdir(curdir);
208 system("rm -rf DIR*");
209 if (errct == 0) {
210 printf("ok\n");
211 exit(0);
212 } else {
213 printf("%d errors\n", errct);
214 exit(1);