net-ldap 0.16.3 → 0.17.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.rdoc +36 -0
- data/README.rdoc +9 -2
- data/lib/net/ldap.rb +25 -2
- data/lib/net/ldap/connection.rb +1 -1
- data/lib/net/ldap/dataset.rb +2 -2
- data/lib/net/ldap/entry.rb +13 -2
- data/lib/net/ldap/error.rb +1 -26
- data/lib/net/ldap/password.rb +4 -0
- data/lib/net/ldap/version.rb +1 -1
- metadata +12 -88
- data/.gitignore +0 -10
- data/.rubocop.yml +0 -20
- data/.rubocop_todo.yml +0 -723
- data/.travis.yml +0 -57
- data/CONTRIBUTING.md +0 -54
- data/Gemfile +0 -2
- data/Rakefile +0 -23
- data/net-ldap.gemspec +0 -37
- data/script/changelog +0 -47
- data/script/ldap-docker +0 -12
- data/script/package +0 -7
- data/script/release +0 -16
- data/test/ber/core_ext/test_array.rb +0 -22
- data/test/ber/core_ext/test_string.rb +0 -25
- data/test/ber/test_ber.rb +0 -153
- data/test/fixtures/ca/docker-ca.pem +0 -18
- data/test/fixtures/ldif/06-retcode.ldif +0 -75
- data/test/fixtures/ldif/50-seed.ldif +0 -374
- data/test/integration/test_add.rb +0 -26
- data/test/integration/test_ber.rb +0 -30
- data/test/integration/test_bind.rb +0 -221
- data/test/integration/test_delete.rb +0 -29
- data/test/integration/test_open.rb +0 -87
- data/test/integration/test_password_modify.rb +0 -93
- data/test/integration/test_return_codes.rb +0 -46
- data/test/integration/test_search.rb +0 -77
- data/test/support/vm/openldap/.gitignore +0 -1
- data/test/test_auth_adapter.rb +0 -15
- data/test/test_dn.rb +0 -43
- data/test/test_entry.rb +0 -66
- data/test/test_filter.rb +0 -223
- data/test/test_filter_parser.rb +0 -29
- data/test/test_helper.rb +0 -73
- data/test/test_ldap.rb +0 -114
- data/test/test_ldap_connection.rb +0 -505
- data/test/test_ldif.rb +0 -104
- data/test/test_password.rb +0 -10
- data/test/test_rename.rb +0 -77
- data/test/test_search.rb +0 -39
- data/test/test_snmp.rb +0 -118
- data/test/test_ssl_ber.rb +0 -44
- data/test/testdata.ldif +0 -101
- data/testserver/ldapserver.rb +0 -200
- data/testserver/testdata.ldif +0 -101
data/test/test_filter_parser.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class TestFilterParser < Test::Unit::TestCase
|
6
|
-
def test_ascii
|
7
|
-
assert_kind_of Net::LDAP::Filter, Net::LDAP::Filter::FilterParser.parse("(cn=name)")
|
8
|
-
end
|
9
|
-
|
10
|
-
def test_multibyte_characters
|
11
|
-
assert_kind_of Net::LDAP::Filter, Net::LDAP::Filter::FilterParser.parse("(cn=名前)")
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_brackets
|
15
|
-
assert_kind_of Net::LDAP::Filter, Net::LDAP::Filter::FilterParser.parse("(cn=[{something}])")
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_slash
|
19
|
-
assert_kind_of Net::LDAP::Filter, Net::LDAP::Filter::FilterParser.parse("(departmentNumber=FOO//BAR/FOO)")
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_colons
|
23
|
-
assert_kind_of Net::LDAP::Filter, Net::LDAP::Filter::FilterParser.parse("(ismemberof=cn=edu:berkeley:app:calmessages:deans,ou=campus groups,dc=berkeley,dc=edu)")
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_attr_tag
|
27
|
-
assert_kind_of Net::LDAP::Filter, Net::LDAP::Filter::FilterParser.parse("(mail;primary=jane@example.org)")
|
28
|
-
end
|
29
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
# Add 'lib' to load path.
|
2
|
-
require 'test/unit'
|
3
|
-
require_relative '../lib/net/ldap'
|
4
|
-
require 'flexmock/test_unit'
|
5
|
-
|
6
|
-
# Whether integration tests should be run.
|
7
|
-
INTEGRATION = ENV.fetch("INTEGRATION", "skip") != "skip"
|
8
|
-
|
9
|
-
# The CA file to verify certs against for tests.
|
10
|
-
# Override with CA_FILE env variable; otherwise checks for the VM-specific path
|
11
|
-
# and falls back to the test/fixtures/cacert.pem for local testing.
|
12
|
-
CA_FILE =
|
13
|
-
ENV.fetch("CA_FILE") do
|
14
|
-
if File.exist?("/etc/ssl/certs/cacert.pem")
|
15
|
-
"/etc/ssl/certs/cacert.pem"
|
16
|
-
else
|
17
|
-
File.expand_path("fixtures/ca/docker-ca.pem", File.dirname(__FILE__))
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
BIND_CREDS = {
|
22
|
-
method: :simple,
|
23
|
-
username: "cn=admin,dc=example,dc=org",
|
24
|
-
password: "admin",
|
25
|
-
}.freeze
|
26
|
-
|
27
|
-
TLS_OPTS = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS.merge({}).freeze
|
28
|
-
|
29
|
-
if RUBY_VERSION < "2.0"
|
30
|
-
class String
|
31
|
-
def b
|
32
|
-
self
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
class MockInstrumentationService
|
38
|
-
def initialize
|
39
|
-
@events = {}
|
40
|
-
end
|
41
|
-
|
42
|
-
def instrument(event, payload)
|
43
|
-
result = yield(payload)
|
44
|
-
@events[event] ||= []
|
45
|
-
@events[event] << [payload, result]
|
46
|
-
result
|
47
|
-
end
|
48
|
-
|
49
|
-
def subscribe(event)
|
50
|
-
@events[event] ||= []
|
51
|
-
@events[event]
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
class LDAPIntegrationTestCase < Test::Unit::TestCase
|
56
|
-
# If integration tests aren't enabled, noop these tests.
|
57
|
-
if !INTEGRATION
|
58
|
-
def run(*)
|
59
|
-
self
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def setup
|
64
|
-
@service = MockInstrumentationService.new
|
65
|
-
@ldap = Net::LDAP.new \
|
66
|
-
host: ENV.fetch('INTEGRATION_HOST', 'localhost'),
|
67
|
-
port: ENV.fetch('INTEGRATION_PORT', 389),
|
68
|
-
search_domains: %w(dc=example,dc=org),
|
69
|
-
uid: 'uid',
|
70
|
-
instrumentation_service: @service
|
71
|
-
@ldap.authenticate "cn=admin,dc=example,dc=org", "admin"
|
72
|
-
end
|
73
|
-
end
|
data/test/test_ldap.rb
DELETED
@@ -1,114 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
|
3
|
-
class TestLDAPInstrumentation < Test::Unit::TestCase
|
4
|
-
# Fake Net::LDAP::Connection for testing
|
5
|
-
class FakeConnection
|
6
|
-
# It's difficult to instantiate Net::LDAP::PDU objects. Faking out what we
|
7
|
-
# need here until that object is brought under test and has it's constructor
|
8
|
-
# cleaned up.
|
9
|
-
class Result < Struct.new(:success?, :result_code); end
|
10
|
-
|
11
|
-
def initialize
|
12
|
-
@bind_success = Result.new(true, Net::LDAP::ResultCodeSuccess)
|
13
|
-
@search_success = Result.new(true, Net::LDAP::ResultCodeSizeLimitExceeded)
|
14
|
-
end
|
15
|
-
|
16
|
-
def bind(args = {})
|
17
|
-
@bind_success
|
18
|
-
end
|
19
|
-
|
20
|
-
def search(*args)
|
21
|
-
yield @search_success if block_given?
|
22
|
-
@search_success
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def setup
|
27
|
-
@connection = flexmock(:connection, :close => true)
|
28
|
-
flexmock(Net::LDAP::Connection).should_receive(:new).and_return(@connection)
|
29
|
-
|
30
|
-
@service = MockInstrumentationService.new
|
31
|
-
@subject = Net::LDAP.new \
|
32
|
-
:host => "test.mocked.com", :port => 636,
|
33
|
-
:force_no_page => true, # so server capabilities are not queried
|
34
|
-
:instrumentation_service => @service
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_instrument_bind
|
38
|
-
events = @service.subscribe "bind.net_ldap"
|
39
|
-
|
40
|
-
fake_connection = FakeConnection.new
|
41
|
-
@subject.connection = fake_connection
|
42
|
-
bind_result = fake_connection.bind
|
43
|
-
|
44
|
-
assert @subject.bind
|
45
|
-
|
46
|
-
payload, result = events.pop
|
47
|
-
assert result
|
48
|
-
assert_equal bind_result, payload[:bind]
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_instrument_search
|
52
|
-
events = @service.subscribe "search.net_ldap"
|
53
|
-
|
54
|
-
fake_connection = FakeConnection.new
|
55
|
-
@subject.connection = fake_connection
|
56
|
-
entry = fake_connection.search
|
57
|
-
|
58
|
-
refute_nil @subject.search(:filter => "(uid=user1)")
|
59
|
-
|
60
|
-
payload, result = events.pop
|
61
|
-
assert_equal [entry], result
|
62
|
-
assert_equal [entry], payload[:result]
|
63
|
-
assert_equal "(uid=user1)", payload[:filter]
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_instrument_search_with_size
|
67
|
-
events = @service.subscribe "search.net_ldap"
|
68
|
-
|
69
|
-
fake_connection = FakeConnection.new
|
70
|
-
@subject.connection = fake_connection
|
71
|
-
entry = fake_connection.search
|
72
|
-
|
73
|
-
refute_nil @subject.search(:filter => "(uid=user1)", :size => 1)
|
74
|
-
|
75
|
-
payload, result = events.pop
|
76
|
-
assert_equal [entry], result
|
77
|
-
assert_equal [entry], payload[:result]
|
78
|
-
assert_equal "(uid=user1)", payload[:filter]
|
79
|
-
assert_equal result.size, payload[:size]
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_obscure_auth
|
83
|
-
password = "opensesame"
|
84
|
-
assert_include(@subject.inspect, "anonymous")
|
85
|
-
@subject.auth "joe_user", password
|
86
|
-
assert_not_include(@subject.inspect, password)
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_encryption
|
90
|
-
enc = @subject.encryption('start_tls')
|
91
|
-
|
92
|
-
assert_equal enc[:method], :start_tls
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_normalize_encryption_symbol
|
96
|
-
enc = @subject.send(:normalize_encryption, :start_tls)
|
97
|
-
assert_equal enc, :method => :start_tls, :tls_options => {}
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_normalize_encryption_nil
|
101
|
-
enc = @subject.send(:normalize_encryption, nil)
|
102
|
-
assert_equal enc, nil
|
103
|
-
end
|
104
|
-
|
105
|
-
def test_normalize_encryption_string
|
106
|
-
enc = @subject.send(:normalize_encryption, 'start_tls')
|
107
|
-
assert_equal enc, :method => :start_tls, :tls_options => {}
|
108
|
-
end
|
109
|
-
|
110
|
-
def test_normalize_encryption_hash
|
111
|
-
enc = @subject.send(:normalize_encryption, :method => :start_tls, :tls_options => { :foo => :bar })
|
112
|
-
assert_equal enc, :method => :start_tls, :tls_options => { :foo => :bar }
|
113
|
-
end
|
114
|
-
end
|
@@ -1,505 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
|
3
|
-
class TestLDAPConnection < Test::Unit::TestCase
|
4
|
-
def capture_stderr
|
5
|
-
stderr, $stderr = $stderr, StringIO.new
|
6
|
-
yield
|
7
|
-
$stderr.string
|
8
|
-
ensure
|
9
|
-
$stderr = stderr
|
10
|
-
end
|
11
|
-
|
12
|
-
# Fake socket for testing
|
13
|
-
#
|
14
|
-
# FakeTCPSocket.new("success", 636)
|
15
|
-
# FakeTCPSocket.new("fail.SocketError", 636) # raises SocketError
|
16
|
-
class FakeTCPSocket
|
17
|
-
def initialize(host, port, socket_opts = {})
|
18
|
-
status, error = host.split(".")
|
19
|
-
raise Object.const_get(error) if status == "fail"
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_list_of_hosts_with_first_host_successful
|
24
|
-
hosts = [
|
25
|
-
["success.host", 636],
|
26
|
-
["fail.SocketError", 636],
|
27
|
-
["fail.SocketError", 636],
|
28
|
-
]
|
29
|
-
|
30
|
-
connection = Net::LDAP::Connection.new(:hosts => hosts, :socket_class => FakeTCPSocket)
|
31
|
-
connection.socket
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_list_of_hosts_with_first_host_failure
|
35
|
-
hosts = [
|
36
|
-
["fail.SocketError", 636],
|
37
|
-
["success.host", 636],
|
38
|
-
["fail.SocketError", 636],
|
39
|
-
]
|
40
|
-
|
41
|
-
connection = Net::LDAP::Connection.new(:hosts => hosts, :socket_class => FakeTCPSocket)
|
42
|
-
connection.socket
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_list_of_hosts_with_all_hosts_failure
|
46
|
-
hosts = [
|
47
|
-
["fail.SocketError", 636],
|
48
|
-
["fail.SocketError", 636],
|
49
|
-
["fail.SocketError", 636],
|
50
|
-
]
|
51
|
-
|
52
|
-
connection = Net::LDAP::Connection.new(:hosts => hosts, :socket_class => FakeTCPSocket)
|
53
|
-
assert_raise Net::LDAP::ConnectionError do
|
54
|
-
connection.socket
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
# This belongs in test_ldap, not test_ldap_connection
|
59
|
-
def test_result_for_connection_failed_is_set
|
60
|
-
flexmock(Socket).should_receive(:tcp).and_raise(Errno::ECONNREFUSED)
|
61
|
-
|
62
|
-
ldap_client = Net::LDAP.new(host: '127.0.0.1', port: 12345)
|
63
|
-
|
64
|
-
assert_raise Net::LDAP::ConnectionRefusedError do
|
65
|
-
ldap_client.bind(method: :simple, username: 'asdf', password: 'asdf')
|
66
|
-
end
|
67
|
-
|
68
|
-
assert_equal(ldap_client.get_operation_result.code, 52)
|
69
|
-
assert_equal(ldap_client.get_operation_result.message, 'Unavailable')
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_unresponsive_host
|
73
|
-
connection = Net::LDAP::Connection.new(:host => "fail.Errno::ETIMEDOUT", :port => 636, :socket_class => FakeTCPSocket)
|
74
|
-
assert_raise Net::LDAP::Error do
|
75
|
-
connection.socket
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
def test_blocked_port
|
80
|
-
connection = Net::LDAP::Connection.new(:host => "fail.SocketError", :port => 636, :socket_class => FakeTCPSocket)
|
81
|
-
assert_raise Net::LDAP::Error do
|
82
|
-
connection.socket
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_connection_refused
|
87
|
-
connection = Net::LDAP::Connection.new(:host => "fail.Errno::ECONNREFUSED", :port => 636, :socket_class => FakeTCPSocket)
|
88
|
-
stderr = capture_stderr do
|
89
|
-
assert_raise Net::LDAP::ConnectionRefusedError do
|
90
|
-
connection.socket
|
91
|
-
end
|
92
|
-
end
|
93
|
-
assert_equal("Deprecation warning: Net::LDAP::ConnectionRefused will be deprecated. Use Errno::ECONNREFUSED instead.\n", stderr)
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_connection_timeout
|
97
|
-
connection = Net::LDAP::Connection.new(:host => "fail.Errno::ETIMEDOUT", :port => 636, :socket_class => FakeTCPSocket)
|
98
|
-
capture_stderr do
|
99
|
-
assert_raise Net::LDAP::Error do
|
100
|
-
connection.socket
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
def test_raises_unknown_exceptions
|
106
|
-
connection = Net::LDAP::Connection.new(:host => "fail.StandardError", :port => 636, :socket_class => FakeTCPSocket)
|
107
|
-
assert_raise StandardError do
|
108
|
-
connection.socket
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_modify_ops_delete
|
113
|
-
args = { :operations => [[:delete, "mail"]] }
|
114
|
-
result = Net::LDAP::Connection.modify_ops(args[:operations])
|
115
|
-
expected = ["0\r\n\x01\x010\b\x04\x04mail1\x00"]
|
116
|
-
assert_equal(expected, result)
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_modify_ops_add
|
120
|
-
args = { :operations => [[:add, "mail", "testuser@example.com"]] }
|
121
|
-
result = Net::LDAP::Connection.modify_ops(args[:operations])
|
122
|
-
expected = ["0#\n\x01\x000\x1E\x04\x04mail1\x16\x04\x14testuser@example.com"]
|
123
|
-
assert_equal(expected, result)
|
124
|
-
end
|
125
|
-
|
126
|
-
def test_modify_ops_replace
|
127
|
-
args = { :operations => [[:replace, "mail", "testuser@example.com"]] }
|
128
|
-
result = Net::LDAP::Connection.modify_ops(args[:operations])
|
129
|
-
expected = ["0#\n\x01\x020\x1E\x04\x04mail1\x16\x04\x14testuser@example.com"]
|
130
|
-
assert_equal(expected, result)
|
131
|
-
end
|
132
|
-
|
133
|
-
def test_write
|
134
|
-
mock = flexmock("socket")
|
135
|
-
mock.should_receive(:write).with([1.to_ber, "request"].to_ber_sequence).and_return(true)
|
136
|
-
conn = Net::LDAP::Connection.new(:socket => mock)
|
137
|
-
conn.send(:write, "request")
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_write_with_controls
|
141
|
-
mock = flexmock("socket")
|
142
|
-
mock.should_receive(:write).with([1.to_ber, "request", "controls"].to_ber_sequence).and_return(true)
|
143
|
-
conn = Net::LDAP::Connection.new(:socket => mock)
|
144
|
-
conn.send(:write, "request", "controls")
|
145
|
-
end
|
146
|
-
|
147
|
-
def test_write_increments_msgid
|
148
|
-
mock = flexmock("socket")
|
149
|
-
mock.should_receive(:write).with([1.to_ber, "request1"].to_ber_sequence).and_return(true)
|
150
|
-
mock.should_receive(:write).with([2.to_ber, "request2"].to_ber_sequence).and_return(true)
|
151
|
-
conn = Net::LDAP::Connection.new(:socket => mock)
|
152
|
-
conn.send(:write, "request1")
|
153
|
-
conn.send(:write, "request2")
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
class TestLDAPConnectionSocketReads < Test::Unit::TestCase
|
158
|
-
def make_message(message_id, options = {})
|
159
|
-
options = {
|
160
|
-
app_tag: Net::LDAP::PDU::SearchResult,
|
161
|
-
code: Net::LDAP::ResultCodeSuccess,
|
162
|
-
matched_dn: "",
|
163
|
-
error_message: "",
|
164
|
-
}.merge(options)
|
165
|
-
result = Net::BER::BerIdentifiedArray.new([options[:code], options[:matched_dn], options[:error_message]])
|
166
|
-
result.ber_identifier = options[:app_tag]
|
167
|
-
[message_id, result]
|
168
|
-
end
|
169
|
-
|
170
|
-
def test_queued_read_drains_queue_before_read
|
171
|
-
result1a = make_message(1, error_message: "one")
|
172
|
-
result1b = make_message(1, error_message: "two")
|
173
|
-
|
174
|
-
mock = flexmock("socket")
|
175
|
-
mock.should_receive(:read_ber).and_return(result1b)
|
176
|
-
conn = Net::LDAP::Connection.new(:socket => mock)
|
177
|
-
|
178
|
-
conn.message_queue[1].push Net::LDAP::PDU.new(result1a)
|
179
|
-
|
180
|
-
assert msg1 = conn.queued_read(1)
|
181
|
-
assert msg2 = conn.queued_read(1)
|
182
|
-
|
183
|
-
assert_equal 1, msg1.message_id
|
184
|
-
assert_equal "one", msg1.error_message
|
185
|
-
assert_equal 1, msg2.message_id
|
186
|
-
assert_equal "two", msg2.error_message
|
187
|
-
end
|
188
|
-
|
189
|
-
def test_queued_read_reads_until_message_id_match
|
190
|
-
result1 = make_message(1)
|
191
|
-
result2 = make_message(2)
|
192
|
-
|
193
|
-
mock = flexmock("socket")
|
194
|
-
mock.should_receive(:read_ber)
|
195
|
-
.and_return(result1)
|
196
|
-
.and_return(result2)
|
197
|
-
conn = Net::LDAP::Connection.new(:socket => mock)
|
198
|
-
|
199
|
-
assert result = conn.queued_read(2)
|
200
|
-
assert_equal 2, result.message_id
|
201
|
-
assert_equal 1, conn.queued_read(1).message_id
|
202
|
-
end
|
203
|
-
|
204
|
-
def test_queued_read_modify
|
205
|
-
result1 = make_message(1, app_tag: Net::LDAP::PDU::SearchResult)
|
206
|
-
result2 = make_message(2, app_tag: Net::LDAP::PDU::ModifyResponse)
|
207
|
-
|
208
|
-
mock = flexmock("socket")
|
209
|
-
mock.should_receive(:read_ber)
|
210
|
-
.and_return(result1)
|
211
|
-
.and_return(result2)
|
212
|
-
mock.should_receive(:write)
|
213
|
-
conn = Net::LDAP::Connection.new(:socket => mock)
|
214
|
-
|
215
|
-
conn.next_msgid # simulates ongoing query
|
216
|
-
|
217
|
-
conn.instance_variable_get("@msgid")
|
218
|
-
|
219
|
-
assert result = conn.modify(dn: "uid=modified-user1,ou=People,dc=rubyldap,dc=com",
|
220
|
-
operations: [[:add, :mail, "modified-user1@example.com"]])
|
221
|
-
assert result.success?
|
222
|
-
assert_equal 2, result.message_id
|
223
|
-
end
|
224
|
-
|
225
|
-
def test_queued_read_add
|
226
|
-
result1 = make_message(1, app_tag: Net::LDAP::PDU::SearchResult)
|
227
|
-
result2 = make_message(2, app_tag: Net::LDAP::PDU::AddResponse)
|
228
|
-
|
229
|
-
mock = flexmock("socket")
|
230
|
-
mock.should_receive(:read_ber)
|
231
|
-
.and_return(result1)
|
232
|
-
.and_return(result2)
|
233
|
-
mock.should_receive(:write)
|
234
|
-
conn = Net::LDAP::Connection.new(:socket => mock)
|
235
|
-
|
236
|
-
conn.next_msgid # simulates ongoing query
|
237
|
-
|
238
|
-
assert result = conn.add(dn: "uid=added-user1,ou=People,dc=rubyldap,dc=com")
|
239
|
-
assert result.success?
|
240
|
-
assert_equal 2, result.message_id
|
241
|
-
end
|
242
|
-
|
243
|
-
def test_queued_read_rename
|
244
|
-
result1 = make_message(1, app_tag: Net::LDAP::PDU::SearchResult)
|
245
|
-
result2 = make_message(2, app_tag: Net::LDAP::PDU::ModifyRDNResponse)
|
246
|
-
|
247
|
-
mock = flexmock("socket")
|
248
|
-
mock.should_receive(:read_ber)
|
249
|
-
.and_return(result1)
|
250
|
-
.and_return(result2)
|
251
|
-
mock.should_receive(:write)
|
252
|
-
conn = Net::LDAP::Connection.new(:socket => mock)
|
253
|
-
|
254
|
-
conn.next_msgid # simulates ongoing query
|
255
|
-
|
256
|
-
assert result = conn.rename(
|
257
|
-
olddn: "uid=renamable-user1,ou=People,dc=rubyldap,dc=com",
|
258
|
-
newrdn: "uid=renamed-user1",
|
259
|
-
)
|
260
|
-
assert result.success?
|
261
|
-
assert_equal 2, result.message_id
|
262
|
-
end
|
263
|
-
|
264
|
-
def test_queued_read_delete
|
265
|
-
result1 = make_message(1, app_tag: Net::LDAP::PDU::SearchResult)
|
266
|
-
result2 = make_message(2, app_tag: Net::LDAP::PDU::DeleteResponse)
|
267
|
-
|
268
|
-
mock = flexmock("socket")
|
269
|
-
mock.should_receive(:read_ber)
|
270
|
-
.and_return(result1)
|
271
|
-
.and_return(result2)
|
272
|
-
mock.should_receive(:write)
|
273
|
-
conn = Net::LDAP::Connection.new(:socket => mock)
|
274
|
-
|
275
|
-
conn.next_msgid # simulates ongoing query
|
276
|
-
|
277
|
-
assert result = conn.delete(dn: "uid=deletable-user1,ou=People,dc=rubyldap,dc=com")
|
278
|
-
assert result.success?
|
279
|
-
assert_equal 2, result.message_id
|
280
|
-
end
|
281
|
-
|
282
|
-
def test_queued_read_setup_encryption_with_start_tls
|
283
|
-
result1 = make_message(1, app_tag: Net::LDAP::PDU::SearchResult)
|
284
|
-
result2 = make_message(2, app_tag: Net::LDAP::PDU::ExtendedResponse)
|
285
|
-
|
286
|
-
mock = flexmock("socket")
|
287
|
-
mock.should_receive(:read_ber)
|
288
|
-
.and_return(result1)
|
289
|
-
.and_return(result2)
|
290
|
-
mock.should_receive(:write)
|
291
|
-
conn = Net::LDAP::Connection.new(:socket => mock)
|
292
|
-
flexmock(Net::LDAP::Connection).should_receive(:wrap_with_ssl).with(mock, {}, nil)
|
293
|
-
.and_return(mock)
|
294
|
-
|
295
|
-
conn.next_msgid # simulates ongoing query
|
296
|
-
|
297
|
-
assert result = conn.setup_encryption(method: :start_tls)
|
298
|
-
assert_equal mock, result
|
299
|
-
end
|
300
|
-
|
301
|
-
def test_queued_read_bind_simple
|
302
|
-
result1 = make_message(1, app_tag: Net::LDAP::PDU::SearchResult)
|
303
|
-
result2 = make_message(2, app_tag: Net::LDAP::PDU::BindResult)
|
304
|
-
|
305
|
-
mock = flexmock("socket")
|
306
|
-
mock.should_receive(:read_ber)
|
307
|
-
.and_return(result1)
|
308
|
-
.and_return(result2)
|
309
|
-
mock.should_receive(:write)
|
310
|
-
conn = Net::LDAP::Connection.new(:socket => mock)
|
311
|
-
|
312
|
-
conn.next_msgid # simulates ongoing query
|
313
|
-
|
314
|
-
assert result = conn.bind(
|
315
|
-
method: :simple,
|
316
|
-
username: "uid=user1,ou=People,dc=rubyldap,dc=com",
|
317
|
-
password: "passworD1",
|
318
|
-
)
|
319
|
-
assert result.success?
|
320
|
-
assert_equal 2, result.message_id
|
321
|
-
end
|
322
|
-
|
323
|
-
def test_queued_read_bind_sasl
|
324
|
-
result1 = make_message(1, app_tag: Net::LDAP::PDU::SearchResult)
|
325
|
-
result2 = make_message(2, app_tag: Net::LDAP::PDU::BindResult)
|
326
|
-
|
327
|
-
mock = flexmock("socket")
|
328
|
-
mock.should_receive(:read_ber)
|
329
|
-
.and_return(result1)
|
330
|
-
.and_return(result2)
|
331
|
-
mock.should_receive(:write)
|
332
|
-
conn = Net::LDAP::Connection.new(:socket => mock)
|
333
|
-
|
334
|
-
conn.next_msgid # simulates ongoing query
|
335
|
-
|
336
|
-
assert result = conn.bind(
|
337
|
-
method: :sasl,
|
338
|
-
mechanism: "fake",
|
339
|
-
initial_credential: "passworD1",
|
340
|
-
challenge_response: flexmock("challenge proc"),
|
341
|
-
)
|
342
|
-
assert result.success?
|
343
|
-
assert_equal 2, result.message_id
|
344
|
-
end
|
345
|
-
|
346
|
-
def test_invalid_pdu_type
|
347
|
-
options = {
|
348
|
-
code: Net::LDAP::ResultCodeSuccess,
|
349
|
-
matched_dn: "",
|
350
|
-
error_message: "",
|
351
|
-
}
|
352
|
-
ber = Net::BER::BerIdentifiedArray.new([options[:code], options[:matched_dn], options[:error_message]])
|
353
|
-
assert_raise Net::LDAP::PDU::Error do
|
354
|
-
Net::LDAP::PDU.new([0, ber])
|
355
|
-
end
|
356
|
-
end
|
357
|
-
end
|
358
|
-
|
359
|
-
class TestLDAPConnectionErrors < Test::Unit::TestCase
|
360
|
-
def setup
|
361
|
-
@tcp_socket = flexmock(:connection)
|
362
|
-
@tcp_socket.should_receive(:write)
|
363
|
-
flexmock(Socket).should_receive(:tcp).and_return(@tcp_socket)
|
364
|
-
@connection = Net::LDAP::Connection.new(:host => 'test.mocked.com', :port => 636)
|
365
|
-
end
|
366
|
-
|
367
|
-
def test_error_failed_operation
|
368
|
-
ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeUnwillingToPerform, "", "The provided password value was rejected by a password validator: The provided password did not contain enough characters from the character set 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. The minimum number of characters from that set that must be present in user passwords is 1"])
|
369
|
-
ber.ber_identifier = Net::LDAP::PDU::ModifyResponse
|
370
|
-
@tcp_socket.should_receive(:read_ber).and_return([1, ber])
|
371
|
-
|
372
|
-
result = @connection.modify(:dn => "1", :operations => [[:replace, "mail", "something@sothsdkf.com"]])
|
373
|
-
assert result.failure?, "should be failure"
|
374
|
-
assert_equal "The provided password value was rejected by a password validator: The provided password did not contain enough characters from the character set 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. The minimum number of characters from that set that must be present in user passwords is 1", result.error_message
|
375
|
-
end
|
376
|
-
|
377
|
-
def test_no_error_on_success
|
378
|
-
ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""])
|
379
|
-
ber.ber_identifier = Net::LDAP::PDU::ModifyResponse
|
380
|
-
@tcp_socket.should_receive(:read_ber).and_return([1, ber])
|
381
|
-
|
382
|
-
result = @connection.modify(:dn => "1", :operations => [[:replace, "mail", "something@sothsdkf.com"]])
|
383
|
-
assert result.success?, "should be success"
|
384
|
-
assert_equal "", result.error_message
|
385
|
-
end
|
386
|
-
end
|
387
|
-
|
388
|
-
class TestLDAPConnectionInstrumentation < Test::Unit::TestCase
|
389
|
-
def setup
|
390
|
-
@tcp_socket = flexmock(:connection)
|
391
|
-
@tcp_socket.should_receive(:write)
|
392
|
-
flexmock(Socket).should_receive(:tcp).and_return(@tcp_socket)
|
393
|
-
|
394
|
-
@service = MockInstrumentationService.new
|
395
|
-
@connection = Net::LDAP::Connection.new \
|
396
|
-
:host => 'test.mocked.com',
|
397
|
-
:port => 636,
|
398
|
-
:instrumentation_service => @service
|
399
|
-
end
|
400
|
-
|
401
|
-
def test_write_net_ldap_connection_event
|
402
|
-
ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""])
|
403
|
-
ber.ber_identifier = Net::LDAP::PDU::BindResult
|
404
|
-
read_result = [1, ber]
|
405
|
-
@tcp_socket.should_receive(:read_ber).and_return(read_result)
|
406
|
-
|
407
|
-
events = @service.subscribe "write.net_ldap_connection"
|
408
|
-
|
409
|
-
result = @connection.bind(method: :anon)
|
410
|
-
assert result.success?, "should be success"
|
411
|
-
|
412
|
-
# a write event
|
413
|
-
payload, result = events.pop
|
414
|
-
assert payload.key?(:result)
|
415
|
-
assert payload.key?(:content_length)
|
416
|
-
end
|
417
|
-
|
418
|
-
def test_read_net_ldap_connection_event
|
419
|
-
ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""])
|
420
|
-
ber.ber_identifier = Net::LDAP::PDU::BindResult
|
421
|
-
read_result = [1, ber]
|
422
|
-
@tcp_socket.should_receive(:read_ber).and_return(read_result)
|
423
|
-
|
424
|
-
events = @service.subscribe "read.net_ldap_connection"
|
425
|
-
|
426
|
-
result = @connection.bind(method: :anon)
|
427
|
-
assert result.success?, "should be success"
|
428
|
-
|
429
|
-
# a read event
|
430
|
-
payload, result = events.pop
|
431
|
-
assert payload.key?(:result)
|
432
|
-
assert_equal read_result, result
|
433
|
-
end
|
434
|
-
|
435
|
-
def test_parse_pdu_net_ldap_connection_event
|
436
|
-
ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""])
|
437
|
-
ber.ber_identifier = Net::LDAP::PDU::BindResult
|
438
|
-
read_result = [1, ber]
|
439
|
-
@tcp_socket.should_receive(:read_ber).and_return(read_result)
|
440
|
-
|
441
|
-
events = @service.subscribe "parse_pdu.net_ldap_connection"
|
442
|
-
|
443
|
-
result = @connection.bind(method: :anon)
|
444
|
-
assert result.success?, "should be success"
|
445
|
-
|
446
|
-
# a parse_pdu event
|
447
|
-
payload, result = events.pop
|
448
|
-
assert payload.key?(:pdu)
|
449
|
-
assert payload.key?(:app_tag)
|
450
|
-
assert payload.key?(:message_id)
|
451
|
-
assert_equal Net::LDAP::PDU::BindResult, payload[:app_tag]
|
452
|
-
assert_equal 1, payload[:message_id]
|
453
|
-
pdu = payload[:pdu]
|
454
|
-
assert_equal Net::LDAP::ResultCodeSuccess, pdu.result_code
|
455
|
-
end
|
456
|
-
|
457
|
-
def test_bind_net_ldap_connection_event
|
458
|
-
ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""])
|
459
|
-
ber.ber_identifier = Net::LDAP::PDU::BindResult
|
460
|
-
bind_result = [1, ber]
|
461
|
-
@tcp_socket.should_receive(:read_ber).and_return(bind_result)
|
462
|
-
|
463
|
-
events = @service.subscribe "bind.net_ldap_connection"
|
464
|
-
|
465
|
-
result = @connection.bind(method: :anon)
|
466
|
-
assert result.success?, "should be success"
|
467
|
-
|
468
|
-
# a read event
|
469
|
-
payload, result = events.pop
|
470
|
-
assert payload.key?(:result)
|
471
|
-
assert result.success?, "should be success"
|
472
|
-
end
|
473
|
-
|
474
|
-
def test_search_net_ldap_connection_event
|
475
|
-
# search data
|
476
|
-
search_data_ber = Net::BER::BerIdentifiedArray.new([1, [
|
477
|
-
"uid=user1,ou=People,dc=rubyldap,dc=com",
|
478
|
-
[["uid", ["user1"]]],
|
479
|
-
]])
|
480
|
-
search_data_ber.ber_identifier = Net::LDAP::PDU::SearchReturnedData
|
481
|
-
search_data = [1, search_data_ber]
|
482
|
-
# search result (end of results)
|
483
|
-
search_result_ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""])
|
484
|
-
search_result_ber.ber_identifier = Net::LDAP::PDU::SearchResult
|
485
|
-
search_result = [1, search_result_ber]
|
486
|
-
@tcp_socket.should_receive(:read_ber).and_return(search_data)
|
487
|
-
.and_return(search_result)
|
488
|
-
|
489
|
-
events = @service.subscribe "search.net_ldap_connection"
|
490
|
-
unread = @service.subscribe "search_messages_unread.net_ldap_connection"
|
491
|
-
|
492
|
-
result = @connection.search(filter: "(uid=user1)", base: "ou=People,dc=rubyldap,dc=com")
|
493
|
-
assert result.success?, "should be success"
|
494
|
-
|
495
|
-
# a search event
|
496
|
-
payload, result = events.pop
|
497
|
-
assert payload.key?(:result)
|
498
|
-
assert payload.key?(:filter)
|
499
|
-
assert_equal "(uid=user1)", payload[:filter].to_s
|
500
|
-
assert result
|
501
|
-
|
502
|
-
# ensure no unread
|
503
|
-
assert unread.empty?, "should not have any leftover unread messages"
|
504
|
-
end
|
505
|
-
end
|