Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / venus / test / fulltest.c
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13
14 #include <sys/types.h>
15 #include <sys/time.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 int
24 main(int argc, char **argv)
25 {
26 char *dirName;
27 char tempName[1024];
28 struct stat tstat;
29 struct timeval tvp[2];
30 int fd1;
31 int code;
32 #ifndef HAVE_GETCWD
33 #define getcwd(x,y) getwd(x)
34 #endif
35
36 /* venus system tester */
37 if (argc != 2)
38 return printf("usage: fulltest <dir-to-screw-up>\n");
39 dirName = argv[1];
40 mkdir(dirName, 0777);
41 if (chdir(dirName) < 0) {
42 perror("chdir");
43 return -1;
44 }
45 if (getcwd(tempName, 1024) == 0) {
46 printf("Could not get working dir.\n");
47 return -1;
48 }
49 /* now create some files */
50 fd1 = open("hi", O_CREAT | O_TRUNC | O_RDWR, 0666);
51 if (fd1 < 0) {
52 perror("open1");
53 return -1;
54 }
55 if (close(fd1) < 0) {
56 perror("close1");
57 return -1;
58 }
59 if (access("hi", 2) < 0) {
60 printf("New file can not be written (access)\n");
61 return -1;
62 }
63 if (chmod("hi", 0741) < 0) {
64 perror("chmod1");
65 return -1;
66 }
67 if (stat("hi", &tstat) < 0) {
68 perror("stat1");
69 return -1;
70 }
71 if ((tstat.st_mode & 0777) != 0741) {
72 printf("chmod failed to set mode properly\n");
73 return -1;
74 }
75 fd1 = open("hi", O_RDWR);
76 if (fd1 < 0) {
77 perror("open2");
78 return -1;
79 }
80 if (fchmod(fd1, 0654) < 0) {
81 perror("fchmod");
82 return -1;
83 }
84 if (fstat(fd1, &tstat) < 0) {
85 perror("fstat1");
86 return -1;
87 }
88 if ((tstat.st_mode & 0777) != 0654) {
89 printf("fchmod failed to set mode properly\n");
90 return -1;
91 }
92 #if 0
93 /* These appear to be defunct routines;
94 * I don't know what, if anything, replaced them */
95 if (osi_ExclusiveLockNoBlock(fd1) < 0)
96 {perror("flock1");return -1;}
97 if (osi_UnLock(fd1) < 0)
98 {perror("flock/unlock");return -1;}
99 #endif
100
101 /* How about shared lock portability? */
102 {
103 struct flock fl;
104
105 fl.l_type = F_RDLCK;
106 fl.l_whence = SEEK_SET;
107 fl.l_start = 0;
108 fl.l_len = 0;
109
110 if (fcntl(fd1, F_SETLK, &fl) == -1) {
111 perror("fcntl1: RDLCK");
112 return -1;
113 }
114
115 fl.l_type = F_UNLCK;
116 fl.l_whence = SEEK_SET;
117 fl.l_start = 0;
118 fl.l_len = 0;
119
120 if (fcntl(fd1, F_SETLK, &fl) == -1) {
121 perror("fcntl2: UNLCK");
122 return -1;
123 }
124
125 fl.l_type = F_WRLCK;
126 fl.l_whence = SEEK_SET;
127 fl.l_start = 0;
128 fl.l_len = 0;
129
130 if (fcntl(fd1, F_SETLK, &fl) == -1) {
131 perror("fcntl3: WRLCK");
132 return -1;
133 }
134
135 fl.l_type = F_UNLCK;
136 fl.l_whence = SEEK_SET;
137 fl.l_start = 0;
138 fl.l_len = 0;
139
140 if (fcntl(fd1, F_SETLK, &fl) == -1) {
141 perror("fcntl4: UNLCK");
142 return -1;
143 }
144 }
145
146 if (fsync(fd1) < 0) {
147 perror("fsync");
148 return -1;
149 }
150 if (write(fd1, "hi\n", 3) != 3) {
151 perror("write");
152 return -1;
153 }
154 if (ftruncate(fd1, 2) < 0) {
155 perror("ftruncate");
156 return -1;
157 }
158 if (close(fd1) < 0) {
159 perror("close2");
160 return -1;
161 }
162
163 fd1 = open("hi", O_RDONLY);
164 if (fd1 < 0) {
165 perror("open3");
166 return -1;
167 }
168 if (read(fd1, tempName, 100) != 2) {
169 perror("read2");
170 return -1;
171 }
172 if (close(fd1) < 0) {
173 perror("close3");
174 return -1;
175 }
176
177 if (link("hi", "bye") < 0) {
178 perror("link");
179 return -1;
180 }
181 if (stat("bye", &tstat) < 0) {
182 perror("link/stat");
183 return -1;
184 }
185
186 if (unlink("bye") < 0) {
187 perror("unlink");
188 return -1;
189 }
190
191 if (symlink("hi", "bye") < 0) {
192 perror("symlink");
193 return -1;
194 }
195 if (readlink("bye", tempName, 100) != 2) {
196 perror("readlink");
197 return -1;
198 }
199 if (strncmp(tempName, "hi", 2) != 0) {
200 printf("readlink contents");
201 return -1;
202 }
203 if (mkdir("tdir", 0777) < 0) {
204 perror("mkdir");
205 return -1;
206 }
207 fd1 = open("tdir/fdsa", O_CREAT | O_TRUNC, 0777);
208 close(fd1);
209 if (rmdir("tdir") == 0) {
210 printf("removed non-empty dir\n");
211 return -1;
212 }
213 if (unlink("tdir/fdsa") < 0) {
214 perror("unlink tdir contents");
215 return -1;
216 }
217 if (rmdir("tdir") < 0) {
218 perror("rmdir");
219 return -1;
220 }
221
222 fd1 = open(".", O_RDONLY);
223 if (fd1 < 0) {
224 perror("open dot");
225 return -1;
226 }
227 if (read(fd1, tempName, 20) < 20)
228 perror("read dir");
229 close(fd1);
230
231 fd1 = open("rotest", O_RDWR | O_CREAT, 0444);
232 if (fd1 < 0) {
233 perror("open ronly");
234 return -1;
235 }
236 code = fchown(fd1, 1, -1); /* don't check error code, may fail on Ultrix */
237 code = write(fd1, "test", 4);
238 if (code != 4) {
239 printf("rotest short read (%d)\n", code);
240 exit(1);
241 }
242 code = close(fd1);
243 if (code) {
244 perror("close ronly");
245 return -1;
246 }
247 code = stat("rotest", &tstat);
248 if (code < 0) {
249 perror("stat ronly");
250 return -1;
251 }
252 if (tstat.st_size != 4) {
253 printf("rotest short close\n");
254 exit(1);
255 }
256 if (unlink("rotest") < 0) {
257 perror("rotest unlink");
258 return -1;
259 }
260
261 if (rename("hi", "bye") < 0) {
262 perror("rename1");
263 return -1;
264 }
265 if (stat("bye", &tstat) < 0) {
266 perror("rename target invisible\n");
267 return -1;
268 }
269 if (stat("hi", &tstat) == 0) {
270 printf("rename source still there\n");
271 return -1;
272 }
273
274 #ifndef AFS_AIX_ENV
275 /* No truncate(2) on aix so the following are excluded */
276 if (truncate("bye", 1) < 0) {
277 perror("truncate");
278 return -1;
279 }
280 if (stat("bye", &tstat) < 0) {
281 perror("truncate zapped");
282 return -1;
283 }
284 if (tstat.st_size != 1) {
285 printf("truncate failed\n");
286 return -1;
287 }
288 #endif
289 if (utimes("bye", tvp) < 0) {
290 perror("utimes");
291 return -1;
292 }
293 if (unlink("bye") < 0) {
294 perror("unlink bye");
295 return -1;
296 }
297
298 /* now finish up */
299 if (chdir("..") < 0) {
300 perror("chdir ..");
301 return -1;
302 }
303 rmdir(dirName);
304 printf("Test completed successfully.\n");
305 return 0;
306 }