Upgrade to Digest::MD5 2.22.
[p5sagit/p5-mst-13.2.git] / ext / Digest / MD5 / t / files.t
index 65ecb54..6351af5 100644 (file)
@@ -1,51 +1,96 @@
 BEGIN {
-        chdir 't' if -d 't';
-        @INC = '../lib';
+       if ($ENV{PERL_CORE}) {
+               chdir 't' if -d 't';
+               @INC = '../lib';
+        }
 }
 
-print "1..2\n";
+print "1..5\n";
 
 use strict;
 use Digest::MD5 qw(md5 md5_hex md5_base64);
 
-#
-# This is the output of: 'md5sum MD5.pm MD5.xs'
-#
-my $EXPECT;
+# To update the EBCDIC section even on a Latin 1 platform,
+# run this script with $ENV{EBCDIC_MD5SUM} set to a true value.
+# (You'll need to have Perl 5.7.3 or later, to have the Encode installed.)
+# (And remember that under the Perl core distribution you should
+#  also have the $ENV{PERL_CORE} set to a true value.)
+# Similarly, to update MacOS section, run with $ENV{MAC_MD5SUM} set.
 
-if (ord('A') == 193) { # EBCDIC
-$EXPECT = <<EOT;
-ee6a09094632cd610199278bbb0f910e  ext/Digest/MD5/MD5.pm
-94f873d905cd20a12d8ef4cdbdbcd89f  ext/Digest/MD5/MD5.xs
+my $EXPECT;
+if (ord "A" == 193) { # EBCDIC
+    $EXPECT = <<EOT;
+ed8efe2e2dbab62fcc9dea2df6682569  Changes
+0565ec21b15c0f23f4c51fb327c8926d  README
+0fcdd6d6e33b8772bd4b4832043035cd  MD5.pm
+d7fd24455b9160aa8706635d15e6177e  MD5.xs
+276da0aa4e9a08b7fe09430c9c5690aa  rfc1321.txt
 EOT
-} else { # ASCII
-$EXPECT = <<EOT;
-665ddc08b12d6b1bf85ac6dc5aae68b3  ext/Digest/MD5/MD5.pm
-5f21e907b2e7dbffe6aba2c762ea93d0  ext/Digest/MD5/MD5.xs
+} elsif ("\n" eq "\015") { # MacOS
+    $EXPECT = <<EOT;
+2879619f967d5fc5a00ffe37b639f2ee  Changes
+6c950a0211a5a28f023bb482037698cd  README
+4e1043f0a7a266416d8408d6fa96f454  MD5.pm
+6bff95ff70ba43a6c81e255c6510a865  MD5.xs
+754b9db19f79dbc4992f7166eb0f37ce  rfc1321.txt
 EOT
+} else {
+    # This is the output of: 'md5sum Changes README MD5.pm MD5.xs rfc1321.txt'
+    $EXPECT = <<EOT;
+2879619f967d5fc5a00ffe37b639f2ee  Changes
+6c950a0211a5a28f023bb482037698cd  README
+4e1043f0a7a266416d8408d6fa96f454  MD5.pm
+6bff95ff70ba43a6c81e255c6510a865  MD5.xs
+754b9db19f79dbc4992f7166eb0f37ce  rfc1321.txt
+EOT
+}
+
+if (!(-f "README") && -f "../README") {
+   chdir("..") or die "Can't chdir: $!";
 }
 
+my $testno = 0;
+
 my $B64 = 1;
 eval { require MIME::Base64; };
 if ($@) {
-    print $@;
-    print "# Will not test base64 methods\n";
+    print "# $@: Will not test base64 methods\n";
     $B64 = 0;
 }
 
-my $testno = 0;
-
-use File::Spec;
-
 for (split /^/, $EXPECT) {
      my($md5hex, $file) = split ' ';
-     my @path = split(m:/:, $file);
-     my $last = pop @path;
-     my $path = File::Spec->updir;
-     while (@path) {
-        $path = File::Spec->catdir($path, shift @path);
+     my $base = $file;
+     if ($ENV{PERL_CORE}) {
+         if ($file eq 'rfc1321.txt') { # Don't have it in core.
+            print "ok ", ++$testno, " # Skip: PERL_CORE\n";
+            next;
+        }
+         use File::Spec;
+        my @path = qw(ext Digest MD5);
+        my $path = File::Spec->updir;
+        while (@path) {
+          $path = File::Spec->catdir($path, shift @path);
+        }
+        $file = File::Spec->catfile($path, $file);
+     }
+#     print "# file = $file\n";
+     unless (-f $file) {
+       warn "No such file: $file\n";
+       next;
+     }
+     if ($ENV{EBCDIC_MD5SUM}) {
+         require Encode;
+        my $data = cat_file($file);    
+        Encode::from_to($data, 'latin1', 'cp1047');
+        print md5_hex($data), "  $base\n";
+        next;
+     }
+     if ($ENV{MAC_MD5SUM}) {
+        my $data = cat_file($file);    
+        print md5_hex($data), "  $base\n";
+        next;
      }
-     $file = File::Spec->catfile($path, $last);
      my $md5bin = pack("H*", $md5hex);
      my $md5b64;
      if ($B64) {
@@ -53,14 +98,15 @@ for (split /^/, $EXPECT) {
         chop($md5b64); chop($md5b64);   # remove padding
      }
      my $failed;
+     my $got;
 
      if (digest_file($file, 'digest') ne $md5bin) {
         print "$file: Bad digest\n";
         $failed++;
      }
 
-     if (digest_file($file, 'hexdigest') ne $md5hex) {
-        print "$file: Bad hexdigest\n";
+     if (($got = digest_file($file, 'hexdigest')) ne $md5hex) {
+        print "$file: Bad hexdigest: got $got expected $md5hex\n";
         $failed++;
      }
 
@@ -126,9 +172,6 @@ sub digest_file
     #print "$file $method\n";
 
     open(FILE, $file) or die "Can't open $file: $!";
-# Digests above are generated on UNIX without CRLF
-# so leave handles in text mode
-#    binmode(FILE);
     my $digest = Digest::MD5->new->addfile(*FILE)->$method();
     close(FILE);
 
@@ -140,9 +183,10 @@ sub cat_file
     my($file) = @_;
     local $/;  # slurp
     open(FILE, $file) or die "Can't open $file: $!";
-# Digests above are generated on UNIX without CRLF
-# so leave handles in text mode
-#    binmode(FILE);
+
+    # For PerlIO in case of UTF-8 locales.
+    eval 'binmode(FILE, ":bytes")' if $] >= 5.008;
+
     my $tmp = <FILE>;
     close(FILE);
     $tmp;