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
16use strict;
17use warnings;
18
19use Test::More tests => 59;
20use Test::Dpkg qw(:paths);
21
22BEGIN {
23    use_ok('Dpkg::Checksums');
24}
25
26my $datadir = test_get_data_path();
27
28my @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
56my %str_checksum;
57foreach 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
63sub 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
79my @expected_checksums = qw(md5 sha1 sha256);
80my @known_checksums = checksums_get_list();
81
82is_deeply(\@known_checksums, \@expected_checksums, 'List of known checksums');
83
84foreach 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
95my $ck = Dpkg::Checksums->new();
96
97is(scalar $ck->get_files(), 0, 'No checksums recorded');
98
99# Check add_from_file()
100
101foreach my $f (@data) {
102    $ck->add_from_file("$datadir/$f->{file}", key => $f->{file});
103}
104
105foreach 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
110test_checksums($ck);
111
112# Check add_from_string()
113
114foreach 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
121test_checksums($ck);
122
123# Check remove_file()
124
125ok($ck->has_file('data-2'), 'To be removed file is present');
126$ck->remove_file('data-2');
127ok(!$ck->has_file('data-2'), 'Remove file is not present');
128
129# Check add_from_control()
130my $ctrl;
131foreach 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}";
135    }
136}
137$ck->add_from_control($ctrl);
138
139test_checksums($ck);
140
141# Check export_to_control()
142
143my $ctrl_export = {};
144$ck->export_to_control($ctrl_export);
145
146foreach my $alg (keys %str_checksum) {
147    is($ctrl_export->{"Checksums-$alg"}, $str_checksum{$alg},
148       "Export checksum $alg to a control object");
149}
150
1511;
152