From fefe50dcef5f3b9dd0d3ab9839eb577c2c36c135 Mon Sep 17 00:00:00 2001 From: Kaspar Schiess Date: Mon, 5 Apr 2010 19:16:15 +0200 Subject: [PATCH] . Converts test_ber into specs --- spec/unit/ber/ber_spec.rb | 81 +++++++++++++++++++++++++++++++++++++-- test/test_ber.rb | 78 ------------------------------------- 2 files changed, 78 insertions(+), 81 deletions(-) delete mode 100644 test/test_ber.rb diff --git a/spec/unit/ber/ber_spec.rb b/spec/unit/ber/ber_spec.rb index 547f9a7..9506122 100644 --- a/spec/unit/ber/ber_spec.rb +++ b/spec/unit/ber/ber_spec.rb @@ -3,16 +3,91 @@ require 'spec_helper' require 'net/ber' require 'net/ldap' -describe "Ber encoding of various types" do +describe "BER encoding of" do def properly_encode_and_decode simple_matcher('properly encode and decode') do |given| given.to_ber.read_ber.should == given end end - context "array" do - it "should properly encode []" do + context "arrays" do + it "should properly encode/decode []" do [].should properly_encode_and_decode end + it "should properly encode/decode [1,2,3]" do + ary = [1,2,3] + encoded_ary = ary.map { |el| el.to_ber }.to_ber + + encoded_ary.read_ber.should == ary + end + end + context "booleans" do + it "should encode true" do + true.to_ber.should == "\x01\x01\x01" + end + it "should encode false" do + false.to_ber.should == "\x01\x01\x00" + end + end + context "numbers" do + # Sample based + { + 0 => "\x02\x01\x00", + 1 => "\x02\x01\x01", + 127 => "\x02\x01\x7F", + 128 => "\x02\x01\x80", + 255 => "\x02\x01\xFF", + 256 => "\x02\x02\x01\x00", + 65535 => "\x02\x02\xFF\xFF", + 65536 => "\x02\x03\x01\x00\x00", + 16_777_215 => "\x02\x03\xFF\xFF\xFF", + 0x01000000 => "\x02\x04\x01\x00\x00\x00", + 0x3FFFFFFF => "\x02\x04\x3F\xFF\xFF\xFF", + 0x4FFFFFFF => "\x02\x04\x4F\xFF\xFF\xFF", + + # Some odd samples... + 5 => "\002\001\005", + 500 => "\002\002\001\364", + 50_000 => "\x02\x02\xC3P", + 5_000_000_000 => "\002\005\001*\005\362\000" + }.each do |number, expected_encoding| + it "should encode #{number} as #{expected_encoding.inspect}" do + number.to_ber.should == expected_encoding + end + end + + # Round-trip encoding: This is mostly to be sure to cover Bignums well. + context "when decoding with #read_ber" do + it "should correctly handle powers of two" do + 100.times do |p| + n = 2 << p + + n.should properly_encode_and_decode + end + end + it "should correctly handle powers of ten" do + 100.times do |p| + n = 5 * 10**p + + n.should properly_encode_and_decode + end + end + end + end +end + +describe "BER decoding of" do + context "numbers" do + it "should decode #{"\002\001\006".inspect} (6)" do + "\002\001\006".read_ber(Net::LDAP::AsnSyntax).should == 6 + end + it "should decode #{"\004\007testing".inspect} ('testing')" do + "\004\007testing".read_ber(Net::LDAP::AsnSyntax).should == 'testing' + end + it "should decode an ldap bind request" do + "0$\002\001\001`\037\002\001\003\004\rAdministrator\200\vad_is_bogus". + read_ber(Net::LDAP::AsnSyntax).should == + [1, [3, "Administrator", "ad_is_bogus"]] + end end end \ No newline at end of file diff --git a/test/test_ber.rb b/test/test_ber.rb deleted file mode 100644 index defe969..0000000 --- a/test/test_ber.rb +++ /dev/null @@ -1,78 +0,0 @@ -# encoding: ASCII-8BIT -require 'common' - -class TestBer < Test::Unit::TestCase - - def test_encode_boolean - assert_equal( "\x01\x01\x01", true.to_ber ) # should actually be: 01 01 ff - assert_equal( "\x01\x01\x00", false.to_ber ) - end - - #def test_encode_nil - # assert_equal( "\x05\x00", nil.to_ber ) - #end - - def test_encode_integer - - # Fixnum - # - #assert_equal( "\x02\x02\x96\x46", -27_066.to_ber ) - #assert_equal( "\x02\x02\xFF\x7F", -129.to_ber ) - #assert_equal( "\x02\x01\x80", -128.to_ber ) - #assert_equal( "\x02\x01\xFF", -1.to_ber ) - - assert_equal( "\x02\x01\x00", 0.to_ber ) - assert_equal( "\x02\x01\x01", 1.to_ber ) - assert_equal( "\x02\x01\x7F", 127.to_ber ) - assert_equal( "\x02\x01\x80", 128.to_ber ) - assert_equal( "\x02\x01\xFF", 255.to_ber ) - - assert_equal( "\x02\x02\x01\x00", 256.to_ber ) - assert_equal( "\x02\x02\xFF\xFF", 65535.to_ber ) - - assert_equal( "\x02\x03\x01\x00\x00", 65536.to_ber ) - assert_equal( "\x02\x03\xFF\xFF\xFF", 16_777_215.to_ber ) - - assert_equal( "\x02\x04\x01\x00\x00\x00", 0x01000000.to_ber ) - assert_equal( "\x02\x04\x3F\xFF\xFF\xFF", 0x3FFFFFFF.to_ber ) - - # Bignum - # - assert_equal( "\x02\x04\x4F\xFF\xFF\xFF", 0x4FFFFFFF.to_ber ) - #assert_equal( "\x02\x05\x00\xFF\xFF\xFF\xFF", 0xFFFFFFFF.to_ber ) - end - - # TOD Add some much bigger numbers - # 5000000000 is a Bignum, which hits different code. - def test_ber_integers - assert_equal( "\002\001\005", 5.to_ber ) - assert_equal( "\002\002\001\364", 500.to_ber ) - assert_equal( "\x02\x02\xC3P", 50000.to_ber ) - assert_equal( "\002\005\001*\005\362\000", 5000000000.to_ber ) - end - - def test_ber_bignums - # Some of these values are Fixnums and some are Bignums. Different BER code. - 100.times do |p| - n = 2 << p - assert_equal(n, n.to_ber.read_ber, "2**#{p} could not be read back") - - n = 5 * 10**p - assert_equal(n, n.to_ber.read_ber) - end - end - - def test_ber_parsing - assert_equal( 6, "\002\001\006".read_ber( Net::LDAP::AsnSyntax )) - assert_equal( "testing", "\004\007testing".read_ber( Net::LDAP::AsnSyntax )) - end - - def test_ber_parser_on_ldap_bind_request - s = StringIO.new( - "0$\002\001\001`\037\002\001\003\004\rAdministrator\200\vad_is_bogus" ) - - assert_equal( - [1, [3, "Administrator", "ad_is_bogus"]], - s.read_ber( Net::LDAP::AsnSyntax )) - end -end