Whamcloud - gitweb
import older libsysio snapshot.
[fs/lustre-release.git] / libsysio / tests / test_stdfd.pl
1 #!/usr/bin/perl -w
2
3 #
4 # stdfd test: Verifies that stdin, stdout, and stderr can be opened and 
5 #             either written to or read from (in the case of stdin)
6
7 use IPC::Open2;
8
9 use strict;
10 use FindBin;
11 use lib "$FindBin::Bin";
12 use helper;
13
14 sub usage
15 {
16   print "Usage ./test_stdfd : Verifies that stdin, stdout, and stderr can be opened and ";
17   print "                   : either written to or read from (in the case of stdin)";
18   exit(-1);
19 }
20
21 sub mkdev
22 {
23   my ($major, $minor) = @_;
24   my $devno = ( (($major & 0xff) << 8) | ($minor & 0xff) );
25
26   return $devno;
27 }
28
29 sub statit
30 {
31   my ($cmdfh, $outfh, $do_print, $name) = @_;
32
33   my $cmd = "CALL stat $name ".'$buf'."\n";
34
35   helper::send_cmd($cmdfh, $outfh, "stat", $cmd);
36   helper::verify_cmd($cmdfh, $outfh, "stat $name");
37
38   # Print out the stat buffer
39   $cmd = 'PRINT $buf 0 8 LONG 12 24 INT 44 8 LONG 52 8 INT 64 24 LONG';
40   $cmd .= "\n";
41   helper::send_cmd($cmdfh, $outfh, "PRINT", $cmd);
42   
43   my $res = <$outfh>;
44   chop($res);
45   my ( $iodev, $ioino, $iomode, $ionlink, $iouid, $iogid, $iordev, 
46        $iosize, $ioblksize, $ioblks, $ioatime, $iomtime, $ioctime ) 
47     = split(' ', $res);
48
49   $iomode = oct($iomode);
50
51   if ($do_print == 1) {
52     # Print out the path
53     my $typechar = helper::get_type($iomode);
54     print STDOUT "$name: $typechar\n";
55   }
56   return 0;
57 }
58
59 sub do_open
60 {
61
62   my ($cmdfh, $outfh, $name, $mode, $num) = @_;
63
64   helper::send_cmd($cmdfh, $outfh, "open", "CALL open $name $mode\n");
65   
66   my $res = helper::verify_cmd($cmdfh, $outfh, "open $name");
67
68   #chop($res);
69   $res = oct($res);
70   if ($res < 0) {
71     helper::print_and_exit($cmdfh, $outfh, 1, "Unable to open $name\n");
72   }
73
74
75   if ($res == $num) {
76       return $res;
77   }
78
79   helper::send_cmd($cmdfh, $outfh, "dup2", "CALL dup2 $res $num\n");
80   $res = helper::verify_cmd($cmdfh, $outfh, "dup2");
81   $res = oct($res);
82
83   if ($res != $num) {
84     helper::print_and_exit($cmdfh, $outfh, 1, "Unable to dup $name (res was $res)\n");
85   }
86 }
87
88 sub do_mknod
89 {
90
91   my ($cmdfh, $outfh, $do_print, $name, $perm_num, $minor) = @_;
92
93   my $perm = 'S_IFCHR|'.$perm_num;
94   my $devno = mkdev(0, $minor);
95
96   helper::send_cmd($cmdfh, $outfh, "mknod", "CALL mknod $name $perm $devno\n");
97   
98   helper::verify_cmd($cmdfh, $outfh, "mknod $name");
99
100   my $statres = statit($cmdfh, $outfh, $do_print, $name);
101   if ($statres != 0) {
102     helper::print_and_exit($cmdfh, $outfh, 1, "stat on $name failed\n");
103   }
104 }
105
106 sub process_cmd
107 {
108   my ($dirname, $do_print, $is_alpha) = @_;
109  
110 # Get tests directory
111 my $testdir = $0;
112 $testdir =~ s/\/\w+.pl$//;
113  
114   eval {
115       if ($is_alpha == 1) {
116           open2(\*OUTFILE, \*CMDFILE, "yod -sz 1 -quiet -batch $testdir/test_driver --np");
117       } else {
118           open2(\*OUTFILE, \*CMDFILE, "$testdir/test_driver --np");
119       }
120   };
121
122   if ($@) {
123     if ($@ =~ /^open2/) {
124       warn "open2 failed: $!\n$@\n";
125       return;
126     }
127     die;
128
129   }
130
131   my $outfh = \*OUTFILE;
132   my $cmdfh = \*CMDFILE;
133
134   if ($is_alpha == 0) {
135       helper::send_cmd($cmdfh, $outfh, "init", "CALL init incore ".'"0777+0+0"'." 0\n");
136       helper::verify_cmd($cmdfh, $outfh, "init incore");
137   }
138
139
140   # Get a stat buffer
141   my $cmd = '$buf = ALLOC ( $size = CALL sizeof stat )'."\n";
142   helper::send_cmd($cmdfh, $outfh, "alloc", $cmd);
143
144   if ($is_alpha == 0) {
145   # Make the test directory
146   $cmd = "CALL mkdir $dirname 0777\n";
147   helper::send_cmd($cmdfh, $outfh, "mkdir", $cmd);
148   helper::verify_cmd($cmdfh, $outfh, "mkdir");
149
150
151   # Change working dir to test dir
152   $cmd = "CALL chdir $dirname\n";
153   helper::send_cmd($cmdfh, $outfh, "chdir", $cmd);
154   helper::verify_cmd($cmdfh, $outfh, "chdir");
155
156
157   # Create the 3 special files
158   do_mknod($cmdfh, $outfh, $do_print, "stdin", "0444", 0);
159   do_mknod($cmdfh, $outfh, $do_print, "stdout", "0222", 1);
160   do_mknod($cmdfh, $outfh, $do_print, "stderr", "0222", 2);
161
162   # Open the 3 files
163   do_open($cmdfh, $outfh, "stdin", "O_RDONLY", 0);
164   do_open($cmdfh, $outfh, "stdout", "O_WRONLY", 1);
165   do_open($cmdfh, $outfh, "stderr", "O_WRONLY", 2);
166  } 
167   #helper::send_cmd($cmdfh, $outfh, "debug", "CALL debug 5\n");
168
169   # Read from stdin, write to stdout and stderr
170
171   # Send "delay" option to read which will give us time to 
172   # put something in stdin (since we can't send an eof)
173   $cmd = "CALL read 0 ".'$buf 38'." delay\n";
174   print $cmdfh $cmd;
175   # Give time to process command
176   sleep 1;
177
178   # Send random junk...
179   print $cmdfh "This message is exactly 38 bytes long\n";
180   sleep 0.5;
181
182   # Make sure read was OK
183   my $res = <$outfh>;
184   chop($res);
185   if ($res ne "0000 ") {
186     helper::print_and_exit($cmdfh, $outfh, 1, "ERROR! Command $cmd failed with code $res\n");
187   }
188     
189   # See how many bytes we got...
190   my $bytes = helper::verify_cmd($cmdfh, $outfh, "read");
191   $bytes = oct($bytes);
192   if ($bytes == 0) {
193     helper::print_and_exit($cmdfh, $outfh, 0, "test_stdfd successful but read nothing\n");
194   }
195
196   if ($bytes < 0) {
197     helper::print_and_exit($cmdfh, $outfh, 0, "test_stdfd unsuccessful\n");
198   }
199
200   $cmd = "CALL write 1 ".'$buf '."$bytes\n";
201   print $cmdfh $cmd;
202
203   # Suck up the stdout...
204   $res = <$outfh>;
205   chop($res);
206   
207   $res = <$outfh>;
208   chop($res);
209   $res = oct($res);
210
211   if ($res != 0) {
212     helper::print_and_exit($cmdfh, $outfh, 1, "ERROR! Command $cmd failed with code $res\n");
213   }
214
215   helper::verify_cmd($cmdfh, $outfh, "write stdout");
216
217   $cmd = "CALL write 2 ".'$buf '."$bytes\n";
218   helper::send_cmd($cmdfh, $outfh, "write stderr", $cmd);
219   helper::verify_cmd($cmdfh, $outfh, "write stderr");
220
221   helper::print_and_exit($cmdfh, $outfh, 0, "test_stdfd successful\n");
222 }
223
224
225 my $is_alpha = 0;
226 my $do_print = 0;
227 my $i;
228 for ($i=0; $i < @ARGV; $i++) {
229   if ($ARGV[$i] eq "-alpha") {
230     $is_alpha =1;
231   } elsif ($ARGV[$i] eq "-print") {
232     $do_print = 1;
233   }
234 }
235
236 $i--;
237 my $dirname = $ARGV[$i];
238
239 process_cmd($dirname, $do_print, $is_alpha);
240
241 exit 0;
242