1 |
BEGIN { $| = 1; print "1..14\n"; } |
2 |
|
3 |
use common::sense; |
4 |
use Convert::BER::XS ':all'; |
5 |
|
6 |
$Convert::BER::XS::SNMP_PROFILE->_set_default; |
7 |
|
8 |
our $test; |
9 |
sub ok($;$) { |
10 |
print $_[0] ? "" : "not ", "ok ", ++$test, " # $_[1]\n"; |
11 |
} |
12 |
|
13 |
sub fail { |
14 |
my ($hex, $match) = @_; |
15 |
|
16 |
y/ //d for $hex; |
17 |
|
18 |
ok (!eval { ber_decode pack "H*", $hex; 1 }, "# fail $hex"); |
19 |
$@ =~ s/ at .*//s; |
20 |
ok ($@ =~ $match, "# $@ =~ $match"); |
21 |
} |
22 |
|
23 |
sub roundtrip { |
24 |
my ($hex, $is, $hex2) = @_; |
25 |
|
26 |
$hex2 ||= $hex; |
27 |
|
28 |
y/ //d for $hex, $hex2; |
29 |
|
30 |
fail "$hex,", "trailing garbage"; |
31 |
|
32 |
my $bin = pack "H*", $hex; |
33 |
my $bin2 = pack "H*", $hex2; |
34 |
|
35 |
my ($dec0, $len) = ber_decode_prefix $bin . chr rand 256; |
36 |
|
37 |
ok ($len == length $bin, "prefix length $len"); |
38 |
|
39 |
my $dec = ber_decode $bin; |
40 |
ok (&ber_is ($dec, @$is), "decode $hex => @$dec"); |
41 |
|
42 |
my $enc = ber_encode $dec; |
43 |
ok ($enc eq $bin2, "encode $hex2 == " . unpack "H*", $enc); |
44 |
|
45 |
ok (($enc eq ber_encode $dec0), "identical recode"); |
46 |
} |
47 |
|
48 |
roundtrip "400416583721", [ASN_APPLICATION, SNMP_IPADDRESS, 0, "22.88.55.33"]; |
49 |
fail "4003165837", "invalid length 3"; |
50 |
roundtrip "42050087654321", [ASN_APPLICATION, SNMP_UNSIGNED32, 0, 0x87654321]; |
51 |
|
52 |
|
53 |
|
54 |
|