1
0

ticket48109_test.py 15 KB

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