Skip to content

Commit 417f457

Browse files
committed
Merge pull request #82 from smangelsdorf/repair-broken-tests
Fix for test failures due to String encoding and ruby-openssl issues
2 parents d698d38 + 839c50f commit 417f457

File tree

7 files changed

+56
-33
lines changed

7 files changed

+56
-33
lines changed

lib/net/ldap.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,14 +1160,24 @@ def getbyte
11601160
end
11611161
end
11621162

1163+
module FixSSLSocketSyncClose
1164+
def close
1165+
super
1166+
io.close
1167+
end
1168+
end
1169+
11631170
def self.wrap_with_ssl(io)
11641171
raise Net::LDAP::LdapError, "OpenSSL is unavailable" unless Net::LDAP::HasOpenSSL
11651172
ctx = OpenSSL::SSL::SSLContext.new
11661173
conn = OpenSSL::SSL::SSLSocket.new(io, ctx)
11671174
conn.connect
1168-
conn.sync_close = true
1175+
1176+
# Doesn't work:
1177+
# conn.sync_close = true
11691178

11701179
conn.extend(GetbyteForSSLSocket) unless conn.respond_to?(:getbyte)
1180+
conn.extend(FixSSLSocketSyncClose)
11711181

11721182
conn
11731183
end

spec/integration/ssl_ber_spec.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
require 'spec_helper'
22

33
require 'net/ldap'
4+
require 'timeout'
45

56
describe "BER serialisation (SSL)" do
67
# Transmits str to #to and reads it back from #from.
78
#
89
def transmit(str)
9-
to.write(str)
10-
to.close
11-
12-
from.read
10+
Timeout::timeout(1) do
11+
to.write(str)
12+
to.close
13+
14+
from.read
15+
end
1316
end
1417

1518
attr_reader :to, :from

spec/spec_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22

33
RSpec.configure do |config|
44
config.mock_with :flexmock
5+
6+
def raw_string(s)
7+
# Conveniently, String#b only needs to be called when it exists
8+
s.respond_to?(:b) ? s.b : s
9+
end
510
end

spec/unit/ber/ber_spec.rb

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,28 @@
3333
context "numbers" do
3434
# Sample based
3535
{
36-
0 => "\x02\x01\x00",
37-
1 => "\x02\x01\x01",
38-
127 => "\x02\x01\x7F",
39-
128 => "\x02\x01\x80",
40-
255 => "\x02\x01\xFF",
41-
256 => "\x02\x02\x01\x00",
42-
65535 => "\x02\x02\xFF\xFF",
43-
65536 => "\x02\x03\x01\x00\x00",
44-
16_777_215 => "\x02\x03\xFF\xFF\xFF",
45-
0x01000000 => "\x02\x04\x01\x00\x00\x00",
46-
0x3FFFFFFF => "\x02\x04\x3F\xFF\xFF\xFF",
47-
0x4FFFFFFF => "\x02\x04\x4F\xFF\xFF\xFF",
48-
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+
4949
# Some odd samples...
50-
5 => "\002\001\005",
51-
500 => "\002\002\001\364",
52-
50_000 => "\x02\x02\xC3P",
53-
5_000_000_000 => "\002\005\001*\005\362\000"
54-
}.each do |number, expected_encoding|
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|
5555
it "should encode #{number} as #{expected_encoding.inspect}" do
5656
number.to_ber.should == expected_encoding
57-
end
57+
end
5858
end
5959

6060
# Round-trip encoding: This is mostly to be sure to cover Bignums well.
@@ -79,15 +79,15 @@
7979
context "strings" do
8080
it "should properly encode UTF-8 strings" do
8181
"\u00e5".force_encoding("UTF-8").to_ber.should ==
82-
"\x04\x02\xC3\xA5"
82+
raw_string("\x04\x02\xC3\xA5")
8383
end
8484
it "should properly encode strings encodable as UTF-8" do
8585
"teststring".encode("US-ASCII").to_ber.should == "\x04\nteststring"
8686
end
8787
it "should properly encode binary data strings using to_ber_bin" do
8888
# This is used for searching for GUIDs in Active Directory
8989
["6a31b4a12aa27a41aca9603f27dd5116"].pack("H*").to_ber_bin.should ==
90-
"\x04\x10" + "j1\xB4\xA1*\xA2zA\xAC\xA9`?'\xDDQ\x16"
90+
raw_string("\x04\x10" + "j1\xB4\xA1*\xA2zA\xAC\xA9`?'\xDDQ\x16")
9191
end
9292
it "should not fail on strings that can not be converted to UTF-8" do
9393
error = Encoding::UndefinedConversionError

spec/unit/ber/core_ext/string_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
context "when passed an ldap bind request and some extra data" do
77
attr_reader :str, :result
88
before(:each) do
9-
@str = "0$\002\001\001`\037\002\001\003\004\rAdministrator\200\vad_is_bogus UNCONSUMED"
9+
@str = raw_string("0$\002\001\001`\037\002\001\003\004\rAdministrator\200\vad_is_bogus UNCONSUMED")
1010
@result = str.read_ber!(Net::LDAP::AsnSyntax)
1111
end
1212

@@ -22,7 +22,7 @@
2222
before(:each) do
2323
stub_exception_class = Class.new(StandardError)
2424

25-
@initial_value = "0$\002\001\001`\037\002\001\003\004\rAdministrator\200\vad_is_bogus"
25+
@initial_value = raw_string("0$\002\001\001`\037\002\001\003\004\rAdministrator\200\vad_is_bogus")
2626
@str = initial_value.dup
2727

2828
# Defines a string

spec/unit/ldap/filter_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ def eq(attribute, value)
8383
end
8484

8585
context 'with a well-known BER string' do
86-
ber = "\xa4\x2d" \
86+
ber = raw_string("\xa4\x2d" \
8787
"\x04\x0b" "objectclass" \
8888
"\x30\x1e" \
8989
"\x80\x08" "foo" "*\\" "bar" \
9090
"\x81\x08" "foo" "*\\" "bar" \
91-
"\x82\x08" "foo" "*\\" "bar"
91+
"\x82\x08" "foo" "*\\" "bar")
9292

9393
describe "<- .to_ber" do
9494
[

test/test_snmp.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
require 'net/snmp'
55

66
class TestSnmp < Test::Unit::TestCase
7-
SnmpGetRequest = "0'\002\001\000\004\006public\240\032\002\002?*\002\001\000\002\001\0000\0160\f\006\b+\006\001\002\001\001\001\000\005\000"
8-
SnmpGetResponse = "0+\002\001\000\004\006public\242\036\002\002'\017\002\001\000\002\001\0000\0220\020\006\b+\006\001\002\001\001\001\000\004\004test"
7+
def self.raw_string(s)
8+
# Conveniently, String#b only needs to be called when it exists
9+
s.respond_to?(:b) ? s.b : s
10+
end
11+
12+
SnmpGetRequest = raw_string("0'\002\001\000\004\006public\240\032\002\002?*\002\001\000\002\001\0000\0160\f\006\b+\006\001\002\001\001\001\000\005\000")
13+
SnmpGetResponse = raw_string("0+\002\001\000\004\006public\242\036\002\002'\017\002\001\000\002\001\0000\0220\020\006\b+\006\001\002\001\001\001\000\004\004test")
914

10-
SnmpGetRequestXXX = "0'\002\001\000\004\006xxxxxx\240\032\002\002?*\002\001\000\002\001\0000\0160\f\006\b+\006\001\002\001\001\001\000\005\000"
15+
SnmpGetRequestXXX = raw_string("0'\002\001\000\004\006xxxxxx\240\032\002\002?*\002\001\000\002\001\0000\0160\f\006\b+\006\001\002\001\001\001\000\005\000")
1116

1217
def test_invalid_packet
1318
data = "xxxx"

0 commit comments

Comments
 (0)