Whamcloud - gitweb
b=11512
[fs/lustre-release.git] / libsysio / tests / helper.pm
1 #!/usr/bin/perl -w
2
3 #
4 # Provides a set of helper routines for use in the Perl 
5 # test scripts
6 #
7
8 package helper;
9 use strict;
10 use POSIX;
11
12 BEGIN{}
13
14 # Print out a given error message, close the command file
15 # and exit
16 sub print_and_exit
17 {
18   my ($cmdfh, $outfh, $exit_num, $exit_str) = @_;
19
20   print STDOUT "$exit_str";
21
22   # Clean up
23   my $cmdstr =  'FREE $buf';
24   $cmdstr = $cmdstr."\n";
25
26   print $cmdfh $cmdstr;
27
28   my $res = <$outfh>;
29         if (defined $res) {
30                 chop($res);
31         } 
32
33
34   print $cmdfh "exit\n";
35   close $outfh;
36
37   # Give test_driver time to finish
38   sleep 0.000001;
39
40   exit $exit_num;
41 }
42
43
44 # Output the given command and make sure that the exit
45 # code for the command was valid
46 sub send_cmd
47 {
48   my ($cmdfh, $outfh, $cmd, $cmdstr) = @_;
49
50   print $cmdfh $cmdstr;
51
52   my $res = <$outfh>;
53         if (defined $res) {
54                 chop($res);
55         } else {
56                 print_and_exit($cmdfh, $outfh, 1, "ERROR! Cmd $cmdstr returned null value!\n");
57         }
58
59   if ($res ne "0000 ") {
60     print_and_exit($cmdfh, $outfh, 1, "ERROR! Command $cmd failed with code $res\n");
61   }
62 }
63
64 # Check the return value from the last libsysio call
65 sub verify_cmd
66 {
67
68   my ($cmdfh, $outfh, $cmd) = @_;
69
70   # Verify the system call's output
71   my $cmdstr = 'PRINT $$';
72   $cmdstr .= "\n";
73   send_cmd($cmdfh, $outfh, "PRINT", $cmdstr);  
74
75   my $res = <$outfh>;
76         if (defined $res) {
77                 chop($res);
78         } else {
79                 print_and_exit($cmdfh, $outfh, 1, "ERROR! Cmd $cmdstr returned null value!\n");
80         }
81
82   if ($res eq "0xffffffff") {
83      
84     # Get the errno
85     $cmdstr = 'PRINT $errno';
86     $cmdstr .= "\n";
87     send_cmd($cmdfh, $outfh, "PRINT", $cmdstr);
88     
89     my $err = <$outfh>;
90                 if (defined $err) {
91                         chop($err);
92                 } else {
93                         print_and_exit($cmdfh, $outfh, 1, "ERROR! Cmd $cmdstr returned null value!\n");
94                 }
95
96     print_and_exit($cmdfh, $outfh, 1, "ERROR!  $cmd returned $err\n");
97   }
98   return $res;
99 }
100
101 # Compares two numbers.  Output error message and exit if
102 # they differ
103 sub cmp_nums
104 {
105   my ($cmdfh, $outfh, $ionum, $pnum, $desc) = @_;
106
107   my $str;
108   if (!defined($ionum)) {
109       print_and_exit($cmdfh, $outfh, 1, "ERROR! ionum for $desc undefined");
110   } elsif (!defined($pnum)) {
111       print_and_exit($cmdfh, $outfh, 1, "ERROR! pnum for $desc undefined");
112   }
113   if ($ionum != $pnum) {
114     my $str = sprintf("ERROR!  Sysio's number %x does not match Perl's (%x)\n", 
115                       $ionum, $pnum);
116     $str = sprintf("%s Numbers were %s\n", $str, $desc);
117     print_and_exit($cmdfh, $outfh, 1, $str);
118   }
119 }
120
121 sub get_type
122 {
123   my $mode = $_[0];
124   my $t = '?';
125
126   if (S_ISDIR($mode)) {
127     $t = 'd';
128   } elsif (S_ISCHR($mode)) {
129     $t = 'c';
130   } elsif (S_ISBLK($mode)) {
131     $t = 'b';
132   } elsif (S_ISREG($mode)) {
133     $t = 'f';
134   } elsif (S_ISFIFO($mode)) {
135     $t = 'p';
136   } elsif (S_ISLNK($mode)) {
137     $t = 'S';
138   } elsif (S_ISSOCK($mode)) {
139     $t = 's';
140   }
141
142   return $t;
143 }
144
145 END{}
146
147 1;