You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testldap.rb 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # $Id: testldap.rb 65 2006-04-23 01:17:49Z blackhedd $
  2. #
  3. #
  4. $:.unshift "lib"
  5. require 'test/unit'
  6. require 'net/ldap'
  7. require 'stringio'
  8. class TestLdapClient < Test::Unit::TestCase
  9. # TODO: these tests crash and burn if the associated
  10. # LDAP testserver isn't up and running.
  11. # We rely on being able to read a file with test data
  12. # in LDIF format.
  13. # TODO, WARNING: for the moment, this data is in a file
  14. # whose name and location are HARDCODED into the
  15. # instance method load_test_data.
  16. def setup
  17. @host = "127.0.0.1"
  18. @port = 3890
  19. @auth = {
  20. :method => :simple,
  21. :username => "cn=bigshot,dc=bayshorenetworks,dc=com",
  22. :password => "opensesame"
  23. }
  24. @ldif = load_test_data
  25. end
  26. # Get some test data which will be used to validate
  27. # the responses from the test LDAP server we will
  28. # connect to.
  29. # TODO, Bogus: we are HARDCODING the location of the file for now.
  30. #
  31. def load_test_data
  32. ary = File.readlines( "tests/testdata.ldif" )
  33. hash = {}
  34. while line = ary.shift and line.chomp!
  35. if line =~ /^dn:[\s]*/i
  36. dn = $'
  37. hash[dn] = {}
  38. while attr = ary.shift and attr.chomp! and attr =~ /^([\w]+)[\s]*:[\s]*/
  39. hash[dn][$1.downcase.intern] ||= []
  40. hash[dn][$1.downcase.intern] << $'
  41. end
  42. end
  43. end
  44. hash
  45. end
  46. # Binding tests.
  47. # Need tests for all kinds of network failures and incorrect auth.
  48. # TODO: Implement a class-level timeout for operations like bind.
  49. # Search has a timeout defined at the protocol level, other ops do not.
  50. # TODO, use constants for the LDAP result codes, rather than hardcoding them.
  51. def test_bind
  52. ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
  53. assert_equal( true, ldap.bind )
  54. assert_equal( 0, ldap.get_operation_result.code )
  55. assert_equal( "Success", ldap.get_operation_result.message )
  56. bad_username = @auth.merge( {:username => "cn=badguy,dc=imposters,dc=com"} )
  57. ldap = Net::LDAP.new :host => @host, :port => @port, :auth => bad_username
  58. assert_equal( false, ldap.bind )
  59. assert_equal( 48, ldap.get_operation_result.code )
  60. assert_equal( "Inappropriate Authentication", ldap.get_operation_result.message )
  61. bad_password = @auth.merge( {:password => "cornhusk"} )
  62. ldap = Net::LDAP.new :host => @host, :port => @port, :auth => bad_password
  63. assert_equal( false, ldap.bind )
  64. assert_equal( 49, ldap.get_operation_result.code )
  65. assert_equal( "Invalid Credentials", ldap.get_operation_result.message )
  66. end
  67. def test_search
  68. ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
  69. search = {:base => "dc=smalldomain,dc=com"}
  70. assert_equal( false, ldap.search( search ))
  71. assert_equal( 32, ldap.get_operation_result.code )
  72. search = {:base => "dc=bayshorenetworks,dc=com"}
  73. assert_equal( true, ldap.search( search ))
  74. assert_equal( 0, ldap.get_operation_result.code )
  75. ldap.search( search ) {|res|
  76. assert_equal( res, @ldif )
  77. }
  78. end
  79. # This is a helper routine for test_search_attributes.
  80. def internal_test_search_attributes attrs_to_search
  81. ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
  82. assert( ldap.bind )
  83. search = {
  84. :base => "dc=bayshorenetworks,dc=com",
  85. :attributes => attrs_to_search
  86. }
  87. ldif = @ldif
  88. ldif.each {|dn,entry|
  89. entry.delete_if {|attr,value|
  90. ! attrs_to_search.include?(attr)
  91. }
  92. }
  93. assert_equal( true, ldap.search( search ))
  94. ldap.search( search ) {|res|
  95. res_keys = res.keys.sort
  96. ldif_keys = ldif.keys.sort
  97. assert( res_keys, ldif_keys )
  98. res.keys.each {|rk|
  99. assert( res[rk], ldif[rk] )
  100. }
  101. }
  102. end
  103. def test_search_attributes
  104. internal_test_search_attributes [:mail]
  105. internal_test_search_attributes [:cn]
  106. internal_test_search_attributes [:ou]
  107. internal_test_search_attributes [:hasaccessprivilege]
  108. internal_test_search_attributes ["mail"]
  109. internal_test_search_attributes ["cn"]
  110. internal_test_search_attributes ["ou"]
  111. internal_test_search_attributes ["hasaccessrole"]
  112. internal_test_search_attributes [:mail, :cn, :ou, :hasaccessrole]
  113. internal_test_search_attributes [:mail, "cn", :ou, "hasaccessrole"]
  114. end
  115. def test_search_filters
  116. ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
  117. search = {
  118. :base => "dc=bayshorenetworks,dc=com",
  119. :filter => Net::LDAP::Filter.eq( "sn", "Fosse" )
  120. }
  121. ldap.search( search ) {|res|
  122. p res
  123. }
  124. end
  125. def test_open
  126. ldap = Net::LDAP.new :host => @host, :port => @port, :auth => @auth
  127. ldap.open {|ldap|
  128. 10.times {
  129. rc = ldap.search( :base => "dc=bayshorenetworks,dc=com" )
  130. assert_equal( true, rc )
  131. }
  132. }
  133. end
  134. def test_ldap_open
  135. Net::LDAP.open( :host => @host, :port => @port, :auth => @auth ) {|ldap|
  136. 10.times {
  137. rc = ldap.search( :base => "dc=bayshorenetworks,dc=com" )
  138. assert_equal( true, rc )
  139. }
  140. }
  141. end
  142. end