ticket47431_test.py 8.5 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 time
  11. import ldap
  12. import logging
  13. import pytest
  14. from lib389 import DirSrv
  15. from lib389._constants import *
  16. from lib389.properties import *
  17. from lib389.tasks import *
  18. from lib389.utils import *
  19. logging.getLogger(__name__).setLevel(logging.DEBUG)
  20. log = logging.getLogger(__name__)
  21. installation1_prefix = None
  22. DN_7BITPLUGIN = "cn=7-bit check,%s" % DN_PLUGIN
  23. ATTRS = ["uid", "mail", "userpassword", ",", SUFFIX, 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. def fin():
  47. standalone.delete()
  48. request.addfinalizer(fin)
  49. return TopologyStandalone(standalone)
  50. def test_ticket47431_0(topology):
  51. '''
  52. Enable 7 bit plugin
  53. '''
  54. log.info("Ticket 47431 - 0: Enable 7bit plugin...")
  55. topology.standalone.plugins.enable(name=PLUGIN_7_BIT_CHECK)
  56. def test_ticket47431_1(topology):
  57. '''
  58. nsslapd-pluginarg0: uid
  59. nsslapd-pluginarg1: mail
  60. nsslapd-pluginarg2: userpassword <== repeat 27 times
  61. nsslapd-pluginarg3: ,
  62. nsslapd-pluginarg4: dc=example,dc=com
  63. The duplicated values are removed by str2entry_dupcheck as follows:
  64. [..] - str2entry_dupcheck: 27 duplicate values for attribute type nsslapd-pluginarg2
  65. detected in entry cn=7-bit check,cn=plugins,cn=config. Extra values ignored.
  66. '''
  67. log.info("Ticket 47431 - 1: Check 26 duplicate values are treated as one...")
  68. expected = "str2entry_dupcheck - .* duplicate values for attribute type nsslapd-pluginarg2 detected in entry cn=7-bit check,cn=plugins,cn=config."
  69. log.debug('modify_s %s' % DN_7BITPLUGIN)
  70. try:
  71. topology.standalone.modify_s(DN_7BITPLUGIN,
  72. [(ldap.MOD_REPLACE, 'nsslapd-pluginarg0', "uid"),
  73. (ldap.MOD_REPLACE, 'nsslapd-pluginarg1', "mail"),
  74. (ldap.MOD_REPLACE, 'nsslapd-pluginarg2', "userpassword"),
  75. (ldap.MOD_REPLACE, 'nsslapd-pluginarg3', ","),
  76. (ldap.MOD_REPLACE, 'nsslapd-pluginarg4', SUFFIX)])
  77. except ValueError:
  78. log.error('modify failed: Some problem occured with a value that was provided')
  79. assert False
  80. arg2 = "nsslapd-pluginarg2: userpassword"
  81. topology.standalone.stop(timeout=10)
  82. dse_ldif = topology.standalone.confdir + '/dse.ldif'
  83. os.system('mv %s %s.47431' % (dse_ldif, dse_ldif))
  84. os.system('sed -e "s/\\(%s\\)/\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1\\n\\1/" %s.47431 > %s' % (arg2, dse_ldif, dse_ldif))
  85. topology.standalone.start(timeout=10)
  86. cmdline = 'egrep -i "%s" %s' % (expected, topology.standalone.errlog)
  87. p = os.popen(cmdline, "r")
  88. line = p.readline()
  89. if line == "":
  90. log.error('Expected error "%s" not logged in %s' % (expected, topology.standalone.errlog))
  91. assert False
  92. else:
  93. log.debug('line: %s' % line)
  94. log.info('Expected error "%s" logged in %s' % (expected, topology.standalone.errlog))
  95. log.info("Ticket 47431 - 1: done")
  96. def test_ticket47431_2(topology):
  97. '''
  98. nsslapd-pluginarg0: uid
  99. nsslapd-pluginarg0: mail
  100. nsslapd-pluginarg1: userpassword
  101. nsslapd-pluginarg2: ,
  102. nsslapd-pluginarg3: dc=example,dc=com
  103. ==>
  104. nsslapd-pluginarg0: uid
  105. nsslapd-pluginarg1: mail
  106. nsslapd-pluginarg2: userpassword
  107. nsslapd-pluginarg3: ,
  108. nsslapd-pluginarg4: dc=example,dc=com
  109. Should be logged in error log:
  110. [..] NS7bitAttr_Init - 0: uid
  111. [..] NS7bitAttr_Init - 1: userpassword
  112. [..] NS7bitAttr_Init - 2: mail
  113. [..] NS7bitAttr_Init - 3: ,
  114. [..] NS7bitAttr_Init - 4: dc=example,dc=com
  115. '''
  116. log.info("Ticket 47431 - 2: Check two values belonging to one arg is fixed...")
  117. try:
  118. topology.standalone.modify_s(DN_7BITPLUGIN,
  119. [(ldap.MOD_REPLACE, 'nsslapd-pluginarg0', "uid"),
  120. (ldap.MOD_ADD, 'nsslapd-pluginarg0', "mail"),
  121. (ldap.MOD_REPLACE, 'nsslapd-pluginarg1', "userpassword"),
  122. (ldap.MOD_REPLACE, 'nsslapd-pluginarg2', ","),
  123. (ldap.MOD_REPLACE, 'nsslapd-pluginarg3', SUFFIX),
  124. (ldap.MOD_DELETE, 'nsslapd-pluginarg4', None)])
  125. except ValueError:
  126. log.error('modify failed: Some problem occured with a value that was provided')
  127. assert False
  128. # PLUGIN LOG LEVEL
  129. topology.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-errorlog-level', '65536')])
  130. topology.standalone.restart(timeout=10)
  131. cmdline = 'egrep -i %s %s' % ("NS7bitAttr_Init", topology.standalone.errlog)
  132. p = os.popen(cmdline, "r")
  133. i = 0
  134. while ATTRS[i]:
  135. line = p.readline()
  136. log.debug('line - %s' % line)
  137. log.debug('ATTRS[%d] %s' % (i, ATTRS[i]))
  138. if line == "":
  139. break
  140. elif line.find(ATTRS[i]) >= 0:
  141. log.debug('%s was logged' % ATTRS[i])
  142. else:
  143. log.error('%s was not logged.' % ATTRS[i])
  144. assert False
  145. i = i + 1
  146. log.info("Ticket 47431 - 2: done")
  147. def test_ticket47431_3(topology):
  148. '''
  149. nsslapd-pluginarg1: uid
  150. nsslapd-pluginarg3: mail
  151. nsslapd-pluginarg5: userpassword
  152. nsslapd-pluginarg7: ,
  153. nsslapd-pluginarg9: dc=example,dc=com
  154. ==>
  155. nsslapd-pluginarg0: uid
  156. nsslapd-pluginarg1: mail
  157. nsslapd-pluginarg2: userpassword
  158. nsslapd-pluginarg3: ,
  159. nsslapd-pluginarg4: dc=example,dc=com
  160. Should be logged in error log:
  161. [..] NS7bitAttr_Init - 0: uid
  162. [..] NS7bitAttr_Init - 1: userpassword
  163. [..] NS7bitAttr_Init - 2: mail
  164. [..] NS7bitAttr_Init - 3: ,
  165. [..] NS7bitAttr_Init - 4: dc=example,dc=com
  166. '''
  167. log.info("Ticket 47431 - 3: Check missing args are fixed...")
  168. try:
  169. topology.standalone.modify_s(DN_7BITPLUGIN,
  170. [(ldap.MOD_DELETE, 'nsslapd-pluginarg0', None),
  171. (ldap.MOD_REPLACE, 'nsslapd-pluginarg1', "uid"),
  172. (ldap.MOD_DELETE, 'nsslapd-pluginarg2', None),
  173. (ldap.MOD_REPLACE, 'nsslapd-pluginarg3', "mail"),
  174. (ldap.MOD_REPLACE, 'nsslapd-pluginarg5', "userpassword"),
  175. (ldap.MOD_REPLACE, 'nsslapd-pluginarg7', ","),
  176. (ldap.MOD_REPLACE, 'nsslapd-pluginarg9', SUFFIX)])
  177. except ValueError:
  178. log.error('modify failed: Some problem occured with a value that was provided')
  179. assert False
  180. # PLUGIN LOG LEVEL
  181. topology.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-errorlog-level', '65536')])
  182. topology.standalone.stop(timeout=10)
  183. os.system('mv %s %s.47431' % (topology.standalone.errlog, topology.standalone.errlog))
  184. os.system('touch %s' % (topology.standalone.errlog))
  185. topology.standalone.start(timeout=10)
  186. cmdline = 'egrep -i %s %s' % ("NS7bitAttr_Init", topology.standalone.errlog)
  187. p = os.popen(cmdline, "r")
  188. i = 0
  189. while ATTRS[i]:
  190. line = p.readline()
  191. if line == "":
  192. break
  193. elif line.find(ATTRS[i]) >= 0:
  194. log.debug('%s was logged' % ATTRS[i])
  195. else:
  196. log.error('%s was not logged.' % ATTRS[i])
  197. assert False
  198. i = i + 1
  199. log.info("Ticket 47431 - 3: done")
  200. log.info('Test complete')
  201. if __name__ == '__main__':
  202. # Run isolated
  203. # -s for DEBUG mode
  204. CURRENT_FILE = os.path.realpath(__file__)
  205. pytest.main("-s %s" % CURRENT_FILE)