33require 'net/ber'
44require 'net/ldap'
55
6- describe "Ber encoding of various types " do
6+ describe "BER encoding of" do
77 def properly_encode_and_decode
88 simple_matcher ( 'properly encode and decode' ) do |given |
99 given . to_ber . read_ber . should == given
1010 end
1111 end
1212
13- context "array " do
14- it "should properly encode []" do
13+ context "arrays " do
14+ it "should properly encode/decode []" do
1515 [ ] . should properly_encode_and_decode
1616 end
17+ it "should properly encode/decode [1,2,3]" do
18+ ary = [ 1 , 2 , 3 ]
19+ encoded_ary = ary . map { |el | el . to_ber } . to_ber
20+
21+ encoded_ary . read_ber . should == ary
22+ end
23+ end
24+ context "booleans" do
25+ it "should encode true" do
26+ true . to_ber . should == "\x01 \x01 \x01 "
27+ end
28+ it "should encode false" do
29+ false . to_ber . should == "\x01 \x01 \x00 "
30+ end
31+ end
32+ context "numbers" do
33+ # Sample based
34+ {
35+ 0 => "\x02 \x01 \x00 " ,
36+ 1 => "\x02 \x01 \x01 " ,
37+ 127 => "\x02 \x01 \x7F " ,
38+ 128 => "\x02 \x01 \x80 " ,
39+ 255 => "\x02 \x01 \xFF " ,
40+ 256 => "\x02 \x02 \x01 \x00 " ,
41+ 65535 => "\x02 \x02 \xFF \xFF " ,
42+ 65536 => "\x02 \x03 \x01 \x00 \x00 " ,
43+ 16_777_215 => "\x02 \x03 \xFF \xFF \xFF " ,
44+ 0x01000000 => "\x02 \x04 \x01 \x00 \x00 \x00 " ,
45+ 0x3FFFFFFF => "\x02 \x04 \x3F \xFF \xFF \xFF " ,
46+ 0x4FFFFFFF => "\x02 \x04 \x4F \xFF \xFF \xFF " ,
47+
48+ # Some odd samples...
49+ 5 => "\002 \001 \005 " ,
50+ 500 => "\002 \002 \001 \364 " ,
51+ 50_000 => "\x02 \x02 \xC3 P" ,
52+ 5_000_000_000 => "\002 \005 \001 *\005 \362 \000 "
53+ } . each do |number , expected_encoding |
54+ it "should encode #{ number } as #{ expected_encoding . inspect } " do
55+ number . to_ber . should == expected_encoding
56+ end
57+ end
58+
59+ # Round-trip encoding: This is mostly to be sure to cover Bignums well.
60+ context "when decoding with #read_ber" do
61+ it "should correctly handle powers of two" do
62+ 100 . times do |p |
63+ n = 2 << p
64+
65+ n . should properly_encode_and_decode
66+ end
67+ end
68+ it "should correctly handle powers of ten" do
69+ 100 . times do |p |
70+ n = 5 * 10 **p
71+
72+ n . should properly_encode_and_decode
73+ end
74+ end
75+ end
76+ end
77+ end
78+
79+ describe "BER decoding of" do
80+ context "numbers" do
81+ it "should decode #{ "\002 \001 \006 " . inspect } (6)" do
82+ "\002 \001 \006 " . read_ber ( Net ::LDAP ::AsnSyntax ) . should == 6
83+ end
84+ it "should decode #{ "\004 \007 testing" . inspect } ('testing')" do
85+ "\004 \007 testing" . read_ber ( Net ::LDAP ::AsnSyntax ) . should == 'testing'
86+ end
87+ it "should decode an ldap bind request" do
88+ "0$\002 \001 \001 `\037 \002 \001 \003 \004 \r Administrator\200 \v ad_is_bogus" .
89+ read_ber ( Net ::LDAP ::AsnSyntax ) . should ==
90+ [ 1 , [ 3 , "Administrator" , "ad_is_bogus" ] ]
91+ end
1792 end
1893end
0 commit comments