-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathtest_ldap_connection.rb
421 lines (341 loc) · 14.4 KB
/
test_ldap_connection.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
require_relative 'test_helper'
class TestLDAPConnection < Test::Unit::TestCase
def test_unresponsive_host
assert_raise Net::LDAP::LdapError do
Net::LDAP::Connection.new(:host => 'test.mocked.com', :port => 636)
end
end
def test_blocked_port
flexmock(Socket).should_receive(:new).and_raise(SocketError)
assert_raise Net::LDAP::LdapError do
Net::LDAP::Connection.new(:host => 'test.mocked.com', :port => 636)
end
end
def test_raises_unknown_exceptions
error = Class.new(StandardError)
flexmock(Socket).should_receive(:new).and_raise(error)
assert_raise error do
Net::LDAP::Connection.new(:host => 'test.mocked.com', :port => 636)
end
end
def test_connection_timeout
socket = flexmock(:socket)
flexmock(Socket).should_receive(:new).and_return(socket)
# standard behavior
socket.should_receive(:setsockopt)
socket.should_receive(:close)
# indicate connection happening in background (wait to write)
socket.should_receive(:connect_nonblock).
and_raise(Errno::EINPROGRESS.new.extend(IO::WaitWritable))
# time out waiting to write
flexmock(IO).should_receive(:select).and_return(nil)
assert_raise Net::LDAP::LdapError do
Net::LDAP::Connection.new(:host => "123.123.123.123", :port => 389, :timeout => 1)
end
end
def test_modify_ops_delete
args = { :operations => [ [ :delete, "mail" ] ] }
result = Net::LDAP::Connection.modify_ops(args[:operations])
expected = [ "0\r\n\x01\x010\b\x04\x04mail1\x00" ]
assert_equal(expected, result)
end
def test_modify_ops_add
args = { :operations => [ [ :add, "mail", "[email protected]" ] ] }
result = Net::LDAP::Connection.modify_ops(args[:operations])
expected = [ "0#\n\x01\x000\x1E\x04\x04mail1\x16\x04\[email protected]" ]
assert_equal(expected, result)
end
def test_modify_ops_replace
args = { :operations =>[ [ :replace, "mail", "[email protected]" ] ] }
result = Net::LDAP::Connection.modify_ops(args[:operations])
expected = [ "0#\n\x01\x020\x1E\x04\x04mail1\x16\x04\[email protected]" ]
assert_equal(expected, result)
end
def test_write
mock = flexmock("socket")
mock.should_receive(:write).with([1.to_ber, "request"].to_ber_sequence).and_return(true)
conn = Net::LDAP::Connection.new(:socket => mock)
conn.send(:write, "request")
end
def test_write_with_controls
mock = flexmock("socket")
mock.should_receive(:write).with([1.to_ber, "request", "controls"].to_ber_sequence).and_return(true)
conn = Net::LDAP::Connection.new(:socket => mock)
conn.send(:write, "request", "controls")
end
def test_write_increments_msgid
mock = flexmock("socket")
mock.should_receive(:write).with([1.to_ber, "request1"].to_ber_sequence).and_return(true)
mock.should_receive(:write).with([2.to_ber, "request2"].to_ber_sequence).and_return(true)
conn = Net::LDAP::Connection.new(:socket => mock)
conn.send(:write, "request1")
conn.send(:write, "request2")
end
end
class TestLDAPConnectionSocketReads < Test::Unit::TestCase
def make_message(message_id, options = {})
options = {
app_tag: Net::LDAP::PDU::SearchResult,
code: Net::LDAP::ResultCodeSuccess,
matched_dn: "",
error_message: ""
}.merge(options)
result = Net::BER::BerIdentifiedArray.new([options[:code], options[:matched_dn], options[:error_message]])
result.ber_identifier = options[:app_tag]
[message_id, result]
end
def test_queued_read_drains_queue_before_read
result1a = make_message(1, error_message: "one")
result1b = make_message(1, error_message: "two")
mock = flexmock("socket")
mock.should_receive(:read_ber).and_return(result1b)
conn = Net::LDAP::Connection.new(:socket => mock)
conn.message_queue[1].push Net::LDAP::PDU.new(result1a)
assert msg1 = conn.queued_read(1)
assert msg2 = conn.queued_read(1)
assert_equal 1, msg1.message_id
assert_equal "one", msg1.error_message
assert_equal 1, msg2.message_id
assert_equal "two", msg2.error_message
end
def test_queued_read_reads_until_message_id_match
result1 = make_message(1)
result2 = make_message(2)
mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
conn = Net::LDAP::Connection.new(:socket => mock)
assert result = conn.queued_read(2)
assert_equal 2, result.message_id
assert_equal 1, conn.queued_read(1).message_id
end
def test_queued_read_modify
result1 = make_message(1, app_tag: Net::LDAP::PDU::SearchResult)
result2 = make_message(2, app_tag: Net::LDAP::PDU::ModifyResponse)
mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)
conn.next_msgid # simulates ongoing query
conn.instance_variable_get("@msgid")
assert result = conn.modify(dn: "uid=modified-user1,ou=People,dc=rubyldap,dc=com",
operations: [[:add, :mail, "[email protected]"]])
assert result.success?
assert_equal 2, result.message_id
end
def test_queued_read_add
result1 = make_message(1, app_tag: Net::LDAP::PDU::SearchResult)
result2 = make_message(2, app_tag: Net::LDAP::PDU::AddResponse)
mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)
conn.next_msgid # simulates ongoing query
assert result = conn.add(dn: "uid=added-user1,ou=People,dc=rubyldap,dc=com")
assert result.success?
assert_equal 2, result.message_id
end
def test_queued_read_rename
result1 = make_message(1, app_tag: Net::LDAP::PDU::SearchResult)
result2 = make_message(2, app_tag: Net::LDAP::PDU::ModifyRDNResponse)
mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)
conn.next_msgid # simulates ongoing query
assert result = conn.rename(
olddn: "uid=renamable-user1,ou=People,dc=rubyldap,dc=com",
newrdn: "uid=renamed-user1"
)
assert result.success?
assert_equal 2, result.message_id
end
def test_queued_read_delete
result1 = make_message(1, app_tag: Net::LDAP::PDU::SearchResult)
result2 = make_message(2, app_tag: Net::LDAP::PDU::DeleteResponse)
mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)
conn.next_msgid # simulates ongoing query
assert result = conn.delete(dn: "uid=deletable-user1,ou=People,dc=rubyldap,dc=com")
assert result.success?
assert_equal 2, result.message_id
end
def test_queued_read_setup_encryption_with_start_tls
result1 = make_message(1, app_tag: Net::LDAP::PDU::SearchResult)
result2 = make_message(2, app_tag: Net::LDAP::PDU::ExtendedResponse)
mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)
flexmock(Net::LDAP::Connection).should_receive(:wrap_with_ssl).with(mock, {}).
and_return(mock)
conn.next_msgid # simulates ongoing query
assert result = conn.setup_encryption(method: :start_tls)
assert_equal mock, result
end
def test_queued_read_bind_simple
result1 = make_message(1, app_tag: Net::LDAP::PDU::SearchResult)
result2 = make_message(2, app_tag: Net::LDAP::PDU::BindResult)
mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)
conn.next_msgid # simulates ongoing query
assert result = conn.bind(
method: :simple,
username: "uid=user1,ou=People,dc=rubyldap,dc=com",
password: "passworD1")
assert result.success?
assert_equal 2, result.message_id
end
def test_queued_read_bind_sasl
result1 = make_message(1, app_tag: Net::LDAP::PDU::SearchResult)
result2 = make_message(2, app_tag: Net::LDAP::PDU::BindResult)
mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)
conn.next_msgid # simulates ongoing query
assert result = conn.bind(
method: :sasl,
mechanism: "fake",
initial_credential: "passworD1",
challenge_response: flexmock("challenge proc"))
assert result.success?
assert_equal 2, result.message_id
end
end
class TestLDAPConnectionErrors < Test::Unit::TestCase
def setup
@tcp_socket = flexmock(:connection)
@tcp_socket.should_receive(:write)
@connection = Net::LDAP::Connection.new(:socket => @tcp_socket)
end
def test_error_failed_operation
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"])
ber.ber_identifier = Net::LDAP::PDU::ModifyResponse
@tcp_socket.should_receive(:read_ber).and_return([1, ber])
result = @connection.modify(:dn => "1", :operations => [[:replace, "mail", "[email protected]"]])
assert result.failure?, "should be failure"
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
end
def test_no_error_on_success
ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""])
ber.ber_identifier = Net::LDAP::PDU::ModifyResponse
@tcp_socket.should_receive(:read_ber).and_return([1, ber])
result = @connection.modify(:dn => "1", :operations => [[:replace, "mail", "[email protected]"]])
assert result.success?, "should be success"
assert_equal "", result.error_message
end
end
class TestLDAPConnectionInstrumentation < Test::Unit::TestCase
def setup
@tcp_socket = flexmock(:connection)
@tcp_socket.should_receive(:write)
@service = MockInstrumentationService.new
@connection = Net::LDAP::Connection.new \
:socket => @tcp_socket,
:instrumentation_service => @service
end
def test_write_net_ldap_connection_event
ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""])
ber.ber_identifier = Net::LDAP::PDU::BindResult
read_result = [1, ber]
@tcp_socket.should_receive(:read_ber).and_return(read_result)
events = @service.subscribe "write.net_ldap_connection"
result = @connection.bind(method: :anon)
assert result.success?, "should be success"
# a write event
payload, result = events.pop
assert payload.has_key?(:result)
assert payload.has_key?(:content_length)
end
def test_read_net_ldap_connection_event
ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""])
ber.ber_identifier = Net::LDAP::PDU::BindResult
read_result = [1, ber]
@tcp_socket.should_receive(:read_ber).and_return(read_result)
events = @service.subscribe "read.net_ldap_connection"
result = @connection.bind(method: :anon)
assert result.success?, "should be success"
# a read event
payload, result = events.pop
assert payload.has_key?(:result)
assert_equal read_result, result
end
def test_parse_pdu_net_ldap_connection_event
ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""])
ber.ber_identifier = Net::LDAP::PDU::BindResult
read_result = [1, ber]
@tcp_socket.should_receive(:read_ber).and_return(read_result)
events = @service.subscribe "parse_pdu.net_ldap_connection"
result = @connection.bind(method: :anon)
assert result.success?, "should be success"
# a parse_pdu event
payload, result = events.pop
assert payload.has_key?(:pdu)
assert payload.has_key?(:app_tag)
assert payload.has_key?(:message_id)
assert_equal Net::LDAP::PDU::BindResult, payload[:app_tag]
assert_equal 1, payload[:message_id]
pdu = payload[:pdu]
assert_equal Net::LDAP::ResultCodeSuccess, pdu.result_code
end
def test_bind_net_ldap_connection_event
ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""])
ber.ber_identifier = Net::LDAP::PDU::BindResult
bind_result = [1, ber]
@tcp_socket.should_receive(:read_ber).and_return(bind_result)
events = @service.subscribe "bind.net_ldap_connection"
result = @connection.bind(method: :anon)
assert result.success?, "should be success"
# a read event
payload, result = events.pop
assert payload.has_key?(:result)
assert result.success?, "should be success"
end
def test_search_net_ldap_connection_event
# search data
search_data_ber = Net::BER::BerIdentifiedArray.new([1, [
"uid=user1,ou=People,dc=rubyldap,dc=com",
[ ["uid", ["user1"]] ]
]])
search_data_ber.ber_identifier = Net::LDAP::PDU::SearchReturnedData
search_data = [1, search_data_ber]
# search result (end of results)
search_result_ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""])
search_result_ber.ber_identifier = Net::LDAP::PDU::SearchResult
search_result = [1, search_result_ber]
@tcp_socket.should_receive(:read_ber).and_return(search_data).
and_return(search_result)
events = @service.subscribe "search.net_ldap_connection"
unread = @service.subscribe "search_messages_unread.net_ldap_connection"
result = @connection.search(filter: "(uid=user1)", base: "ou=People,dc=rubyldap,dc=com")
assert result.success?, "should be success"
# a search event
payload, result = events.pop
assert payload.has_key?(:result)
assert payload.has_key?(:filter)
assert_equal "(uid=user1)", payload[:filter].to_s
assert result
# ensure no unread
assert unread.empty?, "should not have any leftover unread messages"
end
end