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. # Clear out the tmp dir
  61. standalone.clearTmpDir(__file__)
  62. return TopologyStandalone(standalone)
  63. def test_ticket47927_init(topology):
  64. topology.standalone.plugins.enable(name=PLUGIN_ATTR_UNIQUENESS)
  65. try:
  66. topology.standalone.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  67. [(ldap.MOD_REPLACE, 'uniqueness-attribute-name', 'telephonenumber'),
  68. (ldap.MOD_REPLACE, 'uniqueness-subtrees', DEFAULT_SUFFIX),
  69. ])
  70. except ldap.LDAPError as e:
  71. log.fatal('test_ticket47927: Failed to configure plugin for "telephonenumber": error ' + e.message['desc'])
  72. assert False
  73. topology.standalone.restart(timeout=120)
  74. topology.standalone.add_s(Entry((EXCLUDED_CONTAINER_DN, {'objectclass': "top nscontainer".split(),
  75. 'cn': EXCLUDED_CONTAINER_CN})))
  76. topology.standalone.add_s(Entry((EXCLUDED_BIS_CONTAINER_DN, {'objectclass': "top nscontainer".split(),
  77. 'cn': EXCLUDED_BIS_CONTAINER_CN})))
  78. topology.standalone.add_s(Entry((ENFORCED_CONTAINER_DN, {'objectclass': "top nscontainer".split(),
  79. 'cn': ENFORCED_CONTAINER_CN})))
  80. # adding an entry on a stage with a different 'cn'
  81. topology.standalone.add_s(Entry((USER_1_DN, {
  82. 'objectclass': "top person".split(),
  83. 'sn': USER_1_CN,
  84. 'cn': USER_1_CN})))
  85. # adding an entry on a stage with a different 'cn'
  86. topology.standalone.add_s(Entry((USER_2_DN, {
  87. 'objectclass': "top person".split(),
  88. 'sn': USER_2_CN,
  89. 'cn': USER_2_CN})))
  90. topology.standalone.add_s(Entry((USER_3_DN, {
  91. 'objectclass': "top person".split(),
  92. 'sn': USER_3_CN,
  93. 'cn': USER_3_CN})))
  94. topology.standalone.add_s(Entry((USER_4_DN, {
  95. 'objectclass': "top person".split(),
  96. 'sn': USER_4_CN,
  97. 'cn': USER_4_CN})))
  98. def test_ticket47927_one(topology):
  99. '''
  100. Check that uniqueness is enforce on all SUFFIX
  101. '''
  102. UNIQUE_VALUE='1234'
  103. try:
  104. topology.standalone.modify_s(USER_1_DN,
  105. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  106. except ldap.LDAPError as e:
  107. log.fatal('test_ticket47927_one: Failed to set the telephonenumber for %s: %s' % (USER_1_DN, e.message['desc']))
  108. assert False
  109. # we expect to fail because user1 is in the scope of the plugin
  110. try:
  111. topology.standalone.modify_s(USER_2_DN,
  112. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  113. log.fatal('test_ticket47927_one: unexpected success to set the telephonenumber for %s' % (USER_2_DN))
  114. assert False
  115. except ldap.LDAPError as e:
  116. log.fatal('test_ticket47927_one: Failed (expected) to set the telephonenumber for %s: %s' % (USER_2_DN, e.message['desc']))
  117. pass
  118. # we expect to fail because user1 is in the scope of the plugin
  119. try:
  120. topology.standalone.modify_s(USER_3_DN,
  121. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  122. log.fatal('test_ticket47927_one: unexpected success to set the telephonenumber for %s' % (USER_3_DN))
  123. assert False
  124. except ldap.LDAPError as e:
  125. log.fatal('test_ticket47927_one: Failed (expected) to set the telephonenumber for %s: %s' % (USER_3_DN, e.message['desc']))
  126. pass
  127. def test_ticket47927_two(topology):
  128. '''
  129. Exclude the EXCLUDED_CONTAINER_DN from the uniqueness plugin
  130. '''
  131. try:
  132. topology.standalone.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  133. [(ldap.MOD_REPLACE, 'uniqueness-exclude-subtrees', EXCLUDED_CONTAINER_DN)])
  134. except ldap.LDAPError as e:
  135. log.fatal('test_ticket47927_two: Failed to configure plugin for to exclude %s: error %s' % (EXCLUDED_CONTAINER_DN, e.message['desc']))
  136. assert False
  137. topology.standalone.restart(timeout=120)
  138. def test_ticket47927_three(topology):
  139. '''
  140. Check that uniqueness is enforced on full SUFFIX except EXCLUDED_CONTAINER_DN
  141. First case: it exists an entry (with the same attribute value) in the scope
  142. of the plugin and we set the value in an entry that is in an excluded scope
  143. '''
  144. UNIQUE_VALUE='9876'
  145. try:
  146. topology.standalone.modify_s(USER_1_DN,
  147. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  148. except ldap.LDAPError as e:
  149. log.fatal('test_ticket47927_three: Failed to set the telephonenumber ' + e.message['desc'])
  150. assert False
  151. # we should not be allowed to set this value (because user1 is in the scope)
  152. try:
  153. topology.standalone.modify_s(USER_2_DN,
  154. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  155. log.fatal('test_ticket47927_three: unexpected success to set the telephonenumber for %s' % (USER_2_DN))
  156. assert False
  157. except ldap.LDAPError as e:
  158. log.fatal('test_ticket47927_three: Failed (expected) to set the telephonenumber for %s: %s' % (USER_2_DN , e.message['desc']))
  159. # USER_3_DN is in EXCLUDED_CONTAINER_DN so update should be successful
  160. try:
  161. topology.standalone.modify_s(USER_3_DN,
  162. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  163. log.fatal('test_ticket47927_three: success to set the telephonenumber for %s' % (USER_3_DN))
  164. except ldap.LDAPError as e:
  165. log.fatal('test_ticket47927_three: Failed (unexpected) to set the telephonenumber for %s: %s' % (USER_3_DN, e.message['desc']))
  166. assert False
  167. def test_ticket47927_four(topology):
  168. '''
  169. Check that uniqueness is enforced on full SUFFIX except EXCLUDED_CONTAINER_DN
  170. Second case: it exists an entry (with the same attribute value) in an excluded scope
  171. of the plugin and we set the value in an entry is in the scope
  172. '''
  173. UNIQUE_VALUE='1111'
  174. # USER_3_DN is in EXCLUDED_CONTAINER_DN so update should be successful
  175. try:
  176. topology.standalone.modify_s(USER_3_DN,
  177. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  178. log.fatal('test_ticket47927_four: success to set the telephonenumber for %s' % USER_3_DN)
  179. except ldap.LDAPError as e:
  180. log.fatal('test_ticket47927_four: Failed (unexpected) to set the telephonenumber for %s: %s' % (USER_3_DN, e.message['desc']))
  181. assert False
  182. # we should be allowed to set this value (because user3 is excluded from scope)
  183. try:
  184. topology.standalone.modify_s(USER_1_DN,
  185. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  186. except ldap.LDAPError as e:
  187. log.fatal('test_ticket47927_four: Failed to set the telephonenumber for %s: %s' % (USER_1_DN, e.message['desc']))
  188. assert False
  189. # we should not be allowed to set this value (because user1 is in the scope)
  190. try:
  191. topology.standalone.modify_s(USER_2_DN,
  192. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  193. log.fatal('test_ticket47927_four: unexpected success to set the telephonenumber %s' % USER_2_DN)
  194. assert False
  195. except ldap.LDAPError as e:
  196. log.fatal('test_ticket47927_four: Failed (expected) to set the telephonenumber for %s: %s' % (USER_2_DN, e.message['desc']))
  197. pass
  198. def test_ticket47927_five(topology):
  199. '''
  200. Exclude the EXCLUDED_BIS_CONTAINER_DN from the uniqueness plugin
  201. '''
  202. try:
  203. topology.standalone.modify_s('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config',
  204. [(ldap.MOD_ADD, 'uniqueness-exclude-subtrees', EXCLUDED_BIS_CONTAINER_DN)])
  205. except ldap.LDAPError as e:
  206. log.fatal('test_ticket47927_five: Failed to configure plugin for to exclude %s: error %s' % (EXCLUDED_BIS_CONTAINER_DN, e.message['desc']))
  207. assert False
  208. topology.standalone.restart(timeout=120)
  209. topology.standalone.getEntry('cn=' + PLUGIN_ATTR_UNIQUENESS + ',cn=plugins,cn=config', ldap.SCOPE_BASE)
  210. def test_ticket47927_six(topology):
  211. '''
  212. Check that uniqueness is enforced on full SUFFIX except EXCLUDED_CONTAINER_DN
  213. and EXCLUDED_BIS_CONTAINER_DN
  214. First case: it exists an entry (with the same attribute value) in the scope
  215. of the plugin and we set the value in an entry that is in an excluded scope
  216. '''
  217. UNIQUE_VALUE = '222'
  218. try:
  219. topology.standalone.modify_s(USER_1_DN,
  220. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  221. except ldap.LDAPError as e:
  222. log.fatal('test_ticket47927_six: Failed to set the telephonenumber ' + e.message['desc'])
  223. assert False
  224. # we should not be allowed to set this value (because user1 is in the scope)
  225. try:
  226. topology.standalone.modify_s(USER_2_DN,
  227. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  228. log.fatal('test_ticket47927_six: unexpected success to set the telephonenumber for %s' % (USER_2_DN))
  229. assert False
  230. except ldap.LDAPError as e:
  231. log.fatal('test_ticket47927_six: Failed (expected) to set the telephonenumber for %s: %s' % (USER_2_DN , e.message['desc']))
  232. # USER_3_DN is in EXCLUDED_CONTAINER_DN so update should be successful
  233. try:
  234. topology.standalone.modify_s(USER_3_DN,
  235. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  236. log.fatal('test_ticket47927_six: success to set the telephonenumber for %s' % (USER_3_DN))
  237. except ldap.LDAPError as e:
  238. log.fatal('test_ticket47927_six: Failed (unexpected) to set the telephonenumber for %s: %s' % (USER_3_DN, e.message['desc']))
  239. assert False
  240. # USER_4_DN is in EXCLUDED_CONTAINER_DN so update should be successful
  241. try:
  242. topology.standalone.modify_s(USER_4_DN,
  243. [(ldap.MOD_REPLACE, 'telephonenumber', UNIQUE_VALUE)])
  244. log.fatal('test_ticket47927_six: success to set the telephonenumber for %s' % (USER_4_DN))
  245. except ldap.LDAPError as e:
  246. log.fatal('test_ticket47927_six: Failed (unexpected) to set the telephonenumber for %s: %s' % (USER_4_DN, e.message['desc']))
  247. assert False
  248. def test_ticket47927_final(topology):
  249. topology.standalone.delete()
  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()