test: Move test_data_file() to test.h
[dpkg.git] / scripts / t / Dpkg_Compression.t
blob3fd5983d3ec1714eda8dc044e9df9e0498d17bcd
1 #!/usr/bin/perl
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 use strict;
17 use warnings;
19 use Test::More tests => 48;
20 use Test::Dpkg qw(:paths);
22 use IPC::Cmd qw(can_run);
24 use_ok('Dpkg::Compression');
25 use_ok('Dpkg::Compression::FileHandle');
27 my $tmpdir = test_get_temp_path();
28 my @lines = ("One\n", "Two\n", "Three\n");
29 my $fh;
30 my $have_gunzip = can_run('gunzip');
32 sub test_write {
33 my ($filename, $check_result) = @_;
35 $fh = Dpkg::Compression::FileHandle->new();
36 open $fh, '>', $filename or die 'open failed';
37 print { $fh } $lines[0];
38 syswrite($fh, $lines[1]);
39 printf { $fh } '%s', $lines[2];
40 close $fh or die 'close failed';
42 $check_result->($filename, 'std functions');
44 unlink $filename or die "cannot unlink $filename";
46 $fh = Dpkg::Compression::FileHandle->new();
47 $fh->open($filename, 'w');
48 $fh->print($lines[0]);
49 $fh->write($lines[1], length($lines[1]));
50 $fh->printf('%s', $lines[2]);
51 $fh->close() or die 'close failed';
53 $check_result->($filename, 'IO::Handle methods');
56 sub check_uncompressed {
57 my ($filename, $method) = @_;
58 open(my $read_fh, '<', $filename) or die "cannot read $filename";
59 my @read = <$read_fh>;
60 close $read_fh or die 'cannot close';
61 is_deeply(\@lines, \@read, "$filename correctly written ($method)");
64 sub check_compressed {
65 my ($filename, $method) = @_;
66 open my $read_fh, '-|', 'gunzip', '-c', "$tmpdir/myfile.gz"
67 or die 'cannot fork zcat';
68 my @read = <$read_fh>;
69 close $read_fh or die 'cannot close';
70 is_deeply(\@lines, \@read, "$filename correctly written ($method)");
73 sub test_read {
74 my $filename = shift;
76 $fh = Dpkg::Compression::FileHandle->new();
77 open($fh, '<', $filename) or die 'open failed';
78 my @read = <$fh>;
79 close $fh or die 'close failed';
81 is_deeply(\@lines, \@read, "$filename correctly read (std functions)");
83 @read = ();
84 $fh = Dpkg::Compression::FileHandle->new();
85 $fh->open($filename, 'r') or die 'open failed';
86 @read = $fh->getlines();
87 $fh->close() or die 'close failed';
89 is_deeply(\@lines, \@read, "$filename correctly read (IO::Handle methods)");
92 # Check compression properties.
94 my @compressors = compression_get_list();
95 is_deeply([ sort @compressors ] , [ qw(bzip2 gzip lzma xz) ],
96 'supported compressors');
98 is(compression_get_default(), 'xz', 'default compressor is xz');
99 eval {
100 compression_set_default('invented-compressor');
102 ok($@, 'cannot set compressor to an unsupported name');
103 is(compression_get_default(), 'xz', 'default compressor is still xz');
104 compression_set_default('gzip');
105 is(compression_get_default(), 'gzip', 'default compressor changed to gzip');
106 compression_set_default('xz');
107 is(compression_get_default(), 'xz', 'default compressor reset to xz');
109 ok(compression_is_supported('gzip'), 'gzip is supported');
110 ok(compression_is_supported('xz'), 'xz is supported');
111 ok(compression_is_supported('bzip2'), 'bzip2 is supported');
112 ok(compression_is_supported('lzma'), 'lzma is supported');
114 is(compression_guess_from_filename('filename'), undef,
115 'compressor <none> guessed from "filename"');
116 is(compression_guess_from_filename('filename.gz'), 'gzip',
117 'compressor <none> guessed from "filename"');
118 is(compression_guess_from_filename('filename.xz'), 'xz',
119 'compressor <none> guessed from "filename"');
120 is(compression_guess_from_filename('filename.bz2'), 'bzip2',
121 'compressor <none> guessed from "filename"');
122 is(compression_guess_from_filename('filename.lzma'), 'lzma',
123 'compressor <none> guessed from "filename"');
125 is(compression_get_file_extension('gzip'), 'gz', 'gzip file ext');
126 is(compression_get_file_extension('xz'), 'xz', 'xz file ext');
127 is(compression_get_file_extension('bzip2'), 'bz2', 'bzip2 file ext');
128 is(compression_get_file_extension('lzma'), 'lzma', 'lzma file ext');
130 is(compression_get_level('gzip'), 9, 'gzip level is 9');
131 compression_set_level('gzip', 1);
132 is(compression_get_level('gzip'), 1, 'gzip level is now 1');
133 compression_set_level('gzip');
134 is(compression_get_level('gzip'), 9, 'gzip level is back to 9');
135 is(compression_get_level('xz'), 6, 'xz level is 6');
136 is(compression_get_level('bzip2'), 9, 'bzip2 level is 9');
137 is(compression_get_level('lzma'), 6, 'lzma level is 6');
139 my $ext_regex = compression_get_file_extension_regex();
141 ok('filename.gz' =~ m/\.$ext_regex$/, '.gz matches regex');
142 ok('filename.xz' =~ m/\.$ext_regex$/, '.xz matches regex');
143 ok('filename.bz2' =~ m/\.$ext_regex$/, '.bz2 matches regex');
144 ok('filename.lzma' =~ m/\.$ext_regex$/, '.lzma matches regex');
146 # Test changing the default compression levels
147 my $old_level = compression_get_default_level();
148 compression_set_default_level(1);
149 is(compression_get_default_level(), 1, 'change default compression level');
150 compression_set_default_level(5);
151 is(compression_get_default_level(), 5, 'change default compression level');
152 compression_set_default_level(undef);
153 is(compression_get_default_level(), $old_level, 'reset default compression level');
155 ok(! compression_is_valid_level(0), 'compression 0 is invalid');
156 ok(compression_is_valid_level(1), 'compression 1 is valid');
157 ok(compression_is_valid_level(5), 'compression 5 is valid');
158 ok(compression_is_valid_level(9), 'compression 9 is valid');
159 ok(compression_is_valid_level('fast'), 'compression fast is valid');
160 ok(compression_is_valid_level('best'), 'compression best is valid');
162 # Test write on uncompressed file
163 test_write("$tmpdir/myfile", \&check_uncompressed);
165 SKIP: {
166 skip 'gunzip not available', 1 if not $have_gunzip;
168 # Test write on compressed file
169 test_write("$tmpdir/myfile.gz", \&check_compressed);
172 # Test read on uncompressed file
173 test_read("$tmpdir/myfile");
175 # Test read on compressed file
176 test_read("$tmpdir/myfile.gz");