use
Cwd;
BEGIN { use_ok(
'Win32::StreamNames'
) };
sub
create_file ($)
{
my
(
$file
) =
@_
;
open
(FILE,
'>'
,
$file
) or
die
"Unable to open $file: $!"
;
print
FILE
"This is $file\n"
;
close
FILE;
}
sub
create_empty_file ($)
{
my
(
$file
) =
@_
;
open
(FILE,
'>'
,
$file
) or
die
"Unable to open $file: $!"
;
close
FILE;
}
is($^O,
'MSWin32'
,
'OS is Windows'
);
my
$dir
= $0;
$dir
=~ s/([\/\\]).*$/$1/;
my
$sRootPath
= (
split
/[\\\/]/, getcwd())[0].
'\\'
;
my
$osVolName
=
' '
x 260;
my
$osFsType
=
' '
x 260;
GetVolumeInformation(
$sRootPath
,
$osVolName
, 260, [], [], [],
$osFsType
, 260 );
is(
$osFsType
,
"NTFS"
) or diag
"File system $osFsType unsupported"
;
$^E = 0;
my
$file
=
$dir
.
'test.txt'
;
my
@list
;
create_file (
$file
);
create_file (
$file
.
':stream1'
);
create_file (
$file
.
':stream2'
);
create_file (
$file
.
':stream3'
);
create_file (
$file
.
':stream4'
);
ok(-f
$file
,
"$file exists ok"
);
ok(-r
$file
,
"$file exists ok"
);
@list
= StreamNames(
$file
);
is(0+$^E, 0,
'os error ok'
) or diag (
"$^E: Value of \$file is: $file<<\n"
);
for
my
$stream
(
@list
)
{
ok(
open
(HANDLE,
$file
.
$stream
),
"Stream $file$stream ok"
) or diag (
"$file$stream: $!"
);
close
HANDLE;
}
is(
@list
, 4,
'Number of streams'
) or diag (
"@list"
);
unlink
$file
;
@list
= StreamNames(
'.'
);
ok(
@list
== 0,
'Empty directory list'
) or diag (
"@list"
);
is (0+$^E, 0,
'Attempt to open a directory'
);
@list
= StreamNames(
'gash.zzz'
);
ok (!
@list
,
'Empty list on ENOENT'
) or diag (
"@list"
);
is (0+$^E, 2,
'ENOENT'
);
$file
=
$dir
.
'ThisIsAveryLongFileNameWhichGoesOnAndOn'
;
create_file (
$file
);
create_file (
$file
.
':AndThisIsAlsoAVeryLongStreamNameAsWell'
);
@list
= StreamNames(
$file
);
is (
"@list"
,
':AndThisIsAlsoAVeryLongStreamNameAsWell:$DATA'
) or diag (
"@list"
);
is (0+$^E, 0,
'Long one'
);
unlink
$file
;
$file
=
$dir
.
'Embedded space in filename'
;
create_file (
$file
);
create_file (
$file
.
':Embedded space in stream name'
);
@list
= StreamNames(
$file
);
is (
@list
, 1,
'Embedded space in filename, list'
) or diag (
"File: $file, List: @list\n"
);
is (0+$^E, 0,
'Embedded space in filename, oserr'
) or diag (
"@list\n$file"
);
is (
"@list"
,
':Embedded space in stream name:$DATA'
,
'Embedded space in filename, list'
);
unlink
$file
;
$file
=
$dir
.
'NoStreams.txt'
;
create_file (
$file
);
@list
= StreamNames(
$file
);
ok (!
@list
,
'Empty list on no streams'
) or diag (
"@list"
);
is (0+$^E, 0,
'No streams, oserr'
) or diag (
"@list\n$file"
);
unlink
$file
;
my
$testdir
=
$dir
.
'TestDir'
;
mkdir
$testdir
||
die
"Unable to create $testdir: $!"
;
$file
=
"$testdir:dirADStest"
;
create_file (
$file
);
@list
= StreamNames(
$testdir
);
is (
@list
, 1,
'dirADStest, list'
) or diag (
"File: $file, List: @list\n"
);
is (0+$^E, 0,
'dir ADS test, oserr'
) or diag (
"@list\n$file"
);
is (
"@list"
,
':dirADStest:$DATA'
,
'dir ADS test, list'
);
unlink
$file
;
$file
=
$dir
.
'test.txt'
;
create_empty_file (
$file
.
':streamE'
);
@list
= StreamNames(
$file
);
is(0+$^E, 0,
'os error ok'
) or diag (
"$^E: Value of \$file is: $file<<\n"
);
ok(
@list
== 1,
'Empty stream list'
) or diag (
"list:<@list>"
);
unlink
$file
;
my
$file1
=
"$testdir:dirADStest.name1"
;
create_file (
$file1
);
my
$file2
=
"$testdir:dirADStest1.name2"
;
create_empty_file (
$file2
);
my
$file3
=
"$testdir:dirADStest2.name3"
;
create_file (
$file3
);
my
$file4
=
"$testdir:dirADStest3.name4"
;
create_file (
$file4
);
@list
= StreamNames(
$testdir
);
is(0+$^E, 0,
'os error ok'
) or diag (
"$^E: Value of \$file is: $file<<\n"
);
ok(
@list
== 5,
'Mixed stream list'
) or diag (
"list:<@list>"
);
unlink
(
$file1
,
$file2
,
$file2
,
$file4
);
rmdir
$testdir
;