Whamcloud - gitweb
b=11512
[fs/lustre-release.git] / libsysio / tests / test_symlink.pl
1 #!/usr/bin/perl -w
2
3 #
4 # symlink test: Verify that symbolic links work
5 #
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_symlink.pl [-alpha] <src> <dest>: Create a symlink from src to dest\n";
17   exit(-1);
18 }
19
20 sub clean_exit
21 {
22   my ($cmdfh, $outfh, $exit_num, $exit_str) = @_;
23
24   print STDOUT "$exit_str";
25
26   # Free buffers
27   my $cmdstr =  'FREE $srcbuf'."\n";
28
29   print $cmdfh $cmdstr;
30
31   my $res = <$outfh>;
32   chop($res);
33   if ($res ne "0000 ") {
34     print STDOUT "ERROR! Failed to free srcbuf (code $res)\n";
35   }
36
37   $cmdstr =  'FREE $destbuf'."\n";
38
39   print $cmdfh $cmdstr;
40
41   $res = <$outfh>;
42   chop($res);
43   if ($res ne "0000 ") {
44     print STDOUT "ERROR! Failed to free destbuf (code $res)\n";
45   }
46
47   print $cmdfh "exit\n";
48   close $outfh;
49
50   # Give test_driver time to finish
51   sleep 0.000001;
52
53   exit $exit_num;
54 }
55
56 sub process_cmd
57 {
58   my ($src, $dest, $is_alpha) = @_;
59   
60   # Get tests directory
61   my $testdir = $FindBin::Bin;
62
63   eval {
64       if ($is_alpha == 0) {
65           open2(\*OUTFILE, \*CMDFILE, "$testdir/test_driver --np");
66       } else {
67           open2(\*OUTFILE, \*CMDFILE, "yod -quiet -sz 1 $testdir/test_driver --np");
68       }
69   };
70
71   if ($@) {
72     if ($@ =~ /^open2/) {
73       warn "open2 failed: $!\n$@\n";
74       return;
75     }
76     die;
77
78   }
79
80   my $outfh = \*OUTFILE;
81   my $cmdfh = \*CMDFILE;
82
83   if ($is_alpha == 0) {
84     helper::send_cmd($cmdfh, $outfh, "init", "CALL init\n");
85   }
86   
87   # Get the filesize of src
88   my $size = -s $src;
89   my $bufsize;
90
91   if ( $size > 1024) { # Arbitrary limit
92     $bufsize = 1024;
93   } else {
94     $bufsize = $size;
95   }
96
97   # Create the symbolic link from src to dest
98   my $cmdstr = "CALL symlink $src $dest\n";
99   helper::send_cmd($cmdfh, $outfh, "open", $cmdstr);
100
101   helper::verify_cmd($cmdfh, $outfh, "symlink");
102
103   # Open src 
104   $cmdstr = '$src = CALL open '."$src O_RDONLY\n";
105   helper::send_cmd($cmdfh, $outfh, "open", $cmdstr);
106
107   # Open dest 
108   $cmdstr = '$dest = CALL open '."$dest O_RDONLY\n";
109   helper::send_cmd($cmdfh, $outfh, "open", $cmdstr);
110   
111   my $res = helper::verify_cmd($cmdfh, $outfh, "open $dest");
112
113   # Allocate buffer for src
114   $cmdstr = '$srcbuf = ALLOC '."$bufsize\n";
115   helper::send_cmd($cmdfh, $outfh, "ALLOC", $cmdstr);
116
117   # Allocate buffer for dest
118   $cmdstr = '$destbuf = ALLOC '."$bufsize\n";
119   helper::send_cmd($cmdfh, $outfh, "ALLOC", $cmdstr);
120
121
122   # Read size bytes from src and dest, then compare them and verify they
123   # are the same
124   $cmdstr = 'CALL read $src $srcbuf '."$bufsize\n";
125   helper::send_cmd($cmdfh, $outfh, "read $src", $cmdstr);
126     
127   $res = helper::verify_cmd($cmdfh, $outfh, "read $src");
128   my $readb = oct($res);
129
130   # Now read $readb from dest
131   $cmdstr = 'CALL read $dest $destbuf '."$readb\n";
132   helper::send_cmd($cmdfh, $outfh, "read $dest", $cmdstr);
133     
134   $res = helper::verify_cmd($cmdfh, $outfh, "read $dest");
135
136   my $errstr;
137   if ($readb != oct($res)) {
138       $errstr = "ERROR!  Read $readb bytes from src but only $res bytes from dest\n";
139       clean_exit($cmdfh, $outfh, 1, $errstr);
140   }
141
142   # Compare the two buffers
143   $cmdstr = 'CALL cmpstr $srcbuf $destbuf'."\n";
144   helper::send_cmd($cmdfh, $outfh, "cmpstr", $cmdstr);
145
146   # Verify that it returned an error
147   $cmdstr = 'PRINT $$';
148   $cmdstr .= "\n";
149   helper::send_cmd($cmdfh, $outfh, "PRINT", $cmdstr);  
150
151   $res = <$outfh>;
152   chop($res);
153
154   $res = helper::verify_cmd($cmdfh, $outfh, "cmpstr");
155   $res = oct($res);
156   if ($res != 0) {
157       $errstr = "ERROR! Buffers from $src and $dest do not match\n";
158       clean_exit($cmdfh, $outfh, 1, $errstr);
159   }
160
161   # Clean up
162   $cmdstr = 'CALL close $src'."\n";
163   helper::send_cmd($cmdfh, $outfh, "close", $cmdstr);
164   $cmdstr = 'CALL close $dest'."\n";
165   helper::send_cmd($cmdfh, $outfh, "close", $cmdstr);
166
167  # Clear out destbuf
168   $cmdstr = 'CALL clear $destbuf'."\n";
169   helper::send_cmd($cmdfh, $outfh, "CLEAR", $cmdstr);
170
171   # Now remove the symbolic link and make sure everything stays the same
172   
173   # Remove the link (this assumes the link is not in incore)
174   $cmdstr = "CALL unlink $dest\n";
175   helper::send_cmd($cmdfh, $outfh, "unlink", $cmdstr);
176   helper::verify_cmd($cmdfh, $outfh, "unlink");
177
178   # Attempt to open the symbolic link.  This should return an error
179   $cmdstr = 'CALL open '."$dest O_RDONLY\n";
180   helper::send_cmd($cmdfh, $outfh, "open", $cmdstr);
181   
182   # Verify that it returned an error
183   $cmdstr = 'PRINT $$';
184   $cmdstr .= "\n";
185   helper::send_cmd($cmdfh, $outfh, "PRINT", $cmdstr);  
186
187   $res = <$outfh>;
188   chop($res);
189
190   if ($res ne "0xffffffff") {
191       $errstr = "ERROR! Open on $dest succeeded (should have failed)\n";
192       clean_exit($cmdfh, $outfh, 1, $errstr);
193   }
194
195   # Now read from the src again and make sure it matches the original
196
197   # Open src 
198   $cmdstr = '$src2 = CALL open '."$src O_RDONLY\n";
199   helper::send_cmd($cmdfh, $outfh, "open", $cmdstr);
200   helper::verify_cmd($cmdfh, $outfh, "open $src(2)");
201
202   $cmdstr = 'CALL read $src2 $destbuf '."$readb\n";
203   helper::send_cmd($cmdfh, $outfh, "read $src(2)", $cmdstr);
204     
205   $res = helper::verify_cmd($cmdfh, $outfh, "read $src(2)");
206
207   if ($readb != oct($res)) {
208       $errstr = "ERROR!  Read $readb bytes from src originally but now only $res bytes\n";
209       clean_exit($cmdfh, $outfh, 1, $errstr);
210   }
211
212   # Compare the two buffers
213   $cmdstr = 'CALL cmpstr $srcbuf $destbuf'."\n";
214   helper::send_cmd($cmdfh, $outfh, "cmpstr", $cmdstr);
215   $res = helper::verify_cmd($cmdfh, $outfh, "cmpstr");
216   $res = oct($res);
217   if ($res != 0) {
218       $errstr = "ERROR! Original buffers from $src and new buf do not match\n";
219       clean_exit($cmdfh, $outfh, 1, $errstr);
220   }
221
222   # Clean up
223   $cmdstr = 'CALL close $src2'."\n";
224   helper::send_cmd($cmdfh, $outfh, "close", $cmdstr);
225
226   clean_exit($cmdfh, $outfh, 0, "Symlink test successful\n");
227   exit 0;
228 }
229
230 my $currarg = 0;
231 my $is_alpha = 0;
232
233 if (@ARGV < 2) {
234   usage;
235 } elsif (@ARGV > 2 ) {
236   if ($ARGV[$currarg++] eq "-alpha") {
237     $is_alpha = 1;
238   }
239 }
240
241 my $src = $ARGV[$currarg++];
242 my $dest = $ARGV[$currarg];
243
244 process_cmd($src, $dest, $is_alpha);
245
246
247 exit 0;