ticket47970_test.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import os
  2. import sys
  3. import time
  4. import ldap
  5. import ldap.sasl
  6. import logging
  7. import pytest
  8. from lib389 import DirSrv, Entry, tools, tasks
  9. from lib389.tools import DirSrvTools
  10. from lib389._constants import *
  11. from lib389.properties import *
  12. from lib389.tasks import *
  13. log = logging.getLogger(__name__)
  14. installation_prefix = None
  15. USER1_DN = "uid=user1,%s" % DEFAULT_SUFFIX
  16. USER2_DN = "uid=user2,%s" % DEFAULT_SUFFIX
  17. class TopologyStandalone(object):
  18. def __init__(self, standalone):
  19. standalone.open()
  20. self.standalone = standalone
  21. @pytest.fixture(scope="module")
  22. def topology(request):
  23. '''
  24. This fixture is used to standalone topology for the 'module'.
  25. '''
  26. global installation_prefix
  27. if installation_prefix:
  28. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  29. standalone = DirSrv(verbose=False)
  30. # Args for the standalone instance
  31. args_instance[SER_HOST] = HOST_STANDALONE
  32. args_instance[SER_PORT] = PORT_STANDALONE
  33. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  34. args_standalone = args_instance.copy()
  35. standalone.allocate(args_standalone)
  36. # Get the status of the instance and restart it if it exists
  37. instance_standalone = standalone.exists()
  38. # Remove the instance
  39. if instance_standalone:
  40. standalone.delete()
  41. # Create the instance
  42. standalone.create()
  43. # Used to retrieve configuration information (dbdir, confdir...)
  44. standalone.open()
  45. # clear the tmp directory
  46. standalone.clearTmpDir(__file__)
  47. # Here we have standalone instance up and running
  48. return TopologyStandalone(standalone)
  49. def test_ticket47970(topology):
  50. """
  51. Testing that a failed SASL bind does not trigger account lockout -
  52. which would attempt to update the passwordRetryCount on the root dse entry
  53. """
  54. log.info('Testing Ticket 47970 - Testing that a failed SASL bind does not trigger account lockout')
  55. #
  56. # Enable account lockout
  57. #
  58. try:
  59. topology.standalone.modify_s("cn=config", [(ldap.MOD_REPLACE, 'passwordLockout', 'on')])
  60. log.info('account lockout enabled.')
  61. except ldap.LDAPError, e:
  62. log.error('Failed to enable account lockout: ' + e.message['desc'])
  63. assert False
  64. try:
  65. topology.standalone.modify_s("cn=config", [(ldap.MOD_REPLACE, 'passwordMaxFailure', '5')])
  66. log.info('passwordMaxFailure set.')
  67. except ldap.LDAPError, e:
  68. log.error('Failed to to set passwordMaxFailure: ' + e.message['desc'])
  69. assert False
  70. #
  71. # Perform SASL bind that should fail
  72. #
  73. failed_as_expected = False
  74. try:
  75. user_name = "mark"
  76. pw = "secret"
  77. auth_tokens = ldap.sasl.digest_md5(user_name, pw)
  78. topology.standalone.sasl_interactive_bind_s("", auth_tokens)
  79. except ldap.INVALID_CREDENTIALS, e:
  80. log.info("SASL Bind failed as expected")
  81. failed_as_expected = True
  82. if not failed_as_expected:
  83. log.error("SASL bind unexpectedly succeeded!")
  84. assert False
  85. #
  86. # Check that passwordRetryCount was not set on the root dse entry
  87. #
  88. try:
  89. entry = topology.standalone.search_s("", ldap.SCOPE_BASE,
  90. "passwordRetryCount=*",
  91. ['passwordRetryCount'])
  92. except ldap.LDAPError, e:
  93. log.error('Failed to search Root DSE entry: ' + e.message['desc'])
  94. assert False
  95. if entry:
  96. log.error('Root DSE was incorrectly updated')
  97. assert False
  98. # We passed
  99. log.info('Root DSE was correctly not updated')
  100. def test_ticket47970_final(topology):
  101. topology.standalone.delete()
  102. log.info('Testcase PASSED')
  103. def run_isolated():
  104. '''
  105. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  106. To run isolated without py.test, you need to
  107. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  108. - set the installation prefix
  109. - run this program
  110. '''
  111. global installation_prefix
  112. installation_prefix = None
  113. topo = topology(True)
  114. test_ticket47970(topo)
  115. test_ticket47970_final(topo)
  116. if __name__ == '__main__':
  117. run_isolated()