chiark / gitweb /
lib/dpkg/tarfn.c: Kludge `tar_header_decode' to handle spurious `errno'.
[dpkg] / scripts / t / Dpkg_Checksums.t
1 #!/usr/bin/perl
2 #
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.
7 #
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.
12 #
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/>.
15
16 use strict;
17 use warnings;
18
19 use Test::More tests => 44;
20 use Test::Dpkg qw(:paths);
21
22 BEGIN {
23     use_ok('Dpkg::Checksums');
24 }
25
26 my $datadir = test_get_data_path('t/Dpkg_Checksums');
27
28 my @data = (
29     {
30         file => 'empty',
31         size => 0,
32         sums => {
33             md5 => 'd41d8cd98f00b204e9800998ecf8427e',
34             sha1 => 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
35             sha256 => 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
36         }
37     }, {
38         file => 'data-1',
39         size => 7,
40         sums => {
41             md5 => '1b662eff496fde1a63cc5ff97beec10a',
42             sha1 => 'ff66a3dc152f273a19392d3099b2915c311c707e',
43             sha256 => 'f53cb4ee5128363210053c89627757c3dd864ab87e3ac9bff20dd6fe4175a140',
44         }
45     }, {
46         file => 'data-2',
47         size => 14,
48         sums => {
49             md5 => '785400cfc51d16a06e2c34aa511b99ef',
50             sha1 => '329ba56c0c9c63b6e138f3970ac3628e476a6854',
51             sha256 => '217147cd3126a076ba3fd816566a80aaaffff5facc572394dbd6af61a49760d1',
52         }
53     }
54 );
55
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}";
60     }
61 }
62
63 sub test_checksums {
64     my $ck = shift;
65
66     my @known_files = $ck->get_files();
67     my @data_files = map { $_->{file} } @data;
68
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");
75     }
76 }
77
78
79 my @expected_checksums = qw(md5 sha1 sha256);
80 my @known_checksums = checksums_get_list();
81
82 is_deeply(\@known_checksums, \@expected_checksums, 'List of known checksums');
83
84 foreach my $c (@expected_checksums) {
85     ok(checksums_is_supported($c), "Checksum $c is supported");
86
87     my $uc = uc $c;
88     ok(checksums_is_supported($uc), "Checksum $uc (uppercase) is supported");
89
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");
93 }
94
95 my $ck = Dpkg::Checksums->new();
96
97 is(scalar $ck->get_files(), 0, 'No checksums recorded');
98
99 # Check add_from_file()
100
101 foreach my $f (@data) {
102     $ck->add_from_file("$datadir/$f->{file}", key => $f->{file});
103 }
104
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");
108 }
109
110 test_checksums($ck);
111
112 # Check add_from_string()
113
114 foreach my $alg (keys %str_checksum) {
115     $ck->add_from_string($alg, $str_checksum{$alg});
116
117     my $str = $ck->export_to_string($alg);
118     is($str, $str_checksum{$alg}, "Export checksum $alg to string from string");
119 }
120
121 test_checksums($ck);
122
123 1;