1 /* test 16 */ 2 3 #include <sys/types.h> 4 #include <sys/stat.h> 5 #include <errno.h> 6 #include <fcntl.h> 7 #include <stdlib.h> 8 #include <unistd.h> 9 #include <utime.h> 10 #include <stdio.h> 11 12 int max_error = 4; 13 #include "common.h" 14 15 16 int subtest, passes; 17 int V1filesystem = 0; 18 19 20 int main(int argc, char *argv []); 21 void test16init(void); 22 void test16a(void); 23 void test16b(void); 24 void test16c(void); 25 void test16d(void); 26 void test16e(void); 27 void test16f(void); 28 void test16g(void); 29 void test16h(void); 30 void get_times(char *name, time_t *a, time_t *c, time_t *m); 31 32 int main(argc, argv) 33 int argc; 34 char *argv[]; 35 { 36 int i, m; 37 38 start(16); 39 40 m = (argc == 2 ? atoi(argv[1]) : 0xFFFF); 41 42 43 for (i = 0; i < 4; i++) { 44 test16init(); 45 if (m & 0001) test16a(); 46 if (m & 0002) test16b(); 47 if (m & 0004) test16c(); 48 if (m & 0010) test16d(); 49 if (m & 0020) test16e(); 50 if (m & 0040) test16f(); 51 if (m & 0100) test16g(); 52 if (m & 0200) test16h(); 53 passes++; 54 } 55 quit(); 56 return(-1); /* impossible */ 57 } 58 59 void test16init() 60 { 61 /* Test atime, ctime, and mtime. */ 62 63 int fd; 64 char buf[1024]; 65 struct stat s; 66 67 subtest = 0; 68 if (passes > 0) return; /* takes too long to repeat this test */ 69 if (V1filesystem) return; /* no need to spend time testing lacking features */ 70 71 if ( (fd = creat("T16.a", 0666)) < 0) e(1); 72 if (write(fd, buf, 1024) != 1024) e(2); 73 if (close(fd) < 0) e(3); 74 sleep(1); /* wait 1 sec before continuing */ 75 if ( (fd = open("T16.a", O_RDONLY)) < 0) e(4); 76 if (read(fd, buf, 3) != 3) e(5); 77 if (close(fd) != 0) e(6); 78 if (stat("T16.a", &s) != 0) e(7); 79 if (s.st_atime == 0) { 80 /* Almost certainly means we are running a V1 file system. */ 81 printf(" (atime = 0. Probably V1 file system. V2+ tests skipped.) "); 82 V1filesystem = 1; 83 } 84 } 85 86 /* Many system calls affect atime, ctime, and mtime. Test them. They 87 * fall into several groups. The members of each group can be tested 88 * together. Start with creat(), mkdir(), and mkfifo, all of which 89 * set all 3 times on the created object, and ctime and mtime of the dir. 90 */ 91 92 void test16a() 93 { 94 /* Test creat(). */ 95 96 int fd; 97 time_t a, c, m, pa, pc, pm; 98 99 subtest = 1; 100 if (passes > 0) return; /* takes too long to repeat this test */ 101 if (V1filesystem) return; /* no need to spend time testing lacking features */ 102 103 if ( (fd = creat("T16.b", 0666)) < 0) e(8); 104 if (close(fd) != 0) e(9); 105 get_times("T16.b", &a, &c, &m); 106 get_times(".", &pa, &pc, &pm); 107 if (a != c) e(10); 108 if (a != m) e(11); 109 if (a != pc) e(12); 110 if (a != pm) e(13); 111 if (unlink("T16.b") < 0) e(14); 112 } 113 114 void test16b() 115 { 116 /* Test the times for mkfifo. */ 117 int fd; 118 time_t a, c, m, pa, pc, pm; 119 120 subtest = 2; 121 if (passes > 0) return; /* takes too long to repeat this test */ 122 if (V1filesystem) return; /* no need to spend time testing lacking features */ 123 124 if ( (fd = mkfifo("T16.c", 0666)) != 0) e(15); 125 if (access("T16.c", R_OK | W_OK) != 0) e(16); 126 get_times("T16.c", &a, &c, &m); 127 get_times(".", &pa, &pc, &pm); 128 if (a != c) e(17); 129 if (a != m) e(18); 130 if (a != pc) e(19); 131 if (a != pm) e(20); 132 if (unlink("T16.c") < 0) e(21); 133 } 134 135 void test16c() 136 { 137 /* Test the times for mkdir. */ 138 time_t a, c, m, pa, pc, pm, xa, xc, xm; 139 140 subtest = 3; 141 if (passes > 0) return; /* takes too long to repeat this test */ 142 if (V1filesystem) return; /* no need to spend time testing lacking features */ 143 144 if (mkdir("T16.d", 0666) < 0) e(22); 145 get_times("T16.d", &a, &c, &m); 146 get_times(".", &pa, &pc, &pm); 147 if (a != c) e(23); 148 if (a != m) e(24); 149 if (a != pc) e(25); 150 if (a != pm) e(26); 151 sleep(1); 152 if (rmdir("T16.d") < 0) e(27); 153 get_times(".", &xa, &xc, &xm); 154 if (c == xc) e(28); 155 if (m == xm) e(29); 156 if (xc != xm) e(30); 157 } 158 159 void test16d() 160 { 161 /* Test open(file, O_TRUNC). */ 162 int fd; 163 time_t a, c, m, pa, pc, pm, xa, xc, xm, ya, yc, ym; 164 char buf[1024]; 165 166 subtest = 4; 167 if (passes > 0) return; /* takes too long to repeat this test */ 168 if (V1filesystem) return; /* no need to spend time testing lacking features */ 169 170 if ( (fd = open("T16.e", O_WRONLY|O_CREAT, 0666)) < 0) e(31); 171 if (write(fd, buf, 1024) != 1024) e(32); 172 if (close(fd) != 0) e(33); 173 get_times("T16.e", &a, &c, &m); 174 get_times(".", &pa, &pc, &pm); 175 sleep(1); 176 if ( (fd = open("T16.e", O_WRONLY|O_TRUNC)) < 0) e(34); 177 get_times("T16.e", &xa, &xc, &xm); 178 get_times(".", &ya, &yc, &ym); 179 if (c != m) e(35); 180 if (pc != pm) e(36); 181 if (c == xc) e(37); 182 if (m == xm) e(38); 183 if (yc != pc) e(39); 184 if (ym != pm) e(40); 185 if (close(fd) != 0) e(41); 186 /* Try once more, now without changing the file size. */ 187 sleep(1); 188 if ( (fd = open("T16.e", O_WRONLY|O_TRUNC)) < 0) e(89); 189 get_times("T16.e", &a, &c, &m); 190 if (c != m) e(90); 191 if (c == xc) e(91); 192 if (m == xm) e(92); 193 if (close(fd) != 0) e(93); 194 } 195 196 void test16e() 197 { 198 /* Test the times for link/unlink. */ 199 time_t a, c, m, pa, pc, pm, xa, xc, xm, ya, yc, ym; 200 201 subtest = 5; 202 if (passes > 0) return; /* takes too long to repeat this test */ 203 if (V1filesystem) return; /* no need to spend time testing lacking features */ 204 205 get_times("T16.e", &a, &c, &m); 206 get_times(".", &ya, &yc, &ym); 207 sleep(1); 208 if (link("T16.e", "T16.f") != 0) e(42); /* second link */ 209 get_times("T16.e", &xa, &xc, &xm); 210 get_times(".", &pa, &pc, &pm); 211 if (a != xa) e(43); 212 if (m != xm) e(44); 213 #ifndef V1_FILESYSTEM 214 if (c == xc) e(45); 215 #endif 216 if (ya != pa) e(46); 217 if (yc == pc) e(47); 218 if (ym == pm) e(48); 219 if (yc != ym) e(49); 220 if (pc != pm) e(50); 221 sleep(1); 222 if (unlink("T16.f") != 0) e(46); 223 get_times("T16.e", &a, &c, &m); 224 get_times(".", &ya, &yc, &ym); 225 if (a != xa) e(51); 226 if (m != xm) e(52); 227 #ifndef V1_FILESYSTEM 228 if (c == xc) e(53); 229 #endif 230 if (pa != ya) e(54); 231 if (pc == yc) e(55); 232 if (pm == ym) e(56); 233 if (yc != ym) e(57); 234 if (unlink("T16.e") != 0) e(58); 235 } 236 237 void test16f() 238 { 239 /* Test rename, read, write, chmod, utime. */ 240 int fd, fd1, fd2, fd3, fd4; 241 time_t a, c, m, pa, pc, pm, xa, xc, xm, ya, yc, ym, za, zc, zm, ta, tc, tm; 242 time_t wa, wc, wm; 243 char buf[1024]; 244 245 subtest = 6; 246 if (passes > 0) return; /* takes too long to repeat this test */ 247 if (V1filesystem) return; /* no need to spend time testing lacking features */ 248 249 get_times(".", &pa, &pc, &pm); 250 if ( (fd = open("T16.g", O_RDWR|O_CREAT)) < 0) e(59); 251 if ( (fd1 = open("T16.h", O_WRONLY|O_CREAT, 0666)) < 0) e(60); 252 if ( (fd2 = open("T16.i", O_WRONLY|O_CREAT, 0666)) < 0) e(61); 253 if ( (fd3 = open("T16.j", O_WRONLY|O_CREAT, 0666)) < 0) e(62); 254 if ( (fd4 = open("T16.k", O_RDWR|O_CREAT, 0666)) < 0) e(63); 255 if (write(fd, buf, 1024) != 1024) e(64); 256 get_times("T16.g", &a, &c, &m); 257 get_times("T16.h", &pa, &pc, &pm); 258 get_times("T16.i", &xa, &xc, &xm); 259 get_times("T16.j", &ya, &yc, &ym); 260 get_times("T16.k", &za, &zc, &zm); 261 get_times(".", &wa, &wc, &wm); 262 sleep(1); 263 lseek(fd, 0L, SEEK_SET); 264 if (read(fd, buf, 35) != 35) e(65); 265 get_times("T16.g", &ta, &tc, &tm); 266 if (a == ta || c != tc || m != tm) e(66); 267 if (write(fd1, buf, 35) != 35) e(67); 268 get_times("T16.h", &ta, &tc, &tm); 269 if (pa != ta || pc == tc || pm == tm) e(69); 270 if (rename("T16.i", "T16.i1") != 0) e(70); 271 get_times("T16.i1", &ta, &tc, &tm); 272 if (xa != ta || xc != tc || xm != tm) e(71); 273 get_times(".", &a, &c, &m); 274 if (a != wa || c == wc || m == wm || wc != wm) e(72); 275 if (chmod("T16.j", 0777) != 0) e(73); 276 get_times("T16.j", &ta, &tc, &tm); 277 if (ya != ta || yc == tc || ym != tm) e(74); 278 if (utime("T16.k", (void *) 0) != 0) e(75); 279 get_times("T16.k", &ta, &tc, &tm); 280 if (za == ta || zc == tc) e(76); 281 if (close(fd) != 0) e(77); 282 if (close(fd1) != 0) e(78); 283 if (close(fd2) != 0) e(79); 284 if (close(fd3) != 0) e(80); 285 if (close(fd4) != 0) e(81); 286 if (unlink("T16.g") != 0) e(82); 287 if (unlink("T16.h") != 0) e(83); 288 if (unlink("T16.i1") != 0) e(84); 289 if (unlink("T16.j") != 0) e(85); 290 if (unlink("T16.k") != 0) e(86); 291 } 292 293 void test16g() 294 { 295 /* Test the times for truncate. */ 296 time_t a, c, m, ta, tc, tm; 297 struct stat s; 298 299 subtest = 7; 300 if (passes > 0) return; /* takes too long to repeat this test */ 301 if (V1filesystem) return; /* no need to spend time testing lacking features */ 302 303 if (system("echo 1 > T16.l") != 0) e(87); 304 stat("T16.l", &s); 305 get_times("T16.l", &a, &c, &m); 306 sleep(1); 307 truncate("T16.l", s.st_size); 308 get_times("T16.l", &ta, &tc, &tm); 309 if (a != ta || c != tc || m != tm) e(88); 310 311 } 312 313 void test16h() 314 { 315 /* Test utimes, futimes, lutimes, futimens, utimensat. */ 316 int fd, fd1, fd2, fd3, fd4; 317 time_t a, c, m, pa, pc, pm; 318 time_t ta, tc, tm, wa, wc, wm, xa, xc, xm, ya, yc, ym, za, zc, zm; 319 320 subtest = 8; 321 if (passes > 0) return; /* takes too long to repeat this test */ 322 if (V1filesystem) return; /* no need to spend time testing lacking features */ 323 324 get_times(".", &pa, &pc, &pm); 325 if ( (fd = open("T16.m", O_RDWR|O_CREAT, 0666)) < 0) e(89); 326 if ( (fd1 = open("T16.n", O_RDWR|O_CREAT, 0666)) < 0) e(90); 327 if ( (fd2 = open("T16.o", O_RDWR|O_CREAT, 0666)) < 0) e(91); 328 if ( (fd3 = open("T16.p", O_RDWR|O_CREAT, 0666)) < 0) e(92); 329 if ( (fd4 = open("T16.q", O_RDWR|O_CREAT, 0666)) < 0) e(93); 330 get_times("T16.m", &ta, &tc, &tm); 331 get_times("T16.n", &wa, &wc, &wm); 332 get_times("T16.o", &xa, &xc, &xm); 333 get_times("T16.p", &ya, &yc, &ym); 334 get_times("T16.q", &za, &zc, &zm); 335 get_times(".", &pa, &pc, &pm); 336 sleep(1); 337 if (utimes("T16.m", (void *) 0) != 0) e(94); 338 get_times("T16.m", &a, &c, &m); 339 if (a == ta || c == tc) e(95); 340 if (futimes(fd1, (void *) 0) != 0) e(96); 341 get_times("T16.n", &a, &c, &m); 342 if (a == wa || c == wc) e(97); 343 if (lutimes("T16.o", (void *) 0) != 0) e(98); 344 get_times("T16.o", &a, &c, &m); 345 if (a == xa || c == xc) e(99); 346 if (utimensat(AT_FDCWD, "T16.p", (void *) 0, 0) != 0) e(100); 347 get_times("T16.p", &a, &c, &m); 348 if (a == ya || c == yc) e(101); 349 if (futimens(fd4, (void *) 0) != 0) e(102); 350 get_times("T16.q", &a, &c, &m); 351 if (a == za || c == zc) e(103); 352 if (close(fd) != 0) e(104); 353 if (close(fd1) != 0) e(105); 354 if (close(fd2) != 0) e(106); 355 if (close(fd3) != 0) e(107); 356 if (close(fd4) != 0) e(108); 357 if (unlink("T16.m") != 0) e(109); 358 if (unlink("T16.n") != 0) e(110); 359 if (unlink("T16.o") != 0) e(111); 360 if (unlink("T16.p") != 0) e(112); 361 if (unlink("T16.q") != 0) e(113); 362 } 363 364 void get_times(name, a, c, m) 365 char *name; 366 time_t *a, *c, *m; 367 { 368 struct stat s; 369 370 if (stat(name, &s) != 0) e(500); 371 *a = s.st_atime; 372 *c = s.st_ctime; 373 *m = s.st_mtime; 374 } 375 376