1
0

ticket47927_test.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. EXCLUDED_CONTAINER_CN = "excluded_container"
  25. EXCLUDED_CONTAINER_DN = "cn=%s,%s" % (EXCLUDED_CONTAINER_CN, SUFFIX)
  26. EXCLUDED_BIS_CONTAINER_CN = "excluded_bis_container"
  27. EXCLUDED_BIS_CONTAINER_DN = "cn=%s,%s" % (EXCLUDED_BIS_CONTAINER_CN, SUFFIX)
  28. ENFORCED_CONTAINER_CN = "enforced_container"
  29. ENFORCED_CONTAINER_DN = "cn=%s,%s" % (ENFORCED_CONTAINER_CN, SUFFIX)
  30. USER_1_CN = "test_1"
  31. USER_1_DN = "cn=%s,%s" % (USER_1_CN, ENFORCED_CONTAINER_DN)
  32. USER_2_CN = "test_2"
  33. USER_2_DN = "cn=%s,%s" % (USER_2_CN, ENFORCED_CONTAINER_DN)
  34. USER_3_CN = "test_3"
  35. USER_3_DN = "cn=%s,%s" % (USER_3_CN, EXCLUDED_CONTAINER_DN)
  36. USER_4_CN = "test_4"
  37. USER_4_DN = "cn=%s,%s" % (USER_4_CN, EXCLUDED_BIS_CONTAINER_DN)
  38. class TopologyStandalone(object):
  39. def __init__(self, standalone):
  40. standalone.open()
  41. self.standalone = standalone
  42. @pytest.fixture(scope="module")
  43. def topology(request):
  44. global installation1_prefix
  45. # Creating standalone instance ...
  46. standalone = DirSrv(verbose=False)
  47. if installation1_prefix:
  48. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  49. args_instance[SER_HOST] = HOST_STANDALONE
  50. args_instance[SER_PORT] = PORT_STANDALONE
  51. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  52. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  53. args_standalone = args_instance.copy()
  54. standalone.allocate(args_standalone)
  55. instance_standalone = standalone.exists()
  56. if instance_standalone:
  57. standalone.delete()
  58. standalone.create()
  59. standalone.open()
  60. def fin():
  61. standalone.delete()
  62. request.addfinalizer(fin)
  63. return TopologyStandalone(standalone)
  64. def test_ticket47927_init(topology):
  65. topology.standalone.plugins.enable(name=PLUGIN_ATTR_UNIQUENESS)
  66. try:
  67. topology.standalone.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  68. [(ldap.MOD_REPLACE, 'uniqueness-attribute-name', 'telephonenumber'),
  69. (ldap.MOD_REPLACE, 'uniqueness-subtrees', DEFAULT_SUFFIX),
  70. ])
  71. except ldap.LDAPError as e:
  72. log.fatal('test_ticket47927: Failed to configure plugin for "telephonenumber": error ' + e.message['desc'])
  73. assert False
  74. topology.standalone.restart(timeout=120)
  75. topology.standalone.add_s(Entry((EXCLUDED_CONTAINER_DN, {'objectclass': "top nscontainer".split(),
  76. 'cn': EXCLUDED_CONTAINER_CN})))
  77. topology.standalone.add_s(Entry((EXCLUDED_BIS_CONTAINER_DN, {'objectclass': "top nscontainer".split(),
  78. 'cn': EXCLUDED_BIS_CONTAINER_CN})))
  79. topology.standalone.add_s(Entry((ENFORCED_CONTAINER_DN, {'objectclass': "top nscontainer".split(),
  80. 'cn': ENFORCED_CONTAINER_CN})))
  81. # adding an entry on a stage with a different 'cn'
  82. topology.standalone.add_s(Entry((USER_1_DN, {
  83. 'objectclass': "top person".split(),
  84. 'sn': USER_1_CN,
  85. 'cn': USER_1_CN})))
  86. # adding an entry on a stage with a different 'cn'
  87. topology.standalone.add_s(Entry((USER_2_DN, {
  88. 'objectclass': "top person".split(),
  89. 'sn': USER_2_CN,
  90. 'cn': USER_2_CN})))
  91. topology.standalone.add_s(Entry((USER_3_DN, {
  92. 'objectclass': "top person".split(),
  93. 'sn': USER_3_CN,
  94. 'cn': USER_3_CN})))
  95. topology.standalone.add_s(Entry((USER_4_DN, {
  96. 'objectclass': "top person".split(),
  97. 'sn': USER_4_CN,
  98. 'cn': USER_4_CN})))
  99. def test_ticket47927_one(topology):
  100. '''
  101. Check that uniqueness is enforce on all SUFFIX
  102. '''
  103. UNIQUE_VALUE='1234'
  104. try:
  105. topology.standalone.modify_s(USER_1_DN,
  106. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  107. except ldap.LDAPError as e:
  108. log.fatal('test_ticket47927_one: Failed to set the telephonenumber for %s: %s' % (USER_1_DN, e.message['desc']))
  109. assert False
  110. # we expect to fail because user1 is in the scope of the plugin
  111. try:
  112. topology.standalone.modify_s(USER_2_DN,
  113. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  114. log.fatal('test_ticket47927_one: unexpected success to set the telephonenumber for %s' % (USER_2_DN))
  115. assert False
  116. except ldap.LDAPError as e:
  117. log.fatal('test_ticket47927_one: Failed (expected) to set the telephonenumber for %s: %s' % (USER_2_DN, e.message['desc']))
  118. pass
  119. # we expect to fail because user1 is in the scope of the plugin
  120. try:
  121. topology.standalone.modify_s(USER_3_DN,
  122. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  123. log.fatal('test_ticket47927_one: unexpected success to set the telephonenumber for %s' % (USER_3_DN))
  124. assert False
  125. except ldap.LDAPError as e:
  126. log.fatal('test_ticket47927_one: Failed (expected) to set the telephonenumber for %s: %s' % (USER_3_DN, e.message['desc']))
  127. pass
  128. def test_ticket47927_two(topology):
  129. '''
  130. Exclude the EXCLUDED_CONTAINER_DN from the uniqueness plugin
  131. '''
  132. try:
  133. topology.standalone.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  134. [(ldap.MOD_REPLACE, 'uniqueness-exclude-subtrees', EXCLUDED_CONTAINER_DN)])
  135. except ldap.LDAPError as e:
  136. log.fatal('test_ticket47927_two: Failed to configure plugin for to exclude %s: error %s' % (EXCLUDED_CONTAINER_DN, e.message['desc']))
  137. assert False
  138. topology.standalone.restart(timeout=120)
  139. def test_ticket47927_three(topology):
  140. '''
  141. Check that uniqueness is enforced on full SUFFIX except EXCLUDED_CONTAINER_DN
  142. First case: it exists an entry (with the same attribute value) in the scope
  143. of the plugin and we set the value in an entry that is in an excluded scope
  144. '''
  145. UNIQUE_VALUE='9876'
  146. try:
  147. topology.standalone.modify_s(USER_1_DN,
  148. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  149. except ldap.LDAPError as e:
  150. log.fatal('test_ticket47927_three: Failed to set the telephonenumber ' + e.message['desc'])
  151. assert False
  152. # we should not be allowed to set this value (because user1 is in the scope)
  153. try:
  154. topology.standalone.modify_s(USER_2_DN,
  155. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  156. log.fatal('test_ticket47927_three: unexpected success to set the telephonenumber for %s' % (USER_2_DN))
  157. assert False
  158. except ldap.LDAPError as e:
  159. log.fatal('test_ticket47927_three: Failed (expected) to set the telephonenumber for %s: %s' % (USER_2_DN , e.message['desc']))
  160. # USER_3_DN is in EXCLUDED_CONTAINER_DN so update should be successful
  161. try:
  162. topology.standalone.modify_s(USER_3_DN,
  163. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  164. log.fatal('test_ticket47927_three: success to set the telephonenumber for %s' % (USER_3_DN))
  165. except ldap.LDAPError as e:
  166. log.fatal('test_ticket47927_three: Failed (unexpected) to set the telephonenumber for %s: %s' % (USER_3_DN, e.message['desc']))
  167. assert False
  168. def test_ticket47927_four(topology):
  169. '''
  170. Check that uniqueness is enforced on full SUFFIX except EXCLUDED_CONTAINER_DN
  171. Second case: it exists an entry (with the same attribute value) in an excluded scope
  172. of the plugin and we set the value in an entry is in the scope
  173. '''
  174. UNIQUE_VALUE='1111'
  175. # USER_3_DN is in EXCLUDED_CONTAINER_DN so update should be successful
  176. try:
  177. topology.standalone.modify_s(USER_3_DN,
  178. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  179. log.fatal('test_ticket47927_four: success to set the telephonenumber for %s' % USER_3_DN)
  180. except ldap.LDAPError as e:
  181. log.fatal('test_ticket47927_four: Failed (unexpected) to set the telephonenumber for %s: %s' % (USER_3_DN, e.message['desc']))
  182. assert False
  183. # we should be allowed to set this value (because user3 is excluded from scope)
  184. try:
  185. topology.standalone.modify_s(USER_1_DN,
  186. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  187. except ldap.LDAPError as e:
  188. log.fatal('test_ticket47927_four: Failed to set the telephonenumber for %s: %s' % (USER_1_DN, e.message['desc']))
  189. assert False
  190. # we should not be allowed to set this value (because user1 is in the scope)
  191. try:
  192. topology.standalone.modify_s(USER_2_DN,
  193. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  194. log.fatal('test_ticket47927_four: unexpected success to set the telephonenumber %s' % USER_2_DN)
  195. assert False
  196. except ldap.LDAPError as e:
  197. log.fatal('test_ticket47927_four: Failed (expected) to set the telephonenumber for %s: %s' % (USER_2_DN, e.message['desc']))
  198. pass
  199. def test_ticket47927_five(topology):
  200. '''
  201. Exclude the EXCLUDED_BIS_CONTAINER_DN from the uniqueness plugin
  202. '''
  203. try:
  204. topology.standalone.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  205. [(ldap.MOD_ADD, 'uniqueness-exclude-subtrees', EXCLUDED_BIS_CONTAINER_DN)])
  206. except ldap.LDAPError as e:
  207. log.fatal('test_ticket47927_five: Failed to configure plugin for to exclude %s: error %s' % (EXCLUDED_BIS_CONTAINER_DN, e.message['desc']))
  208. assert False
  209. topology.standalone.restart(timeout=120)
  210. topology.standalone.getEntry('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config', ldap.SCOPE_BASE)
  211. def test_ticket47927_six(topology):
  212. '''
  213. Check that uniqueness is enforced on full SUFFIX except EXCLUDED_CONTAINER_DN
  214. and EXCLUDED_BIS_CONTAINER_DN
  215. First case: it exists an entry (with the same attribute value) in the scope
  216. of the plugin and we set the value in an entry that is in an excluded scope
  217. '''
  218. UNIQUE_VALUE = '222'
  219. try:
  220. topology.standalone.modify_s(USER_1_DN,
  221. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  222. except ldap.LDAPError as e:
  223. log.fatal('test_ticket47927_six: Failed to set the telephonenumber ' + e.message['desc'])
  224. assert False
  225. # we should not be allowed to set this value (because user1 is in the scope)
  226. try:
  227. topology.standalone.modify_s(USER_2_DN,
  228. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  229. log.fatal('test_ticket47927_six: unexpected success to set the telephonenumber for %s' % (USER_2_DN))
  230. assert False
  231. except ldap.LDAPError as e:
  232. log.fatal('test_ticket47927_six: Failed (expected) to set the telephonenumber for %s: %s' % (USER_2_DN , e.message['desc']))
  233. # USER_3_DN is in EXCLUDED_CONTAINER_DN so update should be successful
  234. try:
  235. topology.standalone.modify_s(USER_3_DN,
  236. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  237. log.fatal('test_ticket47927_six: success to set the telephonenumber for %s' % (USER_3_DN))
  238. except ldap.LDAPError as e:
  239. log.fatal('test_ticket47927_six: Failed (unexpected) to set the telephonenumber for %s: %s' % (USER_3_DN, e.message['desc']))
  240. assert False
  241. # USER_4_DN is in EXCLUDED_CONTAINER_DN so update should be successful
  242. try:
  243. topology.standalone.modify_s(USER_4_DN,
  244. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  245. log.fatal('test_ticket47927_six: success to set the telephonenumber for %s' % (USER_4_DN))
  246. except ldap.LDAPError as e:
  247. log.fatal('test_ticket47927_six: Failed (unexpected) to set the telephonenumber for %s: %s' % (USER_4_DN, e.message['desc']))
  248. assert False
  249. def test_ticket47927_final(topology):
  250. log.info('Testcase PASSED')
  251. def run_isolated():
  252. global installation1_prefix
  253. installation1_prefix = None
  254. topo = topology(True)
  255. test_ticket47927_init(topo)
  256. test_ticket47927_one(topo)
  257. test_ticket47927_two(topo)
  258. test_ticket47927_three(topo)
  259. test_ticket47927_four(topo)
  260. test_ticket47927_five(topo)
  261. test_ticket47927_six(topo)
  262. test_ticket47927_final(topo)
  263. if __name__ == '__main__':
  264. run_isolated()