dim-ruby-net-ldap 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ # Add 'lib' to load path.
2
+ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'shoulda'
7
+
8
+ require 'net/ldap'
@@ -0,0 +1,100 @@
1
+ # $Id: testber.rb 230 2006-12-19 18:27:57Z blackhedd $
2
+
3
+ require File.dirname(__FILE__) + '/common'
4
+
5
+ class TestBer < Test::Unit::TestCase
6
+
7
+ def test_encode_boolean
8
+ assert_equal( "\x01\x01\x01", true.to_ber ) # should actually be: 01 01 ff
9
+ assert_equal( "\x01\x01\x00", false.to_ber )
10
+ end
11
+
12
+ #def test_encode_nil
13
+ # assert_equal( "\x05\x00", nil.to_ber )
14
+ #end
15
+
16
+ def test_encode_integer
17
+
18
+ # Fixnum
19
+ #
20
+ #assert_equal( "\x02\x02\x96\x46", -27_066.to_ber )
21
+ #assert_equal( "\x02\x02\xFF\x7F", -129.to_ber )
22
+ #assert_equal( "\x02\x01\x80", -128.to_ber )
23
+ #assert_equal( "\x02\x01\xFF", -1.to_ber )
24
+
25
+ assert_equal( "\x02\x01\x00", 0.to_ber )
26
+ assert_equal( "\x02\x01\x01", 1.to_ber )
27
+ assert_equal( "\x02\x01\x7F", 127.to_ber )
28
+ assert_equal( "\x02\x02\x00\x80", 128.to_ber )
29
+ assert_equal( "\x02\x02\x00\xFF", 255.to_ber )
30
+
31
+ assert_equal( "\x02\x02\x01\x00", 256.to_ber )
32
+ assert_equal( "\x02\x03\x00\xFF\xFF", 65535.to_ber )
33
+
34
+ assert_equal( "\x02\x03\x01\x00\x00", 65536.to_ber )
35
+ assert_equal( "\x02\x04\x00\xFF\xFF\xFF", 16_777_215.to_ber )
36
+
37
+ assert_equal( "\x02\x04\x01\x00\x00\x00", 0x01000000.to_ber )
38
+ assert_equal( "\x02\x04\x3F\xFF\xFF\xFF", 0x3FFFFFFF.to_ber )
39
+
40
+ # Bignum
41
+ #
42
+ assert_equal( "\x02\x04\x4F\xFF\xFF\xFF", 0x4FFFFFFF.to_ber )
43
+ #assert_equal( "\x02\x05\x00\xFF\xFF\xFF\xFF", 0xFFFFFFFF.to_ber )
44
+ end
45
+
46
+ # TOD Add some much bigger numbers
47
+ # 5000000000 is a Bignum, which hits different code.
48
+ def test_ber_integers
49
+ assert_equal( "\002\001\005", 5.to_ber )
50
+ assert_equal( "\002\002\001\364", 500.to_ber )
51
+ assert_equal( "\002\003\0\303P", 50000.to_ber )
52
+ assert_equal( "\002\005\001*\005\362\000", 5000000000.to_ber )
53
+ end
54
+
55
+ def test_ber_bignums
56
+ # Some of these values are Fixnums and some are Bignums. Different BER code.
57
+ [
58
+ 5,
59
+ 50,
60
+ 500,
61
+ 5000,
62
+ 50000,
63
+ 500000,
64
+ 5000000,
65
+ 50000000,
66
+ 500000000,
67
+ 1000000000,
68
+ 2000000000,
69
+ 3000000000,
70
+ 4000000000,
71
+ 5000000000
72
+ ].each {|val|
73
+ assert_equal( val, val.to_ber.read_ber )
74
+ }
75
+ end
76
+
77
+ def test_ber_parsing
78
+ assert_equal( 6, "\002\001\006".read_ber( Net::LDAP::AsnSyntax ))
79
+ assert_equal( "testing", "\004\007testing".read_ber( Net::LDAP::AsnSyntax ))
80
+ end
81
+
82
+ def test_ber_parser_on_ldap_bind_request
83
+ require 'stringio'
84
+
85
+ s = StringIO.new(
86
+ "0$\002\001\001`\037\002\001\003\004\rAdministrator\200\vad_is_bogus" )
87
+
88
+ assert_equal(
89
+ [1, [3, "Administrator", "ad_is_bogus"]],
90
+ s.read_ber( Net::LDAP::AsnSyntax ))
91
+ end
92
+
93
+ def test_oid
94
+ oid = Net::BER::BerIdentifiedOid.new( [1,3,6,1,2,1,1,1,0] )
95
+ assert_equal( "\006\b+\006\001\002\001\001\001\000", oid.to_ber )
96
+
97
+ oid = Net::BER::BerIdentifiedOid.new( "1.3.6.1.2.1.1.1.0" )
98
+ assert_equal( "\006\b+\006\001\002\001\001\001\000", oid.to_ber )
99
+ end
100
+ end
@@ -0,0 +1,58 @@
1
+ require 'common'
2
+
3
+ class TestEntry < Test::Unit::TestCase
4
+
5
+ context "An instance of Entry" do
6
+
7
+ setup do
8
+ @entry = Net::LDAP::Entry.new 'cn=Barbara,o=corp'
9
+ end
10
+
11
+ should "be initialized with the DN" do
12
+ assert_equal 'cn=Barbara,o=corp', @entry.dn
13
+ end
14
+
15
+ should 'return an empty array when accessing a nonexistent attribute (index lookup)' do
16
+ assert_equal [], @entry['sn']
17
+ end
18
+
19
+ should 'return an empty array when accessing a nonexistent attribute (method call)' do
20
+ assert_equal [], @entry.sn
21
+ end
22
+
23
+ should 'create an attribute on assignment (index lookup)' do
24
+ @entry['sn'] = 'Jensen'
25
+ assert_equal ['Jensen'], @entry['sn']
26
+ end
27
+
28
+ should 'create an attribute on assignment (method call)' do
29
+ @entry.sn = 'Jensen'
30
+ assert_equal ['Jensen'], @entry.sn
31
+ end
32
+
33
+ should 'have attributes accessible by index lookup' do
34
+ @entry['sn'] = 'Jensen'
35
+ assert_equal ['Jensen'], @entry['sn']
36
+ end
37
+
38
+ should 'have attributes accessible using a Symbol as the index' do
39
+ @entry[:sn] = 'Jensen'
40
+ assert_equal ['Jensen'], @entry[:sn]
41
+ end
42
+
43
+ should 'have attributes accessible by method call' do
44
+ @entry['sn'] = 'Jensen'
45
+ assert_equal ['Jensen'], @entry.sn
46
+ end
47
+
48
+ should 'ignore case of attribute names' do
49
+ @entry['sn'] = 'Jensen'
50
+ assert_equal ['Jensen'], @entry.sn
51
+ assert_equal ['Jensen'], @entry.Sn
52
+ assert_equal ['Jensen'], @entry.SN
53
+ assert_equal ['Jensen'], @entry['sn']
54
+ assert_equal ['Jensen'], @entry['Sn']
55
+ assert_equal ['Jensen'], @entry['SN']
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,83 @@
1
+ # $Id: testfilter.rb 245 2007-05-05 02:44:32Z blackhedd $
2
+
3
+ require 'common'
4
+
5
+ class TestFilter < Test::Unit::TestCase
6
+
7
+ # Note that the RFC doesn't define either less-than or greater-than.
8
+ def test_rfc_2254
9
+ Net::LDAP::Filter.from_rfc2254( " ( uid=george* ) " )
10
+ Net::LDAP::Filter.from_rfc2254( "uid!=george*" )
11
+ Net::LDAP::Filter.from_rfc2254( "uid <= george*" )
12
+ Net::LDAP::Filter.from_rfc2254( "uid>=george*" )
13
+ Net::LDAP::Filter.from_rfc2254( "uid!=george*" )
14
+
15
+ Net::LDAP::Filter.from_rfc2254( "(& (uid!=george* ) (mail=*))" )
16
+ Net::LDAP::Filter.from_rfc2254( "(| (uid!=george* ) (mail=*))" )
17
+ Net::LDAP::Filter.from_rfc2254( "(! (mail=*))" )
18
+ end
19
+
20
+ def test_filters_from_ber
21
+ [
22
+ Net::LDAP::Filter.eq( "objectclass", "*" ),
23
+ Net::LDAP::Filter.pres( "objectclass" ),
24
+ Net::LDAP::Filter.eq( "objectclass", "ou" ),
25
+ Net::LDAP::Filter.ge( "uid", "500" ),
26
+ Net::LDAP::Filter.le( "uid", "500" ),
27
+ (~ Net::LDAP::Filter.pres( "objectclass" )),
28
+ (Net::LDAP::Filter.pres( "objectclass" ) & Net::LDAP::Filter.pres( "ou" )),
29
+ (Net::LDAP::Filter.pres( "objectclass" ) & Net::LDAP::Filter.pres( "ou" ) & Net::LDAP::Filter.pres("sn")),
30
+ (Net::LDAP::Filter.pres( "objectclass" ) | Net::LDAP::Filter.pres( "ou" ) | Net::LDAP::Filter.pres("sn")),
31
+
32
+ Net::LDAP::Filter.eq( "objectclass", "*aaa" ),
33
+ Net::LDAP::Filter.eq( "objectclass", "*aaa*bbb" ),
34
+ Net::LDAP::Filter.eq( "objectclass", "*aaa*bbb*ccc" ),
35
+ Net::LDAP::Filter.eq( "objectclass", "aaa*bbb" ),
36
+ Net::LDAP::Filter.eq( "objectclass", "aaa*bbb*ccc" ),
37
+ Net::LDAP::Filter.eq( "objectclass", "abc*def*1111*22*g" ),
38
+ Net::LDAP::Filter.eq( "objectclass", "*aaa*" ),
39
+ Net::LDAP::Filter.eq( "objectclass", "*aaa*bbb*" ),
40
+ Net::LDAP::Filter.eq( "objectclass", "*aaa*bbb*ccc*" ),
41
+ Net::LDAP::Filter.eq( "objectclass", "aaa*" ),
42
+ Net::LDAP::Filter.eq( "objectclass", "aaa*bbb*" ),
43
+ Net::LDAP::Filter.eq( "objectclass", "aaa*bbb*ccc*" ),
44
+ ].each {|ber|
45
+ f = Net::LDAP::Filter.parse_ber( ber.to_ber.read_ber( Net::LDAP::AsnSyntax) )
46
+ assert( f == ber )
47
+ assert_equal( f.to_ber, ber.to_ber )
48
+ }
49
+
50
+ end
51
+
52
+ def test_ber_from_rfc2254_filter
53
+ [
54
+ Net::LDAP::Filter.construct( "objectclass=*" ),
55
+ Net::LDAP::Filter.construct("objectclass=ou" ),
56
+ Net::LDAP::Filter.construct("uid >= 500" ),
57
+ Net::LDAP::Filter.construct("uid <= 500" ),
58
+ Net::LDAP::Filter.construct("(!(uid=*))" ),
59
+ Net::LDAP::Filter.construct("(&(uid=*)(objectclass=*))" ),
60
+ Net::LDAP::Filter.construct("(&(uid=*)(objectclass=*)(sn=*))" ),
61
+ Net::LDAP::Filter.construct("(|(uid=*)(objectclass=*))" ),
62
+ Net::LDAP::Filter.construct("(|(uid=*)(objectclass=*)(sn=*))" ),
63
+
64
+ Net::LDAP::Filter.construct("objectclass=*aaa"),
65
+ Net::LDAP::Filter.construct("objectclass=*aaa*bbb"),
66
+ Net::LDAP::Filter.construct("objectclass=*aaa*bbb*ccc"),
67
+ Net::LDAP::Filter.construct("objectclass=aaa*bbb"),
68
+ Net::LDAP::Filter.construct("objectclass=aaa*bbb*ccc"),
69
+ Net::LDAP::Filter.construct("objectclass=abc*def*1111*22*g"),
70
+ Net::LDAP::Filter.construct("objectclass=*aaa*"),
71
+ Net::LDAP::Filter.construct("objectclass=*aaa*bbb*"),
72
+ Net::LDAP::Filter.construct("objectclass=*aaa*bbb*ccc*"),
73
+ Net::LDAP::Filter.construct("objectclass=aaa*"),
74
+ Net::LDAP::Filter.construct("objectclass=aaa*bbb*"),
75
+ Net::LDAP::Filter.construct("objectclass=aaa*bbb*ccc*"),
76
+ ].each {|ber|
77
+ f = Net::LDAP::Filter.parse_ber( ber.to_ber.read_ber( Net::LDAP::AsnSyntax) )
78
+ assert( f == ber )
79
+ assert_equal( f.to_ber, ber.to_ber )
80
+ }
81
+ end
82
+
83
+ end
@@ -0,0 +1,59 @@
1
+ # $Id: testldif.rb 61 2006-04-18 20:55:55Z blackhedd $
2
+
3
+ require 'common'
4
+
5
+ require 'net/ldif'
6
+ require 'digest/sha1'
7
+ require 'base64'
8
+
9
+ class TestLdif < Test::Unit::TestCase
10
+
11
+ TestLdifFilename = "#{File.dirname(__FILE__)}/testdata.ldif"
12
+
13
+ def test_empty_ldif
14
+ ds = Net::LDAP::Dataset::read_ldif( StringIO.new )
15
+ assert_equal( true, ds.empty? )
16
+ end
17
+
18
+ def test_ldif_with_comments
19
+ str = ["# Hello from LDIF-land", "# This is an unterminated comment"]
20
+ io = StringIO.new( str[0] + "\r\n" + str[1] )
21
+ ds = Net::LDAP::Dataset::read_ldif( io )
22
+ assert_equal( str, ds.comments )
23
+ end
24
+
25
+ def test_ldif_with_password
26
+ psw = "goldbricks"
27
+ hashed_psw = "{SHA}" + Base64::encode64(Digest::SHA1.digest(psw)).chomp
28
+
29
+ ldif_encoded = Base64::encode64( hashed_psw ).chomp
30
+ ds = Net::LDAP::Dataset::read_ldif( StringIO.new( "dn: Goldbrick\r\nuserPassword:: #{ldif_encoded}\r\n\r\n" ))
31
+ recovered_psw = ds["Goldbrick"][:userpassword].shift
32
+ assert_equal( hashed_psw, recovered_psw )
33
+ end
34
+
35
+ def test_ldif_with_continuation_lines
36
+ ds = Net::LDAP::Dataset::read_ldif( StringIO.new( "dn: abcdefg\r\n hijklmn\r\n\r\n" ))
37
+ assert_equal( true, ds.has_key?( "abcdefg hijklmn" ))
38
+ end
39
+
40
+ # TODO, INADEQUATE. We need some more tests
41
+ # to verify the content.
42
+ def test_ldif
43
+ File.open( TestLdifFilename, "r" ) {|f|
44
+ ds = Net::LDAP::Dataset::read_ldif( f )
45
+ assert_equal( 13, ds.length )
46
+ }
47
+ end
48
+
49
+ # TODO, need some tests.
50
+ # Must test folded lines and base64-encoded lines as well as normal ones.
51
+ #def test_to_ldif
52
+ # File.open( TestLdifFilename, "r" ) {|f|
53
+ # ds = Net::LDAP::Dataset::read_ldif( f )
54
+ # ds.to_ldif
55
+ # assert_equal( true, false ) # REMOVE WHEN WE HAVE SOME TESTS HERE.
56
+ # }
57
+ #end
58
+
59
+ end
@@ -0,0 +1,17 @@
1
+ # $Id: testpsw.rb 72 2006-04-24 21:58:14Z blackhedd $
2
+
3
+ require 'common'
4
+
5
+ class TestPassword < Test::Unit::TestCase
6
+
7
+ def test_psw
8
+ assert_equal(
9
+ "{MD5}xq8jwrcfibi0sZdZYNkSng==",
10
+ Net::LDAP::Password.generate( :md5, "cashflow" ))
11
+
12
+ assert_equal(
13
+ "{SHA}YE4eGkN4BvwNN1f5R7CZz0kFn14=",
14
+ Net::LDAP::Password.generate( :sha, "cashflow" ))
15
+ end
16
+
17
+ end
@@ -0,0 +1,130 @@
1
+ # $Id: testsnmp.rb 231 2006-12-21 15:09:29Z blackhedd $
2
+
3
+ require 'common'
4
+ require 'net/snmp'
5
+
6
+ class TestSnmp < Test::Unit::TestCase
7
+
8
+ SnmpGetRequest = "0'\002\001\000\004\006public\240\032\002\002?*\002\001\000\002\001\0000\0160\f\006\b+\006\001\002\001\001\001\000\005\000"
9
+ SnmpGetResponse = "0+\002\001\000\004\006public\242\036\002\002'\017\002\001\000\002\001\0000\0220\020\006\b+\006\001\002\001\001\001\000\004\004test"
10
+
11
+ SnmpGetRequestXXX = "0'\002\001\000\004\006xxxxxx\240\032\002\002?*\002\001\000\002\001\0000\0160\f\006\b+\006\001\002\001\001\001\000\005\000"
12
+
13
+
14
+ def setup
15
+ end
16
+
17
+ def teardown
18
+ end
19
+
20
+ def test_invalid_packet
21
+ data = "xxxx"
22
+ assert_raise( Net::BER::BerError ) {
23
+ ary = data.read_ber(Net::SNMP::AsnSyntax)
24
+ }
25
+
26
+ end
27
+
28
+ # The method String#read_ber! added by Net::BER consumes a well-formed BER object
29
+ # from the head of a string. If it doesn't find a complete, well-formed BER object,
30
+ # it returns nil and leaves the string unchanged. If it finds an object, it returns
31
+ # the object and removes it from the head of the string. This is good for handling
32
+ # partially-received data streams, such as from network connections.
33
+ def test_consume_string
34
+ data = "xxx"
35
+ assert_equal( nil, data.read_ber! )
36
+ assert_equal( "xxx", data )
37
+
38
+ data = SnmpGetRequest + "!!!"
39
+ ary = data.read_ber!( Net::SNMP::AsnSyntax )
40
+ assert_equal( "!!!", data )
41
+ assert ary.is_a?(Array)
42
+ assert ary.is_a?(Net::BER::BerIdentifiedArray)
43
+ end
44
+
45
+ def test_weird_packet
46
+ assert_raise( Net::SnmpPdu::Error ) {
47
+ Net::SnmpPdu.parse("aaaaaaaaaaaaaa")
48
+ }
49
+ end
50
+
51
+ def test_get_request
52
+ data = SnmpGetRequest.dup
53
+ pkt = data.read_ber(Net::SNMP::AsnSyntax)
54
+ assert pkt.is_a?(Net::BER::BerIdentifiedArray)
55
+ assert_equal( 48, pkt.ber_identifier) # Constructed [0], signifies GetRequest
56
+
57
+ pdu = Net::SnmpPdu.parse(pkt)
58
+ assert_equal(:get_request, pdu.pdu_type )
59
+ assert_equal(16170, pdu.request_id ) # whatever was in the test data. 16170 is not magic.
60
+ assert_equal( [[[1,3,6,1,2,1,1,1,0],nil]], pdu.variables )
61
+
62
+ assert_equal( pdu.to_ber_string, SnmpGetRequest )
63
+ end
64
+
65
+ def test_empty_pdu
66
+ pdu = Net::SnmpPdu.new
67
+ assert_raise( Net::SnmpPdu::Error ) {
68
+ pdu.to_ber_string
69
+ }
70
+ end
71
+
72
+ def test_malformations
73
+ pdu = Net::SnmpPdu.new
74
+ pdu.version = 0
75
+ pdu.version = 2
76
+ assert_raise( Net::SnmpPdu::Error ) {
77
+ pdu.version = 100
78
+ }
79
+
80
+ pdu.pdu_type = :get_request
81
+ pdu.pdu_type = :get_next_request
82
+ pdu.pdu_type = :get_response
83
+ pdu.pdu_type = :set_request
84
+ pdu.pdu_type = :trap
85
+ assert_raise( Net::SnmpPdu::Error ) {
86
+ pdu.pdu_type = :something_else
87
+ }
88
+ end
89
+
90
+ def test_make_response
91
+ pdu = Net::SnmpPdu.new
92
+ pdu.version = 0
93
+ pdu.community = "public"
94
+ pdu.pdu_type = :get_response
95
+ pdu.request_id = 9999
96
+ pdu.error_status = 0
97
+ pdu.error_index = 0
98
+ pdu.add_variable_binding [1,3,6,1,2,1,1,1,0], "test"
99
+
100
+ assert_equal( SnmpGetResponse, pdu.to_ber_string )
101
+ end
102
+
103
+ def test_make_bad_response
104
+ pdu = Net::SnmpPdu.new
105
+ assert_raise(Net::SnmpPdu::Error) {pdu.to_ber_string}
106
+ pdu.pdu_type = :get_response
107
+ pdu.request_id = 999
108
+ pdu.to_ber_string
109
+ # Not specifying variables doesn't create an error. (Maybe it should?)
110
+ end
111
+
112
+ def test_snmp_integers
113
+ c32 = Net::SNMP::Counter32.new(100)
114
+ assert_equal( "A\001d", c32.to_ber )
115
+ g32 = Net::SNMP::Gauge32.new(100)
116
+ assert_equal( "B\001d", g32.to_ber )
117
+ t32 = Net::SNMP::TimeTicks32.new(100)
118
+ assert_equal( "C\001d", t32.to_ber )
119
+ end
120
+
121
+ def test_community
122
+ data = SnmpGetRequestXXX.dup
123
+ ary = data.read_ber(Net::SNMP::AsnSyntax)
124
+ pdu = Net::SnmpPdu.parse( ary )
125
+ assert_equal( "xxxxxx", pdu.community )
126
+ end
127
+
128
+ end
129
+
130
+