pruby-net-ldap 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/tests/testber.rb ADDED
@@ -0,0 +1,42 @@
1
+ # $Id: testber.rb 57 2006-04-18 00:18:48Z blackhedd $
2
+ #
3
+ #
4
+
5
+
6
+ $:.unshift "lib"
7
+
8
+ require 'net/ldap'
9
+ require 'stringio'
10
+
11
+
12
+ class TestBer < Test::Unit::TestCase
13
+
14
+ def setup
15
+ end
16
+
17
+ # TODO: Add some much bigger numbers
18
+ # 5000000000 is a Bignum, which hits different code.
19
+ def test_ber_integers
20
+ assert_equal( "\002\001\005", 5.to_ber )
21
+ assert_equal( "\002\002\203t", 500.to_ber )
22
+ assert_equal( "\002\003\203\206P", 50000.to_ber )
23
+ assert_equal( "\002\005\222\320\227\344\000", 5000000000.to_ber )
24
+ end
25
+
26
+ def test_ber_parsing
27
+ assert_equal( 6, "\002\001\006".read_ber( Net::LDAP::AsnSyntax ))
28
+ assert_equal( "testing", "\004\007testing".read_ber( Net::LDAP::AsnSyntax ))
29
+ end
30
+
31
+
32
+ def test_ber_parser_on_ldap_bind_request
33
+ s = StringIO.new "0$\002\001\001`\037\002\001\003\004\rAdministrator\200\vad_is_bogus"
34
+ assert_equal( [1, [3, "Administrator", "ad_is_bogus"]], s.read_ber( Net::LDAP::AsnSyntax ))
35
+ end
36
+
37
+
38
+
39
+
40
+ end
41
+
42
+
@@ -0,0 +1,101 @@
1
+ # $Id: testdata.ldif 50 2006-04-17 17:57:33Z blackhedd $
2
+ #
3
+ # This is test-data for an LDAP server in LDIF format.
4
+ #
5
+ dn: dc=bayshorenetworks,dc=com
6
+ objectClass: dcObject
7
+ objectClass: organization
8
+ o: Bayshore Networks LLC
9
+ dc: bayshorenetworks
10
+
11
+ dn: cn=Manager,dc=bayshorenetworks,dc=com
12
+ objectClass: organizationalrole
13
+ cn: Manager
14
+
15
+ dn: ou=people,dc=bayshorenetworks,dc=com
16
+ objectClass: organizationalunit
17
+ ou: people
18
+
19
+ dn: ou=privileges,dc=bayshorenetworks,dc=com
20
+ objectClass: organizationalunit
21
+ ou: privileges
22
+
23
+ dn: ou=roles,dc=bayshorenetworks,dc=com
24
+ objectClass: organizationalunit
25
+ ou: roles
26
+
27
+ dn: ou=office,dc=bayshorenetworks,dc=com
28
+ objectClass: organizationalunit
29
+ ou: office
30
+
31
+ dn: mail=nogoodnik@steamheat.net,ou=people,dc=bayshorenetworks,dc=com
32
+ cn: Bob Fosse
33
+ mail: nogoodnik@steamheat.net
34
+ sn: Fosse
35
+ ou: people
36
+ objectClass: top
37
+ objectClass: inetorgperson
38
+ objectClass: authorizedperson
39
+ hasAccessRole: uniqueIdentifier=engineer,ou=roles
40
+ hasAccessRole: uniqueIdentifier=ldapadmin,ou=roles
41
+ hasAccessRole: uniqueIdentifier=ldapsuperadmin,ou=roles
42
+ hasAccessRole: uniqueIdentifier=ogilvy_elephant_user,ou=roles
43
+ hasAccessRole: uniqueIdentifier=ogilvy_eagle_user,ou=roles
44
+ hasAccessRole: uniqueIdentifier=greenplug_user,ou=roles
45
+ hasAccessRole: uniqueIdentifier=brandplace_logging_user,ou=roles
46
+ hasAccessRole: uniqueIdentifier=brandplace_report_user,ou=roles
47
+ hasAccessRole: uniqueIdentifier=workorder_user,ou=roles
48
+ hasAccessRole: uniqueIdentifier=bayshore_eagle_user,ou=roles
49
+ hasAccessRole: uniqueIdentifier=bayshore_eagle_superuser,ou=roles
50
+ hasAccessRole: uniqueIdentifier=kledaras_user,ou=roles
51
+
52
+ dn: mail=elephant@steamheat.net,ou=people,dc=bayshorenetworks,dc=com
53
+ cn: Gwen Verdon
54
+ mail: elephant@steamheat.net
55
+ sn: Verdon
56
+ ou: people
57
+ objectClass: top
58
+ objectClass: inetorgperson
59
+ objectClass: authorizedperson
60
+ hasAccessRole: uniqueIdentifier=brandplace_report_user,ou=roles
61
+ hasAccessRole: uniqueIdentifier=engineer,ou=roles
62
+ hasAccessRole: uniqueIdentifier=ogilvy_elephant_user,ou=roles
63
+ hasAccessRole: uniqueIdentifier=ldapsuperadmin,ou=roles
64
+ hasAccessRole: uniqueIdentifier=ldapadmin,ou=roles
65
+
66
+ dn: uniqueIdentifier=engineering,ou=privileges,dc=bayshorenetworks,dc=com
67
+ uniqueIdentifier: engineering
68
+ ou: privileges
69
+ objectClass: accessPrivilege
70
+
71
+ dn: uniqueIdentifier=engineer,ou=roles,dc=bayshorenetworks,dc=com
72
+ uniqueIdentifier: engineer
73
+ ou: roles
74
+ objectClass: accessRole
75
+ hasAccessPrivilege: uniqueIdentifier=engineering,ou=privileges
76
+
77
+ dn: uniqueIdentifier=ldapadmin,ou=roles,dc=bayshorenetworks,dc=com
78
+ uniqueIdentifier: ldapadmin
79
+ ou: roles
80
+ objectClass: accessRole
81
+
82
+ dn: uniqueIdentifier=ldapsuperadmin,ou=roles,dc=bayshorenetworks,dc=com
83
+ uniqueIdentifier: ldapsuperadmin
84
+ ou: roles
85
+ objectClass: accessRole
86
+
87
+ dn: mail=catperson@steamheat.net,ou=people,dc=bayshorenetworks,dc=com
88
+ cn: Sid Sorokin
89
+ mail: catperson@steamheat.net
90
+ sn: Sorokin
91
+ ou: people
92
+ objectClass: top
93
+ objectClass: inetorgperson
94
+ objectClass: authorizedperson
95
+ hasAccessRole: uniqueIdentifier=engineer,ou=roles
96
+ hasAccessRole: uniqueIdentifier=ogilvy_elephant_user,ou=roles
97
+ hasAccessRole: uniqueIdentifier=ldapsuperadmin,ou=roles
98
+ hasAccessRole: uniqueIdentifier=ogilvy_eagle_user,ou=roles
99
+ hasAccessRole: uniqueIdentifier=greenplug_user,ou=roles
100
+ hasAccessRole: uniqueIdentifier=workorder_user,ou=roles
101
+
data/tests/testem.rb ADDED
@@ -0,0 +1,12 @@
1
+ # $Id: testem.rb 121 2006-05-15 18:36:24Z blackhedd $
2
+ #
3
+ #
4
+
5
+ require 'test/unit'
6
+ require 'tests/testber'
7
+ require 'tests/testldif'
8
+ require 'tests/testldap'
9
+ require 'tests/testpsw'
10
+ require 'tests/testfilter'
11
+
12
+
@@ -0,0 +1,37 @@
1
+ # $Id: testfilter.rb 122 2006-05-15 20:03:56Z blackhedd $
2
+ #
3
+ #
4
+
5
+ require 'test/unit'
6
+
7
+ $:.unshift "lib"
8
+
9
+ require 'net/ldap'
10
+
11
+
12
+ class TestFilter < Test::Unit::TestCase
13
+
14
+ def setup
15
+ end
16
+
17
+
18
+ def teardown
19
+ end
20
+
21
+ def test_rfc_2254
22
+ p Net::LDAP::Filter.from_rfc2254( " ( uid=george* ) " )
23
+ p Net::LDAP::Filter.from_rfc2254( "uid!=george*" )
24
+ p Net::LDAP::Filter.from_rfc2254( "uid<george*" )
25
+ p Net::LDAP::Filter.from_rfc2254( "uid <= george*" )
26
+ p Net::LDAP::Filter.from_rfc2254( "uid>george*" )
27
+ p Net::LDAP::Filter.from_rfc2254( "uid>=george*" )
28
+ p Net::LDAP::Filter.from_rfc2254( "uid!=george*" )
29
+
30
+ p Net::LDAP::Filter.from_rfc2254( "(& (uid!=george* ) (mail=*))" )
31
+ p Net::LDAP::Filter.from_rfc2254( "(| (uid!=george* ) (mail=*))" )
32
+ p Net::LDAP::Filter.from_rfc2254( "(! (mail=*))" )
33
+ end
34
+
35
+
36
+ end
37
+
data/tests/testldap.rb ADDED
@@ -0,0 +1,190 @@
1
+ # $Id: testldap.rb 65 2006-04-23 01:17:49Z blackhedd $
2
+ #
3
+ #
4
+
5
+
6
+ $:.unshift "lib"
7
+
8
+ require 'test/unit'
9
+
10
+ require 'net/ldap'
11
+ require 'stringio'
12
+
13
+
14
+ class TestLdapClient < Test::Unit::TestCase
15
+
16
+ # TODO: these tests crash and burn if the associated
17
+ # LDAP testserver isn't up and running.
18
+ # We rely on being able to read a file with test data
19
+ # in LDIF format.
20
+ # TODO, WARNING: for the moment, this data is in a file
21
+ # whose name and location are HARDCODED into the
22
+ # instance method load_test_data.
23
+
24
+ def setup
25
+ @host = "127.0.0.1"
26
+ @port = 3890
27
+ @auth = {
28
+ :method => :simple,
29
+ :username => "cn=bigshot,dc=bayshorenetworks,dc=com",
30
+ :password => "opensesame"
31
+ }
32
+
33
+ @ldif = load_test_data
34
+ end
35
+
36
+
37
+
38
+ # Get some test data which will be used to validate
39
+ # the responses from the test LDAP server we will
40
+ # connect to.
41
+ # TODO, Bogus: we are HARDCODING the location of the file for now.
42
+ #
43
+ def load_test_data
44
+ ary = File.readlines( "tests/testdata.ldif" )
45
+ hash = {}
46
+ while line = ary.shift and line.chomp!
47
+ if line =~ /^dn:[\s]*/i
48
+ dn = $'
49
+ hash[dn] = {}
50
+ while attr = ary.shift and attr.chomp! and attr =~ /^([\w]+)[\s]*:[\s]*/
51
+ hash[dn][$1.downcase.intern] ||= []
52
+ hash[dn][$1.downcase.intern] << $'
53
+ end
54
+ end
55
+ end
56
+ hash
57
+ end
58
+
59
+
60
+
61
+ # Binding tests.
62
+ # Need tests for all kinds of network failures and incorrect auth.
63
+ # TODO: Implement a class-level timeout for operations like bind.
64
+ # Search has a timeout defined at the protocol level, other ops do not.
65
+ # TODO, use constants for the LDAP result codes, rather than hardcoding them.
66
+ def test_bind
67
+ ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
68
+ assert_equal( true, ldap.bind )
69
+ assert_equal( 0, ldap.get_operation_result.code )
70
+ assert_equal( "Success", ldap.get_operation_result.message )
71
+
72
+ bad_username = @auth.merge( {:username => "cn=badguy,dc=imposters,dc=com"} )
73
+ ldap = Net::LDAP.new :host => @host, :port => @port, :auth => bad_username
74
+ assert_equal( false, ldap.bind )
75
+ assert_equal( 48, ldap.get_operation_result.code )
76
+ assert_equal( "Inappropriate Authentication", ldap.get_operation_result.message )
77
+
78
+ bad_password = @auth.merge( {:password => "cornhusk"} )
79
+ ldap = Net::LDAP.new :host => @host, :port => @port, :auth => bad_password
80
+ assert_equal( false, ldap.bind )
81
+ assert_equal( 49, ldap.get_operation_result.code )
82
+ assert_equal( "Invalid Credentials", ldap.get_operation_result.message )
83
+ end
84
+
85
+
86
+
87
+ def test_search
88
+ ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
89
+
90
+ search = {:base => "dc=smalldomain,dc=com"}
91
+ assert_equal( false, ldap.search( search ))
92
+ assert_equal( 32, ldap.get_operation_result.code )
93
+
94
+ search = {:base => "dc=bayshorenetworks,dc=com"}
95
+ assert_equal( true, ldap.search( search ))
96
+ assert_equal( 0, ldap.get_operation_result.code )
97
+
98
+ ldap.search( search ) {|res|
99
+ assert_equal( res, @ldif )
100
+ }
101
+ end
102
+
103
+
104
+
105
+
106
+ # This is a helper routine for test_search_attributes.
107
+ def internal_test_search_attributes attrs_to_search
108
+ ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
109
+ assert( ldap.bind )
110
+
111
+ search = {
112
+ :base => "dc=bayshorenetworks,dc=com",
113
+ :attributes => attrs_to_search
114
+ }
115
+
116
+ ldif = @ldif
117
+ ldif.each {|dn,entry|
118
+ entry.delete_if {|attr,value|
119
+ ! attrs_to_search.include?(attr)
120
+ }
121
+ }
122
+
123
+ assert_equal( true, ldap.search( search ))
124
+ ldap.search( search ) {|res|
125
+ res_keys = res.keys.sort
126
+ ldif_keys = ldif.keys.sort
127
+ assert( res_keys, ldif_keys )
128
+ res.keys.each {|rk|
129
+ assert( res[rk], ldif[rk] )
130
+ }
131
+ }
132
+ end
133
+
134
+
135
+ def test_search_attributes
136
+ internal_test_search_attributes [:mail]
137
+ internal_test_search_attributes [:cn]
138
+ internal_test_search_attributes [:ou]
139
+ internal_test_search_attributes [:hasaccessprivilege]
140
+ internal_test_search_attributes ["mail"]
141
+ internal_test_search_attributes ["cn"]
142
+ internal_test_search_attributes ["ou"]
143
+ internal_test_search_attributes ["hasaccessrole"]
144
+
145
+ internal_test_search_attributes [:mail, :cn, :ou, :hasaccessrole]
146
+ internal_test_search_attributes [:mail, "cn", :ou, "hasaccessrole"]
147
+ end
148
+
149
+
150
+ def test_search_filters
151
+ ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
152
+ search = {
153
+ :base => "dc=bayshorenetworks,dc=com",
154
+ :filter => Net::LDAP::Filter.eq( "sn", "Fosse" )
155
+ }
156
+
157
+ ldap.search( search ) {|res|
158
+ p res
159
+ }
160
+ end
161
+
162
+
163
+
164
+ def test_open
165
+ ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
166
+ ldap.open {|ldap|
167
+ 10.times {
168
+ rc = ldap.search( :base => "dc=bayshorenetworks,dc=com" )
169
+ assert_equal( true, rc )
170
+ }
171
+ }
172
+ end
173
+
174
+
175
+ def test_ldap_open
176
+ Net::LDAP.open( :host => @host, :port => @port, :auth => @auth ) {|ldap|
177
+ 10.times {
178
+ rc = ldap.search( :base => "dc=bayshorenetworks,dc=com" )
179
+ assert_equal( true, rc )
180
+ }
181
+ }
182
+ end
183
+
184
+
185
+
186
+
187
+
188
+ end
189
+
190
+
data/tests/testldif.rb ADDED
@@ -0,0 +1,69 @@
1
+ # $Id: testldif.rb 61 2006-04-18 20:55:55Z blackhedd $
2
+ #
3
+ #
4
+
5
+
6
+ $:.unshift "lib"
7
+
8
+ require 'test/unit'
9
+
10
+ require 'net/ldap'
11
+ require 'net/ldif'
12
+
13
+ require 'sha1'
14
+ require 'base64'
15
+
16
+ class TestLdif < Test::Unit::TestCase
17
+
18
+ TestLdifFilename = "tests/testdata.ldif"
19
+
20
+ def test_empty_ldif
21
+ ds = Net::LDAP::Dataset::read_ldif( StringIO.new )
22
+ assert_equal( true, ds.empty? )
23
+ end
24
+
25
+ def test_ldif_with_comments
26
+ str = ["# Hello from LDIF-land", "# This is an unterminated comment"]
27
+ io = StringIO.new( str[0] + "\r\n" + str[1] )
28
+ ds = Net::LDAP::Dataset::read_ldif( io )
29
+ assert_equal( str, ds.comments )
30
+ end
31
+
32
+ def test_ldif_with_password
33
+ psw = "goldbricks"
34
+ hashed_psw = "{SHA}" + Base64::encode64( SHA1.new(psw).digest ).chomp
35
+
36
+ ldif_encoded = Base64::encode64( hashed_psw ).chomp
37
+ ds = Net::LDAP::Dataset::read_ldif( StringIO.new( "dn: Goldbrick\r\nuserPassword:: #{ldif_encoded}\r\n\r\n" ))
38
+ recovered_psw = ds["Goldbrick"][:userpassword].shift
39
+ assert_equal( hashed_psw, recovered_psw )
40
+ end
41
+
42
+ def test_ldif_with_continuation_lines
43
+ ds = Net::LDAP::Dataset::read_ldif( StringIO.new( "dn: abcdefg\r\n hijklmn\r\n\r\n" ))
44
+ assert_equal( true, ds.has_key?( "abcdefg hijklmn" ))
45
+ end
46
+
47
+ # TODO, INADEQUATE. We need some more tests
48
+ # to verify the content.
49
+ def test_ldif
50
+ File.open( TestLdifFilename, "r" ) {|f|
51
+ ds = Net::LDAP::Dataset::read_ldif( f )
52
+ assert_equal( 13, ds.length )
53
+ }
54
+ end
55
+
56
+ # TODO, need some tests.
57
+ # Must test folded lines and base64-encoded lines as well as normal ones.
58
+ def test_to_ldif
59
+ File.open( TestLdifFilename, "r" ) {|f|
60
+ ds = Net::LDAP::Dataset::read_ldif( f )
61
+ ds.to_ldif
62
+ assert_equal( true, false ) # REMOVE WHEN WE HAVE SOME TESTS HERE.
63
+ }
64
+ end
65
+
66
+
67
+ end
68
+
69
+
data/tests/testpsw.rb ADDED
@@ -0,0 +1,28 @@
1
+ # $Id: testpsw.rb 72 2006-04-24 21:58:14Z blackhedd $
2
+ #
3
+ #
4
+
5
+
6
+ $:.unshift "lib"
7
+
8
+ require 'net/ldap'
9
+ require 'stringio'
10
+
11
+
12
+ class TestPassword < Test::Unit::TestCase
13
+
14
+ def setup
15
+ end
16
+
17
+
18
+ def test_psw
19
+ assert_equal( "{MD5}xq8jwrcfibi0sZdZYNkSng==", Net::LDAP::Password.generate( :md5, "cashflow" ))
20
+ assert_equal( "{SHA}YE4eGkN4BvwNN1f5R7CZz0kFn14=", Net::LDAP::Password.generate( :sha, "cashflow" ))
21
+ end
22
+
23
+
24
+
25
+
26
+ end
27
+
28
+
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pruby-net-ldap
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Siva Kilaru
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-08 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: bundler
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.0
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: jeweler
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 1.5.2
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ description: Pure Ruby LDAP library.
39
+ email: siva.kilaru@rackspace.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - ChangeLog
46
+ - LICENSE.txt
47
+ - README
48
+ - README.rdoc
49
+ files:
50
+ - .document
51
+ - COPYING
52
+ - ChangeLog
53
+ - Gemfile
54
+ - Gemfile.lock
55
+ - LICENCE
56
+ - LICENSE.txt
57
+ - README
58
+ - README.rdoc
59
+ - Rakefile
60
+ - VERSION
61
+ - lib/net/ber.rb
62
+ - lib/net/ldap.rb
63
+ - lib/net/ldap/dataset.rb
64
+ - lib/net/ldap/entry.rb
65
+ - lib/net/ldap/filter.rb
66
+ - lib/net/ldap/pdu.rb
67
+ - lib/net/ldap/psw.rb
68
+ - lib/net/ldif.rb
69
+ - lib/pruby-net-ldap.rb
70
+ - pre-setup.rb
71
+ - pruby-net-ldap.gemspec
72
+ - setup.rb
73
+ - tests/testber.rb
74
+ - tests/testdata.ldif
75
+ - tests/testem.rb
76
+ - tests/testfilter.rb
77
+ - tests/testldap.rb
78
+ - tests/testldif.rb
79
+ - tests/testpsw.rb
80
+ has_rdoc: true
81
+ homepage: http://github.com/kilaru/pruby-net-ldap
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 304052113
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: "0"
104
+ requirements: []
105
+
106
+ rubyforge_project:
107
+ rubygems_version: 1.5.2
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: See Net::LDAP for documentation and usage samples.
111
+ test_files: []
112
+