From 53c6e81d1cff87ffccfca368adcca8b092f2a0d1 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 1 Nov 2004 06:28:03 +0000 Subject: [PATCH] Add more tests. --- grading/userprog/Makefile | 3 +- grading/userprog/prep-disk | 2 +- grading/userprog/read-boundary.c | 20 +++------- grading/userprog/read-normal.c | 12 ++---- grading/userprog/run-tests | 58 +++++++++++------------------ grading/userprog/sample.inc | 6 +++ grading/userprog/write-bad-fd.c | 12 ++++++ grading/userprog/write-bad-fd.exp | 6 +++ grading/userprog/write-bad-ptr.c | 18 +++++++++ grading/userprog/write-bad-ptr.exp | 6 +++ grading/userprog/write-boundary.c | 38 +++++++++++++++++++ grading/userprog/write-boundary.exp | 3 ++ grading/userprog/write-normal.c | 26 +++++++++++++ grading/userprog/write-normal.exp | 3 ++ grading/userprog/write-stdin.c | 12 ++++++ grading/userprog/write-stdin.exp | 6 +++ grading/userprog/write-zero.c | 22 +++++++++++ grading/userprog/write-zero.exp | 3 ++ src/Makefile.userprog | 2 +- 19 files changed, 195 insertions(+), 63 deletions(-) create mode 100644 grading/userprog/sample.inc create mode 100644 grading/userprog/write-bad-fd.c create mode 100644 grading/userprog/write-bad-fd.exp create mode 100644 grading/userprog/write-bad-ptr.c create mode 100644 grading/userprog/write-bad-ptr.exp create mode 100644 grading/userprog/write-boundary.c create mode 100644 grading/userprog/write-boundary.exp create mode 100644 grading/userprog/write-normal.c create mode 100644 grading/userprog/write-normal.exp create mode 100644 grading/userprog/write-stdin.c create mode 100644 grading/userprog/write-stdin.exp create mode 100644 grading/userprog/write-zero.c create mode 100644 grading/userprog/write-zero.exp diff --git a/grading/userprog/Makefile b/grading/userprog/Makefile index e95e13f..85ee607 100644 --- a/grading/userprog/Makefile +++ b/grading/userprog/Makefile @@ -7,7 +7,8 @@ SINGLETONS = \ $(addprefix create-, normal empty null bad-ptr long exists bound) \ $(addprefix open-, normal missing boundary empty null bad-ptr twice) \ $(addprefix close-, normal twice stdin stdout bad-fd) \ - $(addprefix read-, normal bad-ptr boundary zero stdout bad-fd) + $(addprefix read-, normal bad-ptr boundary zero stdout bad-fd) \ + $(addprefix write-, normal bad-ptr boundary zero stdin bad-fd) define SINGLETON_PROG PROGS += $(1) diff --git a/grading/userprog/prep-disk b/grading/userprog/prep-disk index c775a4e..182c561 100755 --- a/grading/userprog/prep-disk +++ b/grading/userprog/prep-disk @@ -40,7 +40,7 @@ put_file ("sample.txt") qw (open-normal open-boundary open-twice close-normal close-twice read-normal read-bad-ptr read-boundary read-zero - write-normal write-boundary write-zero)); + write-normal write-bad-ptr write-boundary write-zero)); sub put_file { my ($fn) = @_; diff --git a/grading/userprog/read-boundary.c b/grading/userprog/read-boundary.c index 7992730..41c798e 100644 --- a/grading/userprog/read-boundary.c +++ b/grading/userprog/read-boundary.c @@ -2,15 +2,7 @@ #include #include #include - -char expected[] = { - "Amazing Electronic Fact: If you scuffed your feet long enough without\n" - "touching anything, you would build up so many electrons that your\n" - "finger would explode! But this is nothing to worry about unless you\n" - "have carpeting.\n" -}; - - +#include "sample.inc" static char * mk_boundary_string (const char *src) @@ -28,7 +20,7 @@ main (void) int byte_cnt; char *actual_p; - actual_p = mk_boundary_string (expected); + actual_p = mk_boundary_string (sample); printf ("(read-boundary) begin\n"); @@ -36,11 +28,11 @@ main (void) if (handle < 2) printf ("(read-boundary) fail: open() returned %d\n", handle); - byte_cnt = read (handle, actual_p, sizeof expected - 1); - if (byte_cnt != sizeof expected - 1) + byte_cnt = read (handle, actual_p, sizeof sample - 1); + if (byte_cnt != sizeof sample - 1) printf ("(read-boundary) fail: read() returned %d instead of %d\n", - byte_cnt, sizeof expected - 1); - else if (strcmp (expected, actual_p)) + byte_cnt, sizeof sample - 1); + else if (strcmp (sample, actual_p)) printf ("(read-boundary) fail: expected text differs from actual:\n%s", actual_p); diff --git a/grading/userprog/read-normal.c b/grading/userprog/read-normal.c index 4bb7679..2e6f681 100644 --- a/grading/userprog/read-normal.c +++ b/grading/userprog/read-normal.c @@ -1,15 +1,9 @@ #include #include #include +#include "sample.inc" -char expected[] = { - "Amazing Electronic Fact: If you scuffed your feet long enough without\n" - "touching anything, you would build up so many electrons that your\n" - "finger would explode! But this is nothing to worry about unless you\n" - "have carpeting.\n" -}; - -char actual[sizeof expected]; +char actual[sizeof sample]; int main (void) @@ -25,7 +19,7 @@ main (void) if (byte_cnt != sizeof actual - 1) printf ("(read-normal) fail: read() returned %d instead of %d\n", byte_cnt, sizeof actual - 1); - else if (strcmp (expected, actual)) + else if (strcmp (sample, actual)) printf ("(read-normal) fail: expected text differs from actual:\n%s", actual); diff --git a/grading/userprog/run-tests b/grading/userprog/run-tests index 3781649..9ca9381 100755 --- a/grading/userprog/run-tests +++ b/grading/userprog/run-tests @@ -48,6 +48,8 @@ sub usage { close-normal close-twice close-stdin close-stdout close-bad-fd read-normal read-bad-ptr read-boundary read-zero read-stdout read-bad-fd + write-normal write-bad-ptr write-boundary write-zero write-stdin + write-bad-fd ) unless @TESTS > 0; our (%args); @@ -271,15 +273,15 @@ sub grade_test { my (@output) = snarf ("output/$test/run.out"); - if (-e "$GRADES_DIR/$test.exp") { + my ($grade_func) = "grade_$test"; + $grade_func =~ s/-/_/g; + if (-e "$GRADES_DIR/$test.exp" && !defined (&$grade_func)) { eval { verify_common (@output); compare_output ("$GRADES_DIR/$test.exp", @output); } } else { - my ($grade_func); - ($grade_func = $test) =~ s/-/_/g; - eval "grade_$grade_func (\@output)"; + eval "$grade_func (\@output)"; } if ($@) { die $@ if $@ =~ /at \S+ line \d+$/; @@ -288,42 +290,24 @@ sub grade_test { return "ok"; } -sub grade_alarm_multiple { - verify_alarm (7, @_); -} - -sub verify_alarm { - my ($iterations, @output) = @_; - +sub grade_write_normal { + my (@output) = @_; verify_common (@output); - - my (@products); - for (my ($i) = 0; $i < $iterations; $i++) { - for (my ($t) = 0; $t < 5; $t++) { - push (@products, ($i + 1) * ($t + 1) * 10); - } - } - @products = sort {$a <=> $b} @products; - - local ($_); - foreach (@output) { - die $_ if /Out of order/; - - my ($p) = /product=(\d+)$/; - next if !defined $p; - - my ($q) = shift (@products); - die "Too many wakeups.\n" if !defined $q; - die "Out of order wakeups ($p vs. $q).\n" if $p != $q; # FIXME - } - die scalar (@products) . " fewer wakeups than expected.\n" - if @products != 0; + compare_output ("$GRADES_DIR/write-normal.exp", @output); + my ($test_txt) = "output/$test/test.txt"; + get_file ("test.txt", $test_txt) if ! -e $test_txt; + compare_output ("$GRADES_DIR/sample.txt", snarf ($test_txt)); } -sub grade_alarm_zero { - my (@output) = @_; - verify_common (@output); - die "Crashed in timer_sleep()\n" if !grep (/^Success\.$/, @output); +sub get_file { + my ($guest_fn, $host_fn) = @_; + xsystem ("pintos " + . "--os-disk=pintos/src/userprog/build/os.dsk " + . "--fs-disk=output/$test/fs.dsk " + . "-v get $guest_fn $host_fn", + LOG => "$test/get-$guest_fn", + TIMEOUT => 10) + or die "get $guest_fn failed\n"; } sub grade_alarm_negative { diff --git a/grading/userprog/sample.inc b/grading/userprog/sample.inc new file mode 100644 index 0000000..dd3d718 --- /dev/null +++ b/grading/userprog/sample.inc @@ -0,0 +1,6 @@ +char sample[] = { + "Amazing Electronic Fact: If you scuffed your feet long enough without\n" + "touching anything, you would build up so many electrons that your\n" + "finger would explode! But this is nothing to worry about unless you\n" + "have carpeting.\n" +}; diff --git a/grading/userprog/write-bad-fd.c b/grading/userprog/write-bad-fd.c new file mode 100644 index 0000000..33df001 --- /dev/null +++ b/grading/userprog/write-bad-fd.c @@ -0,0 +1,12 @@ +#include +#include + +int +main (void) +{ + char buf = 123; + printf ("(write-bad-fd) begin\n"); + write (0xc0101234, &buf, 1); + printf ("(write-bad-fd) end\n"); + return 0; +} diff --git a/grading/userprog/write-bad-fd.exp b/grading/userprog/write-bad-fd.exp new file mode 100644 index 0000000..dd05467 --- /dev/null +++ b/grading/userprog/write-bad-fd.exp @@ -0,0 +1,6 @@ +(write-bad-fd) begin +(write-bad-fd) end +write-bad-fd: exit(0) +--OR-- +(write-bad-fd) begin +write-bad-fd: exit(-1) diff --git a/grading/userprog/write-bad-ptr.c b/grading/userprog/write-bad-ptr.c new file mode 100644 index 0000000..9431c70 --- /dev/null +++ b/grading/userprog/write-bad-ptr.c @@ -0,0 +1,18 @@ +#include +#include + +int +main (void) +{ + int handle; + printf ("(write-bad-ptr) begin\n"); + + handle = open ("sample.txt"); + if (handle < 2) + printf ("(write-bad-ptr) fail: open() returned %d\n", handle); + + write (handle, (char *) 0xc0101234, 123); + + printf ("(write-bad-ptr) end\n"); + return 0; +} diff --git a/grading/userprog/write-bad-ptr.exp b/grading/userprog/write-bad-ptr.exp new file mode 100644 index 0000000..1540400 --- /dev/null +++ b/grading/userprog/write-bad-ptr.exp @@ -0,0 +1,6 @@ +(write-bad-ptr) begin +(write-bad-ptr) end +write-bad-ptr: exit(0) +--OR-- +(write-bad-ptr) begin +write-bad-ptr: exit(-1) diff --git a/grading/userprog/write-boundary.c b/grading/userprog/write-boundary.c new file mode 100644 index 0000000..a31dad0 --- /dev/null +++ b/grading/userprog/write-boundary.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include "sample.inc" + +static char * +mk_boundary_string (const char *src) +{ + static char dst[8192]; + char *p = dst + (4096 - (uintptr_t) dst % 4096 - strlen (src) / 2); + strlcpy (p, src, 4096); + return p; +} + +int +main (void) +{ + int handle; + int byte_cnt; + char *sample_p; + + sample_p = mk_boundary_string (sample); + + printf ("(write-boundary) begin\n"); + + handle = open ("sample.txt"); + if (handle < 2) + printf ("(write-boundary) fail: open() returned %d\n", handle); + + byte_cnt = write (handle, sample_p, sizeof sample - 1); + if (byte_cnt != sizeof sample - 1) + printf ("(write-boundary) fail: write() returned %d instead of %d\n", + byte_cnt, sizeof sample - 1); + + printf ("(write-boundary) end\n"); + return 0; +} diff --git a/grading/userprog/write-boundary.exp b/grading/userprog/write-boundary.exp new file mode 100644 index 0000000..bf64357 --- /dev/null +++ b/grading/userprog/write-boundary.exp @@ -0,0 +1,3 @@ +(write-boundary) begin +(write-boundary) end +write-boundary: exit(0) diff --git a/grading/userprog/write-normal.c b/grading/userprog/write-normal.c new file mode 100644 index 0000000..b2e96e8 --- /dev/null +++ b/grading/userprog/write-normal.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include "sample.inc" + +int +main (void) +{ + int handle, byte_cnt; + printf ("(write-normal) begin\n"); + + if (!create ("test.txt", sizeof sample - 1)) + printf ("(write-normal) create() failed\n"); + + handle = open ("test.txt"); + if (handle < 2) + printf ("(write-normal) fail: open() returned %d\n", handle); + + byte_cnt = write (handle, sample, sizeof sample - 1); + if (byte_cnt != sizeof sample - 1) + printf ("(write-normal) fail: write() returned %d instead of %d\n", + byte_cnt, sizeof sample - 1); + + printf ("(write-normal) end\n"); + return 0; +} diff --git a/grading/userprog/write-normal.exp b/grading/userprog/write-normal.exp new file mode 100644 index 0000000..87b05e5 --- /dev/null +++ b/grading/userprog/write-normal.exp @@ -0,0 +1,3 @@ +(write-normal) begin +(write-normal) end +write-normal: exit(0) diff --git a/grading/userprog/write-stdin.c b/grading/userprog/write-stdin.c new file mode 100644 index 0000000..1c6862b --- /dev/null +++ b/grading/userprog/write-stdin.c @@ -0,0 +1,12 @@ +#include +#include + +int +main (void) +{ + char buf = 123; + printf ("(write-stdin) begin\n"); + write (0, &buf, 1); + printf ("(write-stdin) end\n"); + return 0; +} diff --git a/grading/userprog/write-stdin.exp b/grading/userprog/write-stdin.exp new file mode 100644 index 0000000..bee46e0 --- /dev/null +++ b/grading/userprog/write-stdin.exp @@ -0,0 +1,6 @@ +(write-stdin) begin +(write-stdin) end +write-stdin: exit(0) +--OR-- +(write-stdin) begin +write-stdin: exit(-1) diff --git a/grading/userprog/write-zero.c b/grading/userprog/write-zero.c new file mode 100644 index 0000000..e5b5b95 --- /dev/null +++ b/grading/userprog/write-zero.c @@ -0,0 +1,22 @@ +#include +#include + +int +main (void) +{ + int handle, byte_cnt; + char buf; + printf ("(write-zero) begin\n"); + + handle = open ("sample.txt"); + if (handle < 2) + printf ("(write-zero) fail: open() returned %d\n", handle); + + buf = 123; + byte_cnt = write (handle, &buf, 0); + if (byte_cnt != 0) + printf ("(write-zero) fail: write() returned %d instead of 0\n", byte_cnt); + + printf ("(write-zero) end\n"); + return 0; +} diff --git a/grading/userprog/write-zero.exp b/grading/userprog/write-zero.exp new file mode 100644 index 0000000..ba76b0c --- /dev/null +++ b/grading/userprog/write-zero.exp @@ -0,0 +1,3 @@ +(write-zero) begin +(write-zero) end +write-zero: exit(0) diff --git a/src/Makefile.userprog b/src/Makefile.userprog index 4cdf078..0d93189 100644 --- a/src/Makefile.userprog +++ b/src/Makefile.userprog @@ -7,7 +7,7 @@ SHELL = /bin/sh VPATH = $(SRCDIR) DEFINES = -DUSER -CPPFLAGS = -nostdinc -I$(SRCDIR) -I- -I$(SRCDIR)/lib -I$(SRCDIR)/lib/user +CPPFLAGS = -nostdinc -I$(SRCDIR) -I- -I$(SRCDIR)/lib -I$(SRCDIR)/lib/user -I. # Linker flags. LDFLAGS = -nostdlib -static -s -- 2.30.2