Whamcloud - gitweb
Land b_release_1_4_6 onto HEAD (20060223_1455)
[fs/lustre-release.git] / libsysio / tests / verifier.pl
1 #!/usr/bin/perl -w
2
3 # Verifies that the contents of a given file produced by producer.pl with the given
4 # seed are good
5
6 use IPC::Open2;
7
8 use strict;
9 use helper;
10
11 sub usage
12 {
13   print "Usage: ./verifier.pl <-seed seed> <-file fname> : Verifies that file fname,\n";
14   print "                                                : produced with the given \n";
15   print "                                                : seed matches\n";
16   exit(-1);
17 }
18
19 sub get_buf
20 {
21   my $MAX_SIZE = 2147483648;
22
23   my $str;
24   my $num;
25   my $len = 0;
26
27   while ($len < 512) {
28     $num = rand $MAX_SIZE;
29     my $tmpstr = sprintf("%d", $num);
30     $str .= $tmpstr;
31     $len += length $tmpstr;
32   }
33
34   return ($len, $str);
35 }
36
37
38 sub check_file
39 {
40   my ($cmdfh, $outfh, $filename) = @_;
41
42
43   # Allocate the read buffer
44   my $cmd = '$buf = ALLOC 1024'."\n";
45   helper::send_cmd($cmdfh, $outfh, "alloc", $cmd);  
46   
47   # Open the file
48   $cmd = '$fd = CALL open '."$filename O_RDONLY\n";
49   helper::send_cmd($cmdfh, $outfh, "open", $cmd);  
50
51   # Verify the system call's output
52   helper::verify_cmd($cmdfh, $outfh, "open");  
53
54   my $total = 0;
55   my $bytes = 0;
56
57   # Read all of the file in 1024 byte chunks
58   do {
59
60     # Clear the buffer
61     $cmd = 'CALL clear $buf'."\n";
62     helper::send_cmd($cmdfh, $outfh, "clear", $cmd);  
63
64     my ($len, $buf) = get_buf;
65
66     $cmd = 'CALL read $fd $buf '."$len\n";
67     helper::send_cmd($cmdfh, $outfh, "read", $cmd);  
68     $bytes = helper::verify_cmd($cmdfh, $outfh, "read");  
69     $bytes = oct($bytes);
70     $total += $bytes;
71     if ($bytes > 0) {
72      
73       # Print out the buffer
74       $cmd = 'PRINT $buf 0 1 STR'."\n";
75       helper::send_cmd($cmdfh, $outfh, "print", $cmd);  
76       my $str = <$outfh>;
77       chop($str);
78       if ($bytes > $len) {
79         $str = substr($str, 0, $len-1);
80       } elsif ($len > $bytes) {
81         $buf = substr($buf, 0, $bytes);
82       }
83       if ($str ne $buf) {
84         my $errstr = "ERROR! Str $str is not equal to str $buf\n";
85         helper::print_and_exit($cmdfh, $outfh, 1, $errstr);
86       }
87     }
88   } while ($bytes > 0);
89
90 }
91
92 sub verify_file
93 {
94   my ($filename, $is_alpha) = @_;
95   
96   eval {
97       if ($is_alpha == 0) {
98           open2(\*OUTFILE, \*CMDFILE, "./test_driver --np");
99       } else {
100           open2(\*OUTFILE, \*CMDFILE, 
101                 "yod -batch -quiet -sz 1 ./test_driver --np");
102       }
103   };
104
105   if ($@) {
106     if ($@ =~ /^open2/) {
107       warn "open2 failed: $!\n$@\n";
108       return;
109     }
110     die;
111
112   }
113
114   my $outfh = \*OUTFILE;
115   my $cmdfh = \*CMDFILE;
116
117   if ($is_alpha == 0) {
118     helper::send_cmd($cmdfh, $outfh, "init", "CALL init\n");
119   }
120
121   # Now check the file
122   check_file($cmdfh, $outfh, $filename);
123
124   # Close the file
125   my $cmd = 'CALL close $fd'."\n";
126   helper::send_cmd($cmdfh, $outfh, "close", $cmd);
127
128   helper::verify_cmd($cmdfh, $outfh, "close");
129
130   # All done
131   helper::print_and_exit($cmdfh, $outfh, 0, "File $filename valid\n");
132 }
133
134
135 my $is_alpha = 0;
136 my $seed = time;
137 my $filename = "randfile.$seed.$$";
138 my $bytes = 1024;
139 for (my $i = 0; $i < @ARGV; $i++) 
140 {
141   if ($ARGV[$i] eq "-file") {
142     $i++;
143     $filename = $ARGV[$i];
144   } elsif ($ARGV[$i] eq "-seed") {
145     $i++;
146     $seed = $ARGV[$i];
147   } elsif ($ARGV[$i] eq "-alpha") {
148     $is_alpha = 1;
149   } 
150 }
151
152 # seed the randome number generator
153 srand $seed;
154
155 verify_file($filename, $is_alpha);
156
157 exit 0;
158
159
160
161