1######################### We start with some black magic to print on failure. 2 3# Change 1..1 below to 1..last_test_to_print . 4# (It may become useful if the test is moved to ./t subdirectory.) 5 6BEGIN {print "1..14\n";} 7END {print "not ok 1\n" unless $loaded;} 8use MD5; 9$loaded = 1; 10print "ok 1\n"; 11 12######################### End of black magic. 13 14# Insert your test code below (better if it prints "ok 13" 15# (correspondingly "not ok 13") depending on the success of chunk 13 16# of the test code): 17 18package MD5Test; 19 20# 2: Constructor 21 22print (($md5 = new MD5) ? "ok 2\n" : "not ok 2\n"); 23 24# 3: Basic test data as defined in RFC 1321 25 26%data = ( 27 "" => "d41d8cd98f00b204e9800998ecf8427e", 28 "a" => "0cc175b9c0f1b6a831c399e269772661", 29 "abc" => "900150983cd24fb0d6963f7d28e17f72", 30 "message digest" 31 => "f96b697d7cb7938d525a2f31aaf161d0", 32 "abcdefghijklmnopqrstuvwxyz" 33 => "c3fcd3d76192e4007dfb496cca67e13b", 34 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" 35 => "d174ab98d277d9f5a5611c2c9f419d9f", 36 "12345678901234567890123456789012345678901234567890123456789012345678901234567890" 37 => "57edf4a22be3c955ac49da2e2107b67a", 38); 39 40$failed = 0; 41foreach (sort(keys(%data))) 42{ 43 $md5->reset; 44 $md5->add($_); 45 $digest = $md5->digest; 46 $hex = unpack("H*", $digest); 47 if ($hex ne $data{$_}) { 48 print STDERR "\$md5->digest: $_\n"; 49 print STDERR "expected: $data{$_}\n", 50 "got : $hex\n"; 51 $failed++; 52 } 53 54 if (Digest::MD5::md5($_) ne $digest) { 55 print STDERR "md5($_) failed\n"; 56 $failed++; 57 } 58 59 if (Digest::MD5::md5_hex($_) ne $hex) { 60 print STDERR "md5_hex($_) failed\n"; 61 $failed++; 62 } 63 64 # same stuff ending with $md5->hexdigest instead 65 $md5->reset; 66 $md5->add($_); 67 $hex = $md5->hexdigest; 68 if ($hex ne $data{$_}) { 69 print STDERR "\$md5->hexdigest: $_\n"; 70 print STDERR "expected: $data{$_}\n", 71 "got : $hex\n"; 72 $failed++; 73 } 74} 75print ($failed ? "not ok 3\n" : "ok 3\n"); 76 77# 4: Various flavours of file-handle to addfile 78 79open(F, "<$0"); 80 81$md5->reset; 82 83$md5->addfile(F); 84$hex = $md5->hexdigest; 85print ($hex ne '' ? "ok 4\n" : "not ok 4\n"); 86 87$orig = $hex; 88 89# 5: Fully qualified with ' operator 90 91seek(F, 0, 0); 92$md5->reset; 93$md5->addfile(MD5Test'F); 94$hex = $md5->hexdigest; 95print ($hex eq $orig ? "ok 5\n" : "not ok 5\n"); 96 97# 6: Fully qualified with :: operator 98 99seek(F, 0, 0); 100$md5->reset; 101$md5->addfile(MD5Test::F); 102$hex = $md5->hexdigest; 103print ($hex eq $orig ? "ok 6\n" : "not ok 6\n"); 104 105# 7: Type glob 106 107seek(F, 0, 0); 108$md5->reset; 109$md5->addfile(*F); 110$hex = $md5->hexdigest; 111print ($hex eq $orig ? "ok 7\n" : "not ok 7\n"); 112 113# 8: Type glob reference (the prefered mechanism) 114 115seek(F, 0, 0); 116$md5->reset; 117$md5->addfile(\*F); 118$hex = $md5->hexdigest; 119print ($hex eq $orig ? "ok 8\n" : "not ok 8\n"); 120 121# 9: File-handle passed by name (really the same as 6) 122 123seek(F, 0, 0); 124$md5->reset; 125$md5->addfile("MD5Test::F"); 126$hex = $md5->hexdigest; 127print ($hex eq $orig ? "ok 9\n" : "not ok 9\n"); 128 129# 10: Other ways of reading the data -- line at a time 130 131seek(F, 0, 0); 132$md5->reset; 133while (<F>) 134{ 135 $md5->add($_); 136} 137$hex = $md5->hexdigest; 138print ($hex eq $orig ? "ok 10\n" : "not ok 10\n"); 139 140# 11: Input lines as a list to add() 141 142seek(F, 0, 0); 143$md5->reset; 144$md5->add(<F>); 145$hex = $md5->hexdigest; 146print ($hex eq $orig ? "ok 11\n" : "not ok 11\n"); 147 148# 12: Random chunks up to 128 bytes 149 150seek(F, 0, 0); 151$md5->reset; 152while (read(F, $hexata, (rand % 128) + 1)) 153{ 154 $md5->add($hexata); 155} 156$hex = $md5->hexdigest; 157print ($hex eq $orig ? "ok 12\n" : "not ok 12\n"); 158 159# 13: All the data at once 160 161seek(F, 0, 0); 162$md5->reset; 163undef $/; 164$data = <F>; 165$hex = $md5->hexhash($data); 166print ($hex eq $orig ? "ok 13\n" : "not ok 13\n"); 167 168close(F); 169 170# 14: Using static member function 171 172$hex = MD5->hexhash($data); 173print ($hex eq $orig ? "ok 14\n" : "not ok 14\n"); 174