#!perl -w # This file is for testing file functionality that is independent of # the file format use strict; use Test::More; use Imager; -d "testout" or mkdir "testout"; Imager->open_log(log => "testout/t1000files.log"); SKIP: { # Test that i_test_format_probe() doesn't pollute stdout # Initally I tried to write this test using open to redirect files, # but there was a buffering problem that made it so the data wasn't # being written to the output file. This external perl call avoids # that problem my $test_script = 'testout/t1000files_probe.pl'; # build a temp test script to use ok(open(SCRIPT, "> $test_script"), "open test script") or skip("no test script $test_script: $!", 2); print SCRIPT <<'PERL'; #!perl use Imager; use strict; my $file = shift or die "No file supplied"; open FH, "< $file" or die "Cannot open file: $!"; binmode FH; my $io = Imager::io_new_fd(fileno(FH)); Imager::i_test_format_probe($io, -1); PERL close SCRIPT; my $perl = $^X; $perl = qq/"$perl"/ if $perl =~ / /; print "# script: $test_script\n"; my $cmd = "$perl -Mblib $test_script t/200-file/100-files.t"; print "# command: $cmd\n"; my $out = `$cmd`; is($?, 0, "command successful"); is($out, '', "output should be empty"); } # test the file limit functions # by default the limits are zero (unlimited) print "# image file limits\n"; is_deeply([ Imager->get_file_limits() ], [0, 0, 0x40000000 ], "check defaults"); ok(Imager->set_file_limits(width=>100), "set only width"); is_deeply([ Imager->get_file_limits() ], [100, 0, 0x40000000 ], "check width set"); ok(Imager->set_file_limits(height=>150, bytes=>10000), "set height and bytes"); is_deeply([ Imager->get_file_limits() ], [ 100, 150, 10000 ], "check all values now set"); ok(Imager->check_file_limits(width => 100, height => 30), "check 100 x 30 (def channels, sample_size) ok") or diag(Imager->errstr); ok(Imager->check_file_limits(width => 100, height => 100, channels => 1), "check 100 x 100 x 1 (def sample_size) ok") or diag(Imager->errstr); ok(Imager->check_file_limits(width => 100, height => 100, channels => 1), "check 100 x 100 x 1 (def sample_size) ok") or diag(Imager->errstr); ok(!Imager->check_file_limits(width => 100, height => 100, channels => 1, sample_size => "float"), "check 100 x 100 x 1 x float should fail"); ok(!Imager->check_file_limits(width => 100, height => 100, channels => 0), "0 channels should fail"); is(Imager->errstr, "file size limit - channels 0 out of range", "check error message"); ok(!Imager->check_file_limits(width => 0, height => 100), "0 width should fail"); is(Imager->errstr, "file size limit - image width of 0 is not positive", "check error message"); ok(!Imager->check_file_limits(width => 100, height => 0), "0 height should fail"); is(Imager->errstr, "file size limit - image height of 0 is not positive", "check error message"); ok(!Imager->check_file_limits(width => 10, height => 10, sample_size => 0), "0 sample_size should fail"); is(Imager->errstr, "file size limit - sample_size 0 out of range", "check error message"); ok(!Imager->check_file_limits(width => 10, height => 10, sample_size => 1000), "1000 sample_size should fail"); is(Imager->errstr, "file size limit - sample_size 1000 out of range", "check error message"); ok(Imager->set_file_limits(reset=>1, height => 99), "set height and reset"); is_deeply([ Imager->get_file_limits() ], [ 0, 99, 0x40000000 ], "check only height is set"); ok(Imager->set_file_limits(reset=>1), "just reset"); is_deeply([ Imager->get_file_limits() ], [ 0, 0, 0x40000000 ], "check all are reset"); # bad parameters is_deeply([ Imager->check_file_limits() ], [], "missing size paramaters"); is(Imager->errstr, "check_file_limits: width must be defined", "check message"); is_deeply([ Imager->check_file_limits(width => 100.5) ], [], "non-integer parameter"); is(Imager->errstr, "check_file_limits: width must be a positive integer", "check message"); # test error handling for loading file handers { # first, no module at all { my $data = "abc"; ok(!Imager->new(data => $data, filetype => "unknown"), "try to read an unknown file type"); like(Imager->errstr, qr(^format 'unknown' not supported - formats .* - Can't locate Imager/File/UNKNOWN.pm or Imager/File/UNKNOWNReader.pm$), "check error message"); } { my $data; my $im = Imager->new(xsize => 10, ysize => 10); ok(!$im->write(data => \$data, type => "unknown"), "try to write an unknown file type"); like($im->errstr, qr(^format 'unknown' not supported - formats .* - Can't locate Imager/File/UNKNOWN.pm or Imager/File/UNKNOWNWriter.pm$), "check error message"); } push @INC, "t/t1000lib"; { my $data = "abc"; ok(!Imager->new(data => $data, filetype => "bad"), "try to read an bad (other load failure) file type"); like(Imager->errstr, qr(^format 'bad' not supported - formats .* available for reading - This module fails to load loading Imager/File/BAD.pm$), "check error message"); } { my $data; my $im = Imager->new(xsize => 10, ysize => 10); ok(!$im->write(data => \$data, type => "bad"), "try to write an bad file type"); like($im->errstr, qr(^format 'bad' not supported - formats .* available for writing - This module fails to load loading Imager/File/BAD.pm$), "check error message"); } } { # test empty image handling for write()/write_multi() my $empty = Imager->new; my $data; ok(!$empty->write(data => \$data, type => "pnm"), "fail to write an empty image"); is($empty->errstr, "write: empty input image", "check error message"); my $good = Imager->new(xsize => 1, ysize => 1); ok(!Imager->write_multi({ data => \$data, type => "pnm" }, $good, $empty), "fail to write_multi an empty image"); is(Imager->errstr, "write_multi: empty input image (image 2)"); Imager->_set_error(""); my $not_imager = bless {}, "Foo"; ok(!Imager->write_multi({ data => \$data, type => "pnm" }, $not_imager), "fail to write_multi() a non-Imager object"); is(Imager->errstr, "write_multi: image 1 is not an Imager image object", "check message"); ok(!Imager->write_multi({ data => \$data, type => "pnm" }, "Imager"), "fail to write_multi() 'Imager' string"); is(Imager->errstr, "write_multi: image 1 is not an Imager image object", "check message"); } # check file type probe probe_ok("49492A41", undef, "not quite tiff"); probe_ok("4D4D0041", undef, "not quite tiff"); probe_ok("49492A00", "tiff", "tiff intel"); probe_ok("4D4D002A", "tiff", "tiff motorola"); probe_ok("474946383961", "gif", "gif 89"); probe_ok("474946383761", "gif", "gif 87"); probe_ok(<add_file_magic(name => 'testtype', bits => " testtype", mask => " xxxxxxxx"), "add magic"); probe_ok(<read_multi(data => $data); is(@mult_data, 0, "read_multi with non-image input data should fail"); is(Imager->errstr, "type parameter missing and it couldn't be determined from the file contents", "check the error message"); my @mult_file = Imager->read_multi(file => "t/200-file/100-files.t"); is(@mult_file, 0, "read_multi with non-image filename should fail"); is(Imager->errstr, "type parameter missing and it couldn't be determined from the file contents or file name", "check the error message"); my $im = Imager->new; ok(!$im->read(data => $data), "read from non-image data should fail"); is($im->errstr, "type parameter missing and it couldn't be determined from the file contents", "check the error message"); ok(!$im->read(file => "t/200-file/100-files.t"), "read from non-image file should fail"); is($im->errstr, "type parameter missing and it couldn't be determined from the file contents or file name", "check the error message"); } { # test def_guess_type my @tests = ( pnm => "pnm", GIF => "gif", tif => "tiff", TIFF => "tiff", JPG => "jpeg", rle => "utah", bmp => "bmp", dib => "bmp", rgb => "sgi", BW => "sgi", TGA => "tga", CUR => "cur", ico => "ico", ILBM => "ilbm", pcx => "pcx", psd => "psd", webp => "webp", ); while (my ($ext, $expect) = splice(@tests, 0, 2)) { my $filename = "foo.$ext"; is(Imager::def_guess_type($filename), $expect, "type for $filename should be $expect"); } Imager->add_type_extensions("x123", "x321"); is(Imager::def_guess_type("foo.x321"), "x123", "test adding a file type works"); } Imager->close_log; done_testing(); unless ($ENV{IMAGER_KEEP_FILES}) { unlink "testout/t1000files.log"; } sub probe_ok { my ($packed, $exp_type, $name) = @_; my $builder = Test::Builder->new; $packed =~ tr/ \r\n//d; # remove whitespace used for layout my $data = pack("H*", $packed); my $io = Imager::io_new_buffer($data); my $result = Imager::_test_format($io); return $builder->is_eq($result, $exp_type, $name) }