2008-11-15 00:22:30 +01:00
|
|
|
# $Id: testldif.rb 61 2006-04-18 20:55:55Z blackhedd $
|
|
|
|
|
|
|
|
require 'common'
|
|
|
|
|
2009-02-21 16:49:24 +01:00
|
|
|
require 'digest/sha1'
|
2008-11-15 00:22:30 +01:00
|
|
|
require 'base64'
|
|
|
|
|
|
|
|
class TestLdif < Test::Unit::TestCase
|
|
|
|
TestLdifFilename = "#{File.dirname(__FILE__)}/testdata.ldif"
|
|
|
|
|
|
|
|
def test_empty_ldif
|
2010-03-27 19:49:14 +01:00
|
|
|
ds = Net::LDAP::Dataset.read_ldif(StringIO.new)
|
|
|
|
assert_equal(true, ds.empty?)
|
2008-11-15 00:22:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ldif_with_comments
|
|
|
|
str = ["# Hello from LDIF-land", "# This is an unterminated comment"]
|
2010-03-27 19:49:14 +01:00
|
|
|
io = StringIO.new(str[0] + "\r\n" + str[1])
|
|
|
|
ds = Net::LDAP::Dataset::read_ldif(io)
|
|
|
|
assert_equal(str, ds.comments)
|
2008-11-15 00:22:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ldif_with_password
|
|
|
|
psw = "goldbricks"
|
2009-02-21 16:49:24 +01:00
|
|
|
hashed_psw = "{SHA}" + Base64::encode64(Digest::SHA1.digest(psw)).chomp
|
2008-11-15 00:22:30 +01:00
|
|
|
|
2010-03-27 19:49:14 +01:00
|
|
|
ldif_encoded = Base64::encode64(hashed_psw).chomp
|
|
|
|
ds = Net::LDAP::Dataset::read_ldif(StringIO.new("dn: Goldbrick\r\nuserPassword:: #{ldif_encoded}\r\n\r\n"))
|
2008-11-15 00:22:30 +01:00
|
|
|
recovered_psw = ds["Goldbrick"][:userpassword].shift
|
2010-03-27 19:49:14 +01:00
|
|
|
assert_equal(hashed_psw, recovered_psw)
|
2008-11-15 00:22:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ldif_with_continuation_lines
|
2010-03-27 19:49:14 +01:00
|
|
|
ds = Net::LDAP::Dataset::read_ldif(StringIO.new("dn: abcdefg\r\n hijklmn\r\n\r\n"))
|
|
|
|
assert_equal(true, ds.has_key?("abcdefg hijklmn"))
|
2008-11-15 00:22:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# TODO, INADEQUATE. We need some more tests
|
|
|
|
# to verify the content.
|
|
|
|
def test_ldif
|
2010-03-27 19:49:14 +01:00
|
|
|
File.open(TestLdifFilename, "r") {|f|
|
|
|
|
ds = Net::LDAP::Dataset::read_ldif(f)
|
|
|
|
assert_equal(13, ds.length)
|
2008-11-15 00:22:30 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Must test folded lines and base64-encoded lines as well as normal ones.
|
2010-03-27 19:49:14 +01:00
|
|
|
def test_to_ldif
|
|
|
|
data = File.open(TestLdifFilename, "rb") { |f| f.read }
|
|
|
|
io = StringIO.new(data)
|
|
|
|
|
2010-04-16 00:01:43 +02:00
|
|
|
# added .lines to turn to array because 1.9 doesn't have
|
|
|
|
# .grep on basic strings
|
|
|
|
entries = data.lines.grep(/^dn:\s*/) { $'.chomp }
|
2010-03-27 19:49:14 +01:00
|
|
|
dn_entries = entries.dup
|
2008-11-15 00:22:30 +01:00
|
|
|
|
2010-03-27 19:49:14 +01:00
|
|
|
ds = Net::LDAP::Dataset::read_ldif(io) { |type, value|
|
|
|
|
case type
|
|
|
|
when :dn
|
|
|
|
assert_equal(dn_entries.first, value)
|
|
|
|
dn_entries.shift
|
|
|
|
end
|
|
|
|
}
|
|
|
|
assert_equal(entries.size, ds.size)
|
|
|
|
assert_equal(entries.sort, ds.to_ldif.grep(/^dn:\s*/) { $'.chomp })
|
|
|
|
end
|
2008-11-15 00:22:30 +01:00
|
|
|
end
|