1# Before `make install' is performed this script should be runnable with
2# `make test'. After `make install' it should work as `perl test.pl'
3
4######################### We start with some black magic to print on failure.
5
6# Change 1..1 below to 1..last_test_to_print .
7# (It may become useful if the test is moved to ./t subdirectory.)
8
9BEGIN { $| = 1; print "1..10\n"; }
10END {print "not ok 1\n" unless $loaded;}
11use Digest::Perl::MD4 qw(md4 md4_hex);;
12$loaded = 1;
13print "ok 1\n";
14
15######################### End of black magic.
16
17# Insert your test code below (better if it prints "ok 13"
18# (correspondingly "not ok 13") depending on the success of chunk 13
19# of the test code):
20
21my $testNum = 1;
22my $errors = 0;
23
24sub Printable {
25    my $a = shift;
26    my @A = split(//,$a);
27    join '', map { (ord($_) >= 040 && ord($_) < 0177
28		    ? $_
29		    : sprintf("\\x%02x", ord($_))) } @A;
30}
31
32sub Check {
33    my ($data, $result) = @_;
34    $testNum++;
35    my $hash = md4_hex($data);
36    print 'MD4 ("', Printable($data), "\") = $hash\n";
37    if ($hash ne $result) {
38	$errors++;
39	warn "Expected $result instead\n";
40	print "not ";
41    }
42    print "ok $testNum\n";
43}
44
45Check("", '31d6cfe0d16ae931b73c59d7e0c089c0');
46Check("a", 'bde52cb31de33e46245e05fbdbd6fb24');
47Check("abc", 'a448017aaf21d8525fc10ae87aa6729d');
48Check("message digest", 'd9130a8164549fe818874806e1c7014b');
49Check("abcdefghijklmnopqrstuvwxyz", 'd79e1c308aa5bbcdeea8ed63df412da9');
50Check("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
51      '043f8582f241db351ce627e153e7f0e4');
52Check("1234567890123456789012345678901234567890123456789012345678901234" .
53      "5678901234567890",
54      'e33b4ddc9c38f2199c3e7b164fcc0536');
55# From CPAN Digest-MD4-1.1:
56#  From draft-ietf-pppext-mschap-00.txt:
57Check("\x4D\x00\x79\x00\x50\x00\x77\x00"
58      => "fc156af7edcd6c0edde3337d427f4eac");
59
60# From draft-brezak-win2k-krb-rc4-hmac-03.txt
61
62sub Unicode {
63    pack 'v*', unpack 'C*', $_[0];
64}
65Check(Unicode("foo") => "ac8e657f83df82beea5d43bdaf7800cc");
66
67# regression test for CPAN Ticket 4961
68# https://rt.cpan.org/Ticket/Display.html?id=4961
69
70Check("1"x40 . "\n" . "2"x40 => "4500d7037b220939ed44938a6a3ce40b");
71
72warn "MD4 Test Failed with $errors errors.\n" if $errors;
73print "MD4 Test Succeeded\n" unless $errors;
74