Browse Source

scripts/t: Add test cases for Dpkg::Checksums

Guillem Jover 9 years ago
parent
commit
f3deda236b

+ 1 - 1
debian/changelog

@@ -108,7 +108,7 @@ dpkg (1.18.0) UNRELEASED; urgency=low
       directories with mode 000.
     - Fix dpkg-divert unit test to work when there is no /dev/full.
     - Skip test cases when there is no c++filt available.
-    - Add test cases for Dpkg::Conf.
+    - Add test cases for Dpkg::Conf and Dpkg::Checksums.
   * Build system:
     - Bump gettext version to 0.19:
       + Use --add-location=file in msgmerge and xgettext commands.

+ 3 - 0
scripts/Makefile.am

@@ -276,6 +276,9 @@ test_data = \
 	t/Dpkg_Changelog/misplaced-tz \
 	t/Dpkg_Changelog/regressions \
 	t/Dpkg_Changelog/shadow \
+	t/Dpkg_Checksums/empty \
+	t/Dpkg_Checksums/data-1 \
+	t/Dpkg_Checksums/data-2 \
 	t/Dpkg_Conf/config-mixed \
 	t/Dpkg_Control/control-1 \
 	t/Dpkg_Control/bogus-unsigned.dsc \

+ 97 - 2
scripts/t/Dpkg_Checksums.t

@@ -16,12 +16,107 @@
 use strict;
 use warnings;
 
-use Test::More tests => 1;
+use Test::More tests => 41;
 
 BEGIN {
     use_ok('Dpkg::Checksums');
 }
 
-# TODO: Add actual test cases.
+my $srcdir = $ENV{srcdir} // '.';
+my $datadir = $srcdir . '/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");
+}
+
+my $ck = Dpkg::Checksums->new();
+
+is(scalar $ck->get_files(), 0, 'No checkums 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;

+ 1 - 0
scripts/t/Dpkg_Checksums/data-1

@@ -0,0 +1 @@
+data-1

+ 1 - 0
scripts/t/Dpkg_Checksums/data-2

@@ -0,0 +1 @@
+data-2 data-2

+ 0 - 0
scripts/t/Dpkg_Checksums/empty