ticket48013_test.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2016 Red Hat, Inc.
  3. # All rights reserved.
  4. #
  5. # License: GPL (version 3 or any later version).
  6. # See LICENSE for details.
  7. # --- END COPYRIGHT BLOCK ---
  8. #
  9. import ldapurl
  10. import pytest
  11. from ldap.ldapobject import SimpleLDAPObject
  12. from ldap.syncrepl import SyncreplConsumer
  13. from lib389.utils import *
  14. from lib389.topologies import topology_st
  15. logging.getLogger(__name__).setLevel(logging.DEBUG)
  16. log = logging.getLogger(__name__)
  17. class SyncObject(SimpleLDAPObject, SyncreplConsumer):
  18. def __init__(self, uri):
  19. # Init the ldap connection
  20. SimpleLDAPObject.__init__(self, uri)
  21. def sync_search(self, test_cookie):
  22. self.syncrepl_search('dc=example,dc=com', ldap.SCOPE_SUBTREE,
  23. filterstr='(objectclass=*)', mode='refreshOnly',
  24. cookie=test_cookie)
  25. def poll(self):
  26. self.syncrepl_poll(all=1)
  27. def test_ticket48013(topology_st):
  28. '''
  29. Content Synchonization: Test that invalid cookies are caught
  30. '''
  31. cookies = ('#', '##', 'a#a#a', 'a#a#1')
  32. # Enable dynamic plugins
  33. try:
  34. topology_st.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-dynamic-plugins', 'on')])
  35. except ldap.LDAPError as e:
  36. ldap.error('Failed to enable dynamic plugin!' + e.message['desc'])
  37. assert False
  38. # Enable retro changelog
  39. topology_st.standalone.plugins.enable(name=PLUGIN_RETRO_CHANGELOG)
  40. # Enbale content sync plugin
  41. topology_st.standalone.plugins.enable(name=PLUGIN_REPL_SYNC)
  42. # Set everything up
  43. ldap_url = ldapurl.LDAPUrl('ldap://%s:%s' % (HOST_STANDALONE,
  44. PORT_STANDALONE))
  45. ldap_connection = SyncObject(ldap_url.initializeUrl())
  46. # Authenticate
  47. try:
  48. ldap_connection.simple_bind_s(DN_DM, PASSWORD)
  49. except ldap.LDAPError as e:
  50. print('Login to LDAP server failed: %s' % e.message['desc'])
  51. assert False
  52. # Test invalid cookies
  53. for invalid_cookie in cookies:
  54. log.info('Testing cookie: %s' % invalid_cookie)
  55. try:
  56. ldap_connection.sync_search(invalid_cookie)
  57. ldap_connection.poll()
  58. log.fatal('Invalid cookie accepted!')
  59. assert False
  60. except Exception as e:
  61. log.info('Invalid cookie correctly rejected: %s' % e.message['info'])
  62. pass
  63. # Success
  64. log.info('Test complete')
  65. if __name__ == '__main__':
  66. # Run isolated
  67. # -s for DEBUG mode
  68. CURRENT_FILE = os.path.realpath(__file__)
  69. pytest.main("-s %s" % CURRENT_FILE)