ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Convert-BER-XS/t/02_simple.t
Revision: 1.3
Committed: Sat Apr 20 15:57:28 2019 UTC (5 years, 1 month ago) by root
Content type: application/x-troff
Branch: MAIN
Changes since 1.2: +4 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.3 BEGIN { $| = 1; print "1..92\n"; }
2 root 1.1
3     use common::sense;
4     use Convert::BER::XS ':all';
5    
6     our $test;
7     sub ok($;$) {
8     print $_[0] ? "" : "not ", "ok ", ++$test, " # $_[1]\n";
9     }
10    
11     sub roundtrip {
12     my ($hex, $is, $hex2) = @_;
13    
14     $hex2 ||= $hex;
15    
16     y/ //d for $hex, $hex2;
17    
18     my $bin2 = pack "H*", $hex2;
19    
20     my $dec = ber_decode pack "H*", $hex;
21     ok (&ber_is ($dec, @$is), "decode $hex => @$dec");
22     my $enc = ber_encode $dec;
23     ok ($enc eq $bin2, "encode $hex2 == " . unpack "H*", $enc);
24     }
25    
26     sub fail {
27     my ($hex, $match) = @_;
28    
29     y/ //d for $hex;
30    
31     ok (!eval { ber_decode pack "H*", $hex; 1 }, "# fail $hex");
32     $@ =~ s/ at .*//s;
33     ok ($@ =~ $match, "# $@ =~ $match");
34     }
35    
36 root 1.3 roundtrip "1f020105", [ASN_UNIVERSAL, ASN_INTEGER, 0, 5], "020105";
37     roundtrip "1f80020105", [ASN_UNIVERSAL, ASN_INTEGER, 0, 5], "020105";
38    
39 root 1.2 roundtrip "020105", [ASN_UNIVERSAL, ASN_INTEGER, 0, 5];
40     roundtrip "0201ff", [ASN_UNIVERSAL, ASN_INTEGER, 0, -1];
41     roundtrip "020200ff", [ASN_UNIVERSAL, ASN_INTEGER, 0, 255];
42     roundtrip "0204ffffffff", [ASN_UNIVERSAL, ASN_INTEGER, 0, -1], "0201ff";
43     roundtrip "020500ffffffff", [ASN_UNIVERSAL, ASN_INTEGER, 0, 4294967295];
44     roundtrip "020488776655", [ASN_UNIVERSAL, ASN_INTEGER, 0, -2005440939];
45     roundtrip "02050088776655", [ASN_UNIVERSAL, ASN_INTEGER, 0, 2289526357];
46 root 1.1
47     # 64 bit tests, clunky
48     if (8 == length pack "j", 0) {
49 root 1.2 roundtrip "0208ffffffffffffffff", [ASN_UNIVERSAL, ASN_INTEGER, 0, -1], "0201ff";
50     roundtrip "020900ffffffffffffffff", [ASN_UNIVERSAL, ASN_INTEGER, 0, 18446744073709551615];
51     roundtrip "0209007fffffffffffffff", [ASN_UNIVERSAL, ASN_INTEGER, 0, 9223372036854775807], "02087fffffffffffffff";
52     roundtrip "0209008fffffffffffffff", [ASN_UNIVERSAL, ASN_INTEGER, 0, 10376293541461622783], "0209008fffffffffffffff";
53 root 1.1 } else {
54     ok (1) for 1 .. 2 * 3;
55     }
56    
57     roundtrip "010100", [ASN_UNIVERSAL, ASN_BOOLEAN, 0, 0];
58     roundtrip "010101", [ASN_UNIVERSAL, ASN_BOOLEAN, 0, 1], "0101ff";
59     roundtrip "010180", [ASN_UNIVERSAL, ASN_BOOLEAN, 0, 1], "0101ff";
60     roundtrip "0101ff", [ASN_UNIVERSAL, ASN_BOOLEAN, 0, 1], "0101ff";
61     fail "0100" , "BER_TYPE_BOOLEAN value with invalid length";
62     fail "01020000", "BER_TYPE_BOOLEAN value with invalid length";
63    
64     roundtrip "0303353637", [ASN_UNIVERSAL, ASN_BIT_STRING, 0, "567"];
65     roundtrip "0403353637", [ASN_UNIVERSAL, ASN_OCTET_STRING, 0, "567"];
66     roundtrip "0402a0ff", [ASN_UNIVERSAL, ASN_OCTET_STRING, 0, "\xa0\xff"];
67     roundtrip "0400", [ASN_UNIVERSAL, ASN_OCTET_STRING, 0, ""];
68     fail "040201", "unexpected end of message buffer";
69     roundtrip "0500", [ASN_UNIVERSAL, ASN_NULL, 0];
70     roundtrip "0500", [ASN_UNIVERSAL, ASN_NULL, 0];
71     fail "050101", "BER_TYPE_NULL value with non-zero length";
72    
73     roundtrip "06053305818219", [ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, 0, "1.11.5.16665"];
74     fail "06053305818299", "unexpected end of message buffer";
75     roundtrip "0d053305818219", [ASN_UNIVERSAL, ASN_RELATIVE_OID, 0, "51.5.16665"];
76     roundtrip "0703353739", [ASN_UNIVERSAL, ASN_OBJECT_DESCRIPTOR, 0, "579"];
77     roundtrip "0a0177", [ASN_UNIVERSAL, ASN_ENUMERATED, 0, 0x77];
78     roundtrip "2b00", [ASN_UNIVERSAL, ASN_EMBEDDED_PDV, 1];
79     roundtrip "0c04c2a0c3bf", [ASN_UNIVERSAL, ASN_UTF8_STRING, 0, "\xa0\xff"];
80     roundtrip "3000", [ASN_UNIVERSAL, ASN_SEQUENCE, 1];
81     roundtrip "3100", [ASN_UNIVERSAL, ASN_SET, 1];
82     roundtrip "1603393334", [ASN_UNIVERSAL, ASN_ASCII_STRING, 0, "934"];
83    
84     roundtrip "1c0400000031", [ASN_UNIVERSAL, ASN_UNIVERSAL_STRING, 0, "1"];
85     roundtrip "1c0411223344", [ASN_UNIVERSAL, ASN_UNIVERSAL_STRING, 0, chr 0x11223344];
86     fail "1c0111", "BER_TYPE_UCS has an invalid number of octets";
87     fail "1c021122", "BER_TYPE_UCS has an invalid number of octets";
88     fail "1c03112234", "BER_TYPE_UCS has an invalid number of octets";
89    
90     roundtrip "1e0400310037", [ASN_UNIVERSAL, ASN_BMP_STRING, 0, "17"];
91     roundtrip "1e0411223344", [ASN_UNIVERSAL, ASN_BMP_STRING, 0, "\x{1122}\x{3344}"];
92     fail "1e0111", "BER_TYPE_UCS has an invalid number of octets";
93     fail "1e03112234", "BER_TYPE_UCS has an invalid number of octets";
94    
95     # TODO, verify they are all raw octets
96     # ASN_NUMERIC_STRING = 0x12,
97     # ASN_PRINTABLE_STRING = 0x13,
98     # ASN_TELETEX_STRING = 0x14,
99     # ASN_T61_STRING = 0x14,
100     # ASN_VIDEOTEX_STRING = 0x15,
101     # ASN_IA5_STRING = 0x16,
102     # ASN_UTC_TIME = 0x17,
103     # ASN_GENERALIZED_TIME = 0x18,
104     # ASN_GRAPHIC_STRING = 0x19,
105     # ASN_VISIBLE_STRING = 0x1a,
106     # ASN_ISO646_STRING = 0x1a,
107     # ASN_GENERAL_STRING = 0x1b,
108     # ASN_CHARACTER_STRING = 0x1d,
109