summaryrefslogtreecommitdiff
path: root/scripts/t/Dpkg_Checksums.t
blob: e549e640ec451d18928a2802404b560f6cb6653c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/perl
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

use strict;
use warnings;

use Test::More tests => 44;
use Test::Dpkg qw(:paths);

BEGIN {
    use_ok('Dpkg::Checksums');
}

my $datadir = test_get_data_path('t/Dpkg_Checksums');

my @data = (
    {
        file => 'empty',
        size => 0,
        sums => {
            md5 => 'd41d8cd98f00b204e9800998ecf8427e',
            sha1 => 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
            sha256 => 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
        }
    }, {
        file => 'data-1',
        size => 7,
        sums => {
            md5 => '1b662eff496fde1a63cc5ff97beec10a',
            sha1 => 'ff66a3dc152f273a19392d3099b2915c311c707e',
            sha256 => 'f53cb4ee5128363210053c89627757c3dd864ab87e3ac9bff20dd6fe4175a140',
        }
    }, {
        file => 'data-2',
        size => 14,
        sums => {
            md5 => '785400cfc51d16a06e2c34aa511b99ef',
            sha1 => '329ba56c0c9c63b6e138f3970ac3628e476a6854',
            sha256 => '217147cd3126a076ba3fd816566a80aaaffff5facc572394dbd6af61a49760d1',
        }
    }
);

my %str_checksum;
foreach my $f (@data) {
    foreach my $alg (keys %{$f->{sums}}) {
        $str_checksum{$alg} .= "\n$f->{sums}->{$alg} $f->{size} $f->{file}";
    }
}

sub test_checksums {
    my $ck = shift;

    my @known_files = $ck->get_files();
    my @data_files = map { $_->{file} } @data;

    is_deeply(\@known_files, \@data_files, 'List of files');
    foreach my $f (@data) {
        ok($ck->has_file($f->{file}), "Known file $f->{file}");
        is($ck->get_size($f->{file}), $f->{size}, "Known file $f->{file} size");
        is_deeply($ck->get_checksum($f->{file}), $f->{sums},
                  "Known file $f->{file} checksums");
    }
}


my @expected_checksums = qw(md5 sha1 sha256);
my @known_checksums = checksums_get_list();

is_deeply(\@known_checksums, \@expected_checksums, 'List of known checksums');

foreach my $c (@expected_checksums) {
    ok(checksums_is_supported($c), "Checksum $c is supported");

    my $uc = uc $c;
    ok(checksums_is_supported($uc), "Checksum $uc (uppercase) is supported");

    ok(defined checksums_get_property($c, 'name'), "Checksum $c has name");
    ok(defined checksums_get_property($c, 'regex'), "Checksum $c has regex");
    ok(defined checksums_get_property($c, 'strong'), "Checksum $c has strong");
}

my $ck = Dpkg::Checksums->new();

is(scalar $ck->get_files(), 0, 'No checksums recorded');

# Check add_from_file()

foreach my $f (@data) {
    $ck->add_from_file("$datadir/$f->{file}", key => $f->{file});
}

foreach my $alg (keys %str_checksum) {
    my $str = $ck->export_to_string($alg);
    is($str, $str_checksum{$alg}, "Export checksum $alg to string from file");
}

test_checksums($ck);

# Check add_from_string()

foreach my $alg (keys %str_checksum) {
    $ck->add_from_string($alg, $str_checksum{$alg});

    my $str = $ck->export_to_string($alg);
    is($str, $str_checksum{$alg}, "Export checksum $alg to string from string");
}

test_checksums($ck);

1;