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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
# $Id: tc_search.rb,v 1.4 2006/02/12 19:55:59 ianmacd Exp $
#
# A suite of unit tests for testing Ruby/LDAP search functionality.
require 'ldap'
require 'ldap/control'
require 'test/unit'
require './setup'
class TC_SearchTest < TC_LDAPTest
# Ensure that giving an incorrect attribute argument raises an exception
# and that passing a string instead of an array is treated as a single
# element array.
#
def test_attrs
assert_raise( TypeError ) do
@@conn.search( @@naming_context, LDAP::LDAP_SCOPE_ONELEVEL,
'(objectClass=*)', false )
end
@@conn.search( @@naming_context, LDAP::LDAP_SCOPE_ONELEVEL,
'(objectClass=*)', [], true ) do |x|
assert_nil( x['objectClass'] )
break
end
@@conn.search( @@naming_context, LDAP::LDAP_SCOPE_ONELEVEL,
'(objectClass=*)', 'objectClass' ) do |x|
x = x.to_hash
x.delete( 'dn' )
assert( x.to_hash.keys == [ 'objectClass' ] )
break
end
end
# Ensure that we can sort on a given attribute.
#
def test_sort_attr
ou = []
@@conn.search( @@naming_context, LDAP::LDAP_SCOPE_ONELEVEL,
'(ou=*)' ) do |x|
ou << x['ou']
end
ou.flatten!
sorted_ou = []
@@conn.search( @@naming_context, LDAP::LDAP_SCOPE_ONELEVEL,
'(ou=*)', nil, nil, 0, 0, 'ou' ) do |x|
sorted_ou << x['ou']
end
sorted_ou.flatten!
assert_not_equal( ou, sorted_ou )
assert_equal( ou.sort, sorted_ou )
end
# Ensure that we can pass a proc object to use for sorting.
#
def test_sort_proc
ct = []
@@conn.search( @@naming_context, LDAP::LDAP_SCOPE_ONELEVEL,
'(objectClass=*)', [ 'createTimestamp' ] ) do |x|
ct << x['createTimestamp']
end
ct.flatten!
sorted_ct = []
s_proc = proc { |a,b| b <=> a }
@@conn.search( @@naming_context, LDAP::LDAP_SCOPE_ONELEVEL,
'(objectClass=*)', [ 'createTimestamp' ], nil, 0, 0,
'createTimestamp', s_proc ) do |x|
sorted_ct << x['createTimestamp']
end
sorted_ct.flatten!
assert_not_equal( ct, sorted_ct )
assert_equal( ct.sort( &s_proc ), sorted_ct )
end
# Ensure that the paged results control works properly.
#
def test_paged_results
total = 0
page_size = 1
cookie = ''
loop do
ber_string = LDAP::Control.encode( page_size, cookie )
ctrl = LDAP::Control.new( LDAP::LDAP_CONTROL_PAGEDRESULTS,
ber_string,
true )
@@conn.set_option( LDAP::LDAP_OPT_SERVER_CONTROLS, [ ctrl ] )
this_page = nil
assert_nothing_raised do
begin
this_page = @@conn.search2( @@naming_context,
LDAP::LDAP_SCOPE_ONELEVEL,
'(objectclass=*)' )
rescue LDAP::ResultError
@@conn = nil
raise
end
end
total += this_page.size
assert_equal( page_size, this_page.size )
@@conn.controls.each do |c|
if c.oid == LDAP::LDAP_CONTROL_PAGEDRESULTS
ctrl = c
break
end
end
assert_equal( ctrl.oid, LDAP::LDAP_CONTROL_PAGEDRESULTS )
fetched_size, cookie = ctrl.decode
page_size = fetched_size if fetched_size.to_i > 0
break if cookie.empty?
end
# Reset connection.
@@conn = nil
setup
unpaged = @@conn.search2( @@naming_context, LDAP::LDAP_SCOPE_ONELEVEL,
'(objectclass=*)' )
# Does the total number of results match the equivalent unpaged search?
# This has a race condition, but we assume the number of top-level OUs is
# more or less static. :-)
assert_equal( total, unpaged.size )
end
end
|