def test_search_kwds(self): """Ensure all valid forms of search keywords work and that invalid keywords are detected early""" mock_sock = MockLDAPSocket() mock_sock.add_root_dse() ldap = LDAP(mock_sock) # create a valid control keyword class MockControl(controls.Control): method = ('search',) keyword = 'mock_control' REQUEST_OID = '9.999.999.999.999.9999999' test_dn = 'o=foo' mock_sock.add_search_res_entry(test_dn, {'description': ['foo']}) mock_sock.add_search_res_done(test_dn) # test with valid search keyword, control keyword, and object keyword list(ldap.search(test_dn, limit=7, mock_control='foo', rdn_attr='ou')) mock_sock.clear_sent() # test with bad keyword with self.assertRaises(TypeError): ldap.search(test_dn, bad_keyword='foo') self.assertEqual(mock_sock.num_sent(), 0)
def test_search_nosuchobject_response(self): """Ensure noSuchObject response is treated as no results""" mock_sock = MockLDAPSocket() mock_sock.add_root_dse() ldap = LDAP(mock_sock) mock_sock.add_search_res_done('', result_code=rfc4511.ResultCode('noSuchObject')) self.assertEqual(len(list(ldap.search(''))), 0)
def test_search_error_response(self): """Ensure non-success results for search are handled correctly""" mock_sock = MockLDAPSocket() mock_sock.add_root_dse() ldap = LDAP(mock_sock) mock_sock.add_search_res_done('', result_code=protoutils.RESULT_compareTrue) with self.assertRaises(exceptions.LDAPError): list(ldap.search(''))
def test_search_referral(self): """Ensure a referral response is handled correctly for search""" mock_sock = MockLDAPSocket() mock_sock.add_root_dse() ldap = LDAP(mock_sock) uri = 'ldap://test.net/ou=foo,o=testing' mock_sock.add_search_res_done(2, '', referral=[uri]) with ldap.search('', follow_referrals=False) as search: results = list(search) self.assertEqual(len(results), 0)
def test_search_result_ref(self): """Ensure searchResRef is handled correctly""" # Note: searchResEntry and searchResDone paths have already been exercised mock_sock = MockLDAPSocket() mock_sock.add_root_dse() ldap = LDAP(mock_sock) base_dn = 'ou=foo,o=testing' uri = 'ldap://test.net/ou=foo,o=testing' mock_sock.add_search_res_ref([uri]) mock_sock.add_search_res_done(base_dn) with ldap.search(base_dn, fetch_result_refs=False) as search: results = list(search) self.assertEqual(len(results), 1) srh = results[0] self.assertIsInstance(srh, laurelin.ldap.base.SearchReferenceHandle) self.assertEqual(len(srh.uris), 1)
def test_pagedresults(self): """Use the paged results controls to exercise the controls subsystem""" extensions.paged_results.require() mock_sock = MockLDAPSocket() mock_sock.add_search_res_entry('', { 'supportedControl': [pagedresults.OID], 'namingContexts': ['o=testing'] }) mock_sock.add_search_res_done('') ldap = LDAP(mock_sock) test_dn = 'o=testing' test_cookie = 'foo' test_size = 2 # prepare response control controls = rfc4511.Controls() control = rfc4511.Control() ctrl_value = pagedresults.RealSearchControlValue() ctrl_value.setComponentByName('size', pagedresults.Size(test_size)) ctrl_value.setComponentByName('cookie', pagedresults.Cookie(test_cookie)) control.setComponentByName('controlType', rfc4511.LDAPOID(pagedresults.OID)) control.setComponentByName('controlValue', ber_encode(ctrl_value)) controls.setComponentByPosition(0, control) # prepare search results mock_sock.add_search_res_entry(test_dn, {}) mock_sock.add_search_res_entry(test_dn, {}) mock_sock.add_search_res_done(test_dn, controls=controls) # do search with critical ctrl_kwds = {pagedresults.LaurelinExtension.PagedResultsControl.keyword: critical(test_size)} search = ldap.search(test_dn, **ctrl_kwds) # get all results results = list(search) self.assertEqual(len(results), test_size) self.assertTrue(hasattr(search, pagedresults.LaurelinExtension.PagedResultsControl.response_attr)) self.assertEqual(getattr(search, pagedresults.LaurelinExtension.PagedResultsControl.response_attr), test_cookie)
def test_pagedresults(self): """Use the paged results controls to exercise the controls subsystem""" LDAP.activate_extension('laurelin.extensions.pagedresults') mock_sock = MockLDAPSocket() mock_sock.add_search_res_entry('', { 'supportedControl': [pagedresults.OID], 'namingContexts': ['o=testing'] }) mock_sock.add_search_res_done('') ldap = LDAP(mock_sock) test_dn = 'o=testing' test_cookie = 'foo' test_size = 2 # prepare response control controls = rfc4511.Controls() control = rfc4511.Control() ctrl_value = pagedresults.RealSearchControlValue() ctrl_value.setComponentByName('size', pagedresults.Size(test_size)) ctrl_value.setComponentByName('cookie', pagedresults.Cookie(test_cookie)) control.setComponentByName('controlType', rfc4511.LDAPOID(pagedresults.OID)) control.setComponentByName('controlValue', ber_encode(ctrl_value)) controls.setComponentByPosition(0, control) # prepare search results mock_sock.add_search_res_entry(test_dn, {}) mock_sock.add_search_res_entry(test_dn, {}) mock_sock.add_search_res_done(test_dn, controls=controls) # do search with critical ctrl_kwds = {pagedresults.PagedResultsControl.keyword: critical(test_size)} search = ldap.search(test_dn, **ctrl_kwds) # get all results results = list(search) self.assertEqual(len(results), test_size) self.assertTrue(hasattr(search, pagedresults.PagedResultsControl.response_attr)) self.assertEqual(getattr(search, pagedresults.PagedResultsControl.response_attr), test_cookie)