~ubuntu-branches/ubuntu/raring/ruby-net-ldap/raring

« back to all changes in this revision

Viewing changes to test/test_ldif.rb

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal
  • Date: 2012-05-14 17:25:45 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120514172545-m08z3m92de2n2pfn
Tags: 0.3.1-1
* Upstream update.
* debian/watch: refer to latest net-ldap project.
* debian/copyright:
  + license changed to Expat, with permission from Ondřej Surý for the
    debian/* part.
  + format 1.0
  + add a lintian-override because Comment mentions old GPL license.
* debian/control:
  + Standards-Version 3.9.3 (no changes required)
  + Use anonscm.d.o in Vcs-* fields.
  + Update Homepage url.
  + XS-Ruby-Versions: all, this module is fine with ruby 1.8 and 1.9.
* debian/patches:
  + Unapply unneeded patches
  + 0003-fix_require_in_tests.patch: tests are called from CURDIR.
* Update debian/ruby-test-files.yaml

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# $Id: testldif.rb 61 2006-04-18 20:55:55Z blackhedd $
 
2
 
 
3
require 'test/common'
 
4
 
 
5
require 'digest/sha1'
 
6
require 'base64'
 
7
 
 
8
class TestLdif < Test::Unit::TestCase
 
9
  TestLdifFilename = "#{File.dirname(__FILE__)}/testdata.ldif"
 
10
 
 
11
  def test_empty_ldif
 
12
    ds = Net::LDAP::Dataset.read_ldif(StringIO.new)
 
13
    assert_equal(true, ds.empty?)
 
14
  end
 
15
 
 
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)
 
21
  end
 
22
 
 
23
  def test_ldif_with_password
 
24
    psw = "goldbricks"
 
25
    hashed_psw = "{SHA}" + Base64::encode64(Digest::SHA1.digest(psw)).chomp
 
26
 
 
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)
 
31
  end
 
32
 
 
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"))
 
36
  end
 
37
 
 
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"))
 
43
  end
 
44
 
 
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"))
 
48
  end
 
49
 
 
50
  # TODO, INADEQUATE. We need some more tests
 
51
  # to verify the content.
 
52
  def test_ldif
 
53
    File.open(TestLdifFilename, "r") {|f|
 
54
      ds = Net::LDAP::Dataset::read_ldif(f)
 
55
      assert_equal(13, ds.length)
 
56
    }
 
57
  end
 
58
 
 
59
  # Must test folded lines and base64-encoded lines as well as normal ones.
 
60
  def test_to_ldif
 
61
    data = File.open(TestLdifFilename, "rb") { |f| f.read }
 
62
    io = StringIO.new(data)
 
63
 
 
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
 
68
 
 
69
    ds = Net::LDAP::Dataset::read_ldif(io) { |type, value|
 
70
      case type
 
71
      when :dn
 
72
        assert_equal(dn_entries.first, value)
 
73
        dn_entries.shift
 
74
      end
 
75
    }
 
76
    assert_equal(entries.size, ds.size)
 
77
    assert_equal(entries.sort, ds.to_ldif.grep(/^dn:\s*/) { $'.chomp })
 
78
  end
 
79
end