summaryrefslogtreecommitdiff
path: root/test/uri/test_ldap.rb
blob: 2625b241030965bfb5bc361a019425b1fd07cf9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# frozen_string_literal: false
require 'test/unit'
require 'uri/ldap'

module URI


class TestLDAP < Test::Unit::TestCase
  def setup
  end

  def teardown
  end

  def uri_to_ary(uri)
    uri.class.component.collect {|c| uri.send(c)}
  end

  def test_parse
    url = 'ldap://ldap.jaist.ac.jp/o=JAIST,c=JP?sn?base?(sn=ttate*)'
    u = URI.parse(url)
    assert_kind_of(URI::LDAP, u)
    assert_equal(url, u.to_s)
    assert_equal('o=JAIST,c=JP', u.dn)
    assert_equal('sn', u.attributes)
    assert_equal('base', u.scope)
    assert_equal('(sn=ttate*)', u.filter)
    assert_equal(nil, u.extensions)

    u.scope = URI::LDAP::SCOPE_SUB
    u.attributes = 'sn,cn,mail'
    assert_equal('ldap://ldap.jaist.ac.jp/o=JAIST,c=JP?sn,cn,mail?sub?(sn=ttate*)', u.to_s)
    assert_equal('o=JAIST,c=JP', u.dn)
    assert_equal('sn,cn,mail', u.attributes)
    assert_equal('sub', u.scope)
    assert_equal('(sn=ttate*)', u.filter)
    assert_equal(nil, u.extensions)

    # from RFC2255, section 6.
    {
      'ldap:///o=University%20of%20Michigan,c=US' =>
      ['ldap', '', URI::LDAP::DEFAULT_PORT,
	'o=University%20of%20Michigan,c=US',
	nil, nil, nil, nil],

      'ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US' =>
      ['ldap', 'ldap.itd.umich.edu', URI::LDAP::DEFAULT_PORT,
	'o=University%20of%20Michigan,c=US',
	nil, nil, nil, nil],

      'ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US?postalAddress' =>
      ['ldap', 'ldap.itd.umich.edu', URI::LDAP::DEFAULT_PORT,
	'o=University%20of%20Michigan,c=US',
	'postalAddress', nil, nil, nil],

      'ldap://host.com:6666/o=University%20of%20Michigan,c=US??sub?(cn=Babs%20Jensen)' =>
      ['ldap', 'host.com', 6666,
	'o=University%20of%20Michigan,c=US',
	nil, 'sub', '(cn=Babs%20Jensen)', nil],

      'ldap://ldap.itd.umich.edu/c=GB?objectClass?one' =>
      ['ldap', 'ldap.itd.umich.edu', URI::LDAP::DEFAULT_PORT,
	'c=GB',
	'objectClass', 'one', nil, nil],

      'ldap://ldap.question.com/o=Question%3f,c=US?mail' =>
      ['ldap', 'ldap.question.com', URI::LDAP::DEFAULT_PORT,
	'o=Question%3f,c=US',
	'mail', nil, nil, nil],

      'ldap://ldap.netscape.com/o=Babsco,c=US??(int=%5c00%5c00%5c00%5c04)' =>
      ['ldap', 'ldap.netscape.com', URI::LDAP::DEFAULT_PORT,
	'o=Babsco,c=US',
	nil, '(int=%5c00%5c00%5c00%5c04)', nil, nil],

      'ldap:///??sub??bindname=cn=Manager%2co=Foo' =>
      ['ldap', '', URI::LDAP::DEFAULT_PORT,
	'',
	nil, 'sub', nil, 'bindname=cn=Manager%2co=Foo'],

      'ldap:///??sub??!bindname=cn=Manager%2co=Foo' =>
      ['ldap', '', URI::LDAP::DEFAULT_PORT,
	'',
	nil, 'sub', nil, '!bindname=cn=Manager%2co=Foo'],
    }.each do |url2, ary|
      u = URI.parse(url2)
      assert_equal(ary, uri_to_ary(u))
    end
  end

  def test_select
    u = URI.parse('ldap:///??sub??!bindname=cn=Manager%2co=Foo')
    assert_equal(uri_to_ary(u), u.select(*u.component))
    assert_raise(ArgumentError) do
      u.select(:scheme, :host, :not_exist, :port)
    end
  end

  def test_parse_invalid_uri
    assert_raise(URI::InvalidURIError) {URI.parse("ldap:https://example.com")}
  end
end


end