Skip to content

Commit 440ce7f

Browse files
author
Tom Maher
committed
tidy up the TLS tests
1 parent 1300bc0 commit 440ce7f

File tree

2 files changed

+70
-66
lines changed

2 files changed

+70
-66
lines changed

test/integration/test_bind.rb

+62-58
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
class TestBindIntegration < LDAPIntegrationTestCase
44
def test_bind_success
5-
assert @ldap.bind(method: :simple, username: "uid=user1,ou=People,dc=rubyldap,dc=com", password: "passworD1"), @ldap.get_operation_result.inspect
5+
assert @ldap.bind(BIND_CREDS),
6+
@ldap.get_operation_result.inspect
67
end
78

89
def test_bind_timeout
910
@ldap.port = 8389
1011
error = assert_raise Net::LDAP::Error do
11-
@ldap.bind(method: :simple, username: "uid=user1,ou=People,dc=rubyldap,dc=com", password: "passworD1")
12+
@ldap.bind BIND_CREDS
1213
end
1314
msgs = ['Operation timed out - user specified timeout',
1415
'Connection timed out - user specified timeout']
1516
assert_send([msgs, :include?, error.message])
1617
end
1718

1819
def test_bind_anonymous_fail
19-
refute @ldap.bind(method: :simple, username: "uid=user1,ou=People,dc=rubyldap,dc=com", password: ""), @ldap.get_operation_result.inspect
20+
refute @ldap.bind(BIND_CREDS.merge(password: '')),
21+
@ldap.get_operation_result.inspect
2022

2123
result = @ldap.get_operation_result
2224
assert_equal Net::LDAP::ResultCodeUnwillingToPerform, result.code
@@ -27,37 +29,40 @@ def test_bind_anonymous_fail
2729
end
2830

2931
def test_bind_fail
30-
refute @ldap.bind(method: :simple, username: "uid=user1,ou=People,dc=rubyldap,dc=com", password: "not my password"), @ldap.get_operation_result.inspect
32+
refute @ldap.bind(BIND_CREDS.merge(password: "not my password")),
33+
@ldap.get_operation_result.inspect
3134
end
3235

3336
def test_bind_tls_with_cafile
34-
tls_options = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.merge(:ca_file => CA_FILE)
35-
@ldap.encryption(method: :start_tls, tls_options: tls_options)
36-
assert @ldap.bind(method: :simple, username: "uid=user1,ou=People,dc=rubyldap,dc=com", password: "passworD1"), @ldap.get_operation_result.inspect
37+
@ldap.encryption(
38+
method: :start_tls,
39+
tls_options: TLS_OPTS.merge(ca_file: CA_FILE),
40+
)
41+
assert @ldap.bind(BIND_CREDS),
42+
@ldap.get_operation_result.inspect
3743
end
3844

3945
def test_bind_tls_with_verify_none
4046
@ldap.host = '127.0.0.1'
4147
@ldap.port = 9389
42-
tls_options = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.merge(
43-
:verify_mode => OpenSSL::SSL::VERIFY_NONE,
48+
@ldap.encryption(
49+
method: :start_tls,
50+
tls_options: TLS_OPTS.merge(verify_mode: OpenSSL::SSL::VERIFY_NONE),
4451
)
45-
@ldap.encryption(method: :start_tls, tls_options: tls_options)
46-
assert @ldap.bind(method: :simple, username: "uid=user1,ou=People,dc=rubyldap,dc=com", password: "passworD1"), @ldap.get_operation_result.inspect
52+
assert @ldap.bind(BIND_CREDS),
53+
@ldap.get_operation_result.inspect
4754
end
4855

4956
def test_bind_tls_with_bad_hostname
5057
@ldap.host = '127.0.0.1'
5158
@ldap.port = 9389
52-
tls_options = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.merge(
53-
:verify_mode => OpenSSL::SSL::VERIFY_PEER,
54-
:ca_file => CA_FILE,
59+
@ldap.encryption(
60+
method: :start_tls,
61+
tls_options: TLS_OPTS.merge(verify_mode: OpenSSL::SSL::VERIFY_PEER,
62+
ca_file: CA_FILE),
5563
)
56-
@ldap.encryption(method: :start_tls, tls_options: tls_options)
5764
error = assert_raise Net::LDAP::Error do
58-
@ldap.bind(method: :simple,
59-
username: "uid=user1,ou=People,dc=rubyldap,dc=com",
60-
password: "passworD1")
65+
@ldap.bind BIND_CREDS
6166
end
6267
assert_equal(
6368
"hostname \"#{@ldap.host}\" does not match the server certificate",
@@ -68,72 +73,71 @@ def test_bind_tls_with_bad_hostname
6873
def test_bind_tls_with_valid_hostname
6974
@ldap.host = 'localhost'
7075
@ldap.port = 9389
71-
tls_options = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.merge(
72-
:verify_mode => OpenSSL::SSL::VERIFY_PEER,
73-
:ca_file => CA_FILE,
76+
@ldap.encryption(
77+
method: :start_tls,
78+
tls_options: TLS_OPTS.merge(verify_mode: OpenSSL::SSL::VERIFY_PEER,
79+
ca_file: CA_FILE),
7480
)
75-
@ldap.encryption(method: :start_tls, tls_options: tls_options)
76-
assert @ldap.bind(method: :simple,
77-
username: "uid=user1,ou=People,dc=rubyldap,dc=com",
78-
password: "passworD1")
79-
@ldap.get_operation_result.inspect
81+
assert @ldap.bind(BIND_CREDS),
82+
@ldap.get_operation_result.inspect
8083
end
8184

8285
# The following depend on /etc/hosts hacking.
8386
# We can do that on CI, but it's less than cool on people's dev boxes
8487
def test_bind_tls_with_multiple_hosts
8588
omit_unless ENV['TRAVIS'] == 'true'
86-
tls_options = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.merge(
87-
:verify_mode => OpenSSL::SSL::VERIFY_PEER,
88-
:ca_file => CA_FILE,
89+
90+
@ldap.host = nil
91+
@ldap.hosts = [['ldap01.example.com', 389], ['ldap02.example.com', 389]]
92+
@ldap.encryption(
93+
method: :start_tls,
94+
tls_options: TLS_OPTS.merge(verify_mode: OpenSSL::SSL::VERIFY_PEER,
95+
ca_file: CA_FILE),
8996
)
90-
@ldap_multi.encryption(method: :start_tls, tls_options: tls_options)
91-
assert @ldap_multi.bind(method: :simple,
92-
username: "uid=user1,ou=People,dc=rubyldap,dc=com",
93-
password: "passworD1")
94-
@ldap_multi.get_operation_result.inspect
97+
assert @ldap.bind(BIND_CREDS),
98+
@ldap.get_operation_result.inspect
9599
end
96100

97101
def test_bind_tls_with_multiple_bogus_hosts
98102
omit_unless ENV['TRAVIS'] == 'true'
99-
tls_options = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.merge(
100-
:verify_mode => OpenSSL::SSL::VERIFY_PEER,
101-
:ca_file => CA_FILE,
103+
104+
@ldap.host = nil
105+
@ldap.hosts = [['127.0.0.1', 389], ['bogus.example.com', 389]]
106+
@ldap.encryption(
107+
method: :start_tls,
108+
tls_options: TLS_OPTS.merge(verify_mode: OpenSSL::SSL::VERIFY_PEER,
109+
ca_file: CA_FILE),
102110
)
103-
@ldap_multi.hosts = [['127.0.0.1', 389], ['bogus.example.com', 389]]
104-
@ldap_multi.encryption(method: :start_tls, tls_options: tls_options)
105111
error = assert_raise Net::LDAP::Error do
106-
@ldap_multi.bind(method: :simple,
107-
username: "uid=user1,ou=People,dc=rubyldap,dc=com",
108-
password: "passworD1")
112+
@ldap.bind BIND_CREDS
109113
end
110114
assert_equal("TODO - fix this",
111115
error.message)
112116
end
113117

114118
def test_bind_tls_with_multiple_bogus_hosts_no_verification
115119
omit_unless ENV['TRAVIS'] == 'true'
116-
tls_options = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.merge(
117-
:verify_mode => OpenSSL::SSL::VERIFY_NONE,
120+
121+
@ldap.host = nil
122+
@ldap.hosts = [['127.0.0.1', 389], ['bogus.example.com', 389]]
123+
@ldap.encryption(
124+
method: :start_tls,
125+
tls_options: TLS_OPTS.merge(verify_mode: OpenSSL::SSL::VERIFY_NONE),
118126
)
119-
@ldap_multi.hosts = [['127.0.0.1', 389], ['bogus.example.com', 389]]
120-
@ldap_multi.encryption(method: :start_tls, tls_options: tls_options)
121-
assert @ldap_multi.bind(method: :simple,
122-
username: "uid=user1,ou=People,dc=rubyldap,dc=com",
123-
password: "passworD1")
124-
@ldap_multi.get_operation_result.inspect
127+
assert @ldap.bind(BIND_CREDS),
128+
@ldap.get_operation_result.inspect
125129
end
126130

127131
def test_bind_tls_with_multiple_bogus_hosts_ca_check_only
128132
omit_unless ENV['TRAVIS'] == 'true'
129-
tls_options = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.merge(
130-
:ca_file => CA_FILE,
133+
134+
@ldap.host = nil
135+
@ldap.hosts = [['127.0.0.1', 389], ['bogus.example.com', 389]]
136+
@ldap.encryption(
137+
method: :start_tls,
138+
tls_options: TLS_OPTS.merge(ca_file: CA_FILE),
131139
)
132-
@ldap_multi.hosts = [['127.0.0.1', 389], ['bogus.example.com', 389]]
133-
@ldap_multi.encryption(method: :start_tls, tls_options: tls_options)
134-
assert @ldap_multi.bind(method: :simple,
135-
username: "uid=user1,ou=People,dc=rubyldap,dc=com",
136-
password: "passworD1")
137-
@ldap_multi.get_operation_result.inspect
140+
assert @ldap.bind(BIND_CREDS),
141+
@ldap.get_operation_result.inspect
138142
end
139143
end

test/test_helper.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
end
1919
end
2020

21+
BIND_CREDS = {
22+
method: :simple,
23+
username: "uid=user1,ou=People,dc=rubyldap,dc=com",
24+
password: "passworD1",
25+
}.freeze
26+
27+
TLS_OPTS = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.merge({}).freeze
28+
2129
if RUBY_VERSION < "2.0"
2230
class String
2331
def b
@@ -62,13 +70,5 @@ def setup
6270
search_domains: %w(dc=rubyldap,dc=com),
6371
uid: 'uid',
6472
instrumentation_service: @service
65-
66-
@ldap_multi = Net::LDAP.new \
67-
hosts: [['ldap01.example.com', 389], ['ldap02.example.com', 389]],
68-
admin_user: 'uid=admin,dc=rubyldap,dc=com',
69-
admin_password: 'passworD1',
70-
search_domains: %w(dc=rubyldap,dc=com),
71-
uid: 'uid',
72-
instrumentation_service: @service
7373
end
7474
end

0 commit comments

Comments
 (0)