make exec test self contained

This commit is contained in:
Frans Kaashoek 2019-09-19 15:14:52 -04:00
parent f6a37c17f2
commit 3f6e372a29

View file

@ -21,7 +21,6 @@
char buf[BUFSZ]; char buf[BUFSZ];
char name[3]; char name[3];
char *echoargv[] = { "echo", "ALL", "TESTS", "PASSED", 0 };
// does chdir() call iput(p->cwd) in a transaction? // does chdir() call iput(p->cwd) in a transaction?
void void
@ -283,10 +282,56 @@ void dirtest(char *s)
void void
exectest(char *s) exectest(char *s)
{ {
int fd, xstatus, pid;
char *echoargv[] = { "echo", "OK", 0 };
char buf[3];
unlink("echo-ok");
pid = fork();
if(pid < 0) {
printf("%s: fork failed\n", s);
exit(1);
}
if(pid == 0) {
close(1);
fd = open("echo-ok", O_CREATE|O_WRONLY);
if(fd < 0) {
printf("%s: create failed\n", s);
exit(1);
}
if(fd != 1) {
printf("%s: wrong fd\n", s);
exit(1);
}
if(exec("echo", echoargv) < 0){ if(exec("echo", echoargv) < 0){
printf("%s: exec echo failed\n", s); printf("%s: exec echo failed\n", s);
exit(1); exit(1);
} }
// won't get to here
}
if (wait(&xstatus) != pid) {
printf("%s: wait failed!\n", s);
}
if(xstatus != 0)
exit(xstatus);
fd = open("echo-ok", O_RDONLY);
if(fd < 0) {
printf("%s: open failed\n", s);
exit(1);
}
if (read(fd, buf, 2) != 2) {
printf("%s: read failed\n", s);
exit(1);
}
unlink("echo-ok");
if(buf[0] == 'O' && buf[1] == 'K')
exit(0);
else {
printf("%s: wrong output\n", s);
exit(1);
}
} }
// simple fork and pipe read/write // simple fork and pipe read/write
@ -1862,7 +1907,8 @@ stacktest(char *s)
exit(xstatus); exit(xstatus);
} }
void // 1 if successful
int
run(void f(char *), char *s) { run(void f(char *), char *s) {
int pid; int pid;
int xstatus; int xstatus;
@ -1880,8 +1926,8 @@ run(void f(char *), char *s) {
printf("test %s FAILED\n", s); printf("test %s FAILED\n", s);
else else
printf("test %s OK\n", s); printf("test %s OK\n", s);
return xstatus == 0;
} }
} }
int int
@ -1909,6 +1955,7 @@ main(int argc, char *argv[])
{subdir, "subdir"}, {subdir, "subdir"},
{fourfiles, "fourfiles"}, {fourfiles, "fourfiles"},
{sharedfd, "sharedfd"}, {sharedfd, "sharedfd"},
{exectest, "exectest"},
{bigargtest, "bigargtest"}, {bigargtest, "bigargtest"},
{bigwrite, "bigwrite"}, {bigwrite, "bigwrite"},
{bsstest, "bsstest"}, {bsstest, "bsstest"},
@ -1933,23 +1980,27 @@ main(int argc, char *argv[])
{iref, "iref"}, {iref, "iref"},
{forktest, "forktest"}, {forktest, "forktest"},
{bigdir, "bigdir"}, // slow {bigdir, "bigdir"}, // slow
{exectest, "exectest"}, // must be last
{ 0, 0}, { 0, 0},
}; };
printf("usertests starting\n"); printf("usertests starting\n");
if(open("usertests.ran", 0) >= 0){ if(open("usertests.ran", 0) >= 0){
printf("already ran user tests -- rebuild fs.img\n"); printf("already ran user tests -- rebuild fs.img (make fs.img)\n");
exit(1); exit(1);
} }
close(open("usertests.ran", O_CREATE)); close(open("usertests.ran", O_CREATE));
int fail = 0;
for (struct test *t = tests; t->s != 0; t++) { for (struct test *t = tests; t->s != 0; t++) {
if((n == 0) || strcmp(t->s, n) == 0) { if((n == 0) || strcmp(t->s, n) == 0) {
run(t->f, t->s); if(!run(t->f, t->s))
fail = 1;
} }
} }
if(!fail)
exit(0); printf("ALL TESTS PASSED\n");
else
printf("SOME TESTS FAILED\n");
exit(1); // not reached.
} }