doc: Move Perl version baseline as the first perl coding style subsection
[dpkg.git] / scripts / t / Dpkg_Checksums.t
blobc1fc5daad54b9f6aa0406cc9f347c3924502792c
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 => 59;
20 use Test::Dpkg qw(:paths);
22 BEGIN {
23 use_ok('Dpkg::Checksums');
26 my $datadir = test_get_data_path();
28 my @data = (
30 file => 'empty',
31 size => 0,
32 sums => {
33 md5 => 'd41d8cd98f00b204e9800998ecf8427e',
34 sha1 => 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
35 sha256 => 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
37 }, {
38 file => 'data-1',
39 size => 7,
40 sums => {
41 md5 => '1b662eff496fde1a63cc5ff97beec10a',
42 sha1 => 'ff66a3dc152f273a19392d3099b2915c311c707e',
43 sha256 => 'f53cb4ee5128363210053c89627757c3dd864ab87e3ac9bff20dd6fe4175a140',
45 }, {
46 file => 'data-2',
47 size => 14,
48 sums => {
49 md5 => '785400cfc51d16a06e2c34aa511b99ef',
50 sha1 => '329ba56c0c9c63b6e138f3970ac3628e476a6854',
51 sha256 => '217147cd3126a076ba3fd816566a80aaaffff5facc572394dbd6af61a49760d1',
56 my %str_checksum;
57 foreach my $f (@data) {
58 foreach my $alg (keys %{$f->{sums}}) {
59 $str_checksum{$alg} .= "\n$f->{sums}->{$alg} $f->{size} $f->{file}";
63 sub test_checksums {
64 my $ck = shift;
66 my @known_files = $ck->get_files();
67 my @data_files = map { $_->{file} } @data;
69 is_deeply(\@known_files, \@data_files, 'List of files');
70 foreach my $f (@data) {
71 ok($ck->has_file($f->{file}), "Known file $f->{file}");
72 is($ck->get_size($f->{file}), $f->{size}, "Known file $f->{file} size");
73 is_deeply($ck->get_checksum($f->{file}), $f->{sums},
74 "Known file $f->{file} checksums");
79 my @expected_checksums = qw(md5 sha1 sha256);
80 my @known_checksums = checksums_get_list();
82 is_deeply(\@known_checksums, \@expected_checksums, 'List of known checksums');
84 foreach my $c (@expected_checksums) {
85 ok(checksums_is_supported($c), "Checksum $c is supported");
87 my $uc = uc $c;
88 ok(checksums_is_supported($uc), "Checksum $uc (uppercase) is supported");
90 ok(defined checksums_get_property($c, 'name'), "Checksum $c has name");
91 ok(defined checksums_get_property($c, 'regex'), "Checksum $c has regex");
92 ok(defined checksums_get_property($c, 'strong'), "Checksum $c has strong");
95 my $ck = Dpkg::Checksums->new();
97 is(scalar $ck->get_files(), 0, 'No checksums recorded');
99 # Check add_from_file()
101 foreach my $f (@data) {
102 $ck->add_from_file("$datadir/$f->{file}", key => $f->{file});
105 foreach my $alg (keys %str_checksum) {
106 my $str = $ck->export_to_string($alg);
107 is($str, $str_checksum{$alg}, "Export checksum $alg to string from file");
110 test_checksums($ck);
112 # Check add_from_string()
114 foreach my $alg (keys %str_checksum) {
115 $ck->add_from_string($alg, $str_checksum{$alg});
117 my $str = $ck->export_to_string($alg);
118 is($str, $str_checksum{$alg}, "Export checksum $alg to string from string");
121 test_checksums($ck);
123 # Check remove_file()
125 ok($ck->has_file('data-2'), 'To be removed file is present');
126 $ck->remove_file('data-2');
127 ok(!$ck->has_file('data-2'), 'Remove file is not present');
129 # Check add_from_control()
130 my $ctrl;
131 foreach my $f (@data) {
132 next if $f->{file} ne 'data-2';
133 foreach my $alg (keys %{$f->{sums}}) {
134 $ctrl->{"Checksums-$alg"} = "\n$f->{sums}->{$alg} $f->{size} $f->{file}";
137 $ck->add_from_control($ctrl);
139 test_checksums($ck);
141 # Check export_to_control()
143 my $ctrl_export = {};
144 $ck->export_to_control($ctrl_export);
146 foreach my $alg (keys %str_checksum) {
147 is($ctrl_export->{"Checksums-$alg"}, $str_checksum{$alg},
148 "Export checksum $alg to a control object");