File: JPEG_1_segments.t

package info (click to toggle)
libimage-metadata-jpeg-perl 0.159-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,236 kB
  • sloc: perl: 3,676; makefile: 2
file content (306 lines) | stat: -rw-r--r-- 10,766 bytes parent folder | download | duplicates (3)
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
use Test::More tests => 63;
BEGIN { require 't/test_setup.pl'; }

my $soi   = "\377\330";
my $eoi   = "\377\331";
my $sos   = "\377\332";
my $com   = "\377\376";
my $len   = "\000\010";
my $first = "\001";
my $last  = "\077";
my $esel  = "\111";
my $forged_sos = "${first}\011${esel}\277\321${last}";
my $name  = "fancydir";
my $data  = "xyz";
my ($segment, $record, $handle, $mem, $result, $dirrec, $problem);
my $trim = sub { join '\n', map { s/^.*\"(.*)\".*$/$1/; $_ }
		 grep { /0:/ } split '\n', $_[0] };
sub reset_mem { close $handle if $handle; open($handle, '>', \$mem); }
sub trap_warn { local $SIG{'__WARN__'} = sub { $problem = shift }; 
		$problem = undef; eval($_[0]); }
	       
#=======================================
diag "Testing [Image::MetaData::JPEG::Segment]";
#=======================================

BEGIN { use_ok ($::tabname, qw(:RecordTypes :TagsAPP0)) or exit; }
BEGIN { use_ok ($::segname) or exit; } # this must be loaded second!

#########################
$segment = newsegment('APP1', \ $forged_sos);
ok( $segment, "APP1 segment created" );

#########################
isa_ok( $segment, $::segname );

#########################
ok( $segment->{error}, "... with error flag set" );

#########################
eval { $segment->update() };
isnt( $@, '', "a faulty segment cannot be updated" );

eval { newsegment(undef, 'COM', undef) };
isnt( $@, '', "Error OK: " . &$trim($@));

#########################
eval { newsegment('COM', undef) };
is( $@, '', "ctor survives to undef data" );

#########################
$segment = newsegment('COM', \ $forged_sos);
ok( $segment, "Comment segment created" );

#########################
ok( ! $segment->{error}, "... with error flag unset" );

#########################
ok( exists $segment->{records}, "the 'records' container exists" );

#########################
ok( exists $segment->{name}, "the 'name' member exists" );

#########################
$record = $segment->search_record('Comment');
ok( $record, "'Comment' record found" );

#########################
isa_ok( $record, "${main::pkgname}::Record" );

#########################
$segment = newsegment('SOS', \ $forged_sos);
ok( $segment, "Forged SOS segment created" );
# This is the structure of the segment:
# [           ScanComponents]<......> = [     BYTE]  1
# [        ComponentSelector]<......> = [     BYTE]  1
# [          EntropySelector]<......> = [  NIBBLES]  0 0
# [   SpectralSelectionStart]<......> = [     BYTE]  0
# [     SpectralSelectionEnd]<......> = [     BYTE]  63
# [ SuccessiveAp...tPosition]<......> = [  NIBBLES]  0 0

#########################
ok( ! $segment->{error}, "... with error flag unset" );

#########################
is( scalar $segment->search_record('EntropySelector')->get(), $esel,
    "search_record with tag works" );

#########################
is( scalar $segment->search_record('FIRST_RECORD')->get(), $first,
    "search_record with 'FIRST_RECORD' works" );

#########################
is( scalar $segment->search_record('LAST_RECORD')->get(),  $last,
    "search_record with 'LAST_RECORD' works" );

#########################
$result = $segment->search_record();
is( $result->get_value(), $segment->{records},
    "search_record() without args gives a fake root record" );

#########################
$result = $segment->search_record_value();
is( $result, $segment->{records},
    "search_record_value() without args gives root" );

#########################
trap_warn('$segment->update()');
like( $problem, qr/[Rr]everting/, "you cannot 'update' this yet" );

#########################
$segment->reparse_as('COM');
ok( ! $segment->{error}, "a SOS can be reparsed as a COM" );

#########################
$segment->reparse_as('APP2');
ok( $segment->{error}, "... but not as an APP2" );

#########################
$segment->reparse_as('SOS'); reset_mem(); 
$result = $segment->output_segment_data($handle);
ok( $result, "output_segment_data does not fail" );

#########################
is( $mem, "${sos}${len}${forged_sos}",
    "... and its return value is correct" );

#########################
isnt( $segment->get_description(), undef, "get_description gives non-undef" );

#########################
$segment = newsegment('APP1', \ $forged_sos, 'NOPARSE');
ok( ! $segment->{error}, "NOPARSE actually avoids parsing" );

#########################
eval { $segment->update() };
isnt( $@, '', "... but then you cannot update" );

#########################
$segment = newsegment('COM');
reset_mem(); $result = $segment->output_segment_data($handle);
is( $mem, "$com\000\002", "output_segment_data works with empty comments" );

#########################
$segment->search_record('Comment')->set_value('*' x 2**16);
trap_warn('$segment->update()');
like( $problem, qr/[Rr]everting/, "size check works in forged comment" );

#########################
$segment = newsegment('COM', \ '');
$segment->search_record('Comment')->set_value('*' x 2**16);
trap_warn('$segment->update()');
like( $problem, qr/[Rr]everting/, "size check works in forged comment (2)" );

#########################
$segment = newsegment('ECS', \ $forged_sos);
reset_mem(); $segment->output_segment_data($handle);
is( $mem, $forged_sos, "Raw output for raw data" );

#########################
$segment = newsegment('Post-EOI', \ $forged_sos);
reset_mem(); $segment->output_segment_data($handle);
is( $mem, $forged_sos, "Raw output for Post-EOI data" );

#########################
$segment = newsegment('SOI');
reset_mem(); $segment->output_segment_data($handle);
is( $mem, $soi, "Correct output for SOI" );

#########################
$segment = newsegment('EOI');
reset_mem(); $segment->output_segment_data($handle);
is( $mem, $eoi, "Correct output for EOI" );

#########################
$segment->provide_subdirectory($name);
$dirrec = $segment->search_record($name);
isnt( $dirrec, undef, "'$name' creation ok" );

#########################
is_deeply( $dirrec->get_value(), [], "... it is an empty array" );

#########################
$dirrec = $segment->search_record_value($name);
$segment->provide_subdirectory($name.$name, $dirrec);
$dirrec = $segment->search_record_value($name.$name, $dirrec);
is_deeply( $dirrec, [], "'$name$name' creation ok" );

#########################
$dirrec = $segment->search_record($name.$name);
is( $dirrec, undef, "... it is not in the root dir" );

#########################
$dirrec = $segment->search_record_value($name);
$segment->provide_subdirectory($name, $dirrec);
$dirrec = $segment->search_record_value($name.'@'.$name);
is_deeply( $dirrec, [], "'$name\@$name' creation ok" );

#########################
$result = $segment->search_record_value($name, $name);
is_deeply( $dirrec, $result, "... search_record alternative syntax OK" );

#########################
$segment = newsegment('APP8', \ '', 'NOPARSE');
$dirrec = $segment->provide_subdirectory('A@B@C');
$record = $segment->search_record_value('A', '', 'B@', '@', '@C');
is( $dirrec, $record, "Spurious args in search_record_value() ignored" );

#########################
$dirrec = $segment->provide_subdirectory('AA@@', 'BB@CC', undef, 'DD');
$record = $segment->search_record_value('AA@BB@CC@DD');
is( $dirrec, $record, "Spurious args in provide_subdirectory() ignored" );

#########################
$dirrec = $segment->provide_subdirectory('a@b@c');
$record = $segment->search_record_value(undef, 'a', '', 'b@', '@c', undef);
is( $dirrec, $record, "search_record_value() resists to undef's" );

#########################
$dirrec = $segment->provide_subdirectory(undef, 'aa@', '@bb@cc', undef);
$record = $segment->search_record_value('aa@bb@cc');
is( $dirrec, $record, "provide_subdirectory() resists to undef's" );

#########################
$record = $segment->create_record('uno', $ASCII, \ $data);
is( $record->get_value(), $data, "create_record ok [ref]" );

#########################
$segment = newsegment('COM', \ $data);
$record = $segment->create_record('uno', $ASCII, 0, length $data);
is( $record->get_value(), $data, "create_record ok [offset]" );

#########################
$result = $segment->read_record($ASCII, \ $data);
is( $result, $data, "read_record   ok [ref]" );

#########################
$result = $segment->read_record($ASCII, 0, length $data);
is( $result, $data, "read_record   ok [offset]" );

#########################
$dirrec = $segment->provide_subdirectory($name);
$result = $segment->store_record($dirrec, 'due', $ASCII, 0, length $data);
is( $result->get_value(), $data, "store_record  ok [ref]" );

#########################
$segment->store_record($dirrec, 'tre', $ASCII, \ $data);
$result = $segment->search_record_value('tre', $dirrec);
is( $result, $data, "store_record  ok [offset]" );

#########################
$segment = newsegment('APP0', \ ($APP0_JFXX_TAG . chr($APP0_JFXX_1B).
				    "\100\040". 'x' x ($APP0_JFXX_PAL+2048)) );
is( $segment->{error}, undef, "The faboulous 1B-JFXX APP0 segment" );

######################### Patent-covered, impossible-to-find segments
$segment = newsegment('DAC', \ "\012\345\274\333");
is( $segment->{error}, undef, "A fake DAC segment" );

#########################
$segment = newsegment('DAC', \ "\012\345\274");
isnt( $segment->{error}, undef, "An invalid DAC segment" );

#########################
$segment = newsegment('EXP', \ "\345");
is( $segment->{error}, undef, "A fake EXP segment" );

#########################
$segment = newsegment('EXP', \ "\012\345");
isnt( $segment->{error}, undef, "An invalid EXP segment" );

#########################
$segment = newsegment('DNL', \ "\012\345");
is( $segment->{error}, undef, "A fake DNL segment" );

#########################
$segment = newsegment('DNL', \ "\012\345\274");
isnt( $segment->{error}, undef, "An invalid DNL segment" );

#########################
{ local $SIG{'__WARN__'} = sub { $problem = shift; };
  $problem = undef; $segment->warn('Fake warning'); }
ok( $problem, "Generation of warning reports works" );

#########################
{ local $SIG{'__WARN__'} = sub { $problem = shift; };
  eval '$'."$::pkgname".'::show_warnings = undef';
  $problem = undef; $segment->warn('Fake warning');
  eval '$'."$::pkgname".'::show_warnings = 1'; }
ok( ! $problem, "Generation of warnings can be inhibited" );

#########################
$segment = newsegment('DNL', \ "\012\345\274");
{ local $SIG{'__DIE__'} = sub { $problem = shift; };
  $problem = undef; eval{$segment->update()}; }
ok( $problem, "Generation of error reports works" );

#########################
{ local $SIG{'__DIE__'} = sub { $problem = shift; };
  eval '$'."$::segname".'::show_warnings = undef';
  $problem = undef; eval{$segment->update()};
  eval '$'."$::segname".'::show_warnings = 1'; }
ok( $problem, "Generation of errors cannot be inhibited" );

### Local Variables: ***
### mode:perl ***
### End: ***