Skip to content

Commit 1538672

Browse files
author
Jerry Cheung
committed
convert to test/unit
1 parent 028402b commit 1538672

File tree

1 file changed

+95
-110
lines changed

1 file changed

+95
-110
lines changed

test/ber/test_ber.rb

Lines changed: 95 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,125 @@
1-
require 'spec_helper'
1+
require 'common'
22

3-
require 'net/ber'
4-
require 'net/ldap'
3+
class TestBEREncoding < Test::Unit::TestCase
4+
def test_empty_array
5+
assert_equal [], [].to_ber.read_ber
6+
end
57

6-
describe "BER encoding of" do
8+
def test_array
9+
ary = [1,2,3]
10+
encoded_ary = ary.map { |el| el.to_ber }.to_ber
711

8-
RSpec::Matchers.define :properly_encode_and_decode do
9-
match do |given|
10-
given.to_ber.read_ber.should == given
11-
end
12+
assert_equal ary, encoded_ary.read_ber
1213
end
1314

14-
context "arrays" do
15-
it "should properly encode/decode []" do
16-
[].should properly_encode_and_decode
17-
end
18-
it "should properly encode/decode [1,2,3]" do
19-
ary = [1,2,3]
20-
encoded_ary = ary.map { |el| el.to_ber }.to_ber
15+
def test_true
16+
assert_equal "\x01\x01\x01", true.to_ber
17+
end
2118

22-
encoded_ary.read_ber.should == ary
23-
end
19+
def test_false
20+
assert_equal "\x01\x01\x00", false.to_ber
2421
end
25-
context "booleans" do
26-
it "should encode true" do
27-
true.to_ber.should == "\x01\x01\x01"
28-
end
29-
it "should encode false" do
30-
false.to_ber.should == "\x01\x01\x00"
22+
23+
# Sample based
24+
{
25+
0 => "\x02\x01\x00",
26+
1 => "\x02\x01\x01",
27+
127 => "\x02\x01\x7F",
28+
128 => "\x02\x01\x80",
29+
255 => "\x02\x01\xFF",
30+
256 => "\x02\x02\x01\x00",
31+
65535 => "\x02\x02\xFF\xFF",
32+
65536 => "\x02\x03\x01\x00\x00",
33+
16_777_215 => "\x02\x03\xFF\xFF\xFF",
34+
0x01000000 => "\x02\x04\x01\x00\x00\x00",
35+
0x3FFFFFFF => "\x02\x04\x3F\xFF\xFF\xFF",
36+
0x4FFFFFFF => "\x02\x04\x4F\xFF\xFF\xFF",
37+
38+
# Some odd samples...
39+
5 => "\002\001\005",
40+
500 => "\002\002\001\364",
41+
50_000 => "\x02\x02\xC3P",
42+
5_000_000_000 => "\002\005\001*\005\362\000"
43+
}.each do |number, expected_encoding|
44+
define_method "test_encode_#{number}" do
45+
assert_equal expected_encoding.b, number.to_ber
3146
end
3247
end
33-
context "numbers" do
34-
# Sample based
35-
{
36-
0 => raw_string("\x02\x01\x00"),
37-
1 => raw_string("\x02\x01\x01"),
38-
127 => raw_string("\x02\x01\x7F"),
39-
128 => raw_string("\x02\x01\x80"),
40-
255 => raw_string("\x02\x01\xFF"),
41-
256 => raw_string("\x02\x02\x01\x00"),
42-
65535 => raw_string("\x02\x02\xFF\xFF"),
43-
65536 => raw_string("\x02\x03\x01\x00\x00"),
44-
16_777_215 => raw_string("\x02\x03\xFF\xFF\xFF"),
45-
0x01000000 => raw_string("\x02\x04\x01\x00\x00\x00"),
46-
0x3FFFFFFF => raw_string("\x02\x04\x3F\xFF\xFF\xFF"),
47-
0x4FFFFFFF => raw_string("\x02\x04\x4F\xFF\xFF\xFF"),
48-
49-
# Some odd samples...
50-
5 => raw_string("\002\001\005"),
51-
500 => raw_string("\002\002\001\364"),
52-
50_000 => raw_string("\x02\x02\xC3P"),
53-
5_000_000_000 => raw_string("\002\005\001*\005\362\000")
54-
}.each do |number, expected_encoding|
55-
it "should encode #{number} as #{expected_encoding.inspect}" do
56-
number.to_ber.should == expected_encoding
57-
end
48+
49+
# Round-trip encoding: This is mostly to be sure to cover Bignums well.
50+
def test_powers_of_two
51+
100.times do |p|
52+
n = 2 << p
53+
54+
assert_equal n, n.to_ber.read_ber
5855
end
56+
end
57+
58+
def test_powers_of_ten
59+
100.times do |p|
60+
n = 5 * 10**p
5961

60-
# Round-trip encoding: This is mostly to be sure to cover Bignums well.
61-
context "when decoding with #read_ber" do
62-
it "should correctly handle powers of two" do
63-
100.times do |p|
64-
n = 2 << p
65-
66-
n.should properly_encode_and_decode
67-
end
68-
end
69-
it "should correctly handle powers of ten" do
70-
100.times do |p|
71-
n = 5 * 10**p
72-
73-
n.should properly_encode_and_decode
74-
end
75-
end
62+
assert_equal n, n.to_ber.read_ber
7663
end
7764
end
65+
7866
if "Ruby 1.9".respond_to?(:encoding)
79-
context "strings" do
80-
it "should properly encode UTF-8 strings" do
81-
"\u00e5".force_encoding("UTF-8").to_ber.should ==
82-
raw_string("\x04\x02\xC3\xA5")
83-
end
84-
it "should properly encode strings encodable as UTF-8" do
85-
"teststring".encode("US-ASCII").to_ber.should == "\x04\nteststring"
86-
end
87-
it "should properly encode binary data strings using to_ber_bin" do
88-
# This is used for searching for GUIDs in Active Directory
89-
["6a31b4a12aa27a41aca9603f27dd5116"].pack("H*").to_ber_bin.should ==
90-
raw_string("\x04\x10" + "j1\xB4\xA1*\xA2zA\xAC\xA9`?'\xDDQ\x16")
91-
end
92-
it "should not fail on strings that can not be converted to UTF-8" do
93-
expect { "\x81".to_ber }.not_to raise_error
94-
end
67+
def test_encode_utf8_strings
68+
assert_equal "\x04\x02\xC3\xA5".b, "\u00e5".force_encoding("UTF-8").to_ber
9569
end
96-
end
97-
end
9870

99-
describe "BER decoding of" do
100-
context "numbers" do
101-
it "should decode #{"\002\001\006".inspect} (6)" do
102-
"\002\001\006".read_ber(Net::LDAP::AsnSyntax).should == 6
71+
def test_utf8_encodable_strings
72+
assert_equal "\x04\nteststring", "teststring".encode("US-ASCII").to_ber
10373
end
104-
it "should decode #{"\004\007testing".inspect} ('testing')" do
105-
"\004\007testing".read_ber(Net::LDAP::AsnSyntax).should == 'testing'
74+
75+
def test_encode_binary_data
76+
# This is used for searching for GUIDs in Active Directory
77+
assert_equal "\x04\x10" + "j1\xB4\xA1*\xA2zA\xAC\xA9`?'\xDDQ\x16".b,
78+
["6a31b4a12aa27a41aca9603f27dd5116"].pack("H*").to_ber_bin
10679
end
107-
it "should decode an ldap bind request" do
108-
"0$\002\001\001`\037\002\001\003\004\rAdministrator\200\vad_is_bogus".
109-
read_ber(Net::LDAP::AsnSyntax).should ==
110-
[1, [3, "Administrator", "ad_is_bogus"]]
80+
81+
def test_non_utf8_encodable_strings
82+
assert_equal "\x04\x01\x81".b, "\x81".to_ber
11183
end
11284
end
11385
end
11486

115-
describe Net::BER::BerIdentifiedString do
116-
describe "initialize" do
117-
subject { Net::BER::BerIdentifiedString.new(data) }
87+
class TestBERDecoding < Test::Unit::TestCase
88+
def test_decode_number
89+
assert_equal 6, "\002\001\006".read_ber(Net::LDAP::AsnSyntax)
90+
end
11891

119-
context "binary data" do
120-
let(:data) { ["6a31b4a12aa27a41aca9603f27dd5116"].pack("H*").force_encoding("ASCII-8BIT") }
92+
def test_decode_string
93+
assert_equal "testing", "\004\007testing".read_ber(Net::LDAP::AsnSyntax)
94+
end
12195

122-
specify { subject.valid_encoding?.should == true }
123-
specify { subject.encoding.name.should == "ASCII-8BIT" }
124-
end
96+
def test_decode_ldap_bind_request
97+
assert_equal [1, [3, "Administrator", "ad_is_bogus"]], "0$\002\001\001`\037\002\001\003\004\rAdministrator\200\vad_is_bogus".read_ber(Net::LDAP::AsnSyntax)
98+
end
99+
end
125100

126-
context "ascii data in UTF-8" do
127-
let(:data) { "some text".force_encoding("UTF-8") }
101+
class TestBERIdentifiedString < Test::Unit::TestCase
102+
def test_binary_data
103+
data = ["6a31b4a12aa27a41aca9603f27dd5116"].pack("H*").force_encoding("ASCII-8BIT")
104+
bis = Net::BER::BerIdentifiedString.new(data)
128105

129-
specify { subject.valid_encoding?.should == true }
130-
specify { subject.encoding.name.should == "UTF-8" }
131-
end
106+
assert_predicate bis, :valid_encoding?
107+
assert_equal "ASCII-8BIT", bis.encoding.name
108+
end
132109

133-
context "UTF-8 data in UTF-8" do
134-
let(:data) { ["e4b8ad"].pack("H*").force_encoding("UTF-8") }
110+
def test_ascii_data_in_utf8
111+
data = "some text".force_encoding("UTF-8")
112+
bis = Net::BER::BerIdentifiedString.new(data)
135113

136-
specify { subject.valid_encoding?.should == true }
137-
specify { subject.encoding.name.should == "UTF-8" }
138-
end
114+
assert_predicate bis, :valid_encoding?
115+
assert_equal "UTF-8", bis.encoding.name
116+
end
117+
118+
def test_ut8_data_in_utf8
119+
data = ["e4b8ad"].pack("H*").force_encoding("UTF-8")
120+
bis = Net::BER::BerIdentifiedString.new(data)
121+
122+
assert_predicate bis, :valid_encoding?
123+
assert_equal "UTF-8", bis.encoding.name
139124
end
140125
end

0 commit comments

Comments
 (0)