Merge branch 'ldap-cleanup', remote branch 'halostatue/ldap-cleanup' into ldap-cleanup

This commit is contained in:
Rory O'Connell 2010-04-15 16:27:08 -05:00
commit 896eaa533c
4 changed files with 346 additions and 322 deletions

View file

@ -7,53 +7,60 @@ require 'digest/sha1'
require 'base64'
class TestLdif < Test::Unit::TestCase
TestLdifFilename = "#{File.dirname(__FILE__)}/testdata.ldif"
def test_empty_ldif
ds = Net::LDAP::Dataset.read_ldif( StringIO.new )
assert_equal( true, ds.empty? )
ds = Net::LDAP::Dataset.read_ldif(StringIO.new)
assert_equal(true, ds.empty?)
end
def test_ldif_with_comments
str = ["# Hello from LDIF-land", "# This is an unterminated comment"]
io = StringIO.new( str[0] + "\r\n" + str[1] )
ds = Net::LDAP::Dataset::read_ldif( io )
assert_equal( str, ds.comments )
io = StringIO.new(str[0] + "\r\n" + str[1])
ds = Net::LDAP::Dataset::read_ldif(io)
assert_equal(str, ds.comments)
end
def test_ldif_with_password
psw = "goldbricks"
hashed_psw = "{SHA}" + Base64::encode64(Digest::SHA1.digest(psw)).chomp
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" ))
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"))
recovered_psw = ds["Goldbrick"][:userpassword].shift
assert_equal( hashed_psw, recovered_psw )
assert_equal(hashed_psw, recovered_psw)
end
def test_ldif_with_continuation_lines
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" ))
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"))
end
# TODO, INADEQUATE. We need some more tests
# to verify the content.
def test_ldif
File.open( TestLdifFilename, "r" ) {|f|
ds = Net::LDAP::Dataset::read_ldif( f )
assert_equal( 13, ds.length )
File.open(TestLdifFilename, "r") {|f|
ds = Net::LDAP::Dataset::read_ldif(f)
assert_equal(13, ds.length)
}
end
# TODO, need some tests.
# Must test folded lines and base64-encoded lines as well as normal ones.
#def test_to_ldif
# File.open( TestLdifFilename, "r" ) {|f|
# ds = Net::LDAP::Dataset::read_ldif( f )
# ds.to_ldif
# assert_equal( true, false ) # REMOVE WHEN WE HAVE SOME TESTS HERE.
# }
#end
def test_to_ldif
data = File.open(TestLdifFilename, "rb") { |f| f.read }
io = StringIO.new(data)
entries = data.grep(/^dn:\s*/) { $'.chomp }
dn_entries = entries.dup
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
end