Dpkg_Checksums.t 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. use strict;
  16. use warnings;
  17. use Test::More tests => 44;
  18. use Test::Dpkg qw(:paths);
  19. BEGIN {
  20. use_ok('Dpkg::Checksums');
  21. }
  22. my $datadir = test_get_data_path('t/Dpkg_Checksums');
  23. my @data = (
  24. {
  25. file => 'empty',
  26. size => 0,
  27. sums => {
  28. md5 => 'd41d8cd98f00b204e9800998ecf8427e',
  29. sha1 => 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
  30. sha256 => 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
  31. }
  32. }, {
  33. file => 'data-1',
  34. size => 7,
  35. sums => {
  36. md5 => '1b662eff496fde1a63cc5ff97beec10a',
  37. sha1 => 'ff66a3dc152f273a19392d3099b2915c311c707e',
  38. sha256 => 'f53cb4ee5128363210053c89627757c3dd864ab87e3ac9bff20dd6fe4175a140',
  39. }
  40. }, {
  41. file => 'data-2',
  42. size => 14,
  43. sums => {
  44. md5 => '785400cfc51d16a06e2c34aa511b99ef',
  45. sha1 => '329ba56c0c9c63b6e138f3970ac3628e476a6854',
  46. sha256 => '217147cd3126a076ba3fd816566a80aaaffff5facc572394dbd6af61a49760d1',
  47. }
  48. }
  49. );
  50. my %str_checksum;
  51. foreach my $f (@data) {
  52. foreach my $alg (keys %{$f->{sums}}) {
  53. $str_checksum{$alg} .= "\n$f->{sums}->{$alg} $f->{size} $f->{file}";
  54. }
  55. }
  56. sub test_checksums {
  57. my $ck = shift;
  58. my @known_files = $ck->get_files();
  59. my @data_files = map { $_->{file} } @data;
  60. is_deeply(\@known_files, \@data_files, 'List of files');
  61. foreach my $f (@data) {
  62. ok($ck->has_file($f->{file}), "Known file $f->{file}");
  63. is($ck->get_size($f->{file}), $f->{size}, "Known file $f->{file} size");
  64. is_deeply($ck->get_checksum($f->{file}), $f->{sums},
  65. "Known file $f->{file} checksums");
  66. }
  67. }
  68. my @expected_checksums = qw(md5 sha1 sha256);
  69. my @known_checksums = checksums_get_list();
  70. is_deeply(\@known_checksums, \@expected_checksums, 'List of known checksums');
  71. foreach my $c (@expected_checksums) {
  72. ok(checksums_is_supported($c), "Checksum $c is supported");
  73. my $uc = uc $c;
  74. ok(checksums_is_supported($uc), "Checksum $uc (uppercase) is supported");
  75. ok(defined checksums_get_property($c, 'name'), "Checksum $c has name");
  76. ok(defined checksums_get_property($c, 'regex'), "Checksum $c has regex");
  77. ok(defined checksums_get_property($c, 'strong'), "Checksum $c has strong");
  78. }
  79. my $ck = Dpkg::Checksums->new();
  80. is(scalar $ck->get_files(), 0, 'No checkums recorded');
  81. # Check add_from_file()
  82. foreach my $f (@data) {
  83. $ck->add_from_file("$datadir/$f->{file}", key => $f->{file});
  84. }
  85. foreach my $alg (keys %str_checksum) {
  86. my $str = $ck->export_to_string($alg);
  87. is($str, $str_checksum{$alg}, "Export checksum $alg to string from file");
  88. }
  89. test_checksums($ck);
  90. # Check add_from_string()
  91. foreach my $alg (keys %str_checksum) {
  92. $ck->add_from_string($alg, $str_checksum{$alg});
  93. my $str = $ck->export_to_string($alg);
  94. is($str, $str_checksum{$alg}, "Export checksum $alg to string from string");
  95. }
  96. test_checksums($ck);
  97. 1;