ticket48109_test.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2016 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 pytest
  10. from lib389.tasks import *
  11. from lib389.utils import *
  12. from lib389.topologies import topology_st
  13. logging.getLogger(__name__).setLevel(logging.DEBUG)
  14. log = logging.getLogger(__name__)
  15. UID_INDEX = 'cn=uid,cn=index,cn=userRoot,cn=ldbm database,cn=plugins,cn=config'
  16. def test_ticket48109(topology_st):
  17. '''
  18. Set SubStr lengths to cn=uid,cn=index,...
  19. objectClass: extensibleObject
  20. nsIndexType: sub
  21. nsSubStrBegin: 2
  22. nsSubStrEnd: 2
  23. '''
  24. log.info('Test case 0')
  25. # add substr setting to UID_INDEX
  26. try:
  27. topology_st.standalone.modify_s(UID_INDEX,
  28. [(ldap.MOD_ADD, 'objectClass', 'extensibleObject'),
  29. (ldap.MOD_ADD, 'nsIndexType', 'sub'),
  30. (ldap.MOD_ADD, 'nsSubStrBegin', '2'),
  31. (ldap.MOD_ADD, 'nsSubStrEnd', '2')])
  32. except ldap.LDAPError as e:
  33. log.error('Failed to add substr lengths: error ' + e.message['desc'])
  34. assert False
  35. # restart the server to apply the indexing
  36. topology_st.standalone.restart(timeout=10)
  37. # add a test user
  38. UID = 'auser0'
  39. USER_DN = 'uid=%s,%s' % (UID, SUFFIX)
  40. try:
  41. topology_st.standalone.add_s(Entry((USER_DN, {
  42. 'objectclass': 'top person organizationalPerson inetOrgPerson'.split(),
  43. 'cn': 'a user0',
  44. 'sn': 'user0',
  45. 'givenname': 'a',
  46. 'mail': UID})))
  47. except ldap.LDAPError as e:
  48. log.error('Failed to add ' + USER_DN + ': error ' + e.message['desc'])
  49. assert False
  50. entries = topology_st.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, '(uid=a*)')
  51. assert len(entries) == 1
  52. # restart the server to check the access log
  53. topology_st.standalone.restart(timeout=10)
  54. cmdline = 'egrep %s %s | egrep "uid=a\*"' % (SUFFIX, topology_st.standalone.accesslog)
  55. p = os.popen(cmdline, "r")
  56. l0 = p.readline()
  57. if l0 == "":
  58. log.error('Search with "(uid=a*)" is not logged in ' + topology_st.standalone.accesslog)
  59. assert False
  60. else:
  61. # regex = re.compile('\(conn=[0-9]* op=[0-9]*\) SRCH .*')
  62. regex = re.compile(r'.*\s+(conn=\d+ op=\d+)\s+SRCH .*')
  63. match = regex.match(l0)
  64. log.info('match: %s' % match.group(1))
  65. cmdline = 'egrep "%s" %s | egrep "RESULT"' % (match.group(1), topology_st.standalone.accesslog)
  66. p = os.popen(cmdline, "r")
  67. l1 = p.readline()
  68. if l1 == "":
  69. log.error('Search result of "(uid=a*)" is not logged in ' + topology_st.standalone.accesslog)
  70. assert False
  71. else:
  72. log.info('l1: %s' % l1)
  73. regex = re.compile(r'.*nentries=(\d+)\s+.*')
  74. match = regex.match(l1)
  75. log.info('match: nentires=%s' % match.group(1))
  76. if match.group(1) == "0":
  77. log.error('Entry uid=a* not found.')
  78. assert False
  79. else:
  80. log.info('Entry uid=a* found.')
  81. regex = re.compile(r'.*(notes=[AU]).*')
  82. match = regex.match(l1)
  83. if match:
  84. log.error('%s - substr index was not used' % match.group(1))
  85. assert False
  86. else:
  87. log.info('Test case 0 - OK - substr index used')
  88. # clean up substr setting to UID_INDEX
  89. try:
  90. topology_st.standalone.modify_s(UID_INDEX,
  91. [(ldap.MOD_DELETE, 'objectClass', 'extensibleObject'),
  92. (ldap.MOD_DELETE, 'nsIndexType', 'sub'),
  93. (ldap.MOD_DELETE, 'nsSubStrBegin', '2'),
  94. (ldap.MOD_DELETE, 'nsSubStrEnd', '2')])
  95. except ldap.LDAPError as e:
  96. log.error('Failed to delete substr lengths: error ' + e.message['desc'])
  97. assert False
  98. '''
  99. Set SubStr lengths to cn=uid,cn=index,...
  100. nsIndexType: sub
  101. nsMatchingRule: nsSubStrBegin=2
  102. nsMatchingRule: nsSubStrEnd=2
  103. '''
  104. log.info('Test case 1')
  105. # add substr setting to UID_INDEX
  106. try:
  107. topology_st.standalone.modify_s(UID_INDEX,
  108. [(ldap.MOD_ADD, 'nsIndexType', 'sub'),
  109. (ldap.MOD_ADD, 'nsMatchingRule', 'nssubstrbegin=2'),
  110. (ldap.MOD_ADD, 'nsMatchingRule', 'nssubstrend=2')])
  111. except ldap.LDAPError as e:
  112. log.error('Failed to add substr lengths: error ' + e.message['desc'])
  113. assert False
  114. # restart the server to apply the indexing
  115. topology_st.standalone.restart(timeout=10)
  116. # add a test user
  117. UID = 'buser1'
  118. USER_DN = 'uid=%s,%s' % (UID, SUFFIX)
  119. try:
  120. topology_st.standalone.add_s(Entry((USER_DN, {
  121. 'objectclass': 'top person organizationalPerson inetOrgPerson'.split(),
  122. 'cn': 'b user1',
  123. 'sn': 'user1',
  124. 'givenname': 'b',
  125. 'mail': UID})))
  126. except ldap.LDAPError as e:
  127. log.error('Failed to add ' + USER_DN + ': error ' + e.message['desc'])
  128. assert False
  129. entries = topology_st.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, '(uid=b*)')
  130. assert len(entries) == 1
  131. # restart the server to check the access log
  132. topology_st.standalone.restart(timeout=10)
  133. cmdline = 'egrep %s %s | egrep "uid=b\*"' % (SUFFIX, topology_st.standalone.accesslog)
  134. p = os.popen(cmdline, "r")
  135. l0 = p.readline()
  136. if l0 == "":
  137. log.error('Search with "(uid=b*)" is not logged in ' + topology_st.standalone.accesslog)
  138. assert False
  139. else:
  140. # regex = re.compile('\(conn=[0-9]* op=[0-9]*\) SRCH .*')
  141. regex = re.compile(r'.*\s+(conn=\d+ op=\d+)\s+SRCH .*')
  142. match = regex.match(l0)
  143. log.info('match: %s' % match.group(1))
  144. cmdline = 'egrep "%s" %s | egrep "RESULT"' % (match.group(1), topology_st.standalone.accesslog)
  145. p = os.popen(cmdline, "r")
  146. l1 = p.readline()
  147. if l1 == "":
  148. log.error('Search result of "(uid=*b)" is not logged in ' + topology_st.standalone.accesslog)
  149. assert False
  150. else:
  151. log.info('l1: %s' % l1)
  152. regex = re.compile(r'.*nentries=(\d+)\s+.*')
  153. match = regex.match(l1)
  154. log.info('match: nentires=%s' % match.group(1))
  155. if match.group(1) == "0":
  156. log.error('Entry uid=*b not found.')
  157. assert False
  158. else:
  159. log.info('Entry uid=*b found.')
  160. regex = re.compile(r'.*(notes=[AU]).*')
  161. match = regex.match(l1)
  162. if match:
  163. log.error('%s - substr index was not used' % match.group(1))
  164. assert False
  165. else:
  166. log.info('Test case 1 - OK - substr index used')
  167. # clean up substr setting to UID_INDEX
  168. try:
  169. topology_st.standalone.modify_s(UID_INDEX,
  170. [(ldap.MOD_DELETE, 'nsIndexType', 'sub'),
  171. (ldap.MOD_DELETE, 'nsMatchingRule', 'nssubstrbegin=2'),
  172. (ldap.MOD_DELETE, 'nsMatchingRule', 'nssubstrend=2')])
  173. except ldap.LDAPError as e:
  174. log.error('Failed to delete substr lengths: error ' + e.message['desc'])
  175. assert False
  176. '''
  177. Set SubStr conflict formats/lengths to cn=uid,cn=index,...
  178. objectClass: extensibleObject
  179. nsIndexType: sub
  180. nsMatchingRule: nsSubStrBegin=3
  181. nsMatchingRule: nsSubStrEnd=3
  182. nsSubStrBegin: 2
  183. nsSubStrEnd: 2
  184. nsSubStr{Begin,End} are honored.
  185. '''
  186. log.info('Test case 2')
  187. # add substr setting to UID_INDEX
  188. try:
  189. topology_st.standalone.modify_s(UID_INDEX,
  190. [(ldap.MOD_ADD, 'nsIndexType', 'sub'),
  191. (ldap.MOD_ADD, 'nsMatchingRule', 'nssubstrbegin=3'),
  192. (ldap.MOD_ADD, 'nsMatchingRule', 'nssubstrend=3'),
  193. (ldap.MOD_ADD, 'objectClass', 'extensibleObject'),
  194. (ldap.MOD_ADD, 'nsSubStrBegin', '2'),
  195. (ldap.MOD_ADD, 'nsSubStrEnd', '2')])
  196. except ldap.LDAPError as e:
  197. log.error('Failed to add substr lengths: error ' + e.message['desc'])
  198. assert False
  199. # restart the server to apply the indexing
  200. topology_st.standalone.restart(timeout=10)
  201. # add a test user
  202. UID = 'cuser2'
  203. USER_DN = 'uid=%s,%s' % (UID, SUFFIX)
  204. try:
  205. topology_st.standalone.add_s(Entry((USER_DN, {
  206. 'objectclass': 'top person organizationalPerson inetOrgPerson'.split(),
  207. 'cn': 'c user2',
  208. 'sn': 'user2',
  209. 'givenname': 'c',
  210. 'mail': UID})))
  211. except ldap.LDAPError as e:
  212. log.error('Failed to add ' + USER_DN + ': error ' + e.message['desc'])
  213. assert False
  214. entries = topology_st.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, '(uid=c*)')
  215. assert len(entries) == 1
  216. entries = topology_st.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, '(uid=*2)')
  217. assert len(entries) == 1
  218. # restart the server to check the access log
  219. topology_st.standalone.restart(timeout=10)
  220. cmdline = 'egrep %s %s | egrep "uid=c\*"' % (SUFFIX, topology_st.standalone.accesslog)
  221. p = os.popen(cmdline, "r")
  222. l0 = p.readline()
  223. if l0 == "":
  224. log.error('Search with "(uid=c*)" is not logged in ' + topology_st.standalone.accesslog)
  225. assert False
  226. else:
  227. # regex = re.compile('\(conn=[0-9]* op=[0-9]*\) SRCH .*')
  228. regex = re.compile(r'.*\s+(conn=\d+ op=\d+)\s+SRCH .*')
  229. match = regex.match(l0)
  230. log.info('match: %s' % match.group(1))
  231. cmdline = 'egrep "%s" %s | egrep "RESULT"' % (match.group(1), topology_st.standalone.accesslog)
  232. p = os.popen(cmdline, "r")
  233. l1 = p.readline()
  234. if l1 == "":
  235. log.error('Search result of "(uid=c*)" is not logged in ' + topology_st.standalone.accesslog)
  236. assert False
  237. else:
  238. log.info('l1: %s' % l1)
  239. regex = re.compile(r'.*nentries=(\d+)\s+.*')
  240. match = regex.match(l1)
  241. log.info('match: nentires=%s' % match.group(1))
  242. if match.group(1) == "0":
  243. log.error('Entry uid=c* not found.')
  244. assert False
  245. else:
  246. log.info('Entry uid=c* found.')
  247. regex = re.compile(r'.*(notes=[AU]).*')
  248. match = regex.match(l1)
  249. if match:
  250. log.error('%s - substr index was not used' % match.group(1))
  251. assert False
  252. else:
  253. log.info('Test case 2-1 - OK - correct substr index used')
  254. cmdline = 'egrep %s %s | egrep "uid=\*2"' % (SUFFIX, topology_st.standalone.accesslog)
  255. p = os.popen(cmdline, "r")
  256. l0 = p.readline()
  257. if l0 == "":
  258. log.error('Search with "(uid=*2)" is not logged in ' + topology_st.standalone.accesslog)
  259. assert False
  260. else:
  261. # regex = re.compile('\(conn=[0-9]* op=[0-9]*\) SRCH .*')
  262. regex = re.compile(r'.*\s+(conn=\d+ op=\d+)\s+SRCH .*')
  263. match = regex.match(l0)
  264. log.info('match: %s' % match.group(1))
  265. cmdline = 'egrep "%s" %s | egrep "RESULT"' % (match.group(1), topology_st.standalone.accesslog)
  266. p = os.popen(cmdline, "r")
  267. l1 = p.readline()
  268. if l1 == "":
  269. log.error('Search result of "(uid=*2)" is not logged in ' + topology_st.standalone.accesslog)
  270. assert False
  271. else:
  272. log.info('l1: %s' % l1)
  273. regex = re.compile(r'.*nentries=(\d+)\s+.*')
  274. match = regex.match(l1)
  275. log.info('match: nentires=%s' % match.group(1))
  276. if match.group(1) == "0":
  277. log.error('Entry uid=*2 not found.')
  278. assert False
  279. else:
  280. log.info('Entry uid=*2 found.')
  281. regex = re.compile(r'.*(notes=[AU]).*')
  282. match = regex.match(l1)
  283. if match:
  284. log.error('%s - substr index was not used' % match.group(1))
  285. assert False
  286. else:
  287. log.info('Test case 2-2 - OK - correct substr index used')
  288. # clean up substr setting to UID_INDEX
  289. try:
  290. topology_st.standalone.modify_s(UID_INDEX,
  291. [(ldap.MOD_DELETE, 'nsIndexType', 'sub'),
  292. (ldap.MOD_DELETE, 'nsMatchingRule', 'nssubstrbegin=3'),
  293. (ldap.MOD_DELETE, 'nsMatchingRule', 'nssubstrend=3'),
  294. (ldap.MOD_DELETE, 'objectClass', 'extensibleObject'),
  295. (ldap.MOD_DELETE, 'nsSubStrBegin', '2'),
  296. (ldap.MOD_DELETE, 'nsSubStrEnd', '2')])
  297. except ldap.LDAPError as e:
  298. log.error('Failed to delete substr lengths: error ' + e.message['desc'])
  299. assert False
  300. log.info('Testcase PASSED')
  301. if __name__ == '__main__':
  302. # Run isolated
  303. # -s for DEBUG mode
  304. CURRENT_FILE = os.path.realpath(__file__)
  305. pytest.main("-s %s" % CURRENT_FILE)