Added unit tests for search-filters
This commit is contained in:
parent
ca38bfc803
commit
497e2be4e7
|
@ -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
|
||||||
|
|
|
@ -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|
|
||||||
|
|
|
@ -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,23 +144,28 @@ 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
|
||||||
|
|
||||||
|
# TODO, what if this returns nil?
|
||||||
|
filter = Net::LDAP::Filter.parse_ldap_filter( filters )
|
||||||
|
|
||||||
$ldif.each {|dn, entry|
|
$ldif.each {|dn, entry|
|
||||||
|
if filter.match( entry )
|
||||||
|
attrs = []
|
||||||
|
entry.each {|k, v|
|
||||||
|
if requested_attrs == :all or requested_attrs.include?(k.downcase)
|
||||||
|
attrvals = v.map {|v1| v1.to_ber}.to_ber_set
|
||||||
|
attrs << [k.to_ber, attrvals].to_ber_sequence
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
attrs = []
|
appseq = [dn.to_ber, attrs.to_ber_sequence].to_ber_appsequence(4)
|
||||||
entry.each {|k, v|
|
pkt = [msgid.to_ber, appseq].to_ber_sequence
|
||||||
if requested_attrs == :all or requested_attrs.include?(k.downcase)
|
send_data pkt
|
||||||
attrvals = v.map {|v1| v1.to_ber}.to_ber_set
|
end
|
||||||
attrs << [k.to_ber, attrvals].to_ber_sequence
|
|
||||||
end
|
|
||||||
}
|
|
||||||
|
|
||||||
appseq = [dn.to_ber, attrs.to_ber_sequence].to_ber_appsequence(4)
|
|
||||||
pkt = [msgid.to_ber, appseq].to_ber_sequence
|
|
||||||
send_data pkt
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue