]> git.imager.perl.org - imager.git/blob - t/t1000files.t
5f87a401fcceb9200f8b450f9e4ffbc298f4a5cc
[imager.git] / t / t1000files.t
1 #!perl -w
2
3 # This file is for testing file functionality that is independent of
4 # the file format
5
6 use strict;
7 use Test::More tests => 43;
8 use Imager;
9
10 -d "testout" or mkdir "testout";
11
12 Imager->open_log(log => "testout/t1000files.log");
13
14 SKIP:
15 {
16   # Initally I tried to write this test using open to redirect files,
17   # but there was a buffering problem that made it so the data wasn't
18   # being written to the output file.  This external perl call avoids
19   # that problem
20
21   my $test_script = 'testout/t1000files_probe.pl';
22
23   # build a temp test script to use
24   ok(open(SCRIPT, "> $test_script"), "open test script")
25     or skip("no test script $test_script: $!", 2);
26   print SCRIPT <<'PERL';
27 #!perl
28 use Imager;
29 use strict;
30 my $file = shift or die "No file supplied";
31 open FH, "< $file" or die "Cannot open file: $!";
32 binmode FH;
33 my $io = Imager::io_new_fd(fileno(FH));
34 Imager::i_test_format_probe($io, -1);
35 PERL
36   close SCRIPT;
37   my $perl = $^X;
38   $perl = qq/"$perl"/ if $perl =~ / /;
39   
40   print "# script: $test_script\n";
41   my $cmd = "$perl -Mblib $test_script t/t1000files.t";
42   print "# command: $cmd\n";
43
44   my $out = `$cmd`;
45   is($?, 0, "command successful");
46   is($out, '', "output should be empty");
47 }
48
49 # test the file limit functions
50 # by default the limits are zero (unlimited)
51 print "# image file limits\n";
52 is_deeply([ Imager->get_file_limits() ], [0, 0, 0x40000000 ],
53           "check defaults");
54 ok(Imager->set_file_limits(width=>100), "set only width");
55 is_deeply([ Imager->get_file_limits() ], [100, 0, 0x40000000 ],
56           "check width set");
57 ok(Imager->set_file_limits(height=>150, bytes=>10000),
58    "set height and bytes");
59 is_deeply([ Imager->get_file_limits() ], [ 100, 150, 10000 ],
60           "check all values now set");
61 ok(Imager->set_file_limits(reset=>1, height => 99),
62    "set height and reset");
63 is_deeply([ Imager->get_file_limits() ], [ 0, 99, 0 ],
64           "check only height is set");
65 ok(Imager->set_file_limits(reset=>1),
66    "just reset");
67 is_deeply([ Imager->get_file_limits() ], [ 0, 0, 0 ],
68           "check all are reset");
69
70 # test error handling for loading file handers
71 {
72   # first, no module at all
73   {
74     my $data = "abc";
75     ok(!Imager->new(data => $data, filetype => "unknown"),
76        "try to read an unknown file type");
77    like(Imager->errstr, qr(^format 'unknown' not supported - formats .* - Can't locate Imager/File/UNKNOWN.pm or Imager/File/UNKNOWNReader.pm$),
78         "check error message");
79   }
80   {
81     my $data;
82     my $im = Imager->new(xsize => 10, ysize => 10);
83     ok(!$im->write(data => \$data, type => "unknown"),
84        "try to write an unknown file type");
85    like($im->errstr, qr(^format 'unknown' not supported - formats .* - Can't locate Imager/File/UNKNOWN.pm or Imager/File/UNKNOWNWriter.pm$),
86         "check error message");
87   }
88   push @INC, "t/t1000lib";
89   {
90     my $data = "abc";
91     ok(!Imager->new(data => $data, filetype => "bad"),
92        "try to read an bad (other load failure) file type");
93    like(Imager->errstr, qr(^format 'bad' not supported - formats .* available for reading - This module fails to load$),
94         "check error message");
95   }
96   {
97     my $data;
98     my $im = Imager->new(xsize => 10, ysize => 10);
99     ok(!$im->write(data => \$data, type => "bad"),
100        "try to write an bad file type");
101    like($im->errstr, qr(^format 'bad' not supported - formats .* available for writing - This module fails to load$),
102         "check error message");
103   }
104 }
105
106 # check file type probe
107 probe_ok("49492A41", undef, "not quite tiff");
108 probe_ok("4D4D0041", undef, "not quite tiff");
109 probe_ok("49492A00", "tiff", "tiff intel");
110 probe_ok("4D4D002A", "tiff", "tiff motorola");
111 probe_ok("474946383961", "gif", "gif 89");
112 probe_ok("474946383761", "gif", "gif 87");
113 probe_ok(<<TGA, "tga", "TGA");
114 00 00 0A 00 00 00 00 00 00 00 00 00 96 00 96 00
115 18 20 FF 00 00 00 95 00 00 00 FF 00 00 00 95 00
116 00 00 FF 00 00 00 95 00 00 00 FF 00 00 00 95 00
117 00 00 FF 00 00 00 95 00 00 00 FF 00 00 00 95 00
118 TGA
119
120 probe_ok(<<TGA, "tga", "TGA 32-bit");
121 00 00 0A 00 00 00 00 00 00 00 00 00 0A 00 0A 00
122 20 08 84 00 00 00 00 84 FF FF FF FF 84 00 00 00
123 00 84 FF FF FF FF 84 00 00 00 00 84 FF FF FF FF
124 TGA
125
126 probe_ok(<<ICO, "ico", "Windows Icon");
127 00 00 01 00 02 00 20 20 10 00 00 00 00 00 E8 02
128 00 00 26 00 00 00 20 20 00 00 00 00 00 00 A8 08
129 00 00 0E 03 00 00 28 00 00 00 20 00 00 00 40 00
130 ICO
131
132 probe_ok(<<ICO, "cur", "Windows Cursor");
133 00 00 02 00 02 00 20 20 10 00 00 00 00 00 E8 02
134 00 00 26 00 00 00 20 20 00 00 00 00 00 00 A8 08
135 00 00 0E 03 00 00 28 00 00 00 20 00 00 00 40 00
136 ICO
137
138 probe_ok(<<SGI, "sgi", "SGI RGB");
139 01 DA 01 01 00 03 00 96 00 96 00 03 00 00 00 00 
140 00 00 00 FF 00 00 00 00 6E 6F 20 6E 61 6D 65 00
141 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
142 SGI
143
144 probe_ok(<<ILBM, "ilbm", "ILBM");
145 46 4F 52 4D 00 00 60 7A 49 4C 42 4D 42 4D 48 44
146 00 00 00 14 00 96 00 96 00 00 00 00 18 00 01 80
147 00 00 0A 0A 00 96 00 96 42 4F 44 59 00 00 60 51
148 ILBM
149
150 probe_ok(<<XPM, "xpm", "XPM");
151 2F 2A 20 58 50 4D 20 2A 2F 0A 73 74 61 74 69 63
152 20 63 68 61 72 20 2A 6E 6F 6E 61 6D 65 5B 5D 20
153 3D 20 7B 0A 2F 2A 20 77 69 64 74 68 20 68 65 69
154 XPM
155
156 probe_ok(<<PCX, "pcx", 'PCX');
157 0A 05 01 08 00 00 00 00 95 00 95 00 96 00 96 00
158 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
159 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
160 PCX
161
162 probe_ok(<<FITS, "fits", "FITS");
163 53 49 4D 50 4C 45 20 20 3D 20 20 20 20 20 20 20 
164 20 20 20 20 20 20 20 20 20 20 20 20 20 54 20 20 
165 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 
166 FITS
167
168 probe_ok(<<PSD, "psd", "Photoshop");
169 38 42 50 53 00 01 00 00 00 00 00 00 00 06 00 00
170 00 3C 00 00 00 96 00 08 00 03 00 00 00 00 00 00
171 0B E6 38 42 49 4D 03 ED 00 00 00 00 00 10 00 90
172 PSD
173
174 probe_ok(<<EPS, "eps", "Encapsulated Postscript");
175 25 21 50 53 2D 41 64 6F 62 65 2D 32 2E 30 20 45
176 50 53 46 2D 32 2E 30 0A 25 25 43 72 65 61 74 6F
177 72 3A 20 70 6E 6D 74 6F 70 73 0A 25 25 54 69 74
178 EPS
179
180 probe_ok(<<UTAH, "utah", "Utah RLE");
181 52 CC 00 00 00 00 0A 00 0A 00 0A 03 08 00 08 00 
182 2F 00 48 49 53 54 4F 52 59 3D 70 6E 6D 74 6F 72 
183 6C 65 20 6F 6E 20 54 68 75 20 4D 61 79 20 31 31 
184 20 31 36 3A 33 35 3A 34 33 20 32 30 30 36 0A 09 
185 UTAH
186
187 probe_ok(<<XWD, "xwd", "X Window Dump");
188 00 00 00 69 00 00 00 07 00 00 00 02 00 00 00 18
189 00 00 01 E4 00 00 01 3C 00 00 00 00 00 00 00 00
190 00 00 00 20 00 00 00 00 00 00 00 20 00 00 00 20
191 00 00 07 90 00 00 00 04 00 FF 00 00 00 00 FF 00
192 XWD
193
194 probe_ok(<<GZIP, "gzip", "gzip compressed");
195 1F 8B 08 08 C2 81 BD 44 02 03 49 6D 61 67 65 72
196 2D 30 2E 35 31 5F 30 33 2E 74 61 72 00 EC 5B 09
197 40 53 C7 BA 9E 24 AC 01 D9 44 04 44 08 8B B2 8A
198 C9 C9 42 92 56 41 50 20 A0 02 41 41 01 17 48 80
199 GZIP
200
201 probe_ok(<<BZIP2, "bzip2", "bzip2 compressed");
202 42 5A 68 39 31 41 59 26 53 59 0F D8 8C 09 00 03
203 28 FF FF FF FF FB 7F FB 77 FF EF BF 6B 7F BE FF
204 FF DF EE C8 0F FF F3 FF FF FF FC FF FB B1 FF FB
205 F4 07 DF D0 03 B8 03 60 31 82 05 2A 6A 06 83 20
206 BZIP2
207
208 probe_ok(<<WEBP, "webp", "Google WEBP");
209 52 49 46 46 2C 99 00 00 57 45 42 50 56 50 38 20
210 20 99 00 00 70 7A 02 9D 01 2A E0 01 80 02 00 87
211 08 85 85 88 85 84 88 88 83 AF E2 F7 64 1F 98 55
212 1B 6A 70 F5 8A 45 09 95 0C 09 7E 25 D9 2E 46 44
213 07 84 FB 01 FD 2C 8A 2F 97 CC ED DB 50 0F 11 3B
214 WEBP
215
216 probe_ok(<<JPEG2K, "jp2", "JPEG 2000");
217 00 00 00 0C 6A 50 20 20 0D 0A 87 0A 00 00 00 14
218 66 74 79 70 6A 70 32 20 00 00 00 00 6A 70 32 20
219 00 00 00 2D 6A 70 32 68 00 00 00 16 69 68 64 72
220 00 00 02 80 00 00 01 E0 00 03 07 07 00 00 00 00
221 00 0F 63 6F 6C 72 01 00 00 00 00 00 10 00 00 00
222 00 6A 70 32 63 FF 4F FF 51 00 2F 00 00 00 00 01
223 JPEG2K
224
225 Imager->close_log;
226
227 unless ($ENV{IMAGER_KEEP_FILES}) {
228   unlink "testout/t1000files.log";
229 }
230
231 sub probe_ok {
232   my ($packed, $exp_type, $name) = @_;
233
234   my $builder = Test::Builder->new;
235   $packed =~ tr/ \r\n//d; # remove whitespace used for layout
236   my $data = pack("H*", $packed);
237
238   my $io = Imager::io_new_buffer($data);
239   my $result = Imager::i_test_format_probe($io, -1);
240
241   return $builder->is_eq($result, $exp_type, $name)
242 }