Added unit tests for search-filters

This commit is contained in:
blackhedd 2006-04-17 20:39:54 +00:00
parent ca38bfc803
commit 497e2be4e7
3 changed files with 55 additions and 14 deletions

View file

@ -171,6 +171,41 @@ class Filter
end end
# We get a Ruby object which comes from parsing an RFC-1777 "Filter"
# object. Convert it to a Net::LDAP::Filter.
# TODO, we're hardcoding the RFC-1777 BER-encodings of the various
# filter types. Could pull them out into a constant.
#
def Filter::parse_ldap_filter obj
case obj.ber_identifier
when 0x87 # present. context-specific primitive 7.
Filter.eq( obj.to_s, "*" )
when 0xa3 # equalityMatch. context-specific constructed 3.
Filter.eq( obj[0], obj[1] )
else
raise LdapError.new( "unknown ldap search-filter type: #{obj.ber_identifier}" )
end
end
# We got a hash of attribute values.
# Do we match the attributes?
# Return T/F, and call match recursively as necessary.
def match entry
case @op
when :eq
if @right == "*"
l = entry[@left] and l.length > 0
else
l = entry[@left] and l = l.to_a and l.index(@right)
end
else
raise LdapError.new( "unknown filter type in match: #{@op}" )
end
end
end # class Net::LDAP::Filter end # class Net::LDAP::Filter
end # class Net::LDAP end # class Net::LDAP

View file

@ -141,7 +141,7 @@ class TestLdapClient < Test::Unit::TestCase
ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
search = { search = {
:base => "dc=bayshorenetworks,dc=com", :base => "dc=bayshorenetworks,dc=com",
:filter => Net::LDAP::Filter.eq( "sn", "Verdon" ) :filter => Net::LDAP::Filter.eq( "sn", "Fosse" )
} }
ldap.search( search ) {|res| ldap.search( search ) {|res|

View file

@ -122,6 +122,7 @@ module LdapServer
# } # }
def handle_search_request pdu def handle_search_request pdu
unless @authenticated unless @authenticated
# NOTE, early exit.
send_ldap_response 5, pdu[0].to_i, 50, "", "Who did you say you were?" send_ldap_response 5, pdu[0].to_i, 50, "", "Who did you say you were?"
return return
end end
@ -143,12 +144,16 @@ module LdapServer
end end
filters = pdu[1][6] filters = pdu[1][6]
if filters.length > 0 if filters.length == 0
p filters.ber_identifier # NOTE, early exit.
send_ldap_response 5, pdu[0].to_i, 53, "", "No filter specified"
end end
$ldif.each {|dn, entry| # TODO, what if this returns nil?
filter = Net::LDAP::Filter.parse_ldap_filter( filters )
$ldif.each {|dn, entry|
if filter.match( entry )
attrs = [] attrs = []
entry.each {|k, v| entry.each {|k, v|
if requested_attrs == :all or requested_attrs.include?(k.downcase) if requested_attrs == :all or requested_attrs.include?(k.downcase)
@ -160,6 +165,7 @@ module LdapServer
appseq = [dn.to_ber, attrs.to_ber_sequence].to_ber_appsequence(4) appseq = [dn.to_ber, attrs.to_ber_sequence].to_ber_appsequence(4)
pkt = [msgid.to_ber, appseq].to_ber_sequence pkt = [msgid.to_ber, appseq].to_ber_sequence
send_data pkt send_data pkt
end
} }