1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
# please insert nothing before this line: -*- mode: cperl; cperl-indent-level: 4; cperl-continued-statement-offset: 4; indent-tabs-mode: nil -*-
package TestAPR::perlio;
# to see what happens inside the io layer, assuming that you built
# mod_perl with MP_TRACE=1, run:
# env MOD_PERL_TRACE=o t/TEST -v -trace=debug apr/perlio
use strict;
use warnings FATAL => 'all';
use Apache::Test;
use Apache::TestUtil;
use Fcntl ();
use File::Spec::Functions qw(catfile);
use Apache2::Const -compile => qw(OK CRLF);
#XXX: APR::LARGE_FILES_CONFLICT constant?
#XXX: you can set to zero if largefile support is not enabled in Perl
use constant LARGE_FILES_CONFLICT => 1;
# apr_file_dup has a bug on win32,
# should be fixed in apr 0.9.4 / httpd-2.0.48
require Apache2::Build;
use constant APR_WIN32_FILE_DUP_BUG =>
Apache2::Build::WIN32() && !have_min_apache_version('2.0.48');
sub handler {
my $r = shift;
my $tests = 22;
$tests += 3 unless LARGE_FILES_CONFLICT;
$tests += 1 unless APR_WIN32_FILE_DUP_BUG;
require APR::PerlIO;
plan $r, tests => $tests,
need { "This Perl build doesn't support PerlIO layers" =>
APR::PerlIO::PERLIO_LAYERS_ARE_ENABLED() };
my $vars = Apache::Test::config()->{vars};
my $dir = catfile $vars->{documentroot}, "perlio";
t_mkdir($dir);
my $sep = "-- sep --\n";
my @lines = ("This is a test: $$\n", "test line --sep two\n");
my $expected = $lines[0];
my $expected_all = join $sep, @lines;
# write file
my $file = catfile $dir, "test";
t_debug "open file $file for writing";
my $foo = "bar";
open my $fh, ">:APR", $file, $r->pool
or die "Cannot open $file for writing: $!";
ok ref($fh) eq 'GLOB';
t_debug "write to a file:\n$expected\n";
print $fh $expected_all;
close $fh;
# open() failure test
{
# workaround for locale setups where the error message may be
# in a different language
open my $fh, "perlio_this_file_cannot_exist";
my $errno_string = "$!";
# non-existent file
my $file = "/this/file/does/not/exist";
if (open my $fh, "<:APR", $file, $r->pool) {
t_debug "must not be able to open $file!";
ok 0;
close $fh;
}
else {
ok t_cmp("$!",
$errno_string,
"expected failure");
}
}
# seek/tell() tests
unless (LARGE_FILES_CONFLICT) {
open my $fh, "<:APR", $file, $r->pool
or die "Cannot open $file for reading: $!";
# read the whole file so we can test the buffer flushed
# correctly on seek.
my $dummy = join '', <$fh>;
# Fcntl::SEEK_SET()
my $pos = 3; # rewinds after reading 6 chars above
seek $fh, $pos, Fcntl::SEEK_SET();
my $got = tell($fh);
ok t_cmp($got,
$pos,
"seek/tell the file Fcntl::SEEK_SET");
# Fcntl::SEEK_CUR()
my $step = 10;
$pos = tell($fh) + $step;
seek $fh, $step, Fcntl::SEEK_CUR();
$got = tell($fh);
ok t_cmp($got,
$pos,
"seek/tell the file Fcntl::SEEK_CUR");
# Fcntl::SEEK_END()
$pos = -s $file;
seek $fh, 0, Fcntl::SEEK_END();
$got = tell($fh);
ok t_cmp($got,
$pos,
"seek/tell the file Fcntl::SEEK_END");
close $fh;
}
# read() tests
{
open my $fh, "<:APR", $file, $r->pool
or die "Cannot open $file for reading: $!";
# basic open test
ok ref($fh) eq 'GLOB';
# basic single line read
ok t_cmp(scalar(<$fh>),
$expected,
"single line read");
# slurp mode
seek $fh, 0, Fcntl::SEEK_SET(); # rewind to the start
local $/;
ok t_cmp(scalar(<$fh>),
$expected_all,
"slurp file");
# test ungetc (a long sep requires read ahead)
seek $fh, 0, Fcntl::SEEK_SET(); # rewind to the start
local $/ = $sep;
my @got_lines = <$fh>;
my @expect = ($lines[0] . $sep, $lines[1]);
ok t_cmp(\@got_lines,
\@expect,
"custom complex input record sep read");
close $fh;
}
# eof() tests
{
open my $fh, "<:APR", $file, $r->pool
or die "Cannot open $file for reading: $!";
ok t_cmp(0,
int eof($fh), # returns false, not 0
"not end of file");
# go to the end and read so eof will return 1
seek $fh, 0, Fcntl::SEEK_END();
my $received = <$fh>;
t_debug($received);
ok t_cmp(eof($fh),
1,
"end of file");
close $fh;
}
# dup() test
{
open my $fh, "<:APR", $file, $r->pool
or die "Cannot open $file for reading: $!";
open my $dup_fh, "<&:APR", $fh
or die "Cannot dup $file for reading: $!";
close $fh;
ok ref($dup_fh) eq 'GLOB';
my $received = <$dup_fh>;
close $dup_fh;
unless (APR_WIN32_FILE_DUP_BUG) {
ok t_cmp($received,
$expected,
"read/write a dupped file");
}
}
# unbuffered write
{
open my $wfh, ">:APR", $file, $r->pool
or die "Cannot open $file for writing: $!";
open my $rfh, "<:APR", $file, $r->pool
or die "Cannot open $file for reading: $!";
my $expected = "This is an un buffering write test";
# unbuffer
my $oldfh = select($wfh); $| = 1; select($oldfh);
print $wfh $expected; # must be flushed to disk immediately
ok t_cmp(scalar(<$rfh>),
$expected,
"file unbuffered write");
# buffer up
$oldfh = select($wfh); $| = 0; select($oldfh);
print $wfh $expected; # should be buffered up and not flushed
ok t_cmp(scalar(<$rfh>),
undef,
"file buffered write");
close $wfh;
close $rfh;
}
# tests reading and writing text and binary files
{
for my $file ('MoonRise.jpeg', 'redrum.txt') {
my $in = catfile $dir, $file;
my $out = catfile $dir, "$file.out";
my ($apr_content, $perl_content);
open my $rfh, "<:APR", $in, $r->pool
or die "Cannot open $in for reading: $!";
{
local $/;
$apr_content = <$rfh>;
}
close $rfh;
open my $pfh, "<", $in
or die "Cannot open $in for reading: $!";
binmode($pfh);
{
local $/;
$perl_content = <$pfh>;
}
close $pfh;
ok t_cmp(length $apr_content,
length $perl_content,
"testing data size of $file");
open my $wfh, ">:APR", $out, $r->pool
or die "Cannot open $out for writing: $!";
print $wfh $apr_content;
close $wfh;
ok t_cmp(-s $out,
-s $in,
"testing file size of $file");
unlink $out;
}
}
# tests for various CRLF and utf-8 issues
{
my $scratch = catfile $dir, 'scratch.dat';
my $text;
my $count = 2000;
open my $wfh, ">:crlf", $scratch
or die "Cannot open $scratch for writing: $!";
print $wfh 'a' . ((('a' x 14) . "\n") x $count);
close $wfh;
open my $rfh, "<:APR", $scratch, $r->pool
or die "Cannot open $scratch for reading: $!";
{
local $/;
$text = <$rfh>;
}
close $rfh;
ok t_cmp(count_chars($text, Apache2::Const::CRLF),
$count,
'testing for presence of \015\012');
ok t_cmp(count_chars($text, "\n"),
$count,
'testing for presence of \n');
open $wfh, ">:APR", $scratch, $r->pool
or die "Cannot open $scratch for writing: $!";
print $wfh 'a' . ((('a' x 14) . Apache2::Const::CRLF) x $count);
close $wfh;
open $rfh, "<:APR", $scratch, $r->pool
or die "Cannot open $scratch for reading: $!";
{
local $/;
$text = <$rfh>;
}
close $rfh;
ok t_cmp(count_chars($text, Apache2::Const::CRLF),
$count,
'testing for presence of \015\012');
ok t_cmp(count_chars($text, "\n"),
$count,
'testing for presence of \n');
open $rfh, "<:crlf", $scratch
or die "Cannot open $scratch for reading: $!";
{
local $/;
$text = <$rfh>;
}
close $rfh;
ok t_cmp(count_chars($text, Apache2::Const::CRLF),
0,
'testing for presence of \015\012');
ok t_cmp(count_chars($text, "\n"),
$count,
'testing for presence of \n');
my $utf8 = "\x{042F} \x{0432}\x{0430}\x{0441} \x{043B}\x{044E}";
open $wfh, ">:APR", $scratch, $r->pool
or die "Cannot open $scratch for writing: $!";
binmode($wfh, ':utf8');
print $wfh $utf8;
close $wfh;
open $rfh, "<:APR", $scratch, $r->pool
or die "Cannot open $scratch for reading: $!";
binmode($rfh, ':utf8');
{
local $/;
$text = <$rfh>;
}
close $rfh;
ok t_cmp($text,
$utf8,
'utf8 binmode test');
unlink $scratch;
}
# XXX: need tests
# - for stdin/out/err as they are handled specially
# XXX: tmpfile is missing:
# consider to use 5.8's syntax:
# open $fh, "+>", undef;
# cleanup: t_mkdir will remove the whole tree including the file
Apache2::Const::OK;
}
sub count_chars {
my ($text, $chars) = @_;
my $seen = 0;
$seen++ while $text =~ /$chars/g;
return $seen;
}
1;
|