[Libsysio-commit] libsysio_tests: libsysio/tests populator.pl test_stdfd.pl verifier.pl
Brought to you by:
lward
From: Sonja T. <so...@us...> - 2003-08-14 18:53:05
|
Update of /cvsroot/libsysio/libsysio/tests In directory sc8-pr-cvs1:/tmp/cvs-serv17651/libsysio/tests Added Files: Tag: libsysio_tests populator.pl test_stdfd.pl verifier.pl Log Message: Adding files. This time hopefully for real --- NEW FILE --- #!/usr/bin/perl -w use IPC::Open2; use strict; use helper; sub usage { print "Usage: ./populator.pl <-seed seed> :\n"; print " <-file filename> :\n"; print " <-bytes bytes> : Create a file, filename, that\n"; print " : is bytes long and populate with\n"; print " : random numbers using the given\n"; print " : seed. Will use defaults if args\n"; print " : not given\n"; exit(-1); } sub get_buf { my $MAX_SIZE = 2147483648; my $str; my $num; my $len = 0; while ($len < 512) { $num = rand $MAX_SIZE; my $tmpstr = sprintf("%d", $num); $str .= $tmpstr; $len += length $tmpstr; } return ($len, $str); } sub write_file { my ($cmdfh, $outfh, $filename, $bytes) = @_; # Allocate the read buffer my $cmd = '$buf = ALLOC 1024'."\n"; helper::send_cmd($cmdfh, $outfh, "alloc", $cmd); # Open (create) the new file $cmd = '$fd = CALL open '."$filename O_RDWR|O_CREAT S_IRWXU\n"; helper::send_cmd($cmdfh, $outfh, "open", $cmd); # Verify the system call's output helper::verify_cmd($cmdfh, $outfh, "open"); my $left_bytes = $bytes; while ($left_bytes > 0) { # Get a buffer filled with random numbers # Buffer will be no less than 512 bytes my ($len, $buf) = get_buf; if ($len > $left_bytes) { $len = $left_bytes; } # Need to fill $buf with the buffer $cmd = "CALL fill $buf STR $len 0 ".'$buf'."\n"; helper::send_cmd($cmdfh, $outfh, "fill", $cmd); # Write out $len bytes to $filename $cmd = 'CALL write $fd $buf '."$len\n"; helper::send_cmd($cmdfh, $outfh, "write", $cmd); my $written_bytes = helper::verify_cmd($cmdfh, $outfh, "write"); $written_bytes = oct($written_bytes); if ($written_bytes != $len) { helper::print_and_exit($cmdfh, $outfh, 1, "ERROR! Meant to print out $len but only printed $written_bytes\n"); } $left_bytes -= $len; } } sub populate_file { my ($filename, $bytes, $is_alpha) = @_; eval { if ($is_alpha == 0) { open2(\*OUTFILE, \*CMDFILE, "./test_driver --np"); } else { open2(\*OUTFILE, \*CMDFILE, "yod -batch -quiet -sz 1 ./test_driver --np"); } }; if ($@) { if ($@ =~ /^open2/) { warn "open2 failed: $!\n$@\n"; return; } die; } my $outfh = \*OUTFILE; my $cmdfh = \*CMDFILE; if ($is_alpha == 0) { helper::send_cmd($cmdfh, $outfh, "init", "CALL init\n"); } # Now write the file write_file($cmdfh, $outfh, $filename, $bytes); # Close the file my $cmd = 'CALL close $fd'."\n"; helper::send_cmd($cmdfh, $outfh, "close", $cmd); helper::verify_cmd($cmdfh, $outfh, "close"); # All done helper::print_and_exit($cmdfh, $outfh, 0, "File $filename successfully created\n"); } my $is_alpha = 0; my $seed = time; my $filename = "randfile.$seed.$$"; my $bytes = 1024; for (my $i = 0; $i < @ARGV; $i++) { if ($ARGV[$i] eq "-file") { $i++; $filename = $ARGV[$i]; } elsif ($ARGV[$i] eq "-seed") { $i++; $seed = $ARGV[$i]; } elsif ($ARGV[$i] eq "-alpha") { $is_alpha = 1; } elsif ($ARGV[$i] eq "-bytes") { $i++; $bytes = $ARGV[$i]; } } # seed the randome number generator srand $seed; populate_file($filename, $bytes, $is_alpha); exit $seed; --- NEW FILE --- #!/usr/bin/perl -w # # stdfd test: Verifies that stdin, stdout, and stderr can be opened and # either written to or read from (in the case of stdin) use IPC::Open2; use strict; use helper; sub usage { print "Usage ./test_stdfd : Verifies that stdin, stdout, and stderr can be opened and "; print " : either written to or read from (in the case of stdin)"; exit(-1); } sub mkdev { my ($major, $minor) = @_; my $devno = ( (($major & 0xff) << 8) | ($minor & 0xff) ); return $devno; } sub statit { my ($cmdfh, $outfh, $do_print, $name) = @_; my $cmd = "CALL stat $name ".'$buf'."\n"; helper::send_cmd($cmdfh, $outfh, "stat", $cmd); helper::verify_cmd($cmdfh, $outfh, "stat $name"); # Print out the stat buffer $cmd = 'PRINT $buf 0 8 LONG 12 24 INT 44 8 LONG 52 8 INT 64 24 LONG'; $cmd .= "\n"; helper::send_cmd($cmdfh, $outfh, "PRINT", $cmd); my $res = <$outfh>; chop($res); my ( $iodev, $ioino, $iomode, $ionlink, $iouid, $iogid, $iordev, $iosize, $ioblksize, $ioblks, $ioatime, $iomtime, $ioctime ) = split(' ', $res); $iomode = oct($iomode); if ($do_print == 1) { # Print out the path my $typechar = helper::get_type($iomode); print STDOUT "$name: $typechar\n"; } return 0; } sub do_open { my ($cmdfh, $outfh, $name, $mode, $num) = @_; helper::send_cmd($cmdfh, $outfh, "open", "CALL open $name $mode\n"); my $res = helper::verify_cmd($cmdfh, $outfh, "open $name"); #chop($res); $res = oct($res); if ($res < 0) { helper::print_and_exit($cmdfh, $outfh, 1, "Unable to open $name\n"); } if ($res == $num) { return $res; } helper::send_cmd($cmdfh, $outfh, "dup2", "CALL dup2 $res $num\n"); $res = helper::verify_cmd($cmdfh, $outfh, "dup2"); $res = oct($res); if ($res != $num) { helper::print_and_exit($cmdfh, $outfh, 1, "Unable to dup $name (res was $res)\n"); } } sub do_mknod { my ($cmdfh, $outfh, $do_print, $name, $perm_num, $minor) = @_; my $perm = 'S_IFCHR|'.$perm_num; my $devno = mkdev(0, $minor); helper::send_cmd($cmdfh, $outfh, "mknod", "CALL mknod $name $perm $devno\n"); helper::verify_cmd($cmdfh, $outfh, "mknod $name"); my $statres = statit($cmdfh, $outfh, $do_print, $name); if ($statres != 0) { helper::print_and_exit($cmdfh, $outfh, 1, "stat on $name failed\n"); } } sub process_cmd { my ($dirname, $do_print, $is_alpha) = @_; eval { if ($is_alpha == 1) { open2(\*OUTFILE, \*CMDFILE, "yod -sz 1 ./test_driver --np"); } else { open2(\*OUTFILE, \*CMDFILE, "./test_driver --np"); } }; if ($@) { if ($@ =~ /^open2/) { warn "open2 failed: $!\n$@\n"; return; } die; } my $outfh = \*OUTFILE; my $cmdfh = \*CMDFILE; if ($is_alpha == 0) { helper::send_cmd($cmdfh, $outfh, "init", "CALL init incore ".'"0777+0+0"'." 0\n"); } my $start = 0; # Get a stat buffer my $cmd = '$buf = ALLOC ( $size = CALL sizeof stat )'."\n"; helper::send_cmd($cmdfh, $outfh, "alloc", $cmd); # Make the test directory $cmd = "CALL mkdir $dirname 0777\n"; helper::send_cmd($cmdfh, $outfh, "mkdir", $cmd); helper::verify_cmd($cmdfh, $outfh, "mkdir"); # Change working dir to test dir $cmd = "CALL chdir $dirname\n"; helper::send_cmd($cmdfh, $outfh, "chdir", $cmd); helper::verify_cmd($cmdfh, $outfh, "chdir"); # Create the 3 special files do_mknod($cmdfh, $outfh, $do_print, "stdin", "0444", 0); do_mknod($cmdfh, $outfh, $do_print, "stdout", "0222", 1); do_mknod($cmdfh, $outfh, $do_print, "stderr", "0222", 2); # Open the 3 files do_open($cmdfh, $outfh, "stdin", "O_RDONLY", 0); do_open($cmdfh, $outfh, "stdout", "O_WRONLY", 1); do_open($cmdfh, $outfh, "stderr", "O_WRONLY", 2); #helper::send_cmd($cmdfh, $outfh, "debug", "CALL debug 5\n"); # Read from stdin, write to stdout and stderr # Send "delay" option to read which will give us time to # put something in stdin (since we can't send an eof) $cmd = "CALL read 0 ".'$buf 38'." delay\n"; print $cmdfh $cmd; # Give time to process command sleep 1; # Send random junk... print $cmdfh "This message is exactly 38 bytes long\n"; sleep 0.5; # Make sure read was OK my $res = <$outfh>; chop($res); if ($res ne "0000 ") { helper::print_and_exit($cmdfh, $outfh, 1, "ERROR! Command $cmd failed with code $res\n"); } # See how many bytes we got... my $bytes = helper::verify_cmd($cmdfh, $outfh, "read"); $bytes = oct($bytes); if ($bytes == 0) { helper::print_and_exit($cmdfh, $outfh, 0, "test_stdfd successful but read nothing\n"); } if ($bytes < 0) { helper::print_and_exit($cmdfh, $outfh, 0, "test_stdfd unsuccessful\n"); } $cmd = "CALL write 1 ".'$buf '."$bytes\n"; print $cmdfh $cmd; # Suck up the stdout... $res = <$outfh>; chop($res); $res = <$outfh>; chop($res); $res = oct($res); if ($res != 0) { helper::print_and_exit($cmdfh, $outfh, 1, "ERROR! Command $cmd failed with code $res\n"); } helper::verify_cmd($cmdfh, $outfh, "write stdout"); $cmd = "CALL write 2 ".'$buf '."$bytes\n"; helper::send_cmd($cmdfh, $outfh, "write stderr", $cmd); helper::verify_cmd($cmdfh, $outfh, "write stderr"); helper::print_and_exit($cmdfh, $outfh, 0, "test_stdfd successful\n"); } my $is_alpha = 0; my $do_print = 0; my $i; for ($i=0; $i < @ARGV; $i++) { if ($ARGV[$i] eq "-alpha") { $is_alpha =1; } elsif ($ARGV[$i] eq "-print") { $do_print = 1; } } $i--; my $dirname = $ARGV[$i]; process_cmd($dirname, $do_print, $is_alpha); exit 0; --- NEW FILE --- #!/usr/bin/perl -w # Verifies that the contents of a given file produced by producer.pl with the given # seed are good use IPC::Open2; use strict; use helper; sub usage { print "Usage: ./verifier.pl <-seed seed> <-file fname> : Verifies that file fname,\n"; print " : produced with the given \n"; print " : seed matches\n"; exit(-1); } sub get_buf { my $MAX_SIZE = 2147483648; my $str; my $num; my $len = 0; while ($len < 512) { $num = rand $MAX_SIZE; my $tmpstr = sprintf("%d", $num); $str .= $tmpstr; $len += length $tmpstr; } return ($len, $str); } sub check_file { my ($cmdfh, $outfh, $filename) = @_; # Allocate the read buffer my $cmd = '$buf = ALLOC 1024'."\n"; helper::send_cmd($cmdfh, $outfh, "alloc", $cmd); # Open the file $cmd = '$fd = CALL open '."$filename O_RDONLY\n"; helper::send_cmd($cmdfh, $outfh, "open", $cmd); # Verify the system call's output helper::verify_cmd($cmdfh, $outfh, "open"); my $total = 0; my $bytes = 0; # Read all of the file in 1024 byte chunks do { # Clear the buffer $cmd = 'CALL clear $buf'."\n"; helper::send_cmd($cmdfh, $outfh, "clear", $cmd); my ($len, $buf) = get_buf; $cmd = 'CALL read $fd $buf '."$len\n"; helper::send_cmd($cmdfh, $outfh, "read", $cmd); $bytes = helper::verify_cmd($cmdfh, $outfh, "read"); $bytes = oct($bytes); $total += $bytes; if ($bytes > 0) { # Print out the buffer $cmd = 'PRINT $buf 0 1 STR'."\n"; helper::send_cmd($cmdfh, $outfh, "print", $cmd); my $str = <$outfh>; chop($str); if ($bytes > $len) { $str = substr($str, 0, $len-1); } elsif ($len > $bytes) { $buf = substr($buf, 0, $bytes); } if ($str ne $buf) { my $errstr = "ERROR! Str $str is not equal to str $buf\n"; helper::print_and_exit($cmdfh, $outfh, 1, $errstr); } } } while ($bytes > 0); } sub verify_file { my ($filename, $is_alpha) = @_; eval { if ($is_alpha == 0) { open2(\*OUTFILE, \*CMDFILE, "./test_driver --np"); } else { open2(\*OUTFILE, \*CMDFILE, "yod -batch -quiet -sz 1 ./test_driver --np"); } }; if ($@) { if ($@ =~ /^open2/) { warn "open2 failed: $!\n$@\n"; return; } die; } my $outfh = \*OUTFILE; my $cmdfh = \*CMDFILE; if ($is_alpha == 0) { helper::send_cmd($cmdfh, $outfh, "init", "CALL init\n"); } # Now check the file check_file($cmdfh, $outfh, $filename); # Close the file my $cmd = 'CALL close $fd'."\n"; helper::send_cmd($cmdfh, $outfh, "close", $cmd); helper::verify_cmd($cmdfh, $outfh, "close"); # All done helper::print_and_exit($cmdfh, $outfh, 0, "File $filename valid\n"); } my $is_alpha = 0; my $seed = time; my $filename = "randfile.$seed.$$"; my $bytes = 1024; for (my $i = 0; $i < @ARGV; $i++) { if ($ARGV[$i] eq "-file") { $i++; $filename = $ARGV[$i]; } elsif ($ARGV[$i] eq "-seed") { $i++; $seed = $ARGV[$i]; } elsif ($ARGV[$i] eq "-alpha") { $is_alpha = 1; } } # seed the randome number generator srand $seed; verify_file($filename, $is_alpha); exit 0; |