ticket48844_test.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. PLUGIN_BITWISE = 'Bitwise Plugin'
  17. TESTBASEDN="dc=bitwise,dc=com"
  18. TESTBACKEND_NAME="TestBitw"
  19. F1 = 'objectclass=testperson'
  20. BITWISE_F2 = '(&(%s)(testUserAccountControl:1.2.840.113556.1.4.803:=514))' % F1
  21. BITWISE_F3 = '(&(%s)(testUserAccountControl:1.2.840.113556.1.4.803:=513))' % F1
  22. BITWISE_F6 = '(&(%s)(testUserAccountControl:1.2.840.113556.1.4.803:=16777216))' % F1
  23. class TopologyStandalone(object):
  24. def __init__(self, standalone):
  25. standalone.open()
  26. self.standalone = standalone
  27. @pytest.fixture(scope="module")
  28. def topology(request):
  29. global installation1_prefix
  30. if installation1_prefix:
  31. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  32. # Creating standalone instance ...
  33. standalone = DirSrv(verbose=False)
  34. if installation1_prefix:
  35. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  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. # Delete each instance in the end
  48. def fin():
  49. standalone.delete()
  50. #request.addfinalizer(fin)
  51. # Clear out the tmp dir
  52. standalone.clearTmpDir(__file__)
  53. return TopologyStandalone(standalone)
  54. def _addBitwiseEntries(topology):
  55. users = [
  56. ('testuser2', '65536' ,'PasswordNeverExpired' ),
  57. ('testuser3', '8388608' ,'PasswordExpired'),
  58. ('testuser4', '256' ,'TempDuplicateAccount'),
  59. ('testuser5', '16777216' ,'TrustedAuthDelegation'),
  60. ('testuser6', '528' ,'AccountLocked'),
  61. ('testuser7', '513' ,'AccountActive'),
  62. ('testuser8', '98536 99512 99528'.split() ,'AccountActive PasswordExxpired AccountLocked'.split()),
  63. ('testuser9', '87536 912'.split() ,'AccountActive PasswordNeverExpired'.split()),
  64. ('testuser10', '89536 97546 96579'.split() ,'TestVerify1 TestVerify2 TestVerify3'.split() ),
  65. ('testuser11', '655236' ,'TestStatus1'),
  66. ('testuser12', '665522' ,'TestStatus2'),
  67. ('testuser13', '266552' ,'TestStatus3')]
  68. try:
  69. topology.standalone.add_s(Entry((TESTBASEDN,
  70. {'objectclass': "top dcobject".split(),
  71. 'dc': 'bitwise',
  72. 'aci': '(target =\"ldap:///dc=bitwise,dc=com\")' +\
  73. '(targetattr != \"userPassword\")' +\
  74. '(version 3.0;acl \"Anonymous read-search access\";' +\
  75. 'allow (read, search, compare)(userdn = \"ldap:///anyone\");)'})))
  76. topology.standalone.add_s(Entry(('uid=btestuser1,%s' % TESTBASEDN,
  77. {'objectclass': 'top testperson organizationalPerson inetorgperson'.split(),
  78. 'mail': '[email protected]',
  79. 'uid': 'btestuser1',
  80. 'givenName': 'bit',
  81. 'sn': 'testuser1',
  82. 'userPassword': 'testuser1',
  83. 'testUserAccountControl': '514',
  84. 'testUserStatus': 'Disabled',
  85. 'cn': 'bit tetsuser1'})))
  86. for (userid, accCtl,accStatus) in users:
  87. topology.standalone.add_s(Entry(('uid=b%s,%s' % (userid, TESTBASEDN),
  88. {'objectclass': 'top testperson organizationalPerson inetorgperson'.split(),
  89. 'mail': '%[email protected]' % userid,
  90. 'uid': 'b%s' % userid,
  91. 'givenName': 'bit',
  92. 'sn': userid,
  93. 'userPassword': userid,
  94. 'testUserAccountControl': accCtl,
  95. 'testUserStatus': accStatus,
  96. 'cn': 'bit %s' % userid})))
  97. except ValueError:
  98. topology.standalone.log.fatal("add_s failed: %s", ValueError)
  99. def test_ticket48844_init(topology):
  100. # create a suffix where test entries will be stored
  101. BITW_SCHEMA_AT_1 = '( NAME \'testUserAccountControl\' DESC \'Attribute Bitwise filteri-Multi-Valued\' SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 )'
  102. BITW_SCHEMA_AT_2 = '( NAME \'testUserStatus\' DESC \'State of User account active/disabled\' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )'
  103. BITW_SCHEMA_OC_1 = '( NAME \'testperson\' SUP top STRUCTURAL MUST ( sn $ cn $ testUserAccountControl $ testUserStatus )' +\
  104. ' MAY ( userPassword $ telephoneNumber $ seeAlso $ description ) X-ORIGIN \'BitWise\' )'
  105. topology.standalone.schema.add_schema('attributetypes', [BITW_SCHEMA_AT_1, BITW_SCHEMA_AT_2])
  106. topology.standalone.schema.add_schema('objectClasses', BITW_SCHEMA_OC_1)
  107. topology.standalone.backend.create(TESTBASEDN, {BACKEND_NAME: TESTBACKEND_NAME})
  108. topology.standalone.mappingtree.create(TESTBASEDN, bename=TESTBACKEND_NAME, parent=None)
  109. _addBitwiseEntries(topology)
  110. def test_ticket48844_bitwise_on(topology):
  111. """
  112. Check that bitwise plugin (old style MR plugin) that defines
  113. Its own indexer create function, is selected to evaluate the filter
  114. """
  115. topology.standalone.plugins.enable(name=PLUGIN_BITWISE)
  116. topology.standalone.restart(timeout=10)
  117. ents = topology.standalone.search_s('cn=%s,cn=plugins,cn=config' % PLUGIN_BITWISE, ldap.SCOPE_BASE, 'objectclass=*')
  118. assert(ents[0].hasValue('nsslapd-pluginEnabled', 'on'))
  119. expect = 2
  120. ents = topology.standalone.search_s(TESTBASEDN, ldap.SCOPE_SUBTREE, BITWISE_F2)
  121. assert (len(ents) == expect)
  122. expect=1
  123. ents = topology.standalone.search_s(TESTBASEDN, ldap.SCOPE_SUBTREE, BITWISE_F3)
  124. assert (len(ents) == expect)
  125. assert (ents[0].hasAttr('testUserAccountControl'))
  126. expect=1
  127. ents = topology.standalone.search_s(TESTBASEDN, ldap.SCOPE_SUBTREE, BITWISE_F6)
  128. assert (len(ents) == expect)
  129. assert (ents[0].hasAttr('testUserAccountControl'))
  130. def test_ticket48844_bitwise_off(topology):
  131. """
  132. Check that when bitwise plugin is not enabled, no plugin
  133. is identified to evaluate the filter -> ldap.UNAVAILABLE_CRITICAL_EXTENSION:
  134. """
  135. topology.standalone.plugins.disable(name=PLUGIN_BITWISE)
  136. topology.standalone.restart(timeout=10)
  137. ents = topology.standalone.search_s('cn=%s,cn=plugins,cn=config' % PLUGIN_BITWISE, ldap.SCOPE_BASE, 'objectclass=*')
  138. assert(ents[0].hasValue('nsslapd-pluginEnabled', 'off'))
  139. res = 0
  140. try:
  141. ents = topology.standalone.search_s(TESTBASEDN, ldap.SCOPE_SUBTREE, BITWISE_F2)
  142. except ldap.UNAVAILABLE_CRITICAL_EXTENSION:
  143. res = 12
  144. assert (res == 12)
  145. if __name__ == '__main__':
  146. # Run isolated
  147. # -s for DEBUG mode
  148. CURRENT_FILE = os.path.realpath(__file__)
  149. pytest.main("-s %s" % CURRENT_FILE)