xv6-65oo2/usertests.c

1632 lines
30 KiB
C
Raw Normal View History

#include "types.h"
#include "stat.h"
#include "user.h"
2006-09-07 13:23:41 +00:00
#include "fs.h"
#include "fcntl.h"
#include "syscall.h"
#include "traps.h"
#include "memlayout.h"
char buf[8192];
2006-09-07 13:23:41 +00:00
char name[3];
char *echoargv[] = { "echo", "ALL", "TESTS", "PASSED", 0 };
2006-09-07 13:23:41 +00:00
int stdout = 1;
// simple file system tests
void
opentest(void)
{
int fd;
printf(stdout, "open test\n");
fd = open("echo", 0);
if(fd < 0){
printf(stdout, "open echo failed!\n");
exit();
}
close(fd);
fd = open("doesnotexist", 0);
if(fd >= 0){
printf(stdout, "open doesnotexist succeeded!\n");
exit();
}
printf(stdout, "open test ok\n");
}
void
writetest(void)
{
int fd;
int i;
printf(stdout, "small file test\n");
fd = open("small", O_CREATE|O_RDWR);
if(fd >= 0){
printf(stdout, "creat small succeeded; ok\n");
} else {
printf(stdout, "error: creat small failed!\n");
exit();
}
for(i = 0; i < 100; i++){
if(write(fd, "aaaaaaaaaa", 10) != 10){
2006-09-07 13:23:41 +00:00
printf(stdout, "error: write aa %d new file failed\n", i);
exit();
}
if(write(fd, "bbbbbbbbbb", 10) != 10){
2006-09-07 13:23:41 +00:00
printf(stdout, "error: write bb %d new file failed\n", i);
exit();
}
}
printf(stdout, "writes ok\n");
close(fd);
fd = open("small", O_RDONLY);
if(fd >= 0){
printf(stdout, "open small succeeded ok\n");
} else {
printf(stdout, "error: open small failed!\n");
exit();
}
i = read(fd, buf, 2000);
if(i == 2000){
2006-09-07 13:23:41 +00:00
printf(stdout, "read succeeded ok\n");
} else {
printf(stdout, "read failed\n");
exit();
}
close(fd);
if(unlink("small") < 0){
2006-09-07 13:23:41 +00:00
printf(stdout, "unlink small failed\n");
exit();
}
printf(stdout, "small file test ok\n");
}
void
writetest1(void)
{
int i, fd, n;
printf(stdout, "big files test\n");
fd = open("big", O_CREATE|O_RDWR);
if(fd < 0){
printf(stdout, "error: creat big failed!\n");
exit();
}
for(i = 0; i < MAXFILE; i++){
((int*)buf)[0] = i;
if(write(fd, buf, 512) != 512){
2006-09-07 13:23:41 +00:00
printf(stdout, "error: write big file failed\n", i);
exit();
}
}
close(fd);
fd = open("big", O_RDONLY);
if(fd < 0){
printf(stdout, "error: open big failed!\n");
exit();
}
n = 0;
for(;;){
2006-09-07 13:23:41 +00:00
i = read(fd, buf, 512);
if(i == 0){
if(n == MAXFILE - 1){
2006-09-07 13:23:41 +00:00
printf(stdout, "read only %d blocks from big", n);
exit();
}
break;
} else if(i != 512){
2006-09-07 13:23:41 +00:00
printf(stdout, "read failed %d\n", i);
exit();
}
if(((int*)buf)[0] != n){
2006-09-07 13:23:41 +00:00
printf(stdout, "read content of block %d is %d\n",
n, ((int*)buf)[0]);
exit();
}
n++;
}
close(fd);
if(unlink("big") < 0){
2006-09-07 13:23:41 +00:00
printf(stdout, "unlink big failed\n");
exit();
}
printf(stdout, "big files ok\n");
}
void
createtest(void)
{
int i, fd;
printf(stdout, "many creates, followed by unlink test\n");
name[0] = 'a';
name[2] = '\0';
for(i = 0; i < 52; i++){
2006-09-07 13:23:41 +00:00
name[1] = '0' + i;
fd = open(name, O_CREATE|O_RDWR);
close(fd);
}
name[0] = 'a';
name[2] = '\0';
for(i = 0; i < 52; i++){
2006-09-07 13:23:41 +00:00
name[1] = '0' + i;
unlink(name);
}
printf(stdout, "many creates, followed by unlink; ok\n");
}
void dirtest(void)
{
printf(stdout, "mkdir test\n");
if(mkdir("dir0") < 0){
2006-09-07 13:23:41 +00:00
printf(stdout, "mkdir failed\n");
exit();
}
if(chdir("dir0") < 0){
2006-09-07 13:23:41 +00:00
printf(stdout, "chdir dir0 failed\n");
exit();
}
if(chdir("..") < 0){
2006-09-07 13:23:41 +00:00
printf(stdout, "chdir .. failed\n");
exit();
}
if(unlink("dir0") < 0){
2006-09-07 13:23:41 +00:00
printf(stdout, "unlink dir0 failed\n");
exit();
}
printf(stdout, "mkdir test\n");
}
void
exectest(void)
{
printf(stdout, "exec test\n");
if(exec("echo", echoargv) < 0){
2006-09-07 13:23:41 +00:00
printf(stdout, "exec echo failed\n");
exit();
}
}
2006-06-27 14:35:53 +00:00
// simple fork and pipe read/write
2006-06-27 14:35:53 +00:00
void
2006-07-17 01:25:22 +00:00
pipe1(void)
2006-06-27 14:35:53 +00:00
{
int fds[2], pid;
2007-08-10 17:17:42 +00:00
int seq, i, n, cc, total;
2006-06-27 14:35:53 +00:00
2006-08-29 19:06:37 +00:00
if(pipe(fds) != 0){
2006-09-07 13:23:41 +00:00
printf(1, "pipe() failed\n");
2006-08-29 19:06:37 +00:00
exit();
}
pid = fork();
2007-08-10 17:17:42 +00:00
seq = 0;
2006-06-27 14:35:53 +00:00
if(pid == 0){
close(fds[0]);
for(n = 0; n < 5; n++){
for(i = 0; i < 1033; i++)
buf[i] = seq++;
if(write(fds[1], buf, 1033) != 1033){
printf(1, "pipe1 oops 1\n");
exit();
}
}
exit();
2006-08-29 19:06:37 +00:00
} else if(pid > 0){
close(fds[1]);
total = 0;
cc = 1;
2006-07-17 01:25:22 +00:00
while((n = read(fds[0], buf, cc)) > 0){
for(i = 0; i < n; i++){
if((buf[i] & 0xff) != (seq++ & 0xff)){
printf(1, "pipe1 oops 2\n");
return;
}
}
total += n;
cc = cc * 2;
if(cc > sizeof(buf))
cc = sizeof(buf);
2006-06-27 14:35:53 +00:00
}
if(total != 5 * 1033)
2006-08-29 19:06:37 +00:00
printf(1, "pipe1 oops 3 total %d\n", total);
close(fds[0]);
wait();
2006-08-29 19:06:37 +00:00
} else {
2006-09-07 13:23:41 +00:00
printf(1, "fork() failed\n");
2006-08-29 19:06:37 +00:00
exit();
2006-06-27 14:35:53 +00:00
}
2006-09-07 13:23:41 +00:00
printf(1, "pipe1 ok\n");
2006-06-27 14:35:53 +00:00
}
// meant to be run w/ at most two CPUs
void
2006-07-17 01:25:22 +00:00
preempt(void)
{
int pid1, pid2, pid3;
int pfds[2];
2007-09-26 23:24:23 +00:00
printf(1, "preempt: ");
pid1 = fork();
if(pid1 == 0)
2006-07-17 01:25:22 +00:00
for(;;)
;
2006-09-06 17:27:19 +00:00
pid2 = fork();
if(pid2 == 0)
2006-07-17 01:25:22 +00:00
for(;;)
;
pipe(pfds);
pid3 = fork();
if(pid3 == 0){
close(pfds[0]);
if(write(pfds[1], "x", 1) != 1)
printf(1, "preempt write error");
close(pfds[1]);
2006-07-17 01:25:22 +00:00
for(;;)
;
}
close(pfds[1]);
if(read(pfds[0], buf, sizeof(buf)) != 1){
printf(1, "preempt read error");
return;
}
close(pfds[0]);
2007-09-26 23:24:23 +00:00
printf(1, "kill... ");
kill(pid1);
kill(pid2);
kill(pid3);
2007-09-26 23:24:23 +00:00
printf(1, "wait... ");
wait();
wait();
wait();
2006-09-07 13:23:41 +00:00
printf(1, "preempt ok\n");
}
// try to find any races between exit and wait
void
2006-07-17 01:25:22 +00:00
exitwait(void)
{
int i, pid;
for(i = 0; i < 100; i++){
pid = fork();
if(pid < 0){
printf(1, "fork failed\n");
return;
}
if(pid){
if(wait() != pid){
printf(1, "wait wrong pid\n");
return;
}
} else {
exit();
}
}
2006-09-07 13:23:41 +00:00
printf(1, "exitwait ok\n");
}
2006-08-24 19:24:36 +00:00
void
mem(void)
{
void *m1, *m2;
int pid, ppid;
2006-08-24 19:24:36 +00:00
printf(1, "mem test\n");
ppid = getpid();
2006-08-29 19:06:37 +00:00
if((pid = fork()) == 0){
m1 = 0;
while((m2 = malloc(10001)) != 0){
*(char**)m2 = m1;
2006-08-29 19:06:37 +00:00
m1 = m2;
}
while(m1){
2006-09-06 17:27:19 +00:00
m2 = *(char**)m1;
2006-08-29 19:06:37 +00:00
free(m1);
m1 = m2;
}
m1 = malloc(1024*20);
if(m1 == 0){
2006-09-07 13:23:41 +00:00
printf(1, "couldn't allocate mem?!!\n");
kill(ppid);
2006-08-29 19:06:37 +00:00
exit();
}
2006-08-24 19:24:36 +00:00
free(m1);
2006-08-29 19:06:37 +00:00
printf(1, "mem ok\n");
2006-08-24 19:24:36 +00:00
exit();
2006-08-29 19:06:37 +00:00
} else {
wait();
2006-08-24 19:24:36 +00:00
}
}
2006-09-07 13:23:41 +00:00
// More file system tests
// two processes write to the same file descriptor
// is the offset shared? does inode locking work?
void
2007-08-08 09:32:39 +00:00
sharedfd(void)
2006-09-07 13:23:41 +00:00
{
int fd, pid, i, n, nc, np;
char buf[10];
printf(1, "sharedfd test\n");
2006-09-07 13:23:41 +00:00
unlink("sharedfd");
fd = open("sharedfd", O_CREATE|O_RDWR);
if(fd < 0){
printf(1, "fstests: cannot open sharedfd for writing");
return;
}
pid = fork();
memset(buf, pid==0?'c':'p', sizeof(buf));
2006-09-07 20:06:08 +00:00
for(i = 0; i < 1000; i++){
2006-09-07 13:23:41 +00:00
if(write(fd, buf, sizeof(buf)) != sizeof(buf)){
printf(1, "fstests: write sharedfd failed\n");
break;
}
}
if(pid == 0)
exit();
else
wait();
close(fd);
fd = open("sharedfd", 0);
if(fd < 0){
printf(1, "fstests: cannot open sharedfd for reading\n");
return;
}
nc = np = 0;
while((n = read(fd, buf, sizeof(buf))) > 0){
for(i = 0; i < sizeof(buf); i++){
if(buf[i] == 'c')
nc++;
if(buf[i] == 'p')
np++;
}
}
close(fd);
unlink("sharedfd");
2006-09-07 20:06:08 +00:00
if(nc == 10000 && np == 10000)
2006-09-07 13:23:41 +00:00
printf(1, "sharedfd ok\n");
else
printf(1, "sharedfd oops %d %d\n", nc, np);
}
// two processes write two different files at the same
// time, to test block allocation.
void
2007-08-08 09:32:39 +00:00
twofiles(void)
2006-09-07 13:23:41 +00:00
{
int fd, pid, i, j, n, total;
char *fname;
printf(1, "twofiles test\n");
unlink("f1");
unlink("f2");
pid = fork();
if(pid < 0){
printf(1, "fork failed\n");
return;
}
fname = pid ? "f1" : "f2";
fd = open(fname, O_CREATE | O_RDWR);
if(fd < 0){
printf(1, "create failed\n");
exit();
}
memset(buf, pid?'p':'c', 512);
for(i = 0; i < 12; i++){
if((n = write(fd, buf, 500)) != 500){
printf(1, "write failed %d\n", n);
exit();
}
}
close(fd);
if(pid)
wait();
else
exit();
for(i = 0; i < 2; i++){
fd = open(i?"f1":"f2", 0);
total = 0;
while((n = read(fd, buf, sizeof(buf))) > 0){
for(j = 0; j < n; j++){
if(buf[j] != (i?'p':'c')){
printf(1, "wrong char\n");
exit();
}
}
total += n;
}
close(fd);
if(total != 12*500){
printf(1, "wrong length %d\n", total);
exit();
}
}
unlink("f1");
unlink("f2");
printf(1, "twofiles ok\n");
}
// two processes create and delete different files in same directory
2006-09-07 13:23:41 +00:00
void
2007-08-08 09:32:39 +00:00
createdelete(void)
2006-09-07 13:23:41 +00:00
{
2007-08-10 17:17:42 +00:00
enum { N = 20 };
2006-09-07 13:23:41 +00:00
int pid, i, fd;
char name[32];
printf(1, "createdelete test\n");
pid = fork();
if(pid < 0){
printf(1, "fork failed\n");
exit();
}
name[0] = pid ? 'p' : 'c';
name[2] = '\0';
2007-08-10 17:17:42 +00:00
for(i = 0; i < N; i++){
2006-09-07 13:23:41 +00:00
name[1] = '0' + i;
fd = open(name, O_CREATE | O_RDWR);
if(fd < 0){
printf(1, "create failed\n");
exit();
}
close(fd);
if(i > 0 && (i % 2 ) == 0){
name[1] = '0' + (i / 2);
if(unlink(name) < 0){
printf(1, "unlink failed\n");
exit();
}
}
}
if(pid==0)
exit();
2007-08-20 21:00:20 +00:00
else
wait();
2006-09-07 13:23:41 +00:00
2007-08-10 17:17:42 +00:00
for(i = 0; i < N; i++){
2006-09-07 13:23:41 +00:00
name[0] = 'p';
name[1] = '0' + i;
fd = open(name, 0);
2007-08-10 17:17:42 +00:00
if((i == 0 || i >= N/2) && fd < 0){
2006-09-07 13:23:41 +00:00
printf(1, "oops createdelete %s didn't exist\n", name);
exit();
2007-08-10 17:17:42 +00:00
} else if((i >= 1 && i < N/2) && fd >= 0){
2006-09-07 13:23:41 +00:00
printf(1, "oops createdelete %s did exist\n", name);
exit();
}
if(fd >= 0)
close(fd);
name[0] = 'c';
name[1] = '0' + i;
fd = open(name, 0);
2007-08-10 17:17:42 +00:00
if((i == 0 || i >= N/2) && fd < 0){
2006-09-07 13:23:41 +00:00
printf(1, "oops createdelete %s didn't exist\n", name);
exit();
2007-08-10 17:17:42 +00:00
} else if((i >= 1 && i < N/2) && fd >= 0){
2006-09-07 13:23:41 +00:00
printf(1, "oops createdelete %s did exist\n", name);
exit();
}
if(fd >= 0)
close(fd);
}
2007-08-10 17:17:42 +00:00
for(i = 0; i < N; i++){
2006-09-07 13:23:41 +00:00
name[0] = 'p';
name[1] = '0' + i;
unlink(name);
name[0] = 'c';
unlink(name);
}
printf(1, "createdelete ok\n");
}
// can I unlink a file and still read it?
void
2007-08-08 09:32:39 +00:00
unlinkread(void)
2006-09-07 13:23:41 +00:00
{
int fd, fd1;
printf(1, "unlinkread test\n");
fd = open("unlinkread", O_CREATE | O_RDWR);
if(fd < 0){
printf(1, "create unlinkread failed\n");
exit();
}
write(fd, "hello", 5);
close(fd);
fd = open("unlinkread", O_RDWR);
if(fd < 0){
printf(1, "open unlinkread failed\n");
exit();
}
if(unlink("unlinkread") != 0){
printf(1, "unlink unlinkread failed\n");
exit();
}
fd1 = open("unlinkread", O_CREATE | O_RDWR);
write(fd1, "yyy", 3);
close(fd1);
if(read(fd, buf, sizeof(buf)) != 5){
printf(1, "unlinkread read failed");
exit();
}
if(buf[0] != 'h'){
printf(1, "unlinkread wrong data\n");
exit();
}
if(write(fd, buf, 10) != 10){
printf(1, "unlinkread write failed\n");
exit();
}
close(fd);
unlink("unlinkread");
printf(1, "unlinkread ok\n");
}
void
2007-08-08 09:32:39 +00:00
linktest(void)
2006-09-07 13:23:41 +00:00
{
int fd;
printf(1, "linktest\n");
unlink("lf1");
unlink("lf2");
fd = open("lf1", O_CREATE|O_RDWR);
if(fd < 0){
printf(1, "create lf1 failed\n");
exit();
}
if(write(fd, "hello", 5) != 5){
printf(1, "write lf1 failed\n");
exit();
}
close(fd);
if(link("lf1", "lf2") < 0){
printf(1, "link lf1 lf2 failed\n");
exit();
}
unlink("lf1");
if(open("lf1", 0) >= 0){
printf(1, "unlinked lf1 but it is still there!\n");
exit();
}
fd = open("lf2", 0);
if(fd < 0){
printf(1, "open lf2 failed\n");
exit();
}
if(read(fd, buf, sizeof(buf)) != 5){
printf(1, "read lf2 failed\n");
exit();
}
close(fd);
if(link("lf2", "lf2") >= 0){
printf(1, "link lf2 lf2 succeeded! oops\n");
exit();
}
unlink("lf2");
if(link("lf2", "lf1") >= 0){
printf(1, "link non-existant succeeded! oops\n");
exit();
}
if(link(".", "lf1") >= 0){
printf(1, "link . lf1 succeeded! oops\n");
exit();
}
printf(1, "linktest ok\n");
}
// test concurrent create/link/unlink of the same file
2006-09-07 13:23:41 +00:00
void
2007-08-08 09:32:39 +00:00
concreate(void)
2006-09-07 13:23:41 +00:00
{
char file[3];
int i, pid, n, fd;
char fa[40];
struct {
ushort inum;
2006-09-07 13:23:41 +00:00
char name[14];
} de;
printf(1, "concreate test\n");
file[0] = 'C';
file[2] = '\0';
for(i = 0; i < 40; i++){
file[1] = '0' + i;
unlink(file);
pid = fork();
if(pid && (i % 3) == 1){
link("C0", file);
} else if(pid == 0 && (i % 5) == 1){
link("C0", file);
} else {
fd = open(file, O_CREATE | O_RDWR);
if(fd < 0){
printf(1, "concreate create %s failed\n", file);
exit();
}
close(fd);
}
if(pid == 0)
exit();
else
wait();
}
memset(fa, 0, sizeof(fa));
fd = open(".", 0);
n = 0;
while(read(fd, &de, sizeof(de)) > 0){
if(de.inum == 0)
continue;
if(de.name[0] == 'C' && de.name[2] == '\0'){
i = de.name[1] - '0';
if(i < 0 || i >= sizeof(fa)){
printf(1, "concreate weird file %s\n", de.name);
exit();
}
if(fa[i]){
printf(1, "concreate duplicate file %s\n", de.name);
exit();
}
fa[i] = 1;
n++;
}
}
close(fd);
if(n != 40){
printf(1, "concreate not enough files in directory listing\n");
exit();
}
for(i = 0; i < 40; i++){
file[1] = '0' + i;
pid = fork();
if(pid < 0){
printf(1, "fork failed\n");
exit();
}
if(((i % 3) == 0 && pid == 0) ||
((i % 3) == 1 && pid != 0)){
close(open(file, 0));
close(open(file, 0));
close(open(file, 0));
close(open(file, 0));
} else {
unlink(file);
unlink(file);
unlink(file);
unlink(file);
}
if(pid == 0)
exit();
else
wait();
2006-09-07 13:23:41 +00:00
}
printf(1, "concreate ok\n");
}
// another concurrent link/unlink/create test,
// to look for deadlocks.
void
linkunlink()
{
int pid, i;
printf(1, "linkunlink test\n");
unlink("x");
pid = fork();
if(pid < 0){
printf(1, "fork failed\n");
exit();
}
unsigned int x = (pid ? 1 : 97);
for(i = 0; i < 100; i++){
x = x * 1103515245 + 12345;
if((x % 3) == 0){
close(open("x", O_RDWR | O_CREATE));
} else if((x % 3) == 1){
link("cat", "x");
} else {
unlink("x");
}
}
if(pid)
wait();
else
exit();
printf(1, "linkunlink ok\n");
}
2006-09-07 13:23:41 +00:00
// directory that uses indirect blocks
void
2007-08-08 09:32:39 +00:00
bigdir(void)
2006-09-07 13:23:41 +00:00
{
int i, fd;
char name[10];
printf(1, "bigdir test\n");
unlink("bd");
fd = open("bd", O_CREATE);
if(fd < 0){
printf(1, "bigdir create failed\n");
exit();
}
close(fd);
for(i = 0; i < 500; i++){
name[0] = 'x';
name[1] = '0' + (i / 64);
name[2] = '0' + (i % 64);
name[3] = '\0';
if(link("bd", name) != 0){
printf(1, "bigdir link failed\n");
exit();
}
}
unlink("bd");
for(i = 0; i < 500; i++){
name[0] = 'x';
name[1] = '0' + (i / 64);
name[2] = '0' + (i % 64);
name[3] = '\0';
if(unlink(name) != 0){
printf(1, "bigdir unlink failed");
exit();
}
}
printf(1, "bigdir ok\n");
}
void
2007-08-08 09:32:39 +00:00
subdir(void)
2006-09-07 13:23:41 +00:00
{
int fd, cc;
printf(1, "subdir test\n");
unlink("ff");
if(mkdir("dd") != 0){
printf(1, "subdir mkdir dd failed\n");
exit();
}
fd = open("dd/ff", O_CREATE | O_RDWR);
if(fd < 0){
printf(1, "create dd/ff failed\n");
exit();
}
write(fd, "ff", 2);
close(fd);
2007-08-22 02:21:22 +00:00
if(unlink("dd") >= 0){
printf(1, "unlink dd (non-empty dir) succeeded!\n");
exit();
}
2006-09-07 13:23:41 +00:00
if(mkdir("/dd/dd") != 0){
printf(1, "subdir mkdir dd/dd failed\n");
exit();
}
fd = open("dd/dd/ff", O_CREATE | O_RDWR);
if(fd < 0){
printf(1, "create dd/dd/ff failed\n");
exit();
}
write(fd, "FF", 2);
close(fd);
fd = open("dd/dd/../ff", 0);
if(fd < 0){
printf(1, "open dd/dd/../ff failed\n");
exit();
}
cc = read(fd, buf, sizeof(buf));
if(cc != 2 || buf[0] != 'f'){
printf(1, "dd/dd/../ff wrong content\n");
exit();
}
close(fd);
if(link("dd/dd/ff", "dd/dd/ffff") != 0){
printf(1, "link dd/dd/ff dd/dd/ffff failed\n");
exit();
}
if(unlink("dd/dd/ff") != 0){
printf(1, "unlink dd/dd/ff failed\n");
exit();
}
2007-08-22 02:21:22 +00:00
if(open("dd/dd/ff", O_RDONLY) >= 0){
printf(1, "open (unlinked) dd/dd/ff succeeded\n");
exit();
}
2006-09-07 13:23:41 +00:00
if(chdir("dd") != 0){
printf(1, "chdir dd failed\n");
exit();
}
if(chdir("dd/../../dd") != 0){
printf(1, "chdir dd/../../dd failed\n");
exit();
}
2007-08-22 02:21:22 +00:00
if(chdir("dd/../../../dd") != 0){
printf(1, "chdir dd/../../dd failed\n");
exit();
}
2006-09-07 13:23:41 +00:00
if(chdir("./..") != 0){
printf(1, "chdir ./.. failed\n");
exit();
}
fd = open("dd/dd/ffff", 0);
if(fd < 0){
printf(1, "open dd/dd/ffff failed\n");
exit();
}
if(read(fd, buf, sizeof(buf)) != 2){
printf(1, "read dd/dd/ffff wrong len\n");
exit();
}
close(fd);
2007-08-22 02:21:22 +00:00
if(open("dd/dd/ff", O_RDONLY) >= 0){
printf(1, "open (unlinked) dd/dd/ff succeeded!\n");
2006-09-07 13:23:41 +00:00
exit();
}
if(open("dd/ff/ff", O_CREATE|O_RDWR) >= 0){
printf(1, "create dd/ff/ff succeeded!\n");
exit();
}
if(open("dd/xx/ff", O_CREATE|O_RDWR) >= 0){
printf(1, "create dd/xx/ff succeeded!\n");
exit();
}
if(open("dd", O_CREATE) >= 0){
printf(1, "create dd succeeded!\n");
exit();
}
if(open("dd", O_RDWR) >= 0){
printf(1, "open dd rdwr succeeded!\n");
exit();
}
if(open("dd", O_WRONLY) >= 0){
printf(1, "open dd wronly succeeded!\n");
exit();
}
if(link("dd/ff/ff", "dd/dd/xx") == 0){
printf(1, "link dd/ff/ff dd/dd/xx succeeded!\n");
exit();
}
if(link("dd/xx/ff", "dd/dd/xx") == 0){
printf(1, "link dd/xx/ff dd/dd/xx succeeded!\n");
exit();
}
if(link("dd/ff", "dd/dd/ffff") == 0){
printf(1, "link dd/ff dd/dd/ffff succeeded!\n");
exit();
}
if(mkdir("dd/ff/ff") == 0){
printf(1, "mkdir dd/ff/ff succeeded!\n");
exit();
}
if(mkdir("dd/xx/ff") == 0){
printf(1, "mkdir dd/xx/ff succeeded!\n");
exit();
}
if(mkdir("dd/dd/ffff") == 0){
printf(1, "mkdir dd/dd/ffff succeeded!\n");
exit();
}
if(unlink("dd/xx/ff") == 0){
printf(1, "unlink dd/xx/ff succeeded!\n");
exit();
}
if(unlink("dd/ff/ff") == 0){
printf(1, "unlink dd/ff/ff succeeded!\n");
exit();
}
if(chdir("dd/ff") == 0){
printf(1, "chdir dd/ff succeeded!\n");
exit();
}
if(chdir("dd/xx") == 0){
printf(1, "chdir dd/xx succeeded!\n");
exit();
}
if(unlink("dd/dd/ffff") != 0){
printf(1, "unlink dd/dd/ff failed\n");
exit();
}
if(unlink("dd/ff") != 0){
printf(1, "unlink dd/ff failed\n");
exit();
}
2007-08-22 02:21:22 +00:00
if(unlink("dd") == 0){
printf(1, "unlink non-empty dd succeeded!\n");
exit();
}
if(unlink("dd/dd") < 0){
printf(1, "unlink dd/dd failed\n");
exit();
}
if(unlink("dd") < 0){
printf(1, "unlink dd failed\n");
exit();
}
2006-09-07 13:23:41 +00:00
printf(1, "subdir ok\n");
}
// test writes that are larger than the log.
void
bigwrite(void)
{
int fd, sz;
printf(1, "bigwrite test\n");
unlink("bigwrite");
for(sz = 499; sz < 12*512; sz += 471){
fd = open("bigwrite", O_CREATE | O_RDWR);
if(fd < 0){
printf(1, "cannot create bigwrite\n");
exit();
}
int i;
for(i = 0; i < 2; i++){
int cc = write(fd, buf, sz);
if(cc != sz){
printf(1, "write(%d) ret %d\n", sz, cc);
exit();
}
}
close(fd);
unlink("bigwrite");
}
printf(1, "bigwrite ok\n");
}
2006-09-07 13:23:41 +00:00
void
2007-08-08 09:32:39 +00:00
bigfile(void)
2006-09-07 13:23:41 +00:00
{
int fd, i, total, cc;
printf(1, "bigfile test\n");
unlink("bigfile");
fd = open("bigfile", O_CREATE | O_RDWR);
if(fd < 0){
printf(1, "cannot create bigfile");
exit();
}
for(i = 0; i < 20; i++){
memset(buf, i, 600);
if(write(fd, buf, 600) != 600){
printf(1, "write bigfile failed\n");
exit();
}
}
close(fd);
fd = open("bigfile", 0);
if(fd < 0){
printf(1, "cannot open bigfile\n");
exit();
}
total = 0;
for(i = 0; ; i++){
cc = read(fd, buf, 300);
if(cc < 0){
printf(1, "read bigfile failed\n");
exit();
}
if(cc == 0)
break;
if(cc != 300){
printf(1, "short read bigfile\n");
exit();
}
if(buf[0] != i/2 || buf[299] != i/2){
printf(1, "read bigfile wrong data\n");
exit();
}
total += cc;
}
close(fd);
if(total != 20*600){
printf(1, "read bigfile wrong total\n");
exit();
}
unlink("bigfile");
printf(1, "bigfile test ok\n");
}
void
2007-08-08 09:32:39 +00:00
fourteen(void)
2006-09-07 13:23:41 +00:00
{
int fd;
2007-08-10 17:53:09 +00:00
// DIRSIZ is 14.
2006-09-07 13:23:41 +00:00
printf(1, "fourteen test\n");
if(mkdir("12345678901234") != 0){
printf(1, "mkdir 12345678901234 failed\n");
exit();
}
if(mkdir("12345678901234/123456789012345") != 0){
printf(1, "mkdir 12345678901234/123456789012345 failed\n");
exit();
}
fd = open("123456789012345/123456789012345/123456789012345", O_CREATE);
if(fd < 0){
printf(1, "create 123456789012345/123456789012345/123456789012345 failed\n");
exit();
}
close(fd);
fd = open("12345678901234/12345678901234/12345678901234", 0);
if(fd < 0){
printf(1, "open 12345678901234/12345678901234/12345678901234 failed\n");
exit();
}
close(fd);
if(mkdir("12345678901234/12345678901234") == 0){
printf(1, "mkdir 12345678901234/12345678901234 succeeded!\n");
exit();
}
if(mkdir("123456789012345/12345678901234") == 0){
printf(1, "mkdir 12345678901234/123456789012345 succeeded!\n");
exit();
}
printf(1, "fourteen ok\n");
}
2007-08-10 17:53:09 +00:00
void
rmdot(void)
{
printf(1, "rmdot test\n");
if(mkdir("dots") != 0){
printf(1, "mkdir dots failed\n");
exit();
}
if(chdir("dots") != 0){
printf(1, "chdir dots failed\n");
exit();
}
if(unlink(".") == 0){
printf(1, "rm . worked!\n");
exit();
}
if(unlink("..") == 0){
printf(1, "rm .. worked!\n");
exit();
}
if(chdir("/") != 0){
printf(1, "chdir / failed\n");
exit();
}
if(unlink("dots/.") == 0){
printf(1, "unlink dots/. worked!\n");
exit();
}
if(unlink("dots/..") == 0){
printf(1, "unlink dots/.. worked!\n");
exit();
}
if(unlink("dots") != 0){
printf(1, "unlink dots failed!\n");
exit();
}
printf(1, "rmdot ok\n");
}
2007-08-24 14:56:17 +00:00
void
dirfile(void)
{
int fd;
printf(1, "dir vs file\n");
fd = open("dirfile", O_CREATE);
if(fd < 0){
printf(1, "create dirfile failed\n");
exit();
}
close(fd);
if(chdir("dirfile") == 0){
printf(1, "chdir dirfile succeeded!\n");
exit();
}
fd = open("dirfile/xx", 0);
if(fd >= 0){
printf(1, "create dirfile/xx succeeded!\n");
exit();
}
fd = open("dirfile/xx", O_CREATE);
if(fd >= 0){
printf(1, "create dirfile/xx succeeded!\n");
exit();
}
if(mkdir("dirfile/xx") == 0){
printf(1, "mkdir dirfile/xx succeeded!\n");
exit();
}
if(unlink("dirfile/xx") == 0){
printf(1, "unlink dirfile/xx succeeded!\n");
exit();
}
if(link("README", "dirfile/xx") == 0){
printf(1, "link to dirfile/xx succeeded!\n");
exit();
}
if(unlink("dirfile") != 0){
printf(1, "unlink dirfile failed!\n");
exit();
}
fd = open(".", O_RDWR);
if(fd >= 0){
printf(1, "open . for writing succeeded!\n");
exit();
}
fd = open(".", 0);
if(write(fd, "x", 1) > 0){
printf(1, "write . succeeded!\n");
exit();
}
close(fd);
printf(1, "dir vs file OK\n");
}
// test that iput() is called at the end of _namei()
void
iref(void)
{
int i, fd;
printf(1, "empty file name\n");
// the 50 is NINODE
for(i = 0; i < 50 + 1; i++){
if(mkdir("irefd") != 0){
printf(1, "mkdir irefd failed\n");
exit();
}
if(chdir("irefd") != 0){
printf(1, "chdir irefd failed\n");
exit();
}
mkdir("");
link("README", "");
fd = open("", O_CREATE);
if(fd >= 0)
close(fd);
fd = open("xx", O_CREATE);
if(fd >= 0)
close(fd);
unlink("xx");
}
chdir("/");
printf(1, "empty file name OK\n");
}
2007-08-24 20:20:23 +00:00
// test that fork fails gracefully
// the forktest binary also does this, but it runs out of proc entries first.
// inside the bigger usertests binary, we run out of memory first.
void
forktest(void)
{
int n, pid;
printf(1, "fork test\n");
for(n=0; n<1000; n++){
pid = fork();
if(pid < 0)
break;
if(pid == 0)
exit();
}
if(n == 1000){
printf(1, "fork claimed to work 1000 times!\n");
exit();
}
for(; n > 0; n--){
if(wait() < 0){
printf(1, "wait stopped early\n");
exit();
}
}
if(wait() != -1){
printf(1, "wait got too many\n");
exit();
}
printf(1, "fork test OK\n");
}
void
sbrktest(void)
{
int fds[2], pid, pids[10], ppid;
char *a, *b, *c, *lastaddr, *oldbrk, *p, scratch;
uint amt;
printf(stdout, "sbrk test\n");
oldbrk = sbrk(0);
// can one sbrk() less than a page?
a = sbrk(0);
int i;
for(i = 0; i < 5000; i++){
b = sbrk(1);
if(b != a){
printf(stdout, "sbrk test failed %d %x %x\n", i, a, b);
exit();
}
*b = 1;
a = b + 1;
}
pid = fork();
if(pid < 0){
printf(stdout, "sbrk test fork failed\n");
exit();
}
c = sbrk(1);
c = sbrk(1);
if(c != a + 1){
printf(stdout, "sbrk test failed post-fork\n");
exit();
}
if(pid == 0)
exit();
wait();
// can one grow address space to something big?
#define BIG (100*1024*1024)
a = sbrk(0);
amt = (BIG) - (uint)a;
p = sbrk(amt);
if (p != a) {
printf(stdout, "sbrk test failed to grow big address space; enough phys mem?\n");
exit();
}
lastaddr = (char*) (BIG-1);
*lastaddr = 99;
// can one de-allocate?
a = sbrk(0);
c = sbrk(-4096);
if(c == (char*)0xffffffff){
printf(stdout, "sbrk could not deallocate\n");
exit();
}
c = sbrk(0);
if(c != a - 4096){
printf(stdout, "sbrk deallocation produced wrong address, a %x c %x\n", a, c);
exit();
}
// can one re-allocate that page?
a = sbrk(0);
c = sbrk(4096);
if(c != a || sbrk(0) != a + 4096){
printf(stdout, "sbrk re-allocation failed, a %x c %x\n", a, c);
exit();
}
if(*lastaddr == 99){
// should be zero
printf(stdout, "sbrk de-allocation didn't really deallocate\n");
exit();
}
a = sbrk(0);
c = sbrk(-(sbrk(0) - oldbrk));
if(c != a){
printf(stdout, "sbrk downsize failed, a %x c %x\n", a, c);
exit();
}
// can we read the kernel's memory?
for(a = (char*)(KERNBASE); a < (char*) (KERNBASE+2000000); a += 50000){
ppid = getpid();
pid = fork();
if(pid < 0){
printf(stdout, "fork failed\n");
exit();
}
if(pid == 0){
printf(stdout, "oops could read %x = %x\n", a, *a);
kill(ppid);
exit();
}
wait();
}
// if we run the system out of memory, does it clean up the last
// failed allocation?
if(pipe(fds) != 0){
printf(1, "pipe() failed\n");
exit();
}
2010-09-01 04:41:25 +00:00
for(i = 0; i < sizeof(pids)/sizeof(pids[0]); i++){
if((pids[i] = fork()) == 0){
// allocate a lot of memory
sbrk(BIG - (uint)sbrk(0));
write(fds[1], "x", 1);
// sit around until killed
2010-09-01 04:41:25 +00:00
for(;;) sleep(1000);
}
2010-09-01 04:41:25 +00:00
if(pids[i] != -1)
read(fds[0], &scratch, 1);
}
// if those failed allocations freed up the pages they did allocate,
// we'll be able to allocate here
c = sbrk(4096);
2010-09-01 04:41:25 +00:00
for(i = 0; i < sizeof(pids)/sizeof(pids[0]); i++){
if(pids[i] == -1)
continue;
kill(pids[i]);
wait();
}
if(c == (char*)0xffffffff){
printf(stdout, "failed sbrk leaked memory\n");
exit();
}
if(sbrk(0) > oldbrk)
sbrk(-(sbrk(0) - oldbrk));
printf(stdout, "sbrk test OK\n");
}
void
validateint(int *p)
{
int res;
asm("mov %%esp, %%ebx\n\t"
"mov %3, %%esp\n\t"
"int %2\n\t"
"mov %%ebx, %%esp" :
"=a" (res) :
"a" (SYS_sleep), "n" (T_SYSCALL), "c" (p) :
"ebx");
}
void
validatetest(void)
{
int hi, pid;
uint p;
printf(stdout, "validate test\n");
hi = 1100*1024;
for(p = 0; p <= (uint)hi; p += 4096){
if((pid = fork()) == 0){
// try to crash the kernel by passing in a badly placed integer
validateint((int*)p);
exit();
}
sleep(0);
sleep(0);
kill(pid);
wait();
// try to crash the kernel by passing in a bad string pointer
if(link("nosuchfile", (char*)p) != -1){
printf(stdout, "link should not succeed\n");
exit();
}
}
printf(stdout, "validate ok\n");
}
2010-09-20 10:00:22 +00:00
// does unintialized data start out zero?
char uninit[10000];
void
bsstest(void)
{
int i;
printf(stdout, "bss test\n");
for(i = 0; i < sizeof(uninit); i++){
if(uninit[i] != '\0'){
printf(stdout, "bss test failed\n");
exit();
}
}
printf(stdout, "bss test ok\n");
}
2010-09-20 10:00:22 +00:00
// does exec do something sensible if the arguments
// are larger than a page?
void
bigargtest(void)
{
int pid, ppid;
ppid = getpid();
pid = fork();
if(pid == 0){
char *args[32+1];
2010-09-20 10:00:22 +00:00
int i;
for(i = 0; i < 32; i++)
args[i] = "bigargs test: failed\n ";
args[32] = 0;
2010-09-20 10:00:22 +00:00
printf(stdout, "bigarg test\n");
exec("echo", args);
printf(stdout, "bigarg test ok\n");
exit();
} else if(pid < 0){
printf(stdout, "bigargtest: fork failed\n");
exit();
}
wait();
}
// what happens when the file system runs out of blocks?
// answer: balloc panics, so this test is not useful.
void
fsfull()
{
int nfiles;
int fsblocks = 0;
printf(1, "fsfull test\n");
for(nfiles = 0; ; nfiles++){
char name[64];
name[0] = 'f';
name[1] = '0' + nfiles / 1000;
name[2] = '0' + (nfiles % 1000) / 100;
name[3] = '0' + (nfiles % 100) / 10;
name[4] = '0' + (nfiles % 10);
name[5] = '\0';
printf(1, "writing %s\n", name);
int fd = open(name, O_CREATE|O_RDWR);
if(fd < 0){
printf(1, "open %s failed\n", name);
break;
}
int total = 0;
while(1){
int cc = write(fd, buf, 512);
if(cc < 512)
break;
total += cc;
fsblocks++;
}
printf(1, "wrote %d bytes\n", total);
close(fd);
if(total == 0)
break;
}
while(nfiles >= 0){
char name[64];
name[0] = 'f';
name[1] = '0' + nfiles / 1000;
name[2] = '0' + (nfiles % 1000) / 100;
name[3] = '0' + (nfiles % 100) / 10;
name[4] = '0' + (nfiles % 10);
name[5] = '\0';
unlink(name);
nfiles--;
}
printf(1, "fsfull test finished\n");
}
int
2006-07-28 22:33:07 +00:00
main(int argc, char *argv[])
2006-06-27 14:35:53 +00:00
{
2006-09-07 13:23:41 +00:00
printf(1, "usertests starting\n");
2006-09-07 20:06:08 +00:00
if(open("usertests.ran", 0) >= 0){
printf(1, "already ran user tests -- rebuild fs.img\n");
exit();
}
close(open("usertests.ran", O_CREATE));
2006-09-07 13:23:41 +00:00
bigwrite();
bigargtest();
bsstest();
sbrktest();
validatetest();
2006-09-07 13:23:41 +00:00
opentest();
writetest();
writetest1();
createtest();
2006-08-24 19:24:36 +00:00
mem();
2006-08-29 17:01:40 +00:00
pipe1();
preempt();
exitwait();
2006-06-27 14:35:53 +00:00
2007-08-10 17:53:09 +00:00
rmdot();
2006-09-07 13:23:41 +00:00
fourteen();
bigfile();
subdir();
concreate();
linkunlink();
2006-09-07 13:23:41 +00:00
linktest();
unlinkread();
createdelete();
twofiles();
sharedfd();
2007-08-24 14:56:17 +00:00
dirfile();
iref();
2007-08-24 20:20:23 +00:00
forktest();
2007-09-27 12:29:06 +00:00
bigdir(); // slow
2006-09-07 13:23:41 +00:00
exectest();
exit();
2006-06-27 14:35:53 +00:00
}