00001 #include "types.h"
00002 #include "stat.h"
00003 #include "user.h"
00004 #include "fs.h"
00005 #include "fcntl.h"
00006
00007 char buf[2048];
00008 char name[3];
00009 char *echoargv[] = { "echo", "ALL", "TESTS", "PASSED", 0 };
00010 int stdout = 1;
00011
00012
00013
00014 void
00015 opentest(void)
00016 {
00017 int fd;
00018
00019 printf(stdout, "open test\n");
00020 fd = open("echo", 0);
00021 if(fd < 0){
00022 printf(stdout, "open echo failed!\n");
00023 exit();
00024 }
00025 close(fd);
00026 fd = open("doesnotexist", 0);
00027 if(fd >= 0){
00028 printf(stdout, "open doesnotexist succeeded!\n");
00029 exit();
00030 }
00031 printf(stdout, "open test ok\n");
00032 }
00033
00034 void
00035 writetest(void)
00036 {
00037 int fd;
00038 int i;
00039
00040 printf(stdout, "small file test\n");
00041 fd = open("small", O_CREATE|O_RDWR);
00042 if(fd >= 0){
00043 printf(stdout, "creat small succeeded; ok\n");
00044 } else {
00045 printf(stdout, "error: creat small failed!\n");
00046 exit();
00047 }
00048 for(i = 0; i < 100; i++) {
00049 if(write(fd, "aaaaaaaaaa", 10) != 10) {
00050 printf(stdout, "error: write aa %d new file failed\n", i);
00051 exit();
00052 }
00053 if(write(fd, "bbbbbbbbbb", 10) != 10) {
00054 printf(stdout, "error: write bb %d new file failed\n", i);
00055 exit();
00056 }
00057 }
00058 printf(stdout, "writes ok\n");
00059 close(fd);
00060 fd = open("small", O_RDONLY);
00061 if(fd >= 0){
00062 printf(stdout, "open small succeeded ok\n");
00063 } else {
00064 printf(stdout, "error: open small failed!\n");
00065 exit();
00066 }
00067 i = read(fd, buf, 2000);
00068 if(i == 2000) {
00069 printf(stdout, "read succeeded ok\n");
00070 } else {
00071 printf(stdout, "read failed\n");
00072 exit();
00073 }
00074 close(fd);
00075
00076 if(unlink("small") < 0) {
00077 printf(stdout, "unlink small failed\n");
00078 exit();
00079 }
00080 printf(stdout, "small file test ok\n");
00081 }
00082
00083 void
00084 writetest1(void)
00085 {
00086 int i, fd, n;
00087
00088 printf(stdout, "big files test\n");
00089
00090 fd = open("big", O_CREATE|O_RDWR);
00091 if(fd < 0){
00092 printf(stdout, "error: creat big failed!\n");
00093 exit();
00094 }
00095
00096 for(i = 0; i < MAXFILE; i++) {
00097 ((int*) buf)[0] = i;
00098 if(write(fd, buf, 512) != 512) {
00099 printf(stdout, "error: write big file failed\n", i);
00100 exit();
00101 }
00102 }
00103
00104 close(fd);
00105
00106 fd = open("big", O_RDONLY);
00107 if(fd < 0){
00108 printf(stdout, "error: open big failed!\n");
00109 exit();
00110 }
00111
00112 n = 0;
00113 for(;;) {
00114 i = read(fd, buf, 512);
00115 if(i == 0) {
00116 if(n == MAXFILE - 1) {
00117 printf(stdout, "read only %d blocks from big", n);
00118 exit();
00119 }
00120 break;
00121 } else if(i != 512) {
00122 printf(stdout, "read failed %d\n", i);
00123 exit();
00124 }
00125 if(((int*)buf)[0] != n) {
00126 printf(stdout, "read content of block %d is %d\n",
00127 n, ((int*)buf)[0]);
00128 exit();
00129 }
00130 n++;
00131 }
00132 close(fd);
00133 if(unlink("big") < 0) {
00134 printf(stdout, "unlink big failed\n");
00135 exit();
00136 }
00137 printf(stdout, "big files ok\n");
00138 }
00139
00140 void
00141 createtest(void)
00142 {
00143 int i, fd;
00144
00145 printf(stdout, "many creates, followed by unlink test\n");
00146
00147 name[0] = 'a';
00148 name[2] = '\0';
00149 for(i = 0; i < 52; i++) {
00150 name[1] = '0' + i;
00151 fd = open(name, O_CREATE|O_RDWR);
00152 close(fd);
00153 }
00154 name[0] = 'a';
00155 name[2] = '\0';
00156 for(i = 0; i < 52; i++) {
00157 name[1] = '0' + i;
00158 unlink(name);
00159 }
00160 printf(stdout, "many creates, followed by unlink; ok\n");
00161 }
00162
00163 void dirtest(void)
00164 {
00165 printf(stdout, "mkdir test\n");
00166
00167 if(mkdir("dir0") < 0) {
00168 printf(stdout, "mkdir failed\n");
00169 exit();
00170 }
00171
00172 if(chdir("dir0") < 0) {
00173 printf(stdout, "chdir dir0 failed\n");
00174 exit();
00175 }
00176
00177 if(chdir("..") < 0) {
00178 printf(stdout, "chdir .. failed\n");
00179 exit();
00180 }
00181
00182 if(unlink("dir0") < 0) {
00183 printf(stdout, "unlink dir0 failed\n");
00184 exit();
00185 }
00186 printf(stdout, "mkdir test\n");
00187 }
00188
00189 void
00190 exectest(void)
00191 {
00192 printf(stdout, "exec test\n");
00193 if(exec("echo", echoargv) < 0) {
00194 printf(stdout, "exec echo failed\n");
00195 exit();
00196 }
00197 }
00198
00199
00200
00201 void
00202 pipe1(void)
00203 {
00204 int fds[2], pid;
00205 int seq, i, n, cc, total;
00206
00207 if(pipe(fds) != 0){
00208 printf(1, "pipe() failed\n");
00209 exit();
00210 }
00211 pid = fork();
00212 seq = 0;
00213 if(pid == 0){
00214 close(fds[0]);
00215 for(n = 0; n < 5; n++){
00216 for(i = 0; i < 1033; i++)
00217 buf[i] = seq++;
00218 if(write(fds[1], buf, 1033) != 1033){
00219 printf(1, "pipe1 oops 1\n");
00220 exit();
00221 }
00222 }
00223 exit();
00224 } else if(pid > 0){
00225 close(fds[1]);
00226 total = 0;
00227 cc = 1;
00228 while((n = read(fds[0], buf, cc)) > 0){
00229 for(i = 0; i < n; i++){
00230 if((buf[i] & 0xff) != (seq++ & 0xff)){
00231 printf(1, "pipe1 oops 2\n");
00232 return;
00233 }
00234 }
00235 total += n;
00236 cc = cc * 2;
00237 if(cc > sizeof(buf))
00238 cc = sizeof(buf);
00239 }
00240 if(total != 5 * 1033)
00241 printf(1, "pipe1 oops 3 total %d\n", total);
00242 close(fds[0]);
00243 wait();
00244 } else {
00245 printf(1, "fork() failed\n");
00246 exit();
00247 }
00248 printf(1, "pipe1 ok\n");
00249 }
00250
00251
00252 void
00253 preempt(void)
00254 {
00255 int pid1, pid2, pid3;
00256 int pfds[2];
00257
00258 printf(1, "preempt: ");
00259 pid1 = fork();
00260 if(pid1 == 0)
00261 for(;;)
00262 ;
00263
00264 pid2 = fork();
00265 if(pid2 == 0)
00266 for(;;)
00267 ;
00268
00269 pipe(pfds);
00270 pid3 = fork();
00271 if(pid3 == 0){
00272 close(pfds[0]);
00273 if(write(pfds[1], "x", 1) != 1)
00274 printf(1, "preempt write error");
00275 close(pfds[1]);
00276 for(;;)
00277 ;
00278 }
00279
00280 close(pfds[1]);
00281 if(read(pfds[0], buf, sizeof(buf)) != 1){
00282 printf(1, "preempt read error");
00283 return;
00284 }
00285 close(pfds[0]);
00286 printf(1, "kill... ");
00287 kill(pid1);
00288 kill(pid2);
00289 kill(pid3);
00290 printf(1, "wait... ");
00291 wait();
00292 wait();
00293 wait();
00294 printf(1, "preempt ok\n");
00295 }
00296
00297
00298 void
00299 exitwait(void)
00300 {
00301 int i, pid;
00302
00303 for(i = 0; i < 100; i++){
00304 pid = fork();
00305 if(pid < 0){
00306 printf(1, "fork failed\n");
00307 return;
00308 }
00309 if(pid){
00310 if(wait() != pid){
00311 printf(1, "wait wrong pid\n");
00312 return;
00313 }
00314 } else {
00315 exit();
00316 }
00317 }
00318 printf(1, "exitwait ok\n");
00319 }
00320
00321 void
00322 mem(void)
00323 {
00324 void *m1, *m2;
00325 int pid;
00326
00327 if((pid = fork()) == 0){
00328 m1 = 0;
00329 while((m2 = malloc(10001)) != 0) {
00330 *(char**) m2 = m1;
00331 m1 = m2;
00332 }
00333 while(m1) {
00334 m2 = *(char**)m1;
00335 free(m1);
00336 m1 = m2;
00337 }
00338 m1 = malloc(1024*20);
00339 if(m1 == 0) {
00340 printf(1, "couldn't allocate mem?!!\n");
00341 exit();
00342 }
00343 free(m1);
00344 printf(1, "mem ok\n");
00345 exit();
00346 } else {
00347 wait();
00348 }
00349 }
00350
00351
00352
00353
00354
00355 void
00356 sharedfd(void)
00357 {
00358 int fd, pid, i, n, nc, np;
00359 char buf[10];
00360
00361 unlink("sharedfd");
00362 fd = open("sharedfd", O_CREATE|O_RDWR);
00363 if(fd < 0){
00364 printf(1, "fstests: cannot open sharedfd for writing");
00365 return;
00366 }
00367 pid = fork();
00368 memset(buf, pid==0?'c':'p', sizeof(buf));
00369 for(i = 0; i < 1000; i++){
00370 if(write(fd, buf, sizeof(buf)) != sizeof(buf)){
00371 printf(1, "fstests: write sharedfd failed\n");
00372 break;
00373 }
00374 }
00375 if(pid == 0)
00376 exit();
00377 else
00378 wait();
00379 close(fd);
00380 fd = open("sharedfd", 0);
00381 if(fd < 0){
00382 printf(1, "fstests: cannot open sharedfd for reading\n");
00383 return;
00384 }
00385 nc = np = 0;
00386 while((n = read(fd, buf, sizeof(buf))) > 0){
00387 for(i = 0; i < sizeof(buf); i++){
00388 if(buf[i] == 'c')
00389 nc++;
00390 if(buf[i] == 'p')
00391 np++;
00392 }
00393 }
00394 close(fd);
00395 unlink("sharedfd");
00396 if(nc == 10000 && np == 10000)
00397 printf(1, "sharedfd ok\n");
00398 else
00399 printf(1, "sharedfd oops %d %d\n", nc, np);
00400 }
00401
00402
00403
00404 void
00405 twofiles(void)
00406 {
00407 int fd, pid, i, j, n, total;
00408 char *fname;
00409
00410 printf(1, "twofiles test\n");
00411
00412 unlink("f1");
00413 unlink("f2");
00414
00415 pid = fork();
00416 if(pid < 0){
00417 printf(1, "fork failed\n");
00418 return;
00419 }
00420
00421 fname = pid ? "f1" : "f2";
00422 fd = open(fname, O_CREATE | O_RDWR);
00423 if(fd < 0){
00424 printf(1, "create failed\n");
00425 exit();
00426 }
00427
00428 memset(buf, pid?'p':'c', 512);
00429 for(i = 0; i < 12; i++){
00430 if((n = write(fd, buf, 500)) != 500){
00431 printf(1, "write failed %d\n", n);
00432 exit();
00433 }
00434 }
00435 close(fd);
00436 if(pid)
00437 wait();
00438 else
00439 exit();
00440
00441 for(i = 0; i < 2; i++){
00442 fd = open(i?"f1":"f2", 0);
00443 total = 0;
00444 while((n = read(fd, buf, sizeof(buf))) > 0){
00445 for(j = 0; j < n; j++){
00446 if(buf[j] != (i?'p':'c')){
00447 printf(1, "wrong char\n");
00448 exit();
00449 }
00450 }
00451 total += n;
00452 }
00453 close(fd);
00454 if(total != 12*500){
00455 printf(1, "wrong length %d\n", total);
00456 exit();
00457 }
00458 }
00459
00460 unlink("f1");
00461 unlink("f2");
00462
00463 printf(1, "twofiles ok\n");
00464 }
00465
00466
00467 void
00468 createdelete(void)
00469 {
00470 enum { N = 20 };
00471 int pid, i, fd;
00472 char name[32];
00473
00474 printf(1, "createdelete test\n");
00475 pid = fork();
00476 if(pid < 0){
00477 printf(1, "fork failed\n");
00478 exit();
00479 }
00480
00481 name[0] = pid ? 'p' : 'c';
00482 name[2] = '\0';
00483 for(i = 0; i < N; i++){
00484 name[1] = '0' + i;
00485 fd = open(name, O_CREATE | O_RDWR);
00486 if(fd < 0){
00487 printf(1, "create failed\n");
00488 exit();
00489 }
00490 close(fd);
00491 if(i > 0 && (i % 2 ) == 0){
00492 name[1] = '0' + (i / 2);
00493 if(unlink(name) < 0){
00494 printf(1, "unlink failed\n");
00495 exit();
00496 }
00497 }
00498 }
00499
00500 if(pid==0)
00501 exit();
00502 else
00503 wait();
00504
00505 for(i = 0; i < N; i++){
00506 name[0] = 'p';
00507 name[1] = '0' + i;
00508 fd = open(name, 0);
00509 if((i == 0 || i >= N/2) && fd < 0){
00510 printf(1, "oops createdelete %s didn't exist\n", name);
00511 exit();
00512 } else if((i >= 1 && i < N/2) && fd >= 0){
00513 printf(1, "oops createdelete %s did exist\n", name);
00514 exit();
00515 }
00516 if(fd >= 0)
00517 close(fd);
00518
00519 name[0] = 'c';
00520 name[1] = '0' + i;
00521 fd = open(name, 0);
00522 if((i == 0 || i >= N/2) && fd < 0){
00523 printf(1, "oops createdelete %s didn't exist\n", name);
00524 exit();
00525 } else if((i >= 1 && i < N/2) && fd >= 0){
00526 printf(1, "oops createdelete %s did exist\n", name);
00527 exit();
00528 }
00529 if(fd >= 0)
00530 close(fd);
00531 }
00532
00533 for(i = 0; i < N; i++){
00534 name[0] = 'p';
00535 name[1] = '0' + i;
00536 unlink(name);
00537 name[0] = 'c';
00538 unlink(name);
00539 }
00540
00541 printf(1, "createdelete ok\n");
00542 }
00543
00544
00545 void
00546 unlinkread(void)
00547 {
00548 int fd, fd1;
00549
00550 printf(1, "unlinkread test\n");
00551 fd = open("unlinkread", O_CREATE | O_RDWR);
00552 if(fd < 0){
00553 printf(1, "create unlinkread failed\n");
00554 exit();
00555 }
00556 write(fd, "hello", 5);
00557 close(fd);
00558
00559 fd = open("unlinkread", O_RDWR);
00560 if(fd < 0){
00561 printf(1, "open unlinkread failed\n");
00562 exit();
00563 }
00564 if(unlink("unlinkread") != 0){
00565 printf(1, "unlink unlinkread failed\n");
00566 exit();
00567 }
00568
00569 fd1 = open("unlinkread", O_CREATE | O_RDWR);
00570 write(fd1, "yyy", 3);
00571 close(fd1);
00572
00573 if(read(fd, buf, sizeof(buf)) != 5){
00574 printf(1, "unlinkread read failed");
00575 exit();
00576 }
00577 if(buf[0] != 'h'){
00578 printf(1, "unlinkread wrong data\n");
00579 exit();
00580 }
00581 if(write(fd, buf, 10) != 10){
00582 printf(1, "unlinkread write failed\n");
00583 exit();
00584 }
00585 close(fd);
00586 unlink("unlinkread");
00587 printf(1, "unlinkread ok\n");
00588 }
00589
00590 void
00591 linktest(void)
00592 {
00593 int fd;
00594
00595 printf(1, "linktest\n");
00596
00597 unlink("lf1");
00598 unlink("lf2");
00599
00600 fd = open("lf1", O_CREATE|O_RDWR);
00601 if(fd < 0){
00602 printf(1, "create lf1 failed\n");
00603 exit();
00604 }
00605 if(write(fd, "hello", 5) != 5){
00606 printf(1, "write lf1 failed\n");
00607 exit();
00608 }
00609 close(fd);
00610
00611 if(link("lf1", "lf2") < 0){
00612 printf(1, "link lf1 lf2 failed\n");
00613 exit();
00614 }
00615 unlink("lf1");
00616
00617 if(open("lf1", 0) >= 0){
00618 printf(1, "unlinked lf1 but it is still there!\n");
00619 exit();
00620 }
00621
00622 fd = open("lf2", 0);
00623 if(fd < 0){
00624 printf(1, "open lf2 failed\n");
00625 exit();
00626 }
00627 if(read(fd, buf, sizeof(buf)) != 5){
00628 printf(1, "read lf2 failed\n");
00629 exit();
00630 }
00631 close(fd);
00632
00633 if(link("lf2", "lf2") >= 0){
00634 printf(1, "link lf2 lf2 succeeded! oops\n");
00635 exit();
00636 }
00637
00638 unlink("lf2");
00639 if(link("lf2", "lf1") >= 0){
00640 printf(1, "link non-existant succeeded! oops\n");
00641 exit();
00642 }
00643
00644 if(link(".", "lf1") >= 0){
00645 printf(1, "link . lf1 succeeded! oops\n");
00646 exit();
00647 }
00648
00649 printf(1, "linktest ok\n");
00650 }
00651
00652
00653 void
00654 concreate(void)
00655 {
00656 char file[3];
00657 int i, pid, n, fd;
00658 char fa[40];
00659 struct {
00660 ushort inum;
00661 char name[14];
00662 } de;
00663
00664 printf(1, "concreate test\n");
00665 file[0] = 'C';
00666 file[2] = '\0';
00667 for(i = 0; i < 40; i++){
00668 file[1] = '0' + i;
00669 unlink(file);
00670 pid = fork();
00671 if(pid && (i % 3) == 1){
00672 link("C0", file);
00673 } else if(pid == 0 && (i % 5) == 1){
00674 link("C0", file);
00675 } else {
00676 fd = open(file, O_CREATE | O_RDWR);
00677 if(fd < 0){
00678 printf(1, "concreate create %s failed\n", file);
00679 exit();
00680 }
00681 close(fd);
00682 }
00683 if(pid == 0)
00684 exit();
00685 else
00686 wait();
00687 }
00688
00689 memset(fa, 0, sizeof(fa));
00690 fd = open(".", 0);
00691 n = 0;
00692 while(read(fd, &de, sizeof(de)) > 0){
00693 if(de.inum == 0)
00694 continue;
00695 if(de.name[0] == 'C' && de.name[2] == '\0'){
00696 i = de.name[1] - '0';
00697 if(i < 0 || i >= sizeof(fa)){
00698 printf(1, "concreate weird file %s\n", de.name);
00699 exit();
00700 }
00701 if(fa[i]){
00702 printf(1, "concreate duplicate file %s\n", de.name);
00703 exit();
00704 }
00705 fa[i] = 1;
00706 n++;
00707 }
00708 }
00709 close(fd);
00710
00711 if(n != 40){
00712 printf(1, "concreate not enough files in directory listing\n");
00713 exit();
00714 }
00715
00716 for(i = 0; i < 40; i++){
00717 file[1] = '0' + i;
00718 pid = fork();
00719 if(pid < 0){
00720 printf(1, "fork failed\n");
00721 exit();
00722 }
00723 if(((i % 3) == 0 && pid == 0) ||
00724 ((i % 3) == 1 && pid != 0)){
00725 fd = open(file, 0);
00726 close(fd);
00727 } else {
00728 unlink(file);
00729 }
00730 if(pid == 0)
00731 exit();
00732 else
00733 wait();
00734 }
00735
00736 printf(1, "concreate ok\n");
00737 }
00738
00739
00740 void
00741 bigdir(void)
00742 {
00743 int i, fd;
00744 char name[10];
00745
00746 printf(1, "bigdir test\n");
00747 unlink("bd");
00748
00749 fd = open("bd", O_CREATE);
00750 if(fd < 0){
00751 printf(1, "bigdir create failed\n");
00752 exit();
00753 }
00754 close(fd);
00755
00756 for(i = 0; i < 500; i++){
00757 name[0] = 'x';
00758 name[1] = '0' + (i / 64);
00759 name[2] = '0' + (i % 64);
00760 name[3] = '\0';
00761 if(link("bd", name) != 0){
00762 printf(1, "bigdir link failed\n");
00763 exit();
00764 }
00765 }
00766
00767 unlink("bd");
00768 for(i = 0; i < 500; i++){
00769 name[0] = 'x';
00770 name[1] = '0' + (i / 64);
00771 name[2] = '0' + (i % 64);
00772 name[3] = '\0';
00773 if(unlink(name) != 0){
00774 printf(1, "bigdir unlink failed");
00775 exit();
00776 }
00777 }
00778
00779 printf(1, "bigdir ok\n");
00780 }
00781
00782 void
00783 subdir(void)
00784 {
00785 int fd, cc;
00786
00787 printf(1, "subdir test\n");
00788
00789 unlink("ff");
00790 if(mkdir("dd") != 0){
00791 printf(1, "subdir mkdir dd failed\n");
00792 exit();
00793 }
00794
00795 fd = open("dd/ff", O_CREATE | O_RDWR);
00796 if(fd < 0){
00797 printf(1, "create dd/ff failed\n");
00798 exit();
00799 }
00800 write(fd, "ff", 2);
00801 close(fd);
00802
00803 if(unlink("dd") >= 0){
00804 printf(1, "unlink dd (non-empty dir) succeeded!\n");
00805 exit();
00806 }
00807
00808 if(mkdir("/dd/dd") != 0){
00809 printf(1, "subdir mkdir dd/dd failed\n");
00810 exit();
00811 }
00812
00813 fd = open("dd/dd/ff", O_CREATE | O_RDWR);
00814 if(fd < 0){
00815 printf(1, "create dd/dd/ff failed\n");
00816 exit();
00817 }
00818 write(fd, "FF", 2);
00819 close(fd);
00820
00821 fd = open("dd/dd/../ff", 0);
00822 if(fd < 0){
00823 printf(1, "open dd/dd/../ff failed\n");
00824 exit();
00825 }
00826 cc = read(fd, buf, sizeof(buf));
00827 if(cc != 2 || buf[0] != 'f'){
00828 printf(1, "dd/dd/../ff wrong content\n");
00829 exit();
00830 }
00831 close(fd);
00832
00833 if(link("dd/dd/ff", "dd/dd/ffff") != 0){
00834 printf(1, "link dd/dd/ff dd/dd/ffff failed\n");
00835 exit();
00836 }
00837
00838 if(unlink("dd/dd/ff") != 0){
00839 printf(1, "unlink dd/dd/ff failed\n");
00840 exit();
00841 }
00842 if(open("dd/dd/ff", O_RDONLY) >= 0){
00843 printf(1, "open (unlinked) dd/dd/ff succeeded\n");
00844 exit();
00845 }
00846
00847 if(chdir("dd") != 0){
00848 printf(1, "chdir dd failed\n");
00849 exit();
00850 }
00851 if(chdir("dd/../../dd") != 0){
00852 printf(1, "chdir dd/../../dd failed\n");
00853 exit();
00854 }
00855 if(chdir("dd/../../../dd") != 0){
00856 printf(1, "chdir dd/../../dd failed\n");
00857 exit();
00858 }
00859 if(chdir("./..") != 0){
00860 printf(1, "chdir ./.. failed\n");
00861 exit();
00862 }
00863
00864 fd = open("dd/dd/ffff", 0);
00865 if(fd < 0){
00866 printf(1, "open dd/dd/ffff failed\n");
00867 exit();
00868 }
00869 if(read(fd, buf, sizeof(buf)) != 2){
00870 printf(1, "read dd/dd/ffff wrong len\n");
00871 exit();
00872 }
00873 close(fd);
00874
00875 if(open("dd/dd/ff", O_RDONLY) >= 0){
00876 printf(1, "open (unlinked) dd/dd/ff succeeded!\n");
00877 exit();
00878 }
00879
00880 if(open("dd/ff/ff", O_CREATE|O_RDWR) >= 0){
00881 printf(1, "create dd/ff/ff succeeded!\n");
00882 exit();
00883 }
00884 if(open("dd/xx/ff", O_CREATE|O_RDWR) >= 0){
00885 printf(1, "create dd/xx/ff succeeded!\n");
00886 exit();
00887 }
00888 if(open("dd", O_CREATE) >= 0){
00889 printf(1, "create dd succeeded!\n");
00890 exit();
00891 }
00892 if(open("dd", O_RDWR) >= 0){
00893 printf(1, "open dd rdwr succeeded!\n");
00894 exit();
00895 }
00896 if(open("dd", O_WRONLY) >= 0){
00897 printf(1, "open dd wronly succeeded!\n");
00898 exit();
00899 }
00900 if(link("dd/ff/ff", "dd/dd/xx") == 0){
00901 printf(1, "link dd/ff/ff dd/dd/xx succeeded!\n");
00902 exit();
00903 }
00904 if(link("dd/xx/ff", "dd/dd/xx") == 0){
00905 printf(1, "link dd/xx/ff dd/dd/xx succeeded!\n");
00906 exit();
00907 }
00908 if(link("dd/ff", "dd/dd/ffff") == 0){
00909 printf(1, "link dd/ff dd/dd/ffff succeeded!\n");
00910 exit();
00911 }
00912 if(mkdir("dd/ff/ff") == 0){
00913 printf(1, "mkdir dd/ff/ff succeeded!\n");
00914 exit();
00915 }
00916 if(mkdir("dd/xx/ff") == 0){
00917 printf(1, "mkdir dd/xx/ff succeeded!\n");
00918 exit();
00919 }
00920 if(mkdir("dd/dd/ffff") == 0){
00921 printf(1, "mkdir dd/dd/ffff succeeded!\n");
00922 exit();
00923 }
00924 if(unlink("dd/xx/ff") == 0){
00925 printf(1, "unlink dd/xx/ff succeeded!\n");
00926 exit();
00927 }
00928 if(unlink("dd/ff/ff") == 0){
00929 printf(1, "unlink dd/ff/ff succeeded!\n");
00930 exit();
00931 }
00932 if(chdir("dd/ff") == 0){
00933 printf(1, "chdir dd/ff succeeded!\n");
00934 exit();
00935 }
00936 if(chdir("dd/xx") == 0){
00937 printf(1, "chdir dd/xx succeeded!\n");
00938 exit();
00939 }
00940
00941 if(unlink("dd/dd/ffff") != 0){
00942 printf(1, "unlink dd/dd/ff failed\n");
00943 exit();
00944 }
00945 if(unlink("dd/ff") != 0){
00946 printf(1, "unlink dd/ff failed\n");
00947 exit();
00948 }
00949 if(unlink("dd") == 0){
00950 printf(1, "unlink non-empty dd succeeded!\n");
00951 exit();
00952 }
00953 if(unlink("dd/dd") < 0){
00954 printf(1, "unlink dd/dd failed\n");
00955 exit();
00956 }
00957 if(unlink("dd") < 0){
00958 printf(1, "unlink dd failed\n");
00959 exit();
00960 }
00961
00962 printf(1, "subdir ok\n");
00963 }
00964
00965 void
00966 bigfile(void)
00967 {
00968 int fd, i, total, cc;
00969
00970 printf(1, "bigfile test\n");
00971
00972 unlink("bigfile");
00973 fd = open("bigfile", O_CREATE | O_RDWR);
00974 if(fd < 0){
00975 printf(1, "cannot create bigfile");
00976 exit();
00977 }
00978 for(i = 0; i < 20; i++){
00979 memset(buf, i, 600);
00980 if(write(fd, buf, 600) != 600){
00981 printf(1, "write bigfile failed\n");
00982 exit();
00983 }
00984 }
00985 close(fd);
00986
00987 fd = open("bigfile", 0);
00988 if(fd < 0){
00989 printf(1, "cannot open bigfile\n");
00990 exit();
00991 }
00992 total = 0;
00993 for(i = 0; ; i++){
00994 cc = read(fd, buf, 300);
00995 if(cc < 0){
00996 printf(1, "read bigfile failed\n");
00997 exit();
00998 }
00999 if(cc == 0)
01000 break;
01001 if(cc != 300){
01002 printf(1, "short read bigfile\n");
01003 exit();
01004 }
01005 if(buf[0] != i/2 || buf[299] != i/2){
01006 printf(1, "read bigfile wrong data\n");
01007 exit();
01008 }
01009 total += cc;
01010 }
01011 close(fd);
01012 if(total != 20*600){
01013 printf(1, "read bigfile wrong total\n");
01014 exit();
01015 }
01016 unlink("bigfile");
01017
01018 printf(1, "bigfile test ok\n");
01019 }
01020
01021 void
01022 fourteen(void)
01023 {
01024 int fd;
01025
01026
01027 printf(1, "fourteen test\n");
01028
01029 if(mkdir("12345678901234") != 0){
01030 printf(1, "mkdir 12345678901234 failed\n");
01031 exit();
01032 }
01033 if(mkdir("12345678901234/123456789012345") != 0){
01034 printf(1, "mkdir 12345678901234/123456789012345 failed\n");
01035 exit();
01036 }
01037 fd = open("123456789012345/123456789012345/123456789012345", O_CREATE);
01038 if(fd < 0){
01039 printf(1, "create 123456789012345/123456789012345/123456789012345 failed\n");
01040 exit();
01041 }
01042 close(fd);
01043 fd = open("12345678901234/12345678901234/12345678901234", 0);
01044 if(fd < 0){
01045 printf(1, "open 12345678901234/12345678901234/12345678901234 failed\n");
01046 exit();
01047 }
01048 close(fd);
01049
01050 if(mkdir("12345678901234/12345678901234") == 0){
01051 printf(1, "mkdir 12345678901234/12345678901234 succeeded!\n");
01052 exit();
01053 }
01054 if(mkdir("123456789012345/12345678901234") == 0){
01055 printf(1, "mkdir 12345678901234/123456789012345 succeeded!\n");
01056 exit();
01057 }
01058
01059 printf(1, "fourteen ok\n");
01060 }
01061
01062 void
01063 rmdot(void)
01064 {
01065 printf(1, "rmdot test\n");
01066 if(mkdir("dots") != 0){
01067 printf(1, "mkdir dots failed\n");
01068 exit();
01069 }
01070 if(chdir("dots") != 0){
01071 printf(1, "chdir dots failed\n");
01072 exit();
01073 }
01074 if(unlink(".") == 0){
01075 printf(1, "rm . worked!\n");
01076 exit();
01077 }
01078 if(unlink("..") == 0){
01079 printf(1, "rm .. worked!\n");
01080 exit();
01081 }
01082 if(chdir("/") != 0){
01083 printf(1, "chdir / failed\n");
01084 exit();
01085 }
01086 if(unlink("dots/.") == 0){
01087 printf(1, "unlink dots/. worked!\n");
01088 exit();
01089 }
01090 if(unlink("dots/..") == 0){
01091 printf(1, "unlink dots/.. worked!\n");
01092 exit();
01093 }
01094 if(unlink("dots") != 0){
01095 printf(1, "unlink dots failed!\n");
01096 exit();
01097 }
01098 printf(1, "rmdot ok\n");
01099 }
01100
01101 void
01102 dirfile(void)
01103 {
01104 int fd;
01105
01106 printf(1, "dir vs file\n");
01107
01108 fd = open("dirfile", O_CREATE);
01109 if(fd < 0){
01110 printf(1, "create dirfile failed\n");
01111 exit();
01112 }
01113 close(fd);
01114 if(chdir("dirfile") == 0){
01115 printf(1, "chdir dirfile succeeded!\n");
01116 exit();
01117 }
01118 fd = open("dirfile/xx", 0);
01119 if(fd >= 0){
01120 printf(1, "create dirfile/xx succeeded!\n");
01121 exit();
01122 }
01123 fd = open("dirfile/xx", O_CREATE);
01124 if(fd >= 0){
01125 printf(1, "create dirfile/xx succeeded!\n");
01126 exit();
01127 }
01128 if(mkdir("dirfile/xx") == 0){
01129 printf(1, "mkdir dirfile/xx succeeded!\n");
01130 exit();
01131 }
01132 if(unlink("dirfile/xx") == 0){
01133 printf(1, "unlink dirfile/xx succeeded!\n");
01134 exit();
01135 }
01136 if(link("README", "dirfile/xx") == 0){
01137 printf(1, "link to dirfile/xx succeeded!\n");
01138 exit();
01139 }
01140 if(unlink("dirfile") != 0){
01141 printf(1, "unlink dirfile failed!\n");
01142 exit();
01143 }
01144
01145 fd = open(".", O_RDWR);
01146 if(fd >= 0){
01147 printf(1, "open . for writing succeeded!\n");
01148 exit();
01149 }
01150 fd = open(".", 0);
01151 if(write(fd, "x", 1) > 0){
01152 printf(1, "write . succeeded!\n");
01153 exit();
01154 }
01155 close(fd);
01156
01157 printf(1, "dir vs file OK\n");
01158 }
01159
01160
01161 void
01162 iref(void)
01163 {
01164 int i, fd;
01165
01166 printf(1, "empty file name\n");
01167
01168
01169 for(i = 0; i < 50 + 1; i++){
01170 if(mkdir("irefd") != 0){
01171 printf(1, "mkdir irefd failed\n");
01172 exit();
01173 }
01174 if(chdir("irefd") != 0){
01175 printf(1, "chdir irefd failed\n");
01176 exit();
01177 }
01178
01179 mkdir("");
01180 link("README", "");
01181 fd = open("", O_CREATE);
01182 if(fd >= 0)
01183 close(fd);
01184 fd = open("xx", O_CREATE);
01185 if(fd >= 0)
01186 close(fd);
01187 unlink("xx");
01188 }
01189
01190 chdir("/");
01191 printf(1, "empty file name OK\n");
01192 }
01193
01194
01195
01196
01197 void
01198 forktest(void)
01199 {
01200 int n, pid;
01201
01202 printf(1, "fork test\n");
01203
01204 for(n=0; n<1000; n++){
01205 pid = fork();
01206 if(pid < 0)
01207 break;
01208 if(pid == 0)
01209 exit();
01210 }
01211
01212 if(n == 1000){
01213 printf(1, "fork claimed to work 1000 times!\n");
01214 exit();
01215 }
01216
01217 for(; n > 0; n--){
01218 if(wait() < 0){
01219 printf(1, "wait stopped early\n");
01220 exit();
01221 }
01222 }
01223
01224 if(wait() != -1){
01225 printf(1, "wait got too many\n");
01226 exit();
01227 }
01228
01229 printf(1, "fork test OK\n");
01230 }
01231
01232 int
01233 main(int argc, char *argv[])
01234 {
01235 printf(1, "usertests starting\n");
01236
01237 if(open("usertests.ran", 0) >= 0){
01238 printf(1, "already ran user tests -- rebuild fs.img\n");
01239 exit();
01240 }
01241 close(open("usertests.ran", O_CREATE));
01242
01243 opentest();
01244 writetest();
01245 writetest1();
01246 createtest();
01247
01248 mem();
01249 pipe1();
01250 preempt();
01251 exitwait();
01252
01253 rmdot();
01254 fourteen();
01255 bigfile();
01256 subdir();
01257 concreate();
01258 linktest();
01259 unlinkread();
01260 createdelete();
01261 twofiles();
01262 sharedfd();
01263 dirfile();
01264 iref();
01265 forktest();
01266 bigdir();
01267
01268 exectest();
01269
01270 exit();
01271 }