| 1 | BEGIN { |
| 2 | $tests = 32; |
| 3 | if ($ENV{'WITH_CRC64'}) { |
| 4 | $tests=$tests+2; |
| 5 | } |
| 6 | $| = 1; |
| 7 | |
| 8 | eval "use Test::More tests => $tests"; |
| 9 | |
| 10 | $@ and eval <<'ENDEV'; |
| 11 | $ok = 1; |
| 12 | |
| 13 | print "1..$tests\n"; |
| 14 | |
| 15 | sub ok { |
| 16 | my($res,$comment) = @_; |
| 17 | defined $comment and print "# $comment\n"; |
| 18 | $res or print "not "; |
| 19 | print "ok ", $ok++, "\n"; |
| 20 | } |
| 21 | ENDEV |
| 22 | } |
| 23 | |
| 24 | use Digest::CRC qw(crc64 crc32 crc16 crcccitt crc8 crcopenpgparmor |
| 25 | crc64_hex crc32_hex crc16_hex crcsaej1850 crcccitt_hex crc8_hex crcopenpgparmor_hex); |
| 26 | ok(1, 'use'); |
| 27 | |
| 28 | my $input = "123456789"; |
| 29 | my ($crc32,$crc16,$crcsaej1850,$crcccitt,$crc8) = (crc32($input),crc16($input),crcsaej1850($input),crcccitt($input),crc8($input)); |
| 30 | |
| 31 | if ($ENV{'WITH_CRC64'}) { |
| 32 | my $crc64 = crc64($input); |
| 33 | ok($crc64 == 5090661014116757502, 'crc64 '.$crc64); |
| 34 | $ctx = Digest::CRC->new(type=>"crc64"); |
| 35 | $ctx->add($input); |
| 36 | $crc64 = $ctx->digest; |
| 37 | ok($crc64 == 5090661014116757502, 'OO crc64 '.$crc64); |
| 38 | } |
| 39 | |
| 40 | ok($crc32 == 3421780262, 'crc32'); |
| 41 | $crc32=$crc32^0xffffffff; |
| 42 | ok(crc32($input.join('', |
| 43 | map {chr(($crc32>>(8*$_))&0xff)} (0,1,2,3))) == 0xffffffff, |
| 44 | 'crc32 Nulltest'); |
| 45 | ok($crcsaej1850 == 75, 'crcsaej1850'); |
| 46 | ok($crcccitt == 10673, 'crcccitt'); |
| 47 | ok($crc16 == 47933, 'crc16'); |
| 48 | ok($crc8 == 244, 'crc8'); |
| 49 | ok(($crc8=crc8($input.chr($crc8))) == 0, 'crc8 Nulltest'); |
| 50 | my $ctx; $ctx = Digest::CRC->new(); |
| 51 | $ctx->add($input); |
| 52 | ok($ctx->digest == 3421780262, 'OO crc32'); |
| 53 | |
| 54 | $crc32=$crc32^0xffffffff; |
| 55 | |
| 56 | |
| 57 | # addfile |
| 58 | open(F,"<README")||die "Cannot open README"; |
| 59 | $ctx->addfile(F); |
| 60 | close(F); |
| 61 | my $y = $ctx->digest; |
| 62 | ok($y == 2527249122, 'OO crc32 with addfile '.$y); |
| 63 | |
| 64 | # start at offset >0 with previous checksum result |
| 65 | $ctx = Digest::CRC->new(type=>"crc32",cont=>1,init=>460478609); |
| 66 | open(F,"<README")||die "Cannot open README"; |
| 67 | use Fcntl qw(:seek); |
| 68 | seek(F,989,Fcntl::SEEK_SET); |
| 69 | $ctx->addfile(F); |
| 70 | close(F); |
| 71 | $y = $ctx->digest; |
| 72 | ok($y == 2316035660, 'OO crc32 with init and addfile '.$y); |
| 73 | |
| 74 | $ctx = Digest::CRC->new(type=>"crcccitt"); |
| 75 | $ctx->add($input); |
| 76 | ok($ctx->digest == 10673, 'OO crcccitt'); |
| 77 | |
| 78 | $ctx = Digest::CRC->new(type=>"crc16"); |
| 79 | $ctx->add($input); |
| 80 | ok($ctx->digest == 47933, 'OO crc16'); |
| 81 | |
| 82 | $ctx = Digest::CRC->new(width=>16,init=>0,xorout=>0,refout=>1,poly=>0x3456, |
| 83 | refin=>1,cont=>0); |
| 84 | $ctx->add($input); |
| 85 | ok($ctx->digest == 12803, 'OO crc16 poly 3456'); |
| 86 | |
| 87 | $ctx = Digest::CRC->new(type=>"crc8"); |
| 88 | $ctx->add($input); |
| 89 | ok($ctx->digest == 244, 'OO crc8'); |
| 90 | |
| 91 | # crc8 test from Mathis Moder <[email protected]> |
| 92 | $ctx = Digest::CRC->new(width=>8, init=>0xab, xorout=>0x00, refout=>0, poly=>0x07, |
| 93 | refin=>0, cont=>0); |
| 94 | $ctx->add($input); |
| 95 | ok($ctx->digest == 135, 'OO crc8 init=ab'); |
| 96 | |
| 97 | $ctx = Digest::CRC->new(width=>8, init=>0xab, xorout=>0xff, refout=>1, poly=>0x07, |
| 98 | refin=>1, cont=>0); |
| 99 | $ctx->add("f1"); |
| 100 | ok($ctx->digest == 106, 'OO crc8 init=ab, refout'); |
| 101 | |
| 102 | $input = join '', 'aa'..'zz'; |
| 103 | ($crc32,$crc16,$crcccitt,$crc8) = (crc32($input),crc16($input),crcccitt($input),crc8($input)); |
| 104 | |
| 105 | # some more large messages |
| 106 | ok($crc32 == 0xCDA63E54, 'crc32'); |
| 107 | ok($crcccitt == 0x9702, 'crcccitt'); |
| 108 | ok($crc16 == 0x0220, 'crc16'); |
| 109 | ok($crc8 == 0x82, 'crc8'); |
| 110 | |
| 111 | # hex digest |
| 112 | my $hexinput = "ae"; |
| 113 | ($crc32,$crc16,$crcccitt,$crc8,$crcopenpgparmor) = (crc32_hex($hexinput),crc16_hex($hexinput),crcccitt_hex($hexinput),crc8_hex($hexinput),crcopenpgparmor_hex($hexinput)); |
| 114 | ok($crc32 == "00e7ddce", 'crc32_hex'); # width padding to 4 bytes |
| 115 | ok($crcccitt == "1917", 'crcccitt_hex'); |
| 116 | ok($crc16 == "bbe9", 'crc16_hex'); |
| 117 | ok($crc8 == "dc", 'crc8_hex'); |
| 118 | ok($crcopenpgparmor == "3e653a", 'crcopenpgparmor_hex'); |
| 119 | |
| 120 | $ctx = Digest::CRC->new(type=>"crc8"); |
| 121 | $ctx->add($hexinput); |
| 122 | ok($ctx->hexdigest == "dc", 'OO crc8 hex'); |
| 123 | $ctx = Digest::CRC->new(type=>"crc16"); |
| 124 | $ctx->add($hexinput); |
| 125 | ok($ctx->hexdigest == "bbe9", 'OO crc16 hex'); |
| 126 | $ctx = Digest::CRC->new(type=>"crc32"); |
| 127 | $ctx->add($hexinput); |
| 128 | ok($ctx->hexdigest == "00e7ddce", 'OO crc32 hex'); |
| 129 | $ctx = Digest::CRC->new(type=>"crcccitt"); |
| 130 | $ctx->add($hexinput); |
| 131 | ok($ctx->hexdigest == "1917", 'OO crcccitt hex'); |
| 132 | $ctx = Digest::CRC->new(type=>"crcopenpgparmor"); |
| 133 | $ctx->add($hexinput); |
| 134 | ok($ctx->hexdigest == "3e653a", 'OO crcopenpgparmor hex'); |
| 135 | |
| 136 | # openpgparmor |
| 137 | my $openpgparmor = crcopenpgparmor($input); |
| 138 | ok($openpgparmor == 4874579, 'openpgparmor '.$openpgparmor); |