attr_uniqueness_test.py 8.5 KB

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