ticket48013_test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 os
  10. import time
  11. import ldap
  12. import logging
  13. import pytest
  14. import ldapurl
  15. from ldap.ldapobject import SimpleLDAPObject
  16. from ldap.syncrepl import SyncreplConsumer
  17. from lib389 import DirSrv
  18. from lib389._constants import *
  19. from lib389.properties import *
  20. from lib389.tasks import *
  21. from lib389.utils import *
  22. logging.getLogger(__name__).setLevel(logging.DEBUG)
  23. log = logging.getLogger(__name__)
  24. installation1_prefix = None
  25. class TopologyStandalone(object):
  26. def __init__(self, standalone):
  27. standalone.open()
  28. self.standalone = standalone
  29. class SyncObject(SimpleLDAPObject, SyncreplConsumer):
  30. def __init__(self, uri):
  31. # Init the ldap connection
  32. SimpleLDAPObject.__init__(self, uri)
  33. def sync_search(self, test_cookie):
  34. self.syncrepl_search('dc=example,dc=com', ldap.SCOPE_SUBTREE,
  35. filterstr='(objectclass=*)', mode='refreshOnly',
  36. cookie=test_cookie)
  37. def poll(self):
  38. self.syncrepl_poll(all=1)
  39. @pytest.fixture(scope="module")
  40. def topology(request):
  41. global installation1_prefix
  42. if installation1_prefix:
  43. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  44. # Creating standalone instance ...
  45. standalone = DirSrv(verbose=False)
  46. args_instance[SER_HOST] = HOST_STANDALONE
  47. args_instance[SER_PORT] = PORT_STANDALONE
  48. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  49. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  50. args_standalone = args_instance.copy()
  51. standalone.allocate(args_standalone)
  52. instance_standalone = standalone.exists()
  53. if instance_standalone:
  54. standalone.delete()
  55. standalone.create()
  56. standalone.open()
  57. def fin():
  58. standalone.delete()
  59. request.addfinalizer(fin)
  60. return TopologyStandalone(standalone)
  61. def test_ticket48013(topology):
  62. '''
  63. Content Synchonization: Test that invalid cookies are caught
  64. '''
  65. cookies = ('#', '##', 'a#a#a', 'a#a#1')
  66. # Enable dynamic plugins
  67. try:
  68. topology.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-dynamic-plugins', 'on')])
  69. except ldap.LDAPError as e:
  70. ldap.error('Failed to enable dynamic plugin!' + e.message['desc'])
  71. assert False
  72. # Enable retro changelog
  73. topology.standalone.plugins.enable(name=PLUGIN_RETRO_CHANGELOG)
  74. # Enbale content sync plugin
  75. topology.standalone.plugins.enable(name=PLUGIN_REPL_SYNC)
  76. # Set everything up
  77. ldap_url = ldapurl.LDAPUrl('ldap://%s:%s' % (HOST_STANDALONE,
  78. PORT_STANDALONE))
  79. ldap_connection = SyncObject(ldap_url.initializeUrl())
  80. # Authenticate
  81. try:
  82. ldap_connection.simple_bind_s(DN_DM, PASSWORD)
  83. except ldap.LDAPError as e:
  84. print('Login to LDAP server failed: %s' % e.message['desc'])
  85. assert False
  86. # Test invalid cookies
  87. for invalid_cookie in cookies:
  88. log.info('Testing cookie: %s' % invalid_cookie)
  89. try:
  90. ldap_connection.sync_search(invalid_cookie)
  91. ldap_connection.poll()
  92. log.fatal('Invalid cookie accepted!')
  93. assert False
  94. except Exception as e:
  95. log.info('Invalid cookie correctly rejected: %s' % e.message['info'])
  96. pass
  97. # Success
  98. log.info('Test complete')
  99. if __name__ == '__main__':
  100. # Run isolated
  101. # -s for DEBUG mode
  102. CURRENT_FILE = os.path.realpath(__file__)
  103. pytest.main("-s %s" % CURRENT_FILE)