Debian Codegraph @

t/opustags.t

raw
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5 use utf8;
6
7 use Test::More tests => 66;
8 use Test::Deep qw(cmp_deeply re);
9
10 use Digest::MD5;
11 use File::Basename;
12 use File::Copy;
13 use IPC::Open3;
14 use List::MoreUtils qw(any);
15 use Symbol 'gensym';
16
17 my $opustags = '../opustags';
18 BAIL_OUT("$opustags does not exist or is not executable") if (! -x $opustags);
19
20 my $is_utf8;
21 open(my $ctype, 'locale -k LC_CTYPE |');
22 while (<$ctype>) { $is_utf8 = 1 if (/^charmap="UTF-?8"$/i) }
23 close($ctype);
24 BAIL_OUT("this test must be run from an UTF-8 environment") unless $is_utf8;
25
26 sub opustags {
27 my %opt;
28 %opt = %{pop @_} if ref $_[-1];
29 my ($pid, $pin, $pout, $perr);
30 $perr = gensym;
31 $pid = open3($pin, $pout, $perr, $opustags, @_);
32 binmode($pin, $opt{mode} // ':utf8');
33 binmode($pout, $opt{mode} // ':utf8');
34 binmode($perr, ':utf8');
35 local $/;
36 print $pin $opt{in} if defined $opt{in};
37 close $pin;
38 my $out = <$pout>;
39 my $err = <$perr>;
40 waitpid($pid, 0);
41 [$out, $err, $?]
42 }
43
44 ####################################################################################################
45 # Tests related to the overall opustags executable, like the help message.
46 # No Opus file is manipulated here.
47
48 is_deeply(opustags(), ['', <<EOF, 512], 'no options is a failure');
49 error: No arguments specified. Use -h for help.
50 EOF
51
52 my $help = opustags('--help');
53 $help->[0] =~ /^([^\n]*+)/;
54 my $version = $1;
55 like($version, qr/^opustags version (\d+\.\d+\.\d+)/, 'get the version string');
56
57 my $expected_help = qr{opustags version .*\n\nUsage: opustags --help\n};
58 cmp_deeply(opustags('--help'), [re($expected_help), '', 0], '--help displays the help message');
59 cmp_deeply(opustags('-h'), [re($expected_help), '', 0], '-h displays the help message too');
60
61 is_deeply(opustags('--derp'), ['', <<"EOF", 512], 'unrecognized option shows an error');
62 error: Unrecognized option '--derp'.
63 EOF
64
65 is_deeply(opustags('../opustags'), ['', <<"EOF", 256], 'not an Ogg stream');
66 ../opustags: error: Input is not a valid Ogg file.
67 EOF
68
69 ####################################################################################################
70 # Test the main features of opustags on an Ogg Opus sample file.
71
72 sub md5 {
73 my ($file) = @_;
74 open(my $fh, '<', $file) or return;
75 my $ctx = Digest::MD5->new;
76 $ctx->addfile($fh);
77 $ctx->hexdigest
78 }
79
80 is(md5('gobble.opus'), '111a483596ac32352fbce4d14d16abd2', 'the sample is the one we expect');
81 is_deeply(opustags('gobble.opus'), [<<'EOF', '', 0], 'read the initial tags');
82 encoder=Lavc58.18.100 libopus
83 EOF
84
85 unlink('out.opus');
86 my $previous_umask = umask(0022);
87 is_deeply(opustags(qw(gobble.opus -o out.opus)), ['', '', 0], 'copy the file without changes');
88 is(md5('out.opus'), '111a483596ac32352fbce4d14d16abd2', 'the copy is faithful');
89 is((stat 'out.opus')[2] & 0777, 0644, 'apply umask on new files');
90 umask($previous_umask);
91
92 # empty out.opus
93 { my $fh; open($fh, '>', 'out.opus') and close($fh) or die }
94 is_deeply(opustags(qw(gobble.opus -o out.opus)), ['', <<'EOF', 256], 'refuse to override');
95 gobble.opus: error: 'out.opus' already exists. Use -y to overwrite.
96 EOF
97 is(md5('out.opus'), 'd41d8cd98f00b204e9800998ecf8427e', 'the output wasn\'t written');
98
99 is_deeply(opustags(qw(gobble.opus -o /dev/null)), ['', '', 0], 'write to /dev/null');
100
101 chmod(0604, 'out.opus');
102 is_deeply(opustags(qw(gobble.opus -o out.opus --overwrite)), ['', '', 0], 'overwrite');
103 is(md5('out.opus'), '111a483596ac32352fbce4d14d16abd2', 'successfully overwritten');
104 is((stat 'out.opus')[2] & 0777, 0604, 'overwriting preserves output file\'s mode');
105
106 chmod(0700, 'out.opus');
107 is_deeply(opustags(qw(--in-place out.opus -a A=B --add=A=C --add), "TITLE=Foo Bar",
108 qw(--delete A --add TITLE=七面鳥 --set encoder=whatever -s 1=2 -s X=1 -a X=2 -s X=3)),
109 ['', '', 0], 'complex tag editing');
110 is(md5('out.opus'), '66780307a6081523dc9040f3c47b0448', 'check the footprint');
111 is((stat 'out.opus')[2] & 0777, 0700, 'in-place editing preserves file mode');
112
113 is_deeply(opustags('out.opus'), [<<'EOF', '', 0], 'check the tags written');
114 A=B
115 A=C
116 TITLE=Foo Bar
117 TITLE=七面鳥
118 encoder=whatever
119 1=2
120 X=1
121 X=2
122 X=3
123 EOF
124
125 is_deeply(opustags(qw(out.opus -d A -d foo -s X=4 -a TITLE=gobble -d title=七面鳥)), [<<'EOF', '', 0], 'dry editing');
126 TITLE=Foo Bar
127 encoder=whatever
128 1=2
129 X=4
130 TITLE=gobble
131 EOF
132 is(md5('out.opus'), '66780307a6081523dc9040f3c47b0448', 'the file did not change');
133
134 is_deeply(opustags(qw(-i out.opus -a fatal=yes -a FOO -a BAR)), ['', <<'EOF', 512], 'bad tag with --add');
135 error: Comment does not contain an equal sign: FOO.
136 EOF
137 is(md5('out.opus'), '66780307a6081523dc9040f3c47b0448', 'the file did not change');
138
139 is_deeply(opustags('out.opus', '-D', '-a', "X=foobar\tquux"), [<<'END_OUT', <<'END_ERR', 0], 'control characters');
140 X=foobar quux
141 END_OUT
142 warning: Some tags contain control characters.
143 END_ERR
144
145 is_deeply(opustags('out.opus', '-D', '-a', "X=foo\n\nbar"), [<<'END_OUT', '', 0], 'newline characters');
146 X=foo
147
148 bar
149 END_OUT
150
151 is_deeply(opustags(qw(-i out.opus -s fatal=yes -s FOO -s BAR)), ['', <<'EOF', 512], 'bad tag with --set');
152 error: Comment does not contain an equal sign: FOO.
153 EOF
154 is(md5('out.opus'), '66780307a6081523dc9040f3c47b0448', 'the file did not change');
155
156 is_deeply(opustags(qw(out.opus --delete-all -a OK=yes)), [<<'EOF', '', 0], 'delete all');
157 OK=yes
158 EOF
159
160 is_deeply(opustags(qw(out.opus --set-all -a A=B -s X=Z -d OK), {in => <<'END_IN'}), [<<'END_OUT', '', 0], 'set all');
161 OK=yes again
162 ARTIST=七面鳥
163
164 A=A
165 X=Y
166 #IGNORE=COMMENTS
167 END_IN
168 OK=yes again
169 ARTIST=七面鳥
170 A=A
171 X=Y
172 A=B
173 X=Z
174 END_OUT
175
176 is_deeply(opustags(qw(out.opus -S), {in => <<'END_IN'}), [<<'END_OUT', <<'END_ERR', 256], 'set all with bad tags');
177 whatever
178 wrong=yes
179 END_IN
180 END_OUT
181 error: Malformed tag: whatever
182 END_ERR
183
184 sub slurp {
185 my ($filename) = @_;
186 local $/;
187 open(my $fh, '<', $filename);
188 binmode($fh);
189 my $data = <$fh>;
190 $data
191 }
192
193 my $data = slurp 'out.opus';
194 is_deeply(opustags('-', '-o', '-', {in => $data, mode => ':raw'}), [$data, '', 0], 'read opus from stdin and write to stdout');
195
196 unlink('out.opus');
197
198 # Test --in-place
199 unlink('out2.opus');
200 copy('gobble.opus', 'out.opus');
201 is_deeply(opustags(qw(out.opus --add BAR=baz -o out2.opus)), ['', '', 0], 'process multiple files with --in-place');
202 is_deeply(opustags(qw(--in-place --add FOO=bar out.opus out2.opus)), ['', '', 0], 'process multiple files with --in-place');
203 is(md5('out.opus'), '30ba30c4f236c09429473f36f8f861d2', 'the tags were added correctly (out.opus)');
204 is(md5('out2.opus'), '0a4d20c287b2e46b26cb0eee353c2069', 'the tags were added correctly (out2.opus)');
205
206 unlink('out.opus');
207 unlink('out2.opus');
208
209 ####################################################################################################
210 # Interactive edition
211
212 $ENV{EDITOR} = 'sed -i -e y/aeiou/AEIOU/ `sleep 0.1`';
213 is_deeply(opustags('gobble.opus', '-eo', "'screaming !'.opus"), ['', '', 0], 'edit a file with EDITOR');
214 is(md5("'screaming !'.opus"), '56e85ccaa83a13c15576d75bbd6d835f', 'the tags were modified');
215
216 $ENV{EDITOR} = 'true';
217 is_deeply(opustags('-ie', "'screaming !'.opus"), ['', "Cancelling edition because the tags file was not modified.\n", 256], 'close -e without saving');
218 is(md5("'screaming !'.opus"), '56e85ccaa83a13c15576d75bbd6d835f', 'the tags were not modified');
219
220 $ENV{EDITOR} = 'false';
221 is_deeply(opustags('-ie', "'screaming !'.opus"), ['', "'screaming !'.opus: error: Child process exited with 1\n", 256], 'editor exiting with an error');
222 is(md5("'screaming !'.opus"), '56e85ccaa83a13c15576d75bbd6d835f', 'the tags were not modified');
223
224 unlink("'screaming !'.opus");
225
226 ####################################################################################################
227 # Test muxed streams
228
229 system('ffmpeg -loglevel error -y -i gobble.opus -c copy -map 0:0 -map 0:0 -shortest muxed.ogg') == 0
230 or BAIL_OUT('could not create a muxed stream');
231
232 is_deeply(opustags('muxed.ogg'), ['', <<'END_ERR', 256], 'muxed streams detection');
233 muxed.ogg: error: Muxed streams are not supported yet.
234 END_ERR
235
236 unlink('muxed.ogg');
237
238 ####################################################################################################
239 # Locale
240
241 my $locale = 'en_US.iso88591';
242 my @all_locales = split(' ', `locale -a`);
243
244 SKIP: {
245 skip "locale $locale is not present", 5 unless (any { $_ eq $locale } @all_locales);
246
247 opustags(qw(gobble.opus -a TITLE=七面鳥 -a ARTIST=éàç -o out.opus -y));
248
249 local $ENV{LC_ALL} = $locale;
250 local $ENV{LANGUAGE} = '';
251
252 is_deeply(opustags(qw(-S out.opus), {in => <<"END_IN", mode => ':raw'}), [<<"END_OUT", '', 0], 'set all in ISO-8859-1');
253 T=\xef\xef\xf6
254 END_IN
255 T=\xef\xef\xf6
256 END_OUT
257
258 is_deeply(opustags('-i', 'out.opus', "--add=I=\xf9\xce", {mode => ':raw'}), ['', '', 0], 'write tags in ISO-8859-1');
259
260 is_deeply(opustags('out.opus', {mode => ':raw'}), [<<"END_OUT", <<"END_ERR", 256], 'read tags in ISO-8859-1 with incompatible characters');
261 encoder=Lavc58.18.100 libopus
262 END_OUT
263 out.opus: error: Invalid or incomplete multibyte or wide character. See --raw.
264 END_ERR
265
266 is_deeply(opustags(qw(out.opus -d TITLE -d ARTIST), {mode => ':raw'}), [<<"END_OUT", '', 0], 'read tags in ISO-8859-1');
267 encoder=Lavc58.18.100 libopus
268 I=\xf9\xce
269 END_OUT
270
271 $ENV{LC_ALL} = '';
272
273 is_deeply(opustags('out.opus'), [<<"END_OUT", '', 0], 'read tags in UTF-8');
274 encoder=Lavc58.18.100 libopus
275 TITLE=七面鳥
276 ARTIST=éàç
277 I=ùÎ
278 END_OUT
279
280 unlink('out.opus');
281 }
282
283 ####################################################################################################
284 # Raw edition
285
286 is_deeply(opustags(qw(-S gobble.opus -o out.opus --raw -a), "U=\xFE", {in => <<"END_IN", mode => ':raw'}), ['', '', 0], 'raw set-all with binary data');
287 T=\xFF
288 END_IN
289
290 is_deeply(opustags(qw(out.opus --raw), { mode => ':raw' }), [<<"END_OUT", '', 0], 'raw read');
291 T=\xFF
292 U=\xFE
293 END_OUT
294
295 unlink('out.opus');
296
297 ####################################################################################################
298 # Multiple-page tags
299
300 my $big_tags = "DATA=x\n" x 15000; # > 90K, which is over the max page size of 64KiB.
301 is_deeply(opustags(qw(-S gobble.opus -o out.opus), {in => $big_tags}), ['', '', 0], 'write multi-page header');
302 is_deeply(opustags('out.opus'), [$big_tags, '', 0], 'read multi-page header');
303 is_deeply(opustags(qw(out.opus -i -D -a), 'encoder=Lavc58.18.100 libopus'), ['', '', 0], 'shrink the header');
304 is(md5('out.opus'), '111a483596ac32352fbce4d14d16abd2', 'the result is identical to the original file');
305 unlink('out.opus');
306
307 ####################################################################################################
308 # Cover arts
309
310 is_deeply(opustags(qw(-D --set-cover pixel.png gobble.opus -o out.opus)), ['', '', 0], 'set the cover');
311 is_deeply(opustags(qw(--output-cover out.png out.opus)), [<<'END_OUT', '', 0], 'extract the cover');
312 METADATA_BLOCK_PICTURE=AAAAAwAAAAlpbWFnZS9wbmcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEWJUE5HDQoaCgAAAA1JSERSAAAAAQAAAAEIAgAAAJB3U94AAAAMSURBVAjXY/j//z8ABf4C/tzMWecAAAAASUVORK5CYII=
313 END_OUT
314 is(md5('out.png'), md5('pixel.png'), 'the extracted cover is identical to the one set');
315 unlink('out.opus');
316 unlink('out.png');
317
318 is_deeply(opustags(qw(-D --set-cover - gobble.opus), { in => "GIF8 x" }), [<<'END_OUT', '', 0], 'read the cover from stdin');
319 METADATA_BLOCK_PICTURE=AAAAAwAAAAlpbWFnZS9naWYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZHSUY4IHg=
320 END_OUT
321
322 ####################################################################################################
323 # Vendor string
324
325 is_deeply(opustags(qw(--vendor gobble.opus)), ["Lavf58.12.100\n", '', 0], 'print the vendor string');
326
327 is_deeply(opustags(qw(--set-vendor opustags gobble.opus -o out.opus)), ['', '', 0], 'set the vendor string');
328 is_deeply(opustags(qw(--vendor out.opus)), ["opustags\n", '', 0], 'the vendor string was updated');
329 unlink('out.opus');
330
331 ####################################################################################################
332 # Multi-line tags
333
334 is_deeply(opustags(qw(--set-all gobble.opus -o out.opus), { in => "MULTILINE=one\n\ttwo\nSIMPLE=three\n" }), ['', '', 0], 'parses continuation lines');
335 is_deeply(opustags(qw(out.opus -z)), ["MULTILINE=one\ntwo\0SIMPLE=three\0", '', 0], 'delimits output with NUL on -z');
336 unlink('out.opus');
337
338 is_deeply(opustags(qw(--set-all gobble.opus -o out.opus -z), { in => "MULTILINE=one\ntwo\0SIMPLE=three\0" }), ['', '', 0], 'delimits input with NUL on -z');
339 is_deeply(opustags(qw(out.opus)), [<<'END', '', 0], 'indents continuation lines');
340 MULTILINE=one
341 two
342 SIMPLE=three
343 END
344 unlink('out.opus');