Chris@0
|
1 # $Id: testldap.rb 65 2006-04-23 01:17:49Z blackhedd $
|
Chris@0
|
2 #
|
Chris@0
|
3 #
|
Chris@0
|
4
|
Chris@0
|
5
|
Chris@0
|
6 $:.unshift "lib"
|
Chris@0
|
7
|
Chris@0
|
8 require 'test/unit'
|
Chris@0
|
9
|
Chris@0
|
10 require 'net/ldap'
|
Chris@0
|
11 require 'stringio'
|
Chris@0
|
12
|
Chris@0
|
13
|
Chris@0
|
14 class TestLdapClient < Test::Unit::TestCase
|
Chris@0
|
15
|
Chris@0
|
16 # TODO: these tests crash and burn if the associated
|
Chris@0
|
17 # LDAP testserver isn't up and running.
|
Chris@0
|
18 # We rely on being able to read a file with test data
|
Chris@0
|
19 # in LDIF format.
|
Chris@0
|
20 # TODO, WARNING: for the moment, this data is in a file
|
Chris@0
|
21 # whose name and location are HARDCODED into the
|
Chris@0
|
22 # instance method load_test_data.
|
Chris@0
|
23
|
Chris@0
|
24 def setup
|
Chris@0
|
25 @host = "127.0.0.1"
|
Chris@0
|
26 @port = 3890
|
Chris@0
|
27 @auth = {
|
Chris@0
|
28 :method => :simple,
|
Chris@0
|
29 :username => "cn=bigshot,dc=bayshorenetworks,dc=com",
|
Chris@0
|
30 :password => "opensesame"
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 @ldif = load_test_data
|
Chris@0
|
34 end
|
Chris@0
|
35
|
Chris@0
|
36
|
Chris@0
|
37
|
Chris@0
|
38 # Get some test data which will be used to validate
|
Chris@0
|
39 # the responses from the test LDAP server we will
|
Chris@0
|
40 # connect to.
|
Chris@0
|
41 # TODO, Bogus: we are HARDCODING the location of the file for now.
|
Chris@0
|
42 #
|
Chris@0
|
43 def load_test_data
|
Chris@0
|
44 ary = File.readlines( "tests/testdata.ldif" )
|
Chris@0
|
45 hash = {}
|
Chris@0
|
46 while line = ary.shift and line.chomp!
|
Chris@0
|
47 if line =~ /^dn:[\s]*/i
|
Chris@0
|
48 dn = $'
|
Chris@0
|
49 hash[dn] = {}
|
Chris@0
|
50 while attr = ary.shift and attr.chomp! and attr =~ /^([\w]+)[\s]*:[\s]*/
|
Chris@0
|
51 hash[dn][$1.downcase.intern] ||= []
|
Chris@0
|
52 hash[dn][$1.downcase.intern] << $'
|
Chris@0
|
53 end
|
Chris@0
|
54 end
|
Chris@0
|
55 end
|
Chris@0
|
56 hash
|
Chris@0
|
57 end
|
Chris@0
|
58
|
Chris@0
|
59
|
Chris@0
|
60
|
Chris@0
|
61 # Binding tests.
|
Chris@0
|
62 # Need tests for all kinds of network failures and incorrect auth.
|
Chris@0
|
63 # TODO: Implement a class-level timeout for operations like bind.
|
Chris@0
|
64 # Search has a timeout defined at the protocol level, other ops do not.
|
Chris@0
|
65 # TODO, use constants for the LDAP result codes, rather than hardcoding them.
|
Chris@0
|
66 def test_bind
|
Chris@0
|
67 ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
|
Chris@0
|
68 assert_equal( true, ldap.bind )
|
Chris@0
|
69 assert_equal( 0, ldap.get_operation_result.code )
|
Chris@0
|
70 assert_equal( "Success", ldap.get_operation_result.message )
|
Chris@0
|
71
|
Chris@0
|
72 bad_username = @auth.merge( {:username => "cn=badguy,dc=imposters,dc=com"} )
|
Chris@0
|
73 ldap = Net::LDAP.new :host => @host, :port => @port, :auth => bad_username
|
Chris@0
|
74 assert_equal( false, ldap.bind )
|
Chris@0
|
75 assert_equal( 48, ldap.get_operation_result.code )
|
Chris@0
|
76 assert_equal( "Inappropriate Authentication", ldap.get_operation_result.message )
|
Chris@0
|
77
|
Chris@0
|
78 bad_password = @auth.merge( {:password => "cornhusk"} )
|
Chris@0
|
79 ldap = Net::LDAP.new :host => @host, :port => @port, :auth => bad_password
|
Chris@0
|
80 assert_equal( false, ldap.bind )
|
Chris@0
|
81 assert_equal( 49, ldap.get_operation_result.code )
|
Chris@0
|
82 assert_equal( "Invalid Credentials", ldap.get_operation_result.message )
|
Chris@0
|
83 end
|
Chris@0
|
84
|
Chris@0
|
85
|
Chris@0
|
86
|
Chris@0
|
87 def test_search
|
Chris@0
|
88 ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
|
Chris@0
|
89
|
Chris@0
|
90 search = {:base => "dc=smalldomain,dc=com"}
|
Chris@0
|
91 assert_equal( false, ldap.search( search ))
|
Chris@0
|
92 assert_equal( 32, ldap.get_operation_result.code )
|
Chris@0
|
93
|
Chris@0
|
94 search = {:base => "dc=bayshorenetworks,dc=com"}
|
Chris@0
|
95 assert_equal( true, ldap.search( search ))
|
Chris@0
|
96 assert_equal( 0, ldap.get_operation_result.code )
|
Chris@0
|
97
|
Chris@0
|
98 ldap.search( search ) {|res|
|
Chris@0
|
99 assert_equal( res, @ldif )
|
Chris@0
|
100 }
|
Chris@0
|
101 end
|
Chris@0
|
102
|
Chris@0
|
103
|
Chris@0
|
104
|
Chris@0
|
105
|
Chris@0
|
106 # This is a helper routine for test_search_attributes.
|
Chris@0
|
107 def internal_test_search_attributes attrs_to_search
|
Chris@0
|
108 ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
|
Chris@0
|
109 assert( ldap.bind )
|
Chris@0
|
110
|
Chris@0
|
111 search = {
|
Chris@0
|
112 :base => "dc=bayshorenetworks,dc=com",
|
Chris@0
|
113 :attributes => attrs_to_search
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 ldif = @ldif
|
Chris@0
|
117 ldif.each {|dn,entry|
|
Chris@0
|
118 entry.delete_if {|attr,value|
|
Chris@0
|
119 ! attrs_to_search.include?(attr)
|
Chris@0
|
120 }
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 assert_equal( true, ldap.search( search ))
|
Chris@0
|
124 ldap.search( search ) {|res|
|
Chris@0
|
125 res_keys = res.keys.sort
|
Chris@0
|
126 ldif_keys = ldif.keys.sort
|
Chris@0
|
127 assert( res_keys, ldif_keys )
|
Chris@0
|
128 res.keys.each {|rk|
|
Chris@0
|
129 assert( res[rk], ldif[rk] )
|
Chris@0
|
130 }
|
Chris@0
|
131 }
|
Chris@0
|
132 end
|
Chris@0
|
133
|
Chris@0
|
134
|
Chris@0
|
135 def test_search_attributes
|
Chris@0
|
136 internal_test_search_attributes [:mail]
|
Chris@0
|
137 internal_test_search_attributes [:cn]
|
Chris@0
|
138 internal_test_search_attributes [:ou]
|
Chris@0
|
139 internal_test_search_attributes [:hasaccessprivilege]
|
Chris@0
|
140 internal_test_search_attributes ["mail"]
|
Chris@0
|
141 internal_test_search_attributes ["cn"]
|
Chris@0
|
142 internal_test_search_attributes ["ou"]
|
Chris@0
|
143 internal_test_search_attributes ["hasaccessrole"]
|
Chris@0
|
144
|
Chris@0
|
145 internal_test_search_attributes [:mail, :cn, :ou, :hasaccessrole]
|
Chris@0
|
146 internal_test_search_attributes [:mail, "cn", :ou, "hasaccessrole"]
|
Chris@0
|
147 end
|
Chris@0
|
148
|
Chris@0
|
149
|
Chris@0
|
150 def test_search_filters
|
Chris@0
|
151 ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
|
Chris@0
|
152 search = {
|
Chris@0
|
153 :base => "dc=bayshorenetworks,dc=com",
|
Chris@0
|
154 :filter => Net::LDAP::Filter.eq( "sn", "Fosse" )
|
Chris@0
|
155 }
|
Chris@0
|
156
|
Chris@0
|
157 ldap.search( search ) {|res|
|
Chris@0
|
158 p res
|
Chris@0
|
159 }
|
Chris@0
|
160 end
|
Chris@0
|
161
|
Chris@0
|
162
|
Chris@0
|
163
|
Chris@0
|
164 def test_open
|
Chris@0
|
165 ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
|
Chris@0
|
166 ldap.open {|ldap|
|
Chris@0
|
167 10.times {
|
Chris@0
|
168 rc = ldap.search( :base => "dc=bayshorenetworks,dc=com" )
|
Chris@0
|
169 assert_equal( true, rc )
|
Chris@0
|
170 }
|
Chris@0
|
171 }
|
Chris@0
|
172 end
|
Chris@0
|
173
|
Chris@0
|
174
|
Chris@0
|
175 def test_ldap_open
|
Chris@0
|
176 Net::LDAP.open( :host => @host, :port => @port, :auth => @auth ) {|ldap|
|
Chris@0
|
177 10.times {
|
Chris@0
|
178 rc = ldap.search( :base => "dc=bayshorenetworks,dc=com" )
|
Chris@0
|
179 assert_equal( true, rc )
|
Chris@0
|
180 }
|
Chris@0
|
181 }
|
Chris@0
|
182 end
|
Chris@0
|
183
|
Chris@0
|
184
|
Chris@0
|
185
|
Chris@0
|
186
|
Chris@0
|
187
|
Chris@0
|
188 end
|
Chris@0
|
189
|
Chris@0
|
190
|