1
# $Id: testldif.rb 61 2006-04-18 20:55:55Z blackhedd $
8
class TestLdif < Test::Unit::TestCase
9
TestLdifFilename = "#{File.dirname(__FILE__)}/testdata.ldif"
12
ds = Net::LDAP::Dataset.read_ldif(StringIO.new)
13
assert_equal(true, ds.empty?)
16
def test_ldif_with_comments
17
str = ["# Hello from LDIF-land", "# This is an unterminated comment"]
18
io = StringIO.new(str[0] + "\r\n" + str[1])
19
ds = Net::LDAP::Dataset::read_ldif(io)
20
assert_equal(str, ds.comments)
23
def test_ldif_with_password
25
hashed_psw = "{SHA}" + Base64::encode64(Digest::SHA1.digest(psw)).chomp
27
ldif_encoded = Base64::encode64(hashed_psw).chomp
28
ds = Net::LDAP::Dataset::read_ldif(StringIO.new("dn: Goldbrick\r\nuserPassword:: #{ldif_encoded}\r\n\r\n"))
29
recovered_psw = ds["Goldbrick"][:userpassword].shift
30
assert_equal(hashed_psw, recovered_psw)
33
def test_ldif_with_continuation_lines
34
ds = Net::LDAP::Dataset::read_ldif(StringIO.new("dn: abcdefg\r\n hijklmn\r\n\r\n"))
35
assert_equal(true, ds.has_key?("abcdefghijklmn"))
38
def test_ldif_with_continuation_lines_and_extra_whitespace
39
ds1 = Net::LDAP::Dataset::read_ldif(StringIO.new("dn: abcdefg\r\n hijklmn\r\n\r\n"))
40
assert_equal(true, ds1.has_key?("abcdefg hijklmn"))
41
ds2 = Net::LDAP::Dataset::read_ldif(StringIO.new("dn: abcdefg\r\n hij klmn\r\n\r\n"))
42
assert_equal(true, ds2.has_key?("abcdefghij klmn"))
45
def test_ldif_tab_is_not_continuation
46
ds = Net::LDAP::Dataset::read_ldif(StringIO.new("dn: key\r\n\tnotcontinued\r\n\r\n"))
47
assert_equal(true, ds.has_key?("key"))
50
# TODO, INADEQUATE. We need some more tests
51
# to verify the content.
53
File.open(TestLdifFilename, "r") {|f|
54
ds = Net::LDAP::Dataset::read_ldif(f)
55
assert_equal(13, ds.length)
59
# Must test folded lines and base64-encoded lines as well as normal ones.
61
data = File.open(TestLdifFilename, "rb") { |f| f.read }
62
io = StringIO.new(data)
64
# added .lines to turn to array because 1.9 doesn't have
65
# .grep on basic strings
66
entries = data.lines.grep(/^dn:\s*/) { $'.chomp }
67
dn_entries = entries.dup
69
ds = Net::LDAP::Dataset::read_ldif(io) { |type, value|
72
assert_equal(dn_entries.first, value)
76
assert_equal(entries.size, ds.size)
77
assert_equal(entries.sort, ds.to_ldif.grep(/^dn:\s*/) { $'.chomp })