Convert-BER-XS

 view release on metacpan or  search on metacpan

t/02_simple.t  view on Meta::CPAN

BEGIN { $| = 1; print "1..459\n"; }

use common::sense;
use Convert::BER::XS ':all';

our $test;
sub ok($;$) {
   print $_[0] ? "" : "not ", "ok ", ++$test, " # $_[1]\n";
}

sub fail {
   my ($hex, $match) = @_;

   y/ //d for $hex;

   ok (!eval { ber_decode pack "H*", $hex; 1 }, "# fail $hex");
   $@ =~ s/ at .*//s;
   ok ($@ =~ $match, "# $@ =~ $match");
}

sub roundtrip {
   my ($hex, $is, $hex2) = @_;

   $hex2 ||= $hex;

   y/ //d for $hex, $hex2;

   fail "$hex,", "trailing garbage";

   my $bin  = pack "H*", $hex;
   my $bin2 = pack "H*", $hex2;

   my ($dec0, $len) = ber_decode_prefix $bin . chr rand 256;

   ok ($len == length $bin, "prefix length $len");

   my $dec = ber_decode $bin;
   ok (&ber_is ($dec, @$is), "decode $hex => @$dec");

   my $enc = ber_encode $dec;
   ok ($enc eq $bin2, "encode $hex2 == " . unpack "H*", $enc);

   ok (($enc eq ber_encode $dec0), "identical recode");
}

# extended tags
roundtrip "1f020105", [ASN_UNIVERSAL, ASN_INTEGER, 0, 5], "020105";

# padding
roundtrip "020100", [ASN_UNIVERSAL, ASN_INTEGER, 0, 0];
roundtrip "0201ff", [ASN_UNIVERSAL, ASN_INTEGER, 0, -1];
fail "0202ffff", "X.690 8.3.2";
fail "02020001", "X.690 8.3.2";
fail "0208ffffffffffffffff", "X.690 8.3.2";

# types
roundtrip "020100", [ASN_UNIVERSAL, ASN_INTEGER, 0, 0];
roundtrip "020105", [ASN_UNIVERSAL, ASN_INTEGER, 0, 5];
roundtrip "0201ff", [ASN_UNIVERSAL, ASN_INTEGER, 0, -1];
roundtrip "020200ff", [ASN_UNIVERSAL, ASN_INTEGER, 0, 255];
roundtrip "020500ffffffff", [ASN_UNIVERSAL, ASN_INTEGER, 0, 4294967295];
roundtrip "020488776655", [ASN_UNIVERSAL, ASN_INTEGER, 0, -2005440939];
roundtrip "02050088776655", [ASN_UNIVERSAL, ASN_INTEGER, 0, 2289526357];

# 64 bit tests, clunky
if (8 == length pack "j", 0) {
   roundtrip "0208feffffffffffffff", [ASN_UNIVERSAL, ASN_INTEGER, 0, -72057594037927937];



( run in 1.976 second using v1.01-cache-2.11-cpan-a49fcb8fa48 )