attr_uniqueness_test.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 logging
  14. import pytest
  15. from lib389 import DirSrv, Entry, tools, tasks
  16. from lib389.tools import DirSrvTools
  17. from lib389._constants import *
  18. from lib389.properties import *
  19. from lib389.tasks import *
  20. from lib389.utils import *
  21. logging.getLogger(__name__).setLevel(logging.DEBUG)
  22. log = logging.getLogger(__name__)
  23. installation1_prefix = None
  24. class TopologyStandalone(object):
  25. def __init__(self, standalone):
  26. standalone.open()
  27. self.standalone = standalone
  28. @pytest.fixture(scope="module")
  29. def topology(request):
  30. global installation1_prefix
  31. if installation1_prefix:
  32. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  33. # Creating standalone instance ...
  34. standalone = DirSrv(verbose=False)
  35. args_instance[SER_HOST] = HOST_STANDALONE
  36. args_instance[SER_PORT] = PORT_STANDALONE
  37. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  38. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  39. args_standalone = args_instance.copy()
  40. standalone.allocate(args_standalone)
  41. instance_standalone = standalone.exists()
  42. if instance_standalone:
  43. standalone.delete()
  44. standalone.create()
  45. standalone.open()
  46. # Clear out the tmp dir
  47. standalone.clearTmpDir(__file__)
  48. return TopologyStandalone(standalone)
  49. def test_attr_uniqueness_init(topology):
  50. '''
  51. Enable dynamic plugins - makes things easier
  52. '''
  53. try:
  54. topology.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-dynamic-plugins', 'on')])
  55. except ldap.LDAPError as e:
  56. ldap.fatal('Failed to enable dynamic plugin!' + e.message['desc'])
  57. assert False
  58. def test_attr_uniqueness(topology):
  59. log.info('Running test_attr_uniqueness...')
  60. #
  61. # Configure plugin
  62. #
  63. try:
  64. topology.standalone.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  65. [(ldap.MOD_REPLACE, 'uniqueness-attribute-name', 'uid')])
  66. except ldap.LDAPError as e:
  67. log.fatal('test_attr_uniqueness: Failed to configure plugin for "uid": error ' + e.message['desc'])
  68. assert False
  69. # Add an entry
  70. try:
  71. topology.standalone.add_s(Entry((USER1_DN, {'objectclass': "top extensibleObject".split(),
  72. 'sn': '1',
  73. 'cn': 'user 1',
  74. 'uid': 'user1',
  75. 'mail': '[email protected]',
  76. 'mailAlternateAddress': '[email protected]',
  77. 'userpassword': 'password'})))
  78. except ldap.LDAPError as e:
  79. log.fatal('test_attr_uniqueness: Failed to add test user' + USER1_DN + ': error ' + e.message['desc'])
  80. assert False
  81. # Add an entry with a duplicate "uid"
  82. try:
  83. topology.standalone.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  84. 'sn': '2',
  85. 'cn': 'user 2',
  86. 'uid': 'user2',
  87. 'uid': 'user1',
  88. 'userpassword': 'password'})))
  89. except ldap.CONSTRAINT_VIOLATION:
  90. pass
  91. else:
  92. log.fatal('test_attr_uniqueness: Adding of 2nd entry(uid) incorrectly succeeded')
  93. assert False
  94. #
  95. # Change config to use "mail" instead of "uid"
  96. #
  97. try:
  98. topology.standalone.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  99. [(ldap.MOD_REPLACE, 'uniqueness-attribute-name', 'mail')])
  100. except ldap.LDAPError as e:
  101. log.fatal('test_attr_uniqueness: Failed to configure plugin for "mail": error ' + e.message['desc'])
  102. assert False
  103. #
  104. # Test plugin - Add an entry, that has a duplicate "mail" value
  105. #
  106. try:
  107. topology.standalone.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  108. 'sn': '2',
  109. 'cn': 'user 2',
  110. 'uid': 'user2',
  111. 'mail': '[email protected]',
  112. 'userpassword': 'password'})))
  113. except ldap.CONSTRAINT_VIOLATION:
  114. pass
  115. else:
  116. log.fatal('test_attr_uniqueness: Adding of 2nd entry(mail) incorrectly succeeded')
  117. assert False
  118. #
  119. # Reconfigure plugin for mail and mailAlternateAddress
  120. #
  121. try:
  122. topology.standalone.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  123. [(ldap.MOD_REPLACE, 'uniqueness-attribute-name', 'mail'),
  124. (ldap.MOD_ADD, 'uniqueness-attribute-name',
  125. 'mailAlternateAddress')])
  126. except ldap.LDAPError as e:
  127. log.error('test_attr_uniqueness: Failed to reconfigure plugin for "mail mailAlternateAddress": error ' +
  128. e.message['desc'])
  129. assert False
  130. #
  131. # Test plugin - Add an entry, that has a duplicate "mail" value
  132. #
  133. try:
  134. topology.standalone.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  135. 'sn': '2',
  136. 'cn': 'user 2',
  137. 'uid': 'user2',
  138. 'mail': '[email protected]',
  139. 'userpassword': 'password'})))
  140. except ldap.CONSTRAINT_VIOLATION:
  141. pass
  142. else:
  143. log.error('test_attr_uniqueness: Adding of 3rd entry(mail) incorrectly succeeded')
  144. assert False
  145. #
  146. # Test plugin - Add an entry, that has a duplicate "mailAlternateAddress" value
  147. #
  148. try:
  149. topology.standalone.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  150. 'sn': '2',
  151. 'cn': 'user 2',
  152. 'uid': 'user2',
  153. 'mailAlternateAddress': '[email protected]',
  154. 'userpassword': 'password'})))
  155. except ldap.CONSTRAINT_VIOLATION:
  156. pass
  157. else:
  158. log.error('test_attr_uniqueness: Adding of 4th entry(mailAlternateAddress) incorrectly succeeded')
  159. assert False
  160. #
  161. # Test plugin - Add an entry, that has a duplicate "mail" value conflicting mailAlternateAddress
  162. #
  163. try:
  164. topology.standalone.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  165. 'sn': '2',
  166. 'cn': 'user 2',
  167. 'uid': 'user2',
  168. 'mail': '[email protected]',
  169. 'userpassword': 'password'})))
  170. except ldap.CONSTRAINT_VIOLATION:
  171. pass
  172. else:
  173. log.error('test_attr_uniqueness: Adding of 5th entry(mailAlternateAddress) incorrectly succeeded')
  174. assert False
  175. #
  176. # Test plugin - Add an entry, that has a duplicate "mailAlternateAddress" conflicting mail
  177. #
  178. try:
  179. topology.standalone.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
  180. 'sn': '2',
  181. 'cn': 'user 2',
  182. 'uid': 'user2',
  183. 'mailAlternateAddress': '[email protected]',
  184. 'userpassword': 'password'})))
  185. except ldap.CONSTRAINT_VIOLATION:
  186. pass
  187. else:
  188. log.error('test_attr_uniqueness: Adding of 6th entry(mail) incorrectly succeeded')
  189. assert False
  190. #
  191. # Cleanup
  192. #
  193. try:
  194. topology.standalone.delete_s(USER1_DN)
  195. except ldap.LDAPError as e:
  196. log.fatal('test_attr_uniqueness: Failed to delete test entry: ' + e.message['desc'])
  197. assert False
  198. log.info('test_attr_uniqueness: PASS\n')
  199. def test_attr_uniqueness_final(topology):
  200. topology.standalone.delete()
  201. log.info('attr_uniqueness test suite PASSED')
  202. def run_isolated():
  203. global installation1_prefix
  204. installation1_prefix = None
  205. topo = topology(True)
  206. test_attr_uniqueness_init(topo)
  207. test_attr_uniqueness(topo)
  208. test_attr_uniqueness_final(topo)
  209. if __name__ == '__main__':
  210. run_isolated()