Skip to content

Commit ca38bfc

Browse files
author
blackhedd
committed
additional unit tests and partial support for filters in the test server.
1 parent 7d08543 commit ca38bfc

File tree

3 files changed

+185
-7
lines changed

3 files changed

+185
-7
lines changed

tests/testdata.ldif

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# $Id$
2+
#
3+
# This is test-data for an LDAP server in LDIF format.
4+
#
5+
dn: dc=bayshorenetworks,dc=com
6+
objectClass: dcObject
7+
objectClass: organization
8+
o: Bayshore Networks LLC
9+
dc: bayshorenetworks
10+
11+
dn: cn=Manager,dc=bayshorenetworks,dc=com
12+
objectClass: organizationalrole
13+
cn: Manager
14+
15+
dn: ou=people,dc=bayshorenetworks,dc=com
16+
objectClass: organizationalunit
17+
ou: people
18+
19+
dn: ou=privileges,dc=bayshorenetworks,dc=com
20+
objectClass: organizationalunit
21+
ou: privileges
22+
23+
dn: ou=roles,dc=bayshorenetworks,dc=com
24+
objectClass: organizationalunit
25+
ou: roles
26+
27+
dn: ou=office,dc=bayshorenetworks,dc=com
28+
objectClass: organizationalunit
29+
ou: office
30+
31+
dn: mail=nogoodnik@steamheat.net,ou=people,dc=bayshorenetworks,dc=com
32+
cn: Bob Fosse
33+
mail: nogoodnik@steamheat.net
34+
sn: Fosse
35+
ou: people
36+
objectClass: top
37+
objectClass: inetorgperson
38+
objectClass: authorizedperson
39+
hasAccessRole: uniqueIdentifier=engineer,ou=roles
40+
hasAccessRole: uniqueIdentifier=ldapadmin,ou=roles
41+
hasAccessRole: uniqueIdentifier=ldapsuperadmin,ou=roles
42+
hasAccessRole: uniqueIdentifier=ogilvy_elephant_user,ou=roles
43+
hasAccessRole: uniqueIdentifier=ogilvy_eagle_user,ou=roles
44+
hasAccessRole: uniqueIdentifier=greenplug_user,ou=roles
45+
hasAccessRole: uniqueIdentifier=brandplace_logging_user,ou=roles
46+
hasAccessRole: uniqueIdentifier=brandplace_report_user,ou=roles
47+
hasAccessRole: uniqueIdentifier=workorder_user,ou=roles
48+
hasAccessRole: uniqueIdentifier=bayshore_eagle_user,ou=roles
49+
hasAccessRole: uniqueIdentifier=bayshore_eagle_superuser,ou=roles
50+
hasAccessRole: uniqueIdentifier=kledaras_user,ou=roles
51+
52+
dn: mail=elephant@steamheat.net,ou=people,dc=bayshorenetworks,dc=com
53+
cn: Gwen Verdon
54+
mail: elephant@steamheat.net
55+
sn: Verdon
56+
ou: people
57+
objectClass: top
58+
objectClass: inetorgperson
59+
objectClass: authorizedperson
60+
hasAccessRole: uniqueIdentifier=brandplace_report_user,ou=roles
61+
hasAccessRole: uniqueIdentifier=engineer,ou=roles
62+
hasAccessRole: uniqueIdentifier=ogilvy_elephant_user,ou=roles
63+
hasAccessRole: uniqueIdentifier=ldapsuperadmin,ou=roles
64+
hasAccessRole: uniqueIdentifier=ldapadmin,ou=roles
65+
66+
dn: uniqueIdentifier=engineering,ou=privileges,dc=bayshorenetworks,dc=com
67+
uniqueIdentifier: engineering
68+
ou: privileges
69+
objectClass: accessPrivilege
70+
71+
dn: uniqueIdentifier=engineer,ou=roles,dc=bayshorenetworks,dc=com
72+
uniqueIdentifier: engineer
73+
ou: roles
74+
objectClass: accessRole
75+
hasAccessPrivilege: uniqueIdentifier=engineering,ou=privileges
76+
77+
dn: uniqueIdentifier=ldapadmin,ou=roles,dc=bayshorenetworks,dc=com
78+
uniqueIdentifier: ldapadmin
79+
ou: roles
80+
objectClass: accessRole
81+
82+
dn: uniqueIdentifier=ldapsuperadmin,ou=roles,dc=bayshorenetworks,dc=com
83+
uniqueIdentifier: ldapsuperadmin
84+
ou: roles
85+
objectClass: accessRole
86+
87+
dn: mail=catperson@steamheat.net,ou=people,dc=bayshorenetworks,dc=com
88+
cn: Sid Sorokin
89+
mail: catperson@steamheat.net
90+
sn: Sorokin
91+
ou: people
92+
objectClass: top
93+
objectClass: inetorgperson
94+
objectClass: authorizedperson
95+
hasAccessRole: uniqueIdentifier=engineer,ou=roles
96+
hasAccessRole: uniqueIdentifier=ogilvy_elephant_user,ou=roles
97+
hasAccessRole: uniqueIdentifier=ldapsuperadmin,ou=roles
98+
hasAccessRole: uniqueIdentifier=ogilvy_eagle_user,ou=roles
99+
hasAccessRole: uniqueIdentifier=greenplug_user,ou=roles
100+
hasAccessRole: uniqueIdentifier=workorder_user,ou=roles
101+

tests/testldap.rb

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class TestLdapClient < Test::Unit::TestCase
1313

1414
# TODO: these tests crash and burn if the associated
1515
# LDAP testserver isn't up and running.
16+
# We rely on being able to read a file with test data
17+
# in LDIF format.
18+
# TODO, WARNING: for the moment, this data is in a file
19+
# whose name and location are HARDCODED into the
20+
# instance method load_test_data.
1621

1722
def setup
1823
@host = "127.0.0.1"
@@ -23,8 +28,34 @@ def setup
2328
:password => "opensesame"
2429
}
2530

31+
@ldif = load_test_data
2632
end
2733

34+
35+
36+
# Get some test data which will be used to validate
37+
# the responses from the test LDAP server we will
38+
# connect to.
39+
# TODO, Bogus: we are HARDCODING the location of the file for now.
40+
#
41+
def load_test_data
42+
ary = File.readlines( "tests/testdata.ldif" )
43+
hash = {}
44+
while line = ary.shift and line.chomp!
45+
if line =~ /^dn:[\s]*/i
46+
dn = $'
47+
hash[dn] = {}
48+
while attr = ary.shift and attr.chomp! and attr =~ /^([\w]+)[\s]*:[\s]*/
49+
hash[dn][$1.downcase.intern] ||= []
50+
hash[dn][$1.downcase.intern] << $'
51+
end
52+
end
53+
end
54+
hash
55+
end
56+
57+
58+
2859
# Binding tests.
2960
# Need tests for all kinds of network failures and incorrect auth.
3061
# TODO: Implement a class-level timeout for operations like bind.
@@ -43,6 +74,8 @@ def test_bind
4374
assert_equal( 49, ldap.bind )
4475
end
4576

77+
78+
4679
def test_search
4780
ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
4881

@@ -53,30 +86,67 @@ def test_search
5386
assert_equal( 0, ldap.search( search ))
5487

5588
ldap.search( search ) {|res|
56-
# STUB.
57-
#p res
89+
assert_equal( res, @ldif )
5890
}
5991
end
6092

6193

62-
def test_search_attributes
94+
95+
96+
# This is a helper routine for test_search_attributes.
97+
def internal_test_search_attributes attrs_to_search
6398
ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
6499
assert_equal( 0, ldap.bind )
65100

66101
search = {
67102
:base => "dc=bayshorenetworks,dc=com",
68-
:attributes => ["mail"]
103+
:attributes => attrs_to_search
69104
}
70-
assert_equal( 0, ldap.search( search ))
71105

106+
ldif = @ldif
107+
ldif.each {|dn,entry|
108+
entry.delete_if {|attr,value|
109+
! attrs_to_search.include?(attr)
110+
}
111+
}
112+
113+
assert_equal( 0, ldap.search( search ))
72114
ldap.search( search ) {|res|
73-
# STUB.
74-
p res
115+
res_keys = res.keys.sort
116+
ldif_keys = ldif.keys.sort
117+
assert( res_keys, ldif_keys )
118+
res.keys.each {|rk|
119+
assert( res[rk], ldif[rk] )
120+
}
75121
}
76122
end
77123

78124

125+
def test_search_attributes
126+
internal_test_search_attributes [:mail]
127+
internal_test_search_attributes [:cn]
128+
internal_test_search_attributes [:ou]
129+
internal_test_search_attributes [:hasaccessprivilege]
130+
internal_test_search_attributes ["mail"]
131+
internal_test_search_attributes ["cn"]
132+
internal_test_search_attributes ["ou"]
133+
internal_test_search_attributes ["hasaccessrole"]
134+
135+
internal_test_search_attributes [:mail, :cn, :ou, :hasaccessrole]
136+
internal_test_search_attributes [:mail, "cn", :ou, "hasaccessrole"]
137+
end
138+
139+
79140
def test_search_filters
141+
ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
142+
search = {
143+
:base => "dc=bayshorenetworks,dc=com",
144+
:filter => Net::LDAP::Filter.eq( "sn", "Verdon" )
145+
}
146+
147+
ldap.search( search ) {|res|
148+
p res
149+
}
80150
end
81151

82152

testserver/ldapserver.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ module LdapServer
5050
0 => :string, # simple auth (password)
5151
7 => :string # present filter
5252
},
53+
:constructed => {
54+
3 => :array # equality filter
55+
},
5356
}
5457
}
5558

@@ -139,6 +142,10 @@ def handle_search_request pdu
139142
:all
140143
end
141144

145+
filters = pdu[1][6]
146+
if filters.length > 0
147+
p filters.ber_identifier
148+
end
142149

143150
$ldif.each {|dn, entry|
144151

0 commit comments

Comments
 (0)