From f3204bde6d8f24aa4c6be632400beb292237ab96 Mon Sep 17 00:00:00 2001 From: Jamstah Date: Thu, 6 Jan 2011 14:06:31 +0000 Subject: [PATCH] Accept filters with only one clause to an & or | --- lib/net/ldap/filter.rb | 2 +- spec/unit/ldap/filter_spec.rb | 5 +++-- test/test_filter.rb | 14 +++++++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/net/ldap/filter.rb b/lib/net/ldap/filter.rb index 4e8aec8..9ff5517 100644 --- a/lib/net/ldap/filter.rb +++ b/lib/net/ldap/filter.rb @@ -716,7 +716,7 @@ class Net::LDAP::Filter filter = nil branches = parse_branches(scanner) - if branches.size >= 2 + if branches.size >= 1 filter = branches.shift while not branches.empty? filter = filter.__send__(op, branches.shift) diff --git a/spec/unit/ldap/filter_spec.rb b/spec/unit/ldap/filter_spec.rb index 623f032..986a934 100644 --- a/spec/unit/ldap/filter_spec.rb +++ b/spec/unit/ldap/filter_spec.rb @@ -24,7 +24,8 @@ describe Net::LDAP::Filter do '(o:dn:=Ace Industry)', '(:dn:2.4.8.10:=Dino)', '(cn:dn:1.2.3.4.5:=John Smith)', - '(sn:dn:2.4.6.8.10:=Barbara Jones)', + '(sn:dn:2.4.6.8.10:=Barbara Jones)', + '(&(sn:dn:2.4.6.8.10:=Barbara Jones))' ].each do |filter_str| context "from_rfc2254(#{filter_str.inspect})" do attr_reader :filter @@ -80,4 +81,4 @@ describe Net::LDAP::Filter do Net::LDAP::Filter.escape('!&*:|~').should == "\\21\\26\\2A\\3A\\7C\\7E" end end -end \ No newline at end of file +end diff --git a/test/test_filter.rb b/test/test_filter.rb index 88495f1..0ece466 100644 --- a/test/test_filter.rb +++ b/test/test_filter.rb @@ -45,6 +45,10 @@ class TestFilter < Test::Unit::TestCase Filter.from_rfc2254("(! (mail=*))").to_rfc2254) end + def test_filter_with_single_clause + assert_equal("(cn=name)", Net::LDAP::Filter.construct("(&(cn=name))").to_s) + end + def test_filters_from_ber [ Net::LDAP::Filter.eq("objectclass", "*"), @@ -69,11 +73,11 @@ class TestFilter < Test::Unit::TestCase Net::LDAP::Filter.eq("objectclass", "aaa*"), Net::LDAP::Filter.eq("objectclass", "aaa*bbb*"), Net::LDAP::Filter.eq("objectclass", "aaa*bbb*ccc*"), - ].each {|ber| + ].each do |ber| f = Net::LDAP::Filter.parse_ber(ber.to_ber.read_ber(Net::LDAP::AsnSyntax)) assert(f == ber) assert_equal(f.to_ber, ber.to_ber) - } + end end def test_ber_from_rfc2254_filter @@ -102,10 +106,10 @@ class TestFilter < Test::Unit::TestCase Net::LDAP::Filter.construct("objectclass=aaa*"), Net::LDAP::Filter.construct("objectclass=aaa*bbb*"), Net::LDAP::Filter.construct("objectclass=aaa*bbb*ccc*"), - ].each {|ber| - f = Net::LDAP::Filter.parse_ber(ber.to_ber.read_ber(Net::LDAP::AsnSyntax)) + ].each do |ber| + f = Net::LDAP::Filter.parse_ber(ber.to_ber.read_ber(Net::LDAP::AsnSyntax)) assert(f == ber) assert_equal(f.to_ber, ber.to_ber) - } + end end end