1
0

ticket47970_test.py 4.4 KB

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