2 # Before `make install' is performed this script should be runnable with
3 # `make test'. After `make install' it should work as `perl test.pl'
5 ######################### We start with some black magic to print on failure.
7 BEGIN { $|= 1; print "1..267\n"; }
8 END {print "not ok 1\n" unless $loaded;}
10 # Win32API::File does an implicit "require Win32", but
11 # the ../lib directory in @INC will no longer work once
12 # we chdir() into the TEMP directory.
15 use Win32API::File qw(:ALL);
19 ######################### End of black magic.
26 $temp= $ENV{"TEMP"} unless -d $temp;
27 $temp= "C:/Temp" unless -d $temp;
28 $temp= "." unless -d $temp;
31 $ENV{WINDIR} = $ENV{SYSTEMROOT} if not exists $ENV{WINDIR};
34 or die "# Can't cd to temp directory, $temp: $!\n";
37 print "# deleting $temp\\$dir\\*\n" if glob "$dir/*";
43 rmdir $dir or die "Could not rmdir $dir: $!";
46 or die "# Can't create temp dir, $temp/$dir: $!\n";
47 print "# chdir $temp\\$dir\n";
49 or die "# Can't cd to my dir, $temp/$dir: $!\n";
51 $h1= createFile( "ReadOnly.txt", "r", { Attributes=>"r" } );
52 $ok= ! $h1 && fileLastError() =~ /not find the file?/i;
53 $ok or print "# ","".fileLastError(),"\n";
54 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 2
55 if( ! $ok ) { CloseHandle($h1); unlink("ReadOnly.txt"); }
57 $ok= $h1= createFile( "ReadOnly.txt", "wcn", { Attributes=>"r" } );
58 $ok or print "# ",fileLastError(),"\n";
59 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 3
61 $ok= WriteFile( $h1, "Original text\n", 0, [], [] );
62 $ok or print "# ",fileLastError(),"\n";
63 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 4
65 $h2= createFile( "ReadOnly.txt", "rcn" );
66 $ok= ! $h2 && fileLastError() =~ /file exists?/i;
67 $ok or print "# ",fileLastError(),"\n";
68 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 5
69 if( ! $ok ) { CloseHandle($h2); }
71 $h2= createFile( "ReadOnly.txt", "rwke" );
72 $ok= ! $h2 && fileLastError() =~ /access is denied?/i;
73 $ok or print "# ",fileLastError(),"\n";
74 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 6
75 if( ! $ok ) { CloseHandle($h2); }
77 $ok= $h2= createFile( "ReadOnly.txt", "r" );
78 $ok or print "# ",fileLastError(),"\n";
79 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 7
81 $ok= SetFilePointer( $h1, length("Original"), [], FILE_BEGIN );
82 $ok or print "# ",fileLastError(),"\n";
83 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 8
85 $ok= WriteFile( $h1, "ly was other text\n", 0, $len, [] )
86 && $len == length("ly was other text\n");
87 $ok or print "# <$len> should be <",
88 length("ly was other text\n"),">: ",fileLastError(),"\n";
89 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 9
91 $ok= ReadFile( $h2, $text, 80, $len, [] )
92 && $len == length($text);
93 $ok or print "# <$len> should be <",length($text),
94 ">: ",fileLastError(),"\n";
95 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 10
97 $ok= $text eq "Originally was other text\n";
99 $text =~ s/\r/\\r/g; $text =~ s/\n/\\n/g;
100 print "# <$text> should be <Originally was other text\\n>.\n";
102 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 11
104 $ok= CloseHandle($h2);
105 $ok or print "# ",fileLastError(),"\n";
106 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 12
108 $ok= ! ReadFile( $h2, $text, 80, $len, [] )
109 && fileLastError() =~ /handle is invalid?/i;
110 $ok or print "# ",fileLastError(),"\n";
111 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 13
115 $ok= $h1= createFile( "CanWrite.txt", "rw", FILE_SHARE_WRITE,
116 { Create=>CREATE_ALWAYS } );
117 $ok or print "# ",fileLastError(),"\n";
118 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 14
120 $ok= WriteFile( $h1, "Just this and not this", 10, [], [] );
121 $ok or print "# ",fileLastError(),"\n";
122 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 15
124 $ok= $h2= createFile( "CanWrite.txt", "wk", { Share=>"rw" } );
125 $ok or print "# ",fileLastError(),"\n";
126 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 16
128 $ok= OsFHandleOpen( "APP", $h2, "wat" );
129 $ok or print "# ",fileLastError(),"\n";
130 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 17
132 $ok= $h2 == GetOsFHandle( "APP" );
133 $ok or print "# $h2 != ",GetOsFHandle("APP"),"\n";
134 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 18
136 { my $save= select(APP); $|= 1; select($save); }
137 $ok= print APP "is enough\n";
138 $ok or print "# ",fileLastError(),"\n";
139 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 19
141 SetFilePointer($h1, 0, [], FILE_BEGIN) if $^O eq 'cygwin';
143 $ok= ReadFile( $h1, $text, 0, [], [] );
144 $ok or print "# ",fileLastError(),"\n";
145 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 20
147 $ok= $text eq "is enough\r\n";
151 print "# <$text> should be <is enough\\r\\n>\n";
153 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 21
156 if ($^O eq 'cygwin') {
158 $skip = " # skip cygwin can delete open files";
161 unlink("CanWrite.txt");
162 $ok= -e "CanWrite.txt" && $! =~ /permission denied/i;
163 $ok or print "# $!\n";
165 print $ok ? "" : "not ", "ok ", ++$test, "$skip\n"; # ok 22
167 close(APP); # Also does C<CloseHandle($h2)>
168 ## CloseHandle( $h2 );
171 $ok= ! DeleteFile( "ReadOnly.txt" )
172 && fileLastError() =~ /access is denied?/i;
173 $ok or print "# ",fileLastError(),"\n";
174 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 23
176 $ok= ! CopyFile( "ReadOnly.txt", "CanWrite.txt", 1 )
177 && fileLastError() =~ /file exists?/i;
178 $ok or print "# ",fileLastError(),"\n";
179 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 24
181 $ok= ! CopyFile( "CanWrite.txt", "ReadOnly.txt", 0 )
182 && fileLastError() =~ /access is denied?/i;
183 $ok or print "# ",fileLastError(),"\n";
184 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 25
186 $ok= ! MoveFile( "NoSuchFile", "NoSuchDest" )
187 && fileLastError() =~ /not find the file/i;
188 $ok or print "# ",fileLastError(),"\n";
189 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 26
191 $ok= ! MoveFileEx( "NoSuchFile", "NoSuchDest", 0 )
192 && fileLastError() =~ /not find the file/i;
193 $ok or print "# ",fileLastError(),"\n";
194 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 27
196 $ok= ! MoveFile( "ReadOnly.txt", "CanWrite.txt" )
197 && fileLastError() =~ /file already exists?/i;
198 $ok or print "# ",fileLastError(),"\n";
199 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 28
201 $ok= ! MoveFileEx( "ReadOnly.txt", "CanWrite.txt", 0 )
202 && fileLastError() =~ /file already exists?/i;
203 $ok or print "# ",fileLastError(),"\n";
204 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 29
206 $ok= CopyFile( "ReadOnly.txt", "ReadOnly.cp", 1 )
207 && CopyFile( "CanWrite.txt", "CanWrite.cp", 1 );
208 $ok or print "# ",fileLastError(),"\n";
209 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 30
211 $ok= ! MoveFileEx( "CanWrite.txt", "ReadOnly.cp", MOVEFILE_REPLACE_EXISTING )
212 && fileLastError() =~ /access is denied?|cannot create/i;
213 $ok or print "# ",fileLastError(),"\n";
214 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 31
216 $ok= MoveFileEx( "ReadOnly.cp", "CanWrite.cp", MOVEFILE_REPLACE_EXISTING );
217 $ok or print "# ",fileLastError(),"\n";
218 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 32
220 $ok= MoveFile( "CanWrite.cp", "Moved.cp" );
221 $ok or print "# ",fileLastError(),"\n";
222 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 33
224 $ok= ! unlink( "ReadOnly.cp" )
225 && $! =~ /no such file/i
226 && ! unlink( "CanWrite.cp" )
227 && $! =~ /no such file/i;
228 $ok or print "# $!\n";
229 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 34
231 $ok= ! DeleteFile( "Moved.cp" )
232 && fileLastError() =~ /access is denied?/i;
233 $ok or print "# ",fileLastError(),"\n";
234 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 35
236 if ($^O eq 'cygwin') {
237 chmod( 0200 | 07777 & (stat("Moved.cp"))[2], "Moved.cp" );
240 system( "attrib -r Moved.cp" );
243 $ok= DeleteFile( "Moved.cp" );
244 $ok or print "# ",fileLastError(),"\n";
245 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 36
247 $new= SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX;
248 $old= SetErrorMode( $new );
249 $renew= SetErrorMode( $old );
250 $reold= SetErrorMode( $old );
253 $ok or print "# $old != $reold: ",fileLastError(),"\n";
254 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 37
256 $ok= ($renew&$new) == $new;
257 $ok or print "# $new != $renew: ",fileLastError(),"\n";
258 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 38
260 $ok= @drives= getLogicalDrives();
261 $ok && print "# @drives\n";
262 $ok or print "# ",fileLastError(),"\n";
263 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 39
265 $ok= $drives[0] !~ /^[ab]/ || DRIVE_REMOVABLE == GetDriveType($drives[0]);
266 $ok or print "# ",DRIVE_REMOVABLE," != ",GetDriveType($drives[0]),
267 ": ",fileLastError(),"\n";
268 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 40
270 $drive= substr( $ENV{WINDIR}, 0, 3 );
272 $ok= 1 == grep /^\Q$drive\E/i, @drives;
273 $ok or print "# No $drive found in list of drives.\n";
274 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 41
276 $ok= DRIVE_FIXED == GetDriveType( $drive );
278 "# ",DRIVE_FIXED," != ",GetDriveType($drive),": ",fileLastError(),"\n";
279 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 42
281 $ok= GetVolumeInformation( $drive, $vol, 64, $ser, $max, $flag, $fs, 16 );
282 $ok or print "# ",fileLastError(),"\n";
283 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 43
284 $vol= $ser= $max= $flag= $fs= ""; # Prevent warnings.
287 $ok= QueryDosDevice( $drive, $dev, 80 );
288 $ok or print "# $drive: ",fileLastError(),"\n";
290 ( $text= $dev ) =~ s/\0/\\0/g;
291 print "# $drive => $text\n";
293 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 44
295 $bits= GetLogicalDrives();
298 while( $bit & $bits ) {
302 $let= pack( "C", $let + unpack("C","A") ) . ":";
303 print "# Querying undefined $let.\n";
305 $ok= DefineDosDevice( 0, $let, $ENV{WINDIR} );
306 $ok or print "# $let,$ENV{WINDIR}: ",fileLastError(),"\n";
307 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 45
309 $ok= -s $let."/Win.ini" == -s $ENV{WINDIR}."/Win.ini";
310 $ok or print "# ", -s $let."/Win.ini", " vs. ",
311 -s $ENV{WINDIR}."/Win.ini", ": ",fileLastError(),"\n";
312 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 46
314 $ok= DefineDosDevice( DDD_REMOVE_DEFINITION|DDD_EXACT_MATCH_ON_REMOVE,
315 $let, $ENV{WINDIR} );
316 $ok or print "# $let,$ENV{WINDIR}: ",fileLastError(),"\n";
317 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 47
319 $ok= ! -f $let."/Win.ini"
320 && $! =~ /no such file/i;
321 $ok or print "# $!\n";
322 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 48
324 $ok= DefineDosDevice( DDD_RAW_TARGET_PATH, $let, $dev );
326 ( $text= $dev ) =~ s/\0/\\0/g;
327 print "# $let,$text: ",fileLastError(),"\n";
329 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 49
331 $ok= -f $let.substr($ENV{WINDIR},3)."/win.ini";
332 $ok or print "# ",fileLastError(),"\n";
333 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 50
335 $ok= DefineDosDevice( DDD_REMOVE_DEFINITION|DDD_EXACT_MATCH_ON_REMOVE
336 |DDD_RAW_TARGET_PATH, $let, $dev );
337 $ok or print "# $let,$dev: ",fileLastError(),"\n";
338 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 51
340 my $path = $ENV{WINDIR};
341 my $attrs = GetFileAttributes( $path );
342 $ok= $attrs != INVALID_FILE_ATTRIBUTES;
343 $ok or print "# $path gave invalid attribute value, attrs=$attrs: ",fileLastError(),"\n";
344 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 52
346 $ok= ($attrs & FILE_ATTRIBUTE_DIRECTORY);
347 $ok or print "# $path not a directory, attrs=$attrs: ",fileLastError(),"\n";
348 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 53
351 $attrs = GetFileAttributes( $path );
352 $ok= $attrs != INVALID_FILE_ATTRIBUTES;
353 $ok or print "# $path gave invalid attribute value, attrs=$attrs: ",fileLastError(),"\n";
354 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 54
356 $ok= !($attrs & FILE_ATTRIBUTE_DIRECTORY);
357 $ok or print "# $path is a directory, attrs=$attrs: ",fileLastError(),"\n";
358 print $ok ? "" : "not ", "ok ", ++$test, "\n"; # ok 55
362 # GetVolumeInformation
364 #Add a drive letter that points to our temp directory
365 #Add a drive letter that points to the drive our directory is in
368 # get first drive letters and use to test disk and storage IOCTLs
369 # "//./PhysicalDrive0"
373 my @consts= @Win32API::File::EXPORT_OK;
374 @consts{@consts}= @consts;
376 my( @noargs, %noargs )= qw(
377 attrLetsToBits fileLastError getLogicalDrives GetLogicalDrives );
378 @noargs{@noargs}= @noargs;
380 foreach $func ( @{$Win32API::File::EXPORT_TAGS{Func}} ) {
381 delete $consts{$func};
382 if( defined( $noargs{$func} ) ) {
383 $ok= ! eval("$func(0,0)") && $@ =~ /(::|\s)_?${func}A?[(:\s]/;
385 $ok= ! eval("$func()") && $@ =~ /(::|\s)_?${func}A?[(:\s]/;
387 $ok or print "# $func: $@\n";
388 print $ok ? "" : "not ", "ok ", ++$test, "\n";
391 foreach $func ( @{$Win32API::File::EXPORT_TAGS{FuncA}},
392 @{$Win32API::File::EXPORT_TAGS{FuncW}} ) {
393 $ok= ! eval("$func()") && $@ =~ /::_?${func}\(/;
394 delete $consts{$func};
395 $ok or print "# $func: $@\n";
396 print $ok ? "" : "not ", "ok ", ++$test, "\n";
399 foreach $const ( keys(%consts) ) {
400 $ok= eval("my \$x= $const(); 1");
401 $ok or print "# Constant $const: $@\n";
402 print $ok ? "" : "not ", "ok ", ++$test, "\n";
406 if (-e "$dir/ReadOnly.txt") {
407 chmod 0777, "$dir/ReadOnly.txt";
408 unlink "$dir/ReadOnly.txt";
410 unlink "$dir/CanWrite.txt" if -e "$dir/CanWrite.txt";