3 use tests::Algorithm::Diff;
4 use File::Temp 'tempfile';
10 our ($test, $src_dir) = @ARGV;
11 our ($src_stem) = "$src_dir/$test";
13 my ($msg_file) = tempfile ();
17 my ($expected) = pop @_;
19 my (@output) = read_text_file ("$test.output");
20 common_checks (@output);
21 compare_output (@options, \@output, $expected);
27 fail "No output at all\n" if @output == 0;
29 check_for_panic (@output);
30 check_for_keyword ("FAIL", @output);
31 check_for_triple_fault (@output);
32 check_for_keyword ("TIMEOUT", @output);
34 fail "Didn't start up properly: no \"Pintos booting\" startup message\n"
35 if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
36 fail "Didn't start up properly: no \"Boot complete\" startup message\n"
37 if !grep (/Boot complete/, @output);
38 fail "Didn't shut down properly: no \"Timer: # ticks\" shutdown message\n"
39 if !grep (/Timer: \d+ ticks/, @output);
40 fail "Didn't shut down properly: no \"Powering off\" shutdown message\n"
41 if !grep (/Powering off/, @output);
47 my ($panic) = grep (/PANIC/, @output);
48 return unless defined $panic;
50 print "Kernel panic: ", substr ($panic, index ($panic, "PANIC")), "\n";
52 my (@stack_line) = grep (/Call stack:/, @output);
53 if (@stack_line != 0) {
54 my (@addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;
55 print "Call stack: @addrs\n";
56 print "Translation of call stack:\n";
57 print `backtrace kernel.o @addrs`;
60 if ($panic =~ /sec_no \< d-\>capacity/) {
62 \nThis assertion commonly fails when accessing a file via an inode that
63 has been closed and freed. Freeing an inode clears all its sector
64 indexes to 0xcccccccc, which is not a valid sector number for disks
65 smaller than about 1.6 TB.
72 sub check_for_keyword {
73 my ($keyword, @output) = @_;
75 my ($kw_line) = grep (/$keyword/, @output);
76 return unless defined $kw_line;
78 # Most output lines are prefixed by (test-name). Eliminate this
79 # from our message for brevity.
80 $kw_line =~ s/^\([^\)]+\)\s+//;
86 sub check_for_triple_fault {
89 return unless grep (/Pintos booting/, @output) > 1;
92 Pintos spontaneously rebooted during this test.
93 This is most often caused by unhandled page faults.
99 # Get @output without header or trailer.
100 sub get_core_output {
102 do { $p = shift; } while (defined ($p) && $p !~ /^Executing '.*':$/);
103 do { $p = pop; } while (defined ($p) && $p !~ /^Execution of '.*' complete.$/);
108 my ($expected) = pop @_;
109 my ($output) = pop @_;
112 my (@output) = get_core_output (@$output);
113 fail "'$test' didn't run or didn't produce any output\n" if !@output;
115 if (exists $options{IGNORE_EXIT_CODES}) {
116 delete $options{IGNORE_EXIT_CODES};
117 @output = grep (!/^[a-zA-Z0-9-_]+: exit\(\d+\)$/, @output);
119 die "unknown option " . (keys (%options))[0] . "\n" if %options;
123 # Compare actual output against each allowed output.
124 foreach my $exp_string (@$expected) {
125 my (@expected) = split ("\n", $exp_string);
127 $msg .= "Acceptable output:\n";
128 $msg .= join ('', map (" $_\n", @expected));
130 # Check whether actual and expected match.
131 # If it's a perfect match, we're done.
132 if ($#output == $#expected) {
134 for (my ($i) = 0; $i <= $#expected; $i++) {
135 $eq = 0 if $output[$i] ne $expected[$i];
140 # They differ. Output a diff.
142 my ($d) = Algorithm::Diff->new (\@expected, \@output);
143 while ($d->Next ()) {
144 my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
146 push (@diff, map (" $_\n", $d->Items (1)));
148 push (@diff, map ("- $_\n", $d->Items (1))) if $d->Items (1);
149 push (@diff, map ("+ $_\n", $d->Items (2))) if $d->Items (2);
153 $msg .= "Differences in `diff -u' format:\n";
154 $msg .= join ('', @diff);
157 # Failed to match. Report failure.
158 fail "Test output failed to match any acceptable form.\n\n$msg";
170 my ($verdict, @rest) = @_;
173 seek ($msg_file, 0, 0);
174 while (<$msg_file>) {
179 my ($result_fn) = "$test.result";
180 open (RESULT, '>', $result_fn) or die "$result_fn: create: $!\n";
181 print RESULT "$verdict\n", $messages, @rest;
184 if ($verdict eq 'PASS') {
185 print STDOUT "pass $test\n";
187 print STDOUT "FAIL $test\n";
189 print STDOUT $messages, @rest, "\n";
195 my ($file_name) = @_;
196 open (FILE, '<', $file_name) or die "$file_name: open: $!\n";
197 my (@content) = <FILE>;