aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/tests.pm
blob: 29e0707ed362a3fdbdb9116685ba8edf888ba8bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
use strict;
use warnings;
use tests::Algorithm::Diff;
use File::Temp 'tempfile';
use Fcntl qw(SEEK_SET SEEK_CUR);

sub fail;
sub pass;

die if @ARGV != 2;
our ($test, $src_dir) = @ARGV;

my ($msg_file) = tempfile ();
select ($msg_file);

our (@prereq_tests) = ();
if ($test =~ /^(.*)-persistence$/) {
    push (@prereq_tests, $1);
}
for my $prereq_test (@prereq_tests) {
    my (@result) = read_text_file ("$prereq_test.result");
    fail "Prerequisite test $prereq_test failed.\n" if $result[0] ne 'PASS';
}


# Generic testing.

sub check_expected {
    my ($expected) = pop @_;
    my (@options) = @_;
    my (@output) = read_text_file ("$test.output");
    common_checks ("run", @output);
    compare_output ("run", @options, \@output, $expected);
}

sub common_checks {
    my ($run, @output) = @_;

    fail "\u$run produced no output at all\n" if @output == 0;

    check_for_panic ($run, @output);
    check_for_keyword ($run, "FAIL", @output);
    check_for_triple_fault ($run, @output);
    check_for_keyword ($run, "TIMEOUT", @output);

    fail "\u$run didn't start up properly: no \"Pintos booting\" message\n"
      if !grep (/Pintos booting with.*kB RAM\.\.\./, @output);
    fail "\u$run didn't start up properly: no \"Boot complete\" message\n"
      if !grep (/Boot complete/, @output);
    fail "\u$run didn't shut down properly: no \"Timer: # ticks\" message\n"
      if !grep (/Timer: \d+ ticks/, @output);
    fail "\u$run didn't shut down properly: no \"Powering off\" message\n"
      if !grep (/Powering off/, @output);
}

sub check_for_panic {
    my ($run, @output) = @_;

    my ($panic) = grep (/PANIC/, @output);
    return unless defined $panic;

    print "Kernel panic in $run: ", substr ($panic, index ($panic, "PANIC")),
      "\n";

    my (@stack_line) = grep (/Call stack:/, @output);
    if (@stack_line != 0) {
	my ($addrs) = $stack_line[0] =~ /Call stack:((?: 0x[0-9a-f]+)+)/;

	# Find a user program to translate user virtual addresses.
	my ($userprog) = "";
	$userprog = "$test"
	  if grep (hex ($_) < 0xc0000000, split (' ', $addrs)) > 0 && -e $test;

	# Get and print the backtrace.
	my ($trace) = scalar (`backtrace kernel.o $userprog $addrs`);
	print "Call stack:$addrs\n";
	print "Translation of call stack:\n";
	print $trace;

	# Print disclaimer.
	if ($userprog ne '' && index ($trace, $userprog) >= 0) {
	    print <<EOF;
Translations of user virtual addresses above are based on a guess at
the binary to use.  If this guess is incorrect, then those
translations will be misleading.
EOF
	}
    }

    if ($panic =~ /sec_no \< d-\>capacity/) {
	print <<EOF;
\nThis assertion commonly fails when accessing a file via an inode that
has been closed and freed.  Freeing an inode clears all its sector
indexes to 0xcccccccc, which is not a valid sector number for disks
smaller than about 1.6 TB.
EOF
    }

    fail;
}

sub check_for_keyword {
    my ($run, $keyword, @output) = @_;
    
    my ($kw_line) = grep (/$keyword/, @output);
    return unless defined $kw_line;

    # Most output lines are prefixed by (test-name).  Eliminate this
    # from our message for brevity.
    $kw_line =~ s/^\([^\)]+\)\s+//;
    print "$run: $kw_line\n";

    fail;
}

sub check_for_triple_fault {
    my ($run, @output) = @_;

    my ($reboots) = grep (/Pintos booting/, @output) - 1;
    return unless $reboots > 0;

    print <<EOF;
\u$run spontaneously rebooted $reboots times.
This is most often caused by unhandled page faults.
Read the Triple Faults section in the Debugging chapter
of the Pintos manual for more information.
EOF

    fail;
}

# Get @output without header or trailer.
sub get_core_output {
    my ($run, @output) = @_;
    my ($p);

    my ($process);
    my ($start);
    for my $i (0...$#_) {
	$start = $i + 1, last
	  if ($process) = $output[$i] =~ /^Executing '(\S+).*':$/;
    }

    my ($end);
    for my $i ($start...$#output) {
	$end = $i - 1, last if $output[$i] =~ /^Execution of '.*' complete.$/;
    }

    fail "\u$run didn't start a thread or process\n" if !defined $start;
    fail "\u$run started '$process' but it never finished\n" if !defined $end;

    return @output[$start...$end];
}

sub compare_output {
    my ($run) = shift @_;
    my ($expected) = pop @_;
    my ($output) = pop @_;
    my (%options) = @_;

    my (@output) = get_core_output ($run, @$output);
    fail "\u$run didn't produce any output" if !@output;

    my $ignore_exit_codes = exists $options{IGNORE_EXIT_CODES};
    if ($ignore_exit_codes) {
	delete $options{IGNORE_EXIT_CODES};
	@output = grep (!/^[a-zA-Z0-9-_]+: exit\(\-?\d+\)$/, @output);
    }
    my $ignore_user_faults = exists $options{IGNORE_USER_FAULTS};
    if ($ignore_user_faults) {
	delete $options{IGNORE_USER_FAULTS};
	@output = grep (!/^Page fault at.*in user context\.$/
			&& !/: dying due to interrupt 0x0e \(.*\).$/
			&& !/^Interrupt 0x0e \(.*\) at eip=/
			&& !/^ cr2=.* error=.*/
			&& !/^ eax=.* ebx=.* ecx=.* edx=.*/
			&& !/^ esi=.* edi=.* esp=.* ebp=.*/
			&& !/^ cs=.* ds=.* es=.* ss=.*/, @output);
    }
    die "unknown option " . (keys (%options))[0] . "\n" if %options;

    my ($msg);

    # Compare actual output against each allowed output.
    if (ref ($expected) eq 'ARRAY') {
	my ($i) = 0;
	$expected = {map ((++$i => $_), @$expected)};
    }
    foreach my $key (keys %$expected) {
	my (@expected) = split ("\n", $expected->{$key});

	$msg .= "Acceptable output:\n";
	$msg .= join ('', map ("  $_\n", @expected));

	# Check whether actual and expected match.
	# If it's a perfect match, we're done.
	if ($#output == $#expected) {
	    my ($eq) = 1;
	    for (my ($i) = 0; $i <= $#expected; $i++) {
		$eq = 0 if $output[$i] ne $expected[$i];
	    }
	    return $key if $eq;
	}

	# They differ.  Output a diff.
	my (@diff) = "";
	my ($d) = Algorithm::Diff->new (\@expected, \@output);
	while ($d->Next ()) {
	    my ($ef, $el, $af, $al) = $d->Get (qw (min1 max1 min2 max2));
	    if ($d->Same ()) {
		push (@diff, map ("  $_\n", $d->Items (1)));
	    } else {
		push (@diff, map ("- $_\n", $d->Items (1))) if $d->Items (1);
		push (@diff, map ("+ $_\n", $d->Items (2))) if $d->Items (2);
	    }
	}

	$msg .= "Differences in `diff -u' format:\n";
	$msg .= join ('', @diff);
    }

    # Failed to match.  Report failure.
    $msg .= "\n(Process exit codes are excluded for matching purposes.)\n"
      if $ignore_exit_codes;
    $msg .= "\n(User fault messages are excluded for matching purposes.)\n"
      if $ignore_user_faults;
    fail "Test output failed to match any acceptable form.\n\n$msg";
}

# File system extraction.

# check_archive (\%CONTENTS)
#
# Checks that the extracted file system's contents match \%CONTENTS.
# Each key in the hash is a file name.  Each value may be:
#
#	- $FILE: Name of a host file containing the expected contents.
#
#	- [$FILE, $OFFSET, $LENGTH]: An excerpt of host file $FILE
#	  comprising the $LENGTH bytes starting at $OFFSET.
#
#	- [$CONTENTS]: The literal expected file contents, as a string.
#
#       - {SUBDIR}: A subdirectory, in the same form described here,
#         recursively.
sub check_archive {
    my ($expected_hier) = @_;

    my (@output) = read_text_file ("$test.output");
    common_checks ("file system extraction run", @output);

    @output = get_core_output ("file system extraction run", @output);
    @output = grep (!/^[a-zA-Z0-9-_]+: exit\(\d+\)$/, @output);
    fail join ("\n", "Error extracting file system:", @output) if @output;

    my ($test_base_name) = $test;
    $test_base_name =~ s%.*/%%;
    $test_base_name =~ s%-persistence$%%;
    $expected_hier->{$test_base_name} = $prereq_tests[0];
    $expected_hier->{'tar'} = 'tests/filesys/extended/tar';

    my (%expected) = normalize_fs (flatten_hierarchy ($expected_hier, ""));
    my (%actual) = read_tar ("$prereq_tests[0].tar");

    my ($errors) = 0;
    foreach my $name (sort keys %expected) {
	if (exists $actual{$name}) {
	    if (is_dir ($actual{$name}) && !is_dir ($expected{$name})) {
		print "$name is a directory but should be an ordinary file.\n";
		$errors++;
	    } elsif (!is_dir ($actual{$name}) && is_dir ($expected{$name})) {
		print "$name is an ordinary file but should be a directory.\n";
		$errors++;
	    }
	} else {
	    print "$name is missing from the file system.\n";
	    $errors++;
	}
    }
    foreach my $name (sort keys %actual) {
	if (!exists $expected{$name}) {
	    if ($name =~ /^[[:print:]]+$/) {
		print "$name exists in the file system but it should not.\n";
	    } else {
		my ($esc_name) = $name;
		$esc_name =~ s/[^[:print:]]/./g;
		print <<EOF;
$esc_name exists in the file system but should not.  (The name
of this file contains unusual characters that were printed as `.'.)
EOF
	    }
	    $errors++;
 	}
    }
    if ($errors) {
	print "\nActual contents of file system:\n";
	print_fs (%actual);
	print "\nExpected contents of file system:\n";
	print_fs (%expected);
    } else {
	foreach my $name (sort keys %expected) {
	    if (!is_dir ($expected{$name})) {
		my ($exp_file, $exp_length) = open_file ($expected{$name});
		my ($act_file, $act_length) = open_file ($actual{$name});
		$errors += !compare_files ($exp_file, $exp_length,
					   $act_file, $act_length, $name,
					   !$errors);
		close ($exp_file);
		close ($act_file);
	    }
	}
    }
    fail "Extracted file system contents are not correct.\n" if $errors;
}

# open_file ([$FILE, $OFFSET, $LENGTH])
# open_file ([$CONTENTS])
#
# Opens a file for the contents passed in, which must be in one of
# the two above forms that correspond to check_archive() arguments.
#
# Returns ($HANDLE, $LENGTH), where $HANDLE is the file's handle and
# $LENGTH is the number of bytes in the file's content.
sub open_file {
    my ($value) = @_;
    die if ref ($value) ne 'ARRAY';

    my ($file) = tempfile ();
    my ($length);
    if (@$value == 1) {
	$length = length ($value->[0]);
	$file = tempfile ();
	syswrite ($file, $value->[0]) == $length
	  or die "writing temporary file: $!\n";
	sysseek ($file, 0, SEEK_SET);
    } elsif (@$value == 3) {
	$length = $value->[2];
	open ($file, '<', $value->[0]) or die "$value->[0]: open: $!\n";
	die "$value->[0]: file is smaller than expected\n"
	  if -s $file < $value->[1] + $length;
	sysseek ($file, $value->[1], SEEK_SET);
    } else {
	die;
    }
    return ($file, $length);
}

# compare_files ($A, $A_SIZE, $B, $B_SIZE, $NAME, $VERBOSE)
#
# Compares $A_SIZE bytes in $A to $B_SIZE bytes in $B.
# ($A and $B are handles.)
# If their contents differ, prints a brief message describing
# the differences, using $NAME to identify the file.
# The message contains more detail if $VERBOSE is nonzero.
# Returns 1 if the contents are identical, 0 otherwise.
sub compare_files {
    my ($a, $a_size, $b, $b_size, $name, $verbose) = @_;
    my ($ofs) = 0;
    select(STDOUT);
    for (;;) {
	my ($a_amt) = $a_size >= 1024 ? 1024 : $a_size;
	my ($b_amt) = $b_size >= 1024 ? 1024 : $b_size;
	my ($a_data, $b_data);
	if (!defined (sysread ($a, $a_data, $a_amt))
	    || !defined (sysread ($b, $b_data, $b_amt))) {
	    die "reading $name: $!\n";
	}

	my ($a_len) = length $a_data;
	my ($b_len) = length $b_data;
	last if $a_len == 0 && $b_len == 0;

	if ($a_data ne $b_data) {
	    my ($min_len) = $a_len < $b_len ? $a_len : $b_len;
	    my ($diff_ofs);
	    for ($diff_ofs = 0; $diff_ofs < $min_len; $diff_ofs++) {
		last if (substr ($a_data, $diff_ofs, 1)
			 ne substr ($b_data, $diff_ofs, 1));
	    }

	    printf "\nFile $name differs from expected "
	      . "starting at offset 0x%x.\n", $ofs + $diff_ofs;
	    if ($verbose ) {
		print "Expected contents:\n";
		hex_dump (substr ($a_data, $diff_ofs, 64), $ofs + $diff_ofs);
		print "Actual contents:\n";
		hex_dump (substr ($b_data, $diff_ofs, 64), $ofs + $diff_ofs);
	    }
	    return 0;
	}

	$ofs += $a_len;
	$a_size -= $a_len;
	$b_size -= $b_len;
    }
    return 1;
}

# hex_dump ($DATA, $OFS)
#
# Prints $DATA in hex and text formats.
# The first byte of $DATA corresponds to logical offset $OFS
# in whatever file the data comes from.
sub hex_dump {
    my ($data, $ofs) = @_;

    if ($data eq '') {
	printf "  (File ends at offset %08x.)\n", $ofs;
	return;
    }

    my ($per_line) = 16;
    while ((my $size = length ($data)) > 0) {
	my ($start) = $ofs % $per_line;
	my ($end) = $per_line;
	$end = $start + $size if $end - $start > $size;
	my ($n) = $end - $start;

	printf "0x%08x  ", int ($ofs / $per_line) * $per_line;

	# Hex version.
	print "   " x $start;
	for my $i ($start...$end - 1) {
	    printf "%02x", ord (substr ($data, $i - $start, 1));
	    print $i == $per_line / 2 - 1 ? '-' : ' ';
	}
	print "   " x ($per_line - $end);

	# Character version.
	my ($esc_data) = substr ($data, 0, $n);
	$esc_data =~ s/[^[:print:]]/./g;
	print "|", " " x $start, $esc_data, " " x ($per_line - $end), "|";

	print "\n";

	$data = substr ($data, $n);
	$ofs += $n;
    }
}

# print_fs (%FS)
#
# Prints a list of files in %FS, which must be a file system
# as flattened by flatten_hierarchy() and normalized by
# normalize_fs().
sub print_fs {
    my (%fs) = @_;
    foreach my $name (sort keys %fs) {
	my ($esc_name) = $name;
	$esc_name =~ s/[^[:print:]]/./g;
	print "$esc_name: ";
	if (!is_dir ($fs{$name})) {
	    print +file_size ($fs{$name}), "-byte file";
	} else {
	    print "directory";
	}
	print "\n";
    }
    print "(empty)\n" if !@_;
}

# normalize_fs (%FS)
#
# Takes a file system as flattened by flatten_hierarchy().
# Returns a similar file system in which values of the form $FILE
# are replaced by those of the form [$FILE, $OFFSET, $LENGTH].
sub normalize_fs {
    my (%fs) = @_;
    foreach my $name (keys %fs) {
	my ($value) = $fs{$name};
	next if is_dir ($value) || ref ($value) ne '';
	die "can't open $value\n" if !stat $value;
	$fs{$name} = [$value, 0, -s _];
    }
    return %fs;
}

# is_dir ($VALUE)
#
# Takes a value like one in the hash returned by flatten_hierarchy()
# and returns 1 if it represents a directory, 0 otherwise.
sub is_dir {
    my ($value) = @_;
    return ref ($value) eq '' && $value eq 'directory';
}

# file_size ($VALUE)
#
# Takes a value like one in the hash returned by flatten_hierarchy()
# and returns the size of the file it represents.
sub file_size {
    my ($value) = @_;
    die if is_dir ($value);
    die if ref ($value) ne 'ARRAY';
    return @$value > 1 ? $value->[2] : length ($value->[0]);
}

# flatten_hierarchy ($HIER_FS, $PREFIX)
#
# Takes a file system in the format expected by check_archive() and
# returns a "flattened" version in which file names include all parent
# directory names and the value of directories is just "directory".
sub flatten_hierarchy {
    my (%hier_fs) = %{$_[0]};
    my ($prefix) = $_[1];
    my (%flat_fs);
    for my $name (keys %hier_fs) {
	my ($value) = $hier_fs{$name};
	if (ref $value eq 'HASH') {
	    %flat_fs = (%flat_fs, flatten_hierarchy ($value, "$prefix$name/"));
	    $flat_fs{"$prefix$name"} = 'directory';
	} else {
	    $flat_fs{"$prefix$name"} = $value;
	}
    }
    return %flat_fs;
}

# read_tar ($ARCHIVE)
#
# Reads the ustar-format tar file in $ARCHIVE
# and returns a flattened file system for it.
sub read_tar {
    my ($archive) = @_;
    my (%content);
    open (ARCHIVE, '<', $archive) or fail "$archive: open: $!\n";
    for (;;) {
	my ($header);
	if ((my $retval = sysread (ARCHIVE, $header, 512)) != 512) {
	    fail "$archive: unexpected end of file\n" if $retval >= 0;
	    fail "$archive: read: $!\n";
	}

	last if $header eq "\0" x 512;

	# Verify magic numbers.
	if (substr ($header, 257, 6) ne "ustar\0"
	    || substr ($header, 263, 2) ne '00') {
	    fail "$archive: corrupt ustar header\n";
	}

	# Verify checksum.
	my ($chksum) = oct (unpack ("Z*", substr ($header, 148, 8, ' ' x 8)));
	my ($correct_chksum) = unpack ("%32a*", $header);
	fail "$archive: bad header checksum\n" if $chksum != $correct_chksum;

	# Get file name.
	my ($name) = unpack ("Z100", $header);
	my ($prefix) = unpack ("Z*", substr ($header, 345));
	$name = "$prefix/$name" if $prefix ne '';
	fail "$archive: contains file with empty name" if $name eq '';

	# Get type.
	my ($typeflag) = substr ($header, 156, 1);
	$typeflag = '0' if $typeflag eq "\0";
	fail "unknown file type '$typeflag'\n" if $typeflag !~ /[05]/;

	# Get size.
	my ($size) = oct (unpack ("Z*", substr ($header, 124, 12)));
	fail "bad size $size\n" if $size < 0;
	$size = 0 if $typeflag eq '5';

	# Store content.
	if (exists $content{$name}) {
	    fail "$archive: contains multiple entries for $name\n";
	}
	if ($typeflag eq '5') {
	    $content{$name} = 'directory';
	} else {
	    my ($position) = sysseek (ARCHIVE, 0, SEEK_CUR);
	    $content{$name} = [$archive, $position, $size];
	    sysseek (ARCHIVE, int (($size + 511) / 512) * 512, SEEK_CUR);
	}
    }
    close (ARCHIVE);
    return %content;
}

# Utilities.

sub fail {
    finish ("FAIL", @_);
}

sub pass {
    finish ("PASS", @_);
}

sub finish {
    my ($verdict, @messages) = @_;

    seek ($msg_file, 0, 0);
    push (@messages, <$msg_file>);
    close ($msg_file);
    chomp (@messages);

    my ($result_fn) = "$test.result";
    open (RESULT, '>', $result_fn) or die "$result_fn: create: $!\n";
    print RESULT "$verdict\n";
    print RESULT "$_\n" foreach @messages;
    close (RESULT);

    if ($verdict eq 'PASS') {
	print STDOUT "pass $test\n";
    } else {
	print STDOUT "FAIL $test\n";
    }
    print STDOUT "$_\n" foreach @messages;

    exit 0;
}

sub read_text_file {
    my ($file_name) = @_;
    open (FILE, '<', $file_name) or die "$file_name: open: $!\n";
    my (@content) = <FILE>;
    chomp (@content);
    close (FILE);
    return @content;
}

1;