sort: add -x hex sort feature back
[minix.git] / test / test23.c
blob6f78b96f59006005d20e09890d0d67c621302903
1 /* test23: chdir(), getcwd() Author: Jan-Mark Wams (jms@cs.vu.nl) */
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <limits.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <fcntl.h>
11 #include <stdio.h>
13 #define MAX_ERROR 4
14 #define ITERATIONS 3
16 #include "common.c"
18 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
19 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
21 int subtest;
22 int superuser; /* True if we are root. */
24 char cwd[PATH_MAX]; /* Space for path names. */
25 char cwd2[PATH_MAX];
26 char buf[PATH_MAX];
27 char *MaxName; /* Name of maximum length */
28 char MaxPath[PATH_MAX]; /* Same for path */
29 char *ToLongName; /* Name of maximum +1 length */
30 char ToLongPath[PATH_MAX + 1]; /* Same for path, both too long */
32 void test23a(void);
33 void test23b(void);
34 void test23c(void);
35 void makelongnames(void);
37 int main(int argc, char *argv[])
39 int i, m = 0xFFFF;
41 sync();
42 if (argc == 2) m = atoi(argv[1]);
43 start(23);
44 makelongnames();
45 superuser = (geteuid() == 0);
47 for (i = 0; i < ITERATIONS; i++) {
48 if (m & 0001) test23a(); /* Test normal operation */
49 if (m & 0002) test23b(); /* Test critical operation */
50 if (m & 0004) test23c(); /* Test error operation */
53 quit();
54 return 1;
57 void test23a()
58 { /* Test normal operation. */
59 register int i;
61 subtest = 1;
63 System("rm -rf ../DIR_23/*");
65 /* Let's do some fiddeling with path names. */
66 if (getcwd(cwd, PATH_MAX) != cwd) e(1);
67 if (chdir(cwd) != 0) e(2);
68 if (getcwd(buf, PATH_MAX) != buf) e(3);
69 if (strcmp(buf, cwd) != 0) e(4);
70 if (chdir(".") != 0) e(5);
71 if (getcwd(buf, PATH_MAX) != buf) e(6);
72 if (strcmp(buf, cwd) != 0) e(7);
73 if (chdir("./././.") != 0) e(8);
74 if (getcwd(buf, PATH_MAX) != buf) e(9);
75 if (strcmp(buf, cwd) != 0) e(10);
77 /* Creat a working dir named "foo", remove any previous residues. */
78 System("rm -rf foo");
79 if (mkdir("foo", 0777) != 0) e(11);
81 /* Do some more fiddeling with path names. */
82 if (chdir("foo/.././foo/..") != 0) e(12); /* change to cwd */
83 if (getcwd(buf, PATH_MAX) != buf) e(13);
84 if (strcmp(buf, cwd) != 0) e(13);
85 if (chdir("foo") != 0) e(14); /* change to foo */
86 if (chdir("..") != 0) e(15); /* and back again */
87 if (getcwd(buf, PATH_MAX) != buf) e(16);
88 if (strcmp(buf, cwd) != 0) e(17);
90 /* Make 30 sub dirs, eg. ./bar/bar/bar/bar/bar...... */
91 System("rm -rf bar"); /* get ridd of bar */
92 for (i = 0; i < 30; i++) {
93 if (mkdir("bar", 0777) != 0) e(18);
94 if (chdir("bar") != 0) e(19); /* change to bar */
96 for (i = 0; i < 30; i++) {
97 if (chdir("..") != 0) e(20); /* and back again */
98 if (rmdir("bar") != 0) e(21);
101 /* Make sure we are back where we started. */
102 if (getcwd(buf, PATH_MAX) != buf) e(22);
103 if (strcmp(buf, cwd) != 0) e(23);
104 System("rm -rf bar"); /* just incase */
106 /* Do some normal checks on `Chdir()' and `getcwd()' */
107 if (chdir("/") != 0) e(24);
108 if (getcwd(buf, PATH_MAX) != buf) e(25);
109 if (strcmp(buf, "/") != 0) e(26);
110 if (chdir("..") != 0) e(27); /* go to parent of / */
111 if (getcwd(buf, PATH_MAX) != buf) e(28);
112 if (strcmp(buf, "/") != 0) e(29);
113 if (chdir(cwd) != 0) e(30);
114 if (getcwd(buf, PATH_MAX) != buf) e(31);
115 if (strcmp(buf, cwd) != 0) e(32);
116 if (chdir("/etc") != 0) e(33); /* /etc might be on RAM */
117 if (getcwd(buf, PATH_MAX) != buf) e(34); /* might make a difference */
118 if (strcmp(buf, "/etc") != 0) e(35);
119 if (chdir(cwd) != 0) e(36);
120 if (getcwd(buf, PATH_MAX) != buf) e(37);
121 if (strcmp(buf, cwd) != 0) e(38);
122 if (chdir(".//.//") != 0) e(39); /* .//.// == current dir */
123 if (getcwd(buf, PATH_MAX) != buf) e(40);
124 if (strcmp(buf, cwd) != 0) e(41); /* we might be at '/' */
125 System("rm -rf foo");
128 void test23b()
129 { /* Test critical values. */
130 int does_truncate;
131 subtest = 2;
133 System("rm -rf ../DIR_23/*");
135 /* Fiddle with the size (2nd) parameter of `getcwd ()'. */
136 if (getcwd(cwd, PATH_MAX) != cwd) e(1); /* get cwd */
137 if (getcwd(buf, strlen(cwd)) != (char *) 0) e(2); /* size 1 to small */
138 if (errno != ERANGE) e(3);
139 if (getcwd(buf, PATH_MAX) != buf) e(4);
140 if (strcmp(buf, cwd) != 0) e(5);
141 Chdir(cwd); /* getcwd might cd / */
142 if (getcwd(buf, strlen(cwd) + 1) != buf) e(6); /* size just ok */
143 if (getcwd(buf, PATH_MAX) != buf) e(7);
144 if (strcmp(buf, cwd) != 0) e(8);
146 /* Let's see how "MaxName" and "ToLongName" are handled. */
147 if (mkdir(MaxName, 0777) != 0) e(9);
148 if (chdir(MaxName) != 0) e(10);
149 if (chdir("..") != 0) e(11);
150 if (rmdir(MaxName) != 0) e(12);
151 if (getcwd(buf, PATH_MAX) != buf) e(13);
152 if (strcmp(buf, cwd) != 0) e(14);
153 if (chdir(MaxPath) != 0) e(15);
154 if (getcwd(buf, PATH_MAX) != buf) e(16);
155 if (strcmp(buf, cwd) != 0) e(17);
157 does_truncate = does_fs_truncate();
158 if (chdir(ToLongName) != -1) e(18);
159 if (does_truncate) {
160 if (errno != ENOENT) e(19);
161 } else {
162 if (errno != ENAMETOOLONG) e(20);
165 if (getcwd(buf, PATH_MAX) != buf) e(21);
166 if (strcmp(buf, cwd) != 0) e(22);
167 if (chdir(ToLongPath) != -1) e(23);
168 if (errno != ENAMETOOLONG) e(24);
169 if (getcwd(buf, PATH_MAX) != buf) e(25);
170 if (strcmp(buf, cwd) != 0) e(26);
173 void test23c()
174 { /* Check reaction to errors */
175 subtest = 3;
177 System("rm -rf ../DIR_23/*");
179 if (getcwd(cwd, PATH_MAX) != cwd) e(1); /* get cwd */
181 /* Creat a working dir named "foo", remove any previous residues. */
182 System("rm -rf foo; mkdir foo");
184 /* Check some obviouse errors. */
185 if (chdir("") != -1) e(2);
186 if (errno != ENOENT) e(3);
187 if (getcwd(buf, PATH_MAX) != buf) e(4);
188 if (strcmp(buf, cwd) != 0) e(5);
189 if (getcwd(buf, 0) != (char *) 0) e(6);
190 if (errno != EINVAL) e(7);
191 if (getcwd(buf, PATH_MAX) != buf) e(8);
192 if (strcmp(buf, cwd) != 0) e(9);
193 if (getcwd(buf, 0) != (char *) 0) e(10);
194 if (errno != EINVAL) e(11);
195 if (getcwd(buf, PATH_MAX) != buf) e(12);
196 if (strcmp(buf, cwd) != 0) e(13);
197 if (chdir(cwd) != 0) e(14); /* getcwd might be buggy. */
199 /* Change the mode of foo, and check the effect. */
200 if (chdir("foo") != 0) e(15); /* change to foo */
201 if (mkdir("bar", 0777) != 0) e(16); /* make a new dir bar */
202 if (getcwd(cwd2, PATH_MAX) != cwd2) e(17); /* get the new cwd */
203 if (getcwd(buf, 3) != (char *) 0) e(18); /* size is too small */
204 if (errno != ERANGE) e(19);
205 if (getcwd(buf, PATH_MAX) != buf) e(20);
206 if (strcmp(buf, cwd2) != 0) e(21);
207 Chdir(cwd2); /* getcwd() might cd / */
208 System("chmod 377 ."); /* make foo unreadable */
209 if (getcwd(buf, PATH_MAX) != buf) e(22); /* dir not readable */
210 if (getcwd(buf, PATH_MAX) != buf) e(23);
211 if (strcmp(buf, cwd2) != 0) e(24);
212 if (chdir("bar") != 0) e(25); /* at .../foo/bar */
213 if (!superuser) {
214 if (getcwd(buf, PATH_MAX) != (char *) 0) e(26);
215 if (errno != EACCES) e(27);
217 if (superuser) {
218 if (getcwd(buf, PATH_MAX) != buf) e(28);
220 if (chdir(cwd2) != 0) e(29);
221 if (getcwd(buf, PATH_MAX) != buf) e(30);
222 if (strcmp(buf, cwd2) != 0) e(31);
223 System("chmod 677 ."); /* make foo inaccessable */
224 if (!superuser) {
225 if (getcwd(buf, PATH_MAX) != (char *) 0) e(32); /* try to get cwd */
226 if (errno != EACCES) e(33); /* but no access */
227 if (chdir("..") != -1) e(34); /* try to get back */
228 if (errno != EACCES) e(35); /* again no access */
229 if (chdir(cwd) != 0) e(36); /* get back to cwd */
230 /* `Chdir()' might do path optimizing, it shouldn't. */
231 if (chdir("foo/..") != -1) e(37); /* no op */
232 if (chdir("foo") != -1) e(38); /* try to cd to foo */
233 if (errno != EACCES) e(39); /* no have access */
234 if (getcwd(buf, PATH_MAX) != buf) e(40);
235 if (strcmp(buf, cwd) != 0) e(41);
237 if (superuser) {
238 if (getcwd(buf, PATH_MAX) != buf) e(42);
239 if (strcmp(buf, cwd2) != 0) e(43);
240 if (chdir("..") != 0) e(44); /* get back to cwd */
241 if (chdir("foo") != 0) e(45); /* get back to foo */
242 if (chdir(cwd) != 0) e(46); /* get back to cwd */
244 if (getcwd(buf, PATH_MAX) != buf) e(47); /* check we are */
245 if (strcmp(buf, cwd) != 0) e(48); /* back at cwd. */
246 Chdir(cwd); /* just in case... */
248 if (chdir("/etc/passwd") != -1) e(49); /* try to change to a file */
249 if (errno != ENOTDIR) e(50);
250 if (getcwd(buf, PATH_MAX) != buf) e(51);
251 if (strcmp(buf, cwd) != 0) e(52);
252 if (chdir("/notexist") != -1) e(53);
253 if (errno != ENOENT) e(54);
254 if (getcwd(buf, PATH_MAX) != buf) e(55);
255 if (strcmp(buf, cwd) != 0) e(56);
256 System("chmod 777 foo");
257 if (chdir("foo") != 0) e(57);
259 /* XXX - this comment was botched by 'pretty'. */
260 /* * Since `foo' is the cwd, it should not be removeable but * if it
261 * were, this code would be found here; *
263 * System ("cd .. ; rm -rf foo"); remove foo * if (chdir (".")
264 * != -1) e(); try go to. * if (errno != ENOENT) e();
265 * hould not be an entry * if (chdir ("..") != -1) e(); try
266 * to get back * if (errno != ENOENT) e(); should not be
267 * an entry * if (getcwd (buf, PATH_MAX) != (char *)0) e(); don't
268 * know where we are *
270 * What should errno be now ? The cwd might be gone if te superuser *
271 * removed the cwd. (Might even have linked it first.) But this *
272 * testing should be done by the test program for `rmdir()'. */
273 if (chdir(cwd) != 0) e(58);
276 void makelongnames()
278 register int i;
279 int max_name_length;
281 max_name_length = name_max("."); /* Aka NAME_MAX, but not every FS supports
282 * the same length, hence runtime check */
283 MaxName = malloc(max_name_length + 1);
284 ToLongName = malloc(max_name_length + 1 + 1); /* Name of maximum +1 length */
285 memset(MaxName, 'a', max_name_length);
286 MaxName[max_name_length] = '\0';
288 for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
289 MaxPath[i++] = '.';
290 MaxPath[i] = '/';
292 MaxPath[PATH_MAX - 1] = '\0';
294 strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
295 strcpy(ToLongPath, MaxPath);
297 ToLongName[max_name_length] = 'a';
298 ToLongName[max_name_length+1] = '\0';/* extend ToLongName by one too many */
299 ToLongPath[PATH_MAX - 1] = '/';
300 ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */