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
|
require_relative 'test_helper'
class TestLDAPInstrumentation < Test::Unit::TestCase
# Fake Net::LDAP::Connection for testing
class FakeConnection
# It's difficult to instantiate Net::LDAP::PDU objects. Faking out what we
# need here until that object is brought under test and has it's constructor
# cleaned up.
class Result < Struct.new(:success?, :result_code); end
def initialize
@bind_success = Result.new(true, Net::LDAP::ResultCodeSuccess)
@search_success = Result.new(true, Net::LDAP::ResultCodeSizeLimitExceeded)
end
def bind(args = {})
@bind_success
end
def search(*args)
yield @search_success if block_given?
@search_success
end
end
def setup
@connection = flexmock(:connection, :close => true)
flexmock(Net::LDAP::Connection).should_receive(:new).and_return(@connection)
@service = MockInstrumentationService.new
@subject = Net::LDAP.new \
:host => "test.mocked.com", :port => 636,
:force_no_page => true, # so server capabilities are not queried
:instrumentation_service => @service
end
def test_instrument_bind
events = @service.subscribe "bind.net_ldap"
fake_connection = FakeConnection.new
@subject.connection = fake_connection
bind_result = fake_connection.bind
assert @subject.bind
payload, result = events.pop
assert result
assert_equal bind_result, payload[:bind]
end
def test_instrument_search
events = @service.subscribe "search.net_ldap"
fake_connection = FakeConnection.new
@subject.connection = fake_connection
entry = fake_connection.search
refute_nil @subject.search(:filter => "(uid=user1)")
payload, result = events.pop
assert_equal [entry], result
assert_equal [entry], payload[:result]
assert_equal "(uid=user1)", payload[:filter]
end
def test_instrument_search_with_size
events = @service.subscribe "search.net_ldap"
fake_connection = FakeConnection.new
@subject.connection = fake_connection
entry = fake_connection.search
refute_nil @subject.search(:filter => "(uid=user1)", :size => 1)
payload, result = events.pop
assert_equal [entry], result
assert_equal [entry], payload[:result]
assert_equal "(uid=user1)", payload[:filter]
assert_equal result.size, payload[:size]
end
def test_obscure_auth
password = "opensesame"
assert_include(@subject.inspect, "anonymous")
@subject.auth "joe_user", password
assert_not_include(@subject.inspect, password)
end
def test_encryption
enc = @subject.encryption('start_tls')
assert_equal enc[:method], :start_tls
end
def test_normalize_encryption_symbol
enc = @subject.send(:normalize_encryption, :start_tls)
assert_equal enc, :method => :start_tls, :tls_options => {}
end
def test_normalize_encryption_nil
enc = @subject.send(:normalize_encryption, nil)
assert_equal enc, nil
end
def test_normalize_encryption_string
enc = @subject.send(:normalize_encryption, 'start_tls')
assert_equal enc, :method => :start_tls, :tls_options => {}
end
def test_normalize_encryption_hash
enc = @subject.send(:normalize_encryption, :method => :start_tls, :tls_options => { :foo => :bar })
assert_equal enc, :method => :start_tls, :tls_options => { :foo => :bar }
end
end
|