Skip to content

Commit c6f7c1f

Browse files
author
Jerry Cheung
committed
convert filter spec to test/unit
1 parent f73b9c1 commit c6f7c1f

File tree

2 files changed

+97
-116
lines changed

2 files changed

+97
-116
lines changed

spec/unit/ldap/filter_spec.rb

Lines changed: 0 additions & 115 deletions
This file was deleted.

test/test_filter.rb

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_convenience_filters
2828
assert_equal("(uid=\\2A)", Filter.equals("uid", "*").to_s)
2929
assert_equal("(uid=\\28*)", Filter.begins("uid", "(").to_s)
3030
assert_equal("(uid=*\\29)", Filter.ends("uid", ")").to_s)
31-
assert_equal("(uid=*\\5C*)", Filter.contains("uid", "\\").to_s)
31+
assert_equal("(uid=*\\5C*)", Filter.contains("uid", "\\").to_s)
3232
end
3333

3434
def test_c2
@@ -120,3 +120,99 @@ def test_ber_from_rfc2254_filter
120120
end
121121
end
122122
end
123+
124+
# tests ported over from rspec. Not sure if these overlap with the above
125+
# https://github.com/ruby-ldap/ruby-net-ldap/pull/121
126+
class TestFilterRSpec < Test::Unit::TestCase
127+
def test_ex_convert
128+
assert_equal '(foo:=bar)', Net::LDAP::Filter.ex('foo', 'bar').to_s
129+
end
130+
131+
def test_ex_rfc2254_roundtrip
132+
filter = Net::LDAP::Filter.ex('foo', 'bar')
133+
assert_equal filter, Net::LDAP::Filter.from_rfc2254(filter.to_s)
134+
end
135+
136+
def test_ber_conversion
137+
filter = Net::LDAP::Filter.ex('foo', 'bar')
138+
ber = filter.to_ber
139+
assert_equal filter, Net::LDAP::Filter.parse_ber(ber.read_ber(Net::LDAP::AsnSyntax))
140+
end
141+
142+
[
143+
'(o:dn:=Ace Industry)',
144+
'(:dn:2.4.8.10:=Dino)',
145+
'(cn:dn:1.2.3.4.5:=John Smith)',
146+
'(sn:dn:2.4.6.8.10:=Barbara Jones)',
147+
'(&(sn:dn:2.4.6.8.10:=Barbara Jones))'
148+
].each_with_index do |filter_str, index|
149+
define_method "test_decode_filter_#{index}" do
150+
filter = Net::LDAP::Filter.from_rfc2254(filter_str)
151+
assert_kind_of Net::LDAP::Filter, filter
152+
end
153+
154+
define_method "test_ber_conversion_#{index}" do
155+
filter = Net::LDAP::Filter.from_rfc2254(filter_str)
156+
ber = Net::LDAP::Filter.from_rfc2254(filter_str).to_ber
157+
assert_equal filter, Net::LDAP::Filter.parse_ber(ber.read_ber(Net::LDAP::AsnSyntax))
158+
end
159+
end
160+
161+
def test_apostrophes
162+
assert_equal "(uid=O'Keefe)", Net::LDAP::Filter.construct("uid=O'Keefe").to_rfc2254
163+
end
164+
165+
def test_equals
166+
assert_equal Net::LDAP::Filter.eq('dn', 'f\2Aoo'), Net::LDAP::Filter.equals('dn', 'f*oo')
167+
end
168+
169+
def test_begins
170+
assert_equal Net::LDAP::Filter.eq('dn', 'f\2Aoo*'), Net::LDAP::Filter.begins('dn', 'f*oo')
171+
end
172+
173+
def test_ends
174+
assert_equal Net::LDAP::Filter.eq('dn', '*f\2Aoo'), Net::LDAP::Filter.ends('dn', 'f*oo')
175+
end
176+
177+
def test_contains
178+
assert_equal Net::LDAP::Filter.eq('dn', '*f\2Aoo*'), Net::LDAP::Filter.contains('dn', 'f*oo')
179+
end
180+
181+
def test_escape
182+
# escapes nul, *, (, ) and \\
183+
assert_equal "\\00\\2A\\28\\29\\5C", Net::LDAP::Filter.escape("\0*()\\")
184+
end
185+
186+
def test_well_known_ber_string
187+
ber = "\xa4\x2d" \
188+
"\x04\x0b" "objectclass" \
189+
"\x30\x1e" \
190+
"\x80\x08" "foo" "*\\" "bar" \
191+
"\x81\x08" "foo" "*\\" "bar" \
192+
"\x82\x08" "foo" "*\\" "bar".b
193+
194+
[
195+
"foo" "\\2A\\5C" "bar",
196+
"foo" "\\2a\\5c" "bar",
197+
"foo" "\\2A\\5c" "bar",
198+
"foo" "\\2a\\5C" "bar"
199+
].each do |escaped|
200+
# unescapes escaped characters
201+
filter = Net::LDAP::Filter.eq("objectclass", "#{escaped}*#{escaped}*#{escaped}")
202+
assert_equal ber, filter.to_ber
203+
end
204+
end
205+
206+
def test_parse_ber_escapes_characters
207+
ber = "\xa4\x2d" \
208+
"\x04\x0b" "objectclass" \
209+
"\x30\x1e" \
210+
"\x80\x08" "foo" "*\\" "bar" \
211+
"\x81\x08" "foo" "*\\" "bar" \
212+
"\x82\x08" "foo" "*\\" "bar".b
213+
214+
escaped = Net::LDAP::Filter.escape("foo" "*\\" "bar")
215+
filter = Net::LDAP::Filter.parse_ber(ber.read_ber(Net::LDAP::AsnSyntax))
216+
assert_equal "(objectclass=#{escaped}*#{escaped}*#{escaped})", filter.to_s
217+
end
218+
end

0 commit comments

Comments
 (0)