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