ticket48109_test.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. UID_INDEX = 'cn=uid,cn=index,cn=userRoot,cn=ldbm database,cn=plugins,cn=config'
  25. class TopologyStandalone(object):
  26. def __init__(self, standalone):
  27. standalone.open()
  28. self.standalone = standalone
  29. @pytest.fixture(scope="module")
  30. def topology(request):
  31. global installation1_prefix
  32. if installation1_prefix:
  33. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  34. # Creating standalone instance ...
  35. standalone = DirSrv(verbose=False)
  36. args_instance[SER_HOST] = HOST_STANDALONE
  37. args_instance[SER_PORT] = PORT_STANDALONE
  38. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  39. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  40. args_standalone = args_instance.copy()
  41. standalone.allocate(args_standalone)
  42. instance_standalone = standalone.exists()
  43. if instance_standalone:
  44. standalone.delete()
  45. standalone.create()
  46. standalone.open()
  47. # Clear out the tmp dir
  48. standalone.clearTmpDir(__file__)
  49. return TopologyStandalone(standalone)
  50. def test_ticket48109_0(topology):
  51. '''
  52. Set SubStr lengths to cn=uid,cn=index,...
  53. objectClass: extensibleObject
  54. nsIndexType: sub
  55. nsSubStrBegin: 2
  56. nsSubStrEnd: 2
  57. '''
  58. log.info('Test case 0')
  59. # add substr setting to UID_INDEX
  60. try:
  61. topology.standalone.modify_s(UID_INDEX,
  62. [(ldap.MOD_ADD, 'objectClass', 'extensibleObject'),
  63. (ldap.MOD_ADD, 'nsIndexType', 'sub'),
  64. (ldap.MOD_ADD, 'nsSubStrBegin', '2'),
  65. (ldap.MOD_ADD, 'nsSubStrEnd', '2')])
  66. except ldap.LDAPError as e:
  67. log.error('Failed to add substr lengths: error ' + e.message['desc'])
  68. assert False
  69. # restart the server to apply the indexing
  70. topology.standalone.restart(timeout=10)
  71. # add a test user
  72. UID = 'auser0'
  73. USER_DN = 'uid=%s,%s' % (UID, SUFFIX)
  74. try:
  75. topology.standalone.add_s(Entry((USER_DN, {
  76. 'objectclass': 'top person organizationalPerson inetOrgPerson'.split(),
  77. 'cn': 'a user0',
  78. 'sn': 'user0',
  79. 'givenname': 'a',
  80. 'mail': UID})))
  81. except ldap.LDAPError as e:
  82. log.error('Failed to add ' + USER_DN + ': error ' + e.message['desc'])
  83. assert False
  84. entries = topology.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, '(uid=a*)')
  85. assert len(entries) == 1
  86. # restart the server to check the access log
  87. topology.standalone.restart(timeout=10)
  88. cmdline = 'egrep %s %s | egrep "uid=a\*"' % (SUFFIX, topology.standalone.accesslog)
  89. p = os.popen(cmdline, "r")
  90. l0 = p.readline()
  91. if l0 == "":
  92. log.error('Search with "(uid=a*)" is not logged in ' + topology.standalone.accesslog)
  93. assert False
  94. else:
  95. #regex = re.compile('\(conn=[0-9]* op=[0-9]*\) SRCH .*')
  96. regex = re.compile(r'.*\s+(conn=\d+ op=\d+)\s+SRCH .*')
  97. match = regex.match(l0)
  98. log.info('match: %s' % match.group(1))
  99. cmdline = 'egrep "%s" %s | egrep "RESULT"' % (match.group(1), topology.standalone.accesslog)
  100. p = os.popen(cmdline, "r")
  101. l1 = p.readline()
  102. if l1 == "":
  103. log.error('Search result of "(uid=a*)" is not logged in ' + topology.standalone.accesslog)
  104. assert False
  105. else:
  106. log.info('l1: %s' % l1)
  107. regex = re.compile(r'.*nentries=(\d+)\s+.*')
  108. match = regex.match(l1)
  109. log.info('match: nentires=%s' % match.group(1))
  110. if match.group(1) == "0":
  111. log.error('Entry uid=a* not found.')
  112. assert False
  113. else:
  114. log.info('Entry uid=a* found.')
  115. regex = re.compile(r'.*(notes=[AU]).*')
  116. match = regex.match(l1)
  117. if match:
  118. log.error('%s - substr index was not used' % match.group(1))
  119. assert False
  120. else:
  121. log.info('Test case 0 - OK - substr index used')
  122. # clean up substr setting to UID_INDEX
  123. try:
  124. topology.standalone.modify_s(UID_INDEX,
  125. [(ldap.MOD_DELETE, 'objectClass', 'extensibleObject'),
  126. (ldap.MOD_DELETE, 'nsIndexType', 'sub'),
  127. (ldap.MOD_DELETE, 'nsSubStrBegin', '2'),
  128. (ldap.MOD_DELETE, 'nsSubStrEnd', '2')])
  129. except ldap.LDAPError as e:
  130. log.error('Failed to delete substr lengths: error ' + e.message['desc'])
  131. assert False
  132. def test_ticket48109_1(topology):
  133. '''
  134. Set SubStr lengths to cn=uid,cn=index,...
  135. nsIndexType: sub
  136. nsMatchingRule: nsSubStrBegin=2
  137. nsMatchingRule: nsSubStrEnd=2
  138. '''
  139. log.info('Test case 1')
  140. # add substr setting to UID_INDEX
  141. try:
  142. topology.standalone.modify_s(UID_INDEX,
  143. [(ldap.MOD_ADD, 'nsIndexType', 'sub'),
  144. (ldap.MOD_ADD, 'nsMatchingRule', 'nssubstrbegin=2'),
  145. (ldap.MOD_ADD, 'nsMatchingRule', 'nssubstrend=2')])
  146. except ldap.LDAPError as e:
  147. log.error('Failed to add substr lengths: error ' + e.message['desc'])
  148. assert False
  149. # restart the server to apply the indexing
  150. topology.standalone.restart(timeout=10)
  151. # add a test user
  152. UID = 'buser1'
  153. USER_DN = 'uid=%s,%s' % (UID, SUFFIX)
  154. try:
  155. topology.standalone.add_s(Entry((USER_DN, {
  156. 'objectclass': 'top person organizationalPerson inetOrgPerson'.split(),
  157. 'cn': 'b user1',
  158. 'sn': 'user1',
  159. 'givenname': 'b',
  160. 'mail': UID})))
  161. except ldap.LDAPError as e:
  162. log.error('Failed to add ' + USER_DN + ': error ' + e.message['desc'])
  163. assert False
  164. entries = topology.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, '(uid=b*)')
  165. assert len(entries) == 1
  166. # restart the server to check the access log
  167. topology.standalone.restart(timeout=10)
  168. cmdline = 'egrep %s %s | egrep "uid=b\*"' % (SUFFIX, topology.standalone.accesslog)
  169. p = os.popen(cmdline, "r")
  170. l0 = p.readline()
  171. if l0 == "":
  172. log.error('Search with "(uid=b*)" is not logged in ' + topology.standalone.accesslog)
  173. assert False
  174. else:
  175. #regex = re.compile('\(conn=[0-9]* op=[0-9]*\) SRCH .*')
  176. regex = re.compile(r'.*\s+(conn=\d+ op=\d+)\s+SRCH .*')
  177. match = regex.match(l0)
  178. log.info('match: %s' % match.group(1))
  179. cmdline = 'egrep "%s" %s | egrep "RESULT"' % (match.group(1), topology.standalone.accesslog)
  180. p = os.popen(cmdline, "r")
  181. l1 = p.readline()
  182. if l1 == "":
  183. log.error('Search result of "(uid=*b)" is not logged in ' + topology.standalone.accesslog)
  184. assert False
  185. else:
  186. log.info('l1: %s' % l1)
  187. regex = re.compile(r'.*nentries=(\d+)\s+.*')
  188. match = regex.match(l1)
  189. log.info('match: nentires=%s' % match.group(1))
  190. if match.group(1) == "0":
  191. log.error('Entry uid=*b not found.')
  192. assert False
  193. else:
  194. log.info('Entry uid=*b found.')
  195. regex = re.compile(r'.*(notes=[AU]).*')
  196. match = regex.match(l1)
  197. if match:
  198. log.error('%s - substr index was not used' % match.group(1))
  199. assert False
  200. else:
  201. log.info('Test case 1 - OK - substr index used')
  202. # clean up substr setting to UID_INDEX
  203. try:
  204. topology.standalone.modify_s(UID_INDEX,
  205. [(ldap.MOD_DELETE, 'nsIndexType', 'sub'),
  206. (ldap.MOD_DELETE, 'nsMatchingRule', 'nssubstrbegin=2'),
  207. (ldap.MOD_DELETE, 'nsMatchingRule', 'nssubstrend=2')])
  208. except ldap.LDAPError as e:
  209. log.error('Failed to delete substr lengths: error ' + e.message['desc'])
  210. assert False
  211. def test_ticket48109_2(topology):
  212. '''
  213. Set SubStr conflict formats/lengths to cn=uid,cn=index,...
  214. objectClass: extensibleObject
  215. nsIndexType: sub
  216. nsMatchingRule: nsSubStrBegin=3
  217. nsMatchingRule: nsSubStrEnd=3
  218. nsSubStrBegin: 2
  219. nsSubStrEnd: 2
  220. nsSubStr{Begin,End} are honored.
  221. '''
  222. log.info('Test case 2')
  223. # add substr setting to UID_INDEX
  224. try:
  225. topology.standalone.modify_s(UID_INDEX,
  226. [(ldap.MOD_ADD, 'nsIndexType', 'sub'),
  227. (ldap.MOD_ADD, 'nsMatchingRule', 'nssubstrbegin=3'),
  228. (ldap.MOD_ADD, 'nsMatchingRule', 'nssubstrend=3'),
  229. (ldap.MOD_ADD, 'objectClass', 'extensibleObject'),
  230. (ldap.MOD_ADD, 'nsSubStrBegin', '2'),
  231. (ldap.MOD_ADD, 'nsSubStrEnd', '2')])
  232. except ldap.LDAPError as e:
  233. log.error('Failed to add substr lengths: error ' + e.message['desc'])
  234. assert False
  235. # restart the server to apply the indexing
  236. topology.standalone.restart(timeout=10)
  237. # add a test user
  238. UID = 'cuser2'
  239. USER_DN = 'uid=%s,%s' % (UID, SUFFIX)
  240. try:
  241. topology.standalone.add_s(Entry((USER_DN, {
  242. 'objectclass': 'top person organizationalPerson inetOrgPerson'.split(),
  243. 'cn': 'c user2',
  244. 'sn': 'user2',
  245. 'givenname': 'c',
  246. 'mail': UID})))
  247. except ldap.LDAPError as e:
  248. log.error('Failed to add ' + USER_DN + ': error ' + e.message['desc'])
  249. assert False
  250. entries = topology.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, '(uid=c*)')
  251. assert len(entries) == 1
  252. entries = topology.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, '(uid=*2)')
  253. assert len(entries) == 1
  254. # restart the server to check the access log
  255. topology.standalone.restart(timeout=10)
  256. cmdline = 'egrep %s %s | egrep "uid=c\*"' % (SUFFIX, topology.standalone.accesslog)
  257. p = os.popen(cmdline, "r")
  258. l0 = p.readline()
  259. if l0 == "":
  260. log.error('Search with "(uid=c*)" is not logged in ' + topology.standalone.accesslog)
  261. assert False
  262. else:
  263. #regex = re.compile('\(conn=[0-9]* op=[0-9]*\) SRCH .*')
  264. regex = re.compile(r'.*\s+(conn=\d+ op=\d+)\s+SRCH .*')
  265. match = regex.match(l0)
  266. log.info('match: %s' % match.group(1))
  267. cmdline = 'egrep "%s" %s | egrep "RESULT"' % (match.group(1), topology.standalone.accesslog)
  268. p = os.popen(cmdline, "r")
  269. l1 = p.readline()
  270. if l1 == "":
  271. log.error('Search result of "(uid=c*)" is not logged in ' + topology.standalone.accesslog)
  272. assert False
  273. else:
  274. log.info('l1: %s' % l1)
  275. regex = re.compile(r'.*nentries=(\d+)\s+.*')
  276. match = regex.match(l1)
  277. log.info('match: nentires=%s' % match.group(1))
  278. if match.group(1) == "0":
  279. log.error('Entry uid=c* not found.')
  280. assert False
  281. else:
  282. log.info('Entry uid=c* found.')
  283. regex = re.compile(r'.*(notes=[AU]).*')
  284. match = regex.match(l1)
  285. if match:
  286. log.error('%s - substr index was not used' % match.group(1))
  287. assert False
  288. else:
  289. log.info('Test case 2-1 - OK - correct substr index used')
  290. cmdline = 'egrep %s %s | egrep "uid=\*2"' % (SUFFIX, topology.standalone.accesslog)
  291. p = os.popen(cmdline, "r")
  292. l0 = p.readline()
  293. if l0 == "":
  294. log.error('Search with "(uid=*2)" is not logged in ' + topology.standalone.accesslog)
  295. assert False
  296. else:
  297. #regex = re.compile('\(conn=[0-9]* op=[0-9]*\) SRCH .*')
  298. regex = re.compile(r'.*\s+(conn=\d+ op=\d+)\s+SRCH .*')
  299. match = regex.match(l0)
  300. log.info('match: %s' % match.group(1))
  301. cmdline = 'egrep "%s" %s | egrep "RESULT"' % (match.group(1), topology.standalone.accesslog)
  302. p = os.popen(cmdline, "r")
  303. l1 = p.readline()
  304. if l1 == "":
  305. log.error('Search result of "(uid=*2)" is not logged in ' + topology.standalone.accesslog)
  306. assert False
  307. else:
  308. log.info('l1: %s' % l1)
  309. regex = re.compile(r'.*nentries=(\d+)\s+.*')
  310. match = regex.match(l1)
  311. log.info('match: nentires=%s' % match.group(1))
  312. if match.group(1) == "0":
  313. log.error('Entry uid=*2 not found.')
  314. assert False
  315. else:
  316. log.info('Entry uid=*2 found.')
  317. regex = re.compile(r'.*(notes=[AU]).*')
  318. match = regex.match(l1)
  319. if match:
  320. log.error('%s - substr index was not used' % match.group(1))
  321. assert False
  322. else:
  323. log.info('Test case 2-2 - OK - correct substr index used')
  324. # clean up substr setting to UID_INDEX
  325. try:
  326. topology.standalone.modify_s(UID_INDEX,
  327. [(ldap.MOD_DELETE, 'nsIndexType', 'sub'),
  328. (ldap.MOD_DELETE, 'nsMatchingRule', 'nssubstrbegin=3'),
  329. (ldap.MOD_DELETE, 'nsMatchingRule', 'nssubstrend=3'),
  330. (ldap.MOD_DELETE, 'objectClass', 'extensibleObject'),
  331. (ldap.MOD_DELETE, 'nsSubStrBegin', '2'),
  332. (ldap.MOD_DELETE, 'nsSubStrEnd', '2')])
  333. except ldap.LDAPError as e:
  334. log.error('Failed to delete substr lengths: error ' + e.message['desc'])
  335. assert False
  336. log.info('Test complete')
  337. def test_ticket48109_final(topology):
  338. topology.standalone.delete()
  339. log.info('Testcase PASSED')
  340. def run_isolated():
  341. global installation1_prefix
  342. installation1_prefix = None
  343. topo = topology(True)
  344. test_ticket48109_0(topo)
  345. test_ticket48109_1(topo)
  346. test_ticket48109_2(topo)
  347. test_ticket48109_final(topo)
  348. if __name__ == '__main__':
  349. run_isolated()