Skip to content

Commit 36904e2

Browse files
committed
+ Rewrote tests as specs. Ruby1.9 compatible escape code.
1 parent a696aa8 commit 36904e2

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

lib/net/ldap/filter.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Encoding: UTF-8
12
# Copyright (C) 2006 by Francis Cianfrocca and other contributors. All
23
# Rights Reserved.
34
#
@@ -243,10 +244,28 @@ def present?(attribute)
243244
alias_method :present, :present?
244245
alias_method :pres, :present?
245246

247+
# http://tools.ietf.org/html/rfc4515 lists these exceptions from UTF1
248+
# charset for filters. All of the following must be escaped in any normal
249+
# string using a single backslash ('\') as escape.
250+
#
251+
ESCAPES = {
252+
'!' => '21', # EXCLAMATION = %x21 ; exclamation mark ("!")
253+
'&' => '26', # AMPERSAND = %x26 ; ampersand (or AND symbol) ("&")
254+
'*' => '2A', # ASTERISK = %x2A ; asterisk ("*")
255+
':' => '3A', # COLON = %x3A ; colon (":")
256+
'|' => '7C', # VERTBAR = %x7C ; vertical bar (or pipe) ("|")
257+
'~' => '7E', # TILDE = %x7E ; tilde ("~")
258+
}
259+
# Compiled character class regexp using the keys from the above hash.
260+
ESCAPE_RE = Regexp.new(
261+
"[" +
262+
ESCAPES.keys.map { |e| Regexp.escape(e) }.join +
263+
"]")
264+
246265
##
247266
# Escape a string for use in an LDAP filter
248267
def escape(string)
249-
string.gsub(/[\*\(\)\\\0]/) {|s| sprintf("\\%02x", s[0]) }
268+
string.gsub(ESCAPE_RE) { |char| "\\" + ESCAPES[char] }
250269
end
251270

252271
##

spec/unit/ldap/filter_spec.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,35 @@
4949
Net::LDAP::Filter.construct("uid=O'Keefe").to_rfc2254.should == "(uid=O'Keefe)"
5050
end
5151
end
52-
52+
53+
describe "convenience filter constructors" do
54+
def eq(attribute, value)
55+
described_class.eq(attribute, value)
56+
end
57+
describe "<- .equals(attr, val)" do
58+
it "should delegate to .eq with escaping" do
59+
described_class.equals('dn', 'f*oo').should == eq('dn', 'f\2Aoo')
60+
end
61+
end
62+
describe "<- .begins(attr, val)" do
63+
it "should delegate to .eq with escaping" do
64+
described_class.begins('dn', 'f*oo').should == eq('dn', 'f\2Aoo*')
65+
end
66+
end
67+
describe "<- .ends(attr, val)" do
68+
it "should delegate to .eq with escaping" do
69+
described_class.ends('dn', 'f*oo').should == eq('dn', '*f\2Aoo')
70+
end
71+
end
72+
describe "<- .contains(attr, val)" do
73+
it "should delegate to .eq with escaping" do
74+
described_class.contains('dn', 'f*oo').should == eq('dn', '*f\2Aoo*')
75+
end
76+
end
77+
end
78+
describe "<- .escape(str)" do
79+
it "should escape !, &, *, :, | and ~" do
80+
Net::LDAP::Filter.escape('!&*:|~').should == "\\21\\26\\2A\\3A\\7C\\7E"
81+
end
82+
end
5383
end

test/test_filter.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ def test_to_s
2424
assert_equal("(uid=george *)", Filter.eq("uid", "george *").to_s)
2525
end
2626

27-
def test_convenience_filters
28-
assert_equal("(uid=\\2a)", Filter.equals("uid", "*").to_s)
29-
assert_equal("(uid=\\28*)", Filter.begins("uid", "(").to_s)
30-
assert_equal("(uid=*\\29)", Filter.ends("uid", ")").to_s)
31-
assert_equal("(uid=*\\5c*)", Filter.contains("uid", "\\").to_s)
32-
end
33-
3427
def test_c2
3528
assert_equal("(uid=george *)",
3629
Filter.from_rfc2254("uid=george *").to_rfc2254)

0 commit comments

Comments
 (0)