ticket47927_test.py 13 KB

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