syntax_test.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. """
  2. # --- BEGIN COPYRIGHT BLOCK ---
  3. # Copyright (C) 2019 Red Hat, Inc.
  4. # All rights reserved.
  5. #
  6. # License: GPL (version 3 or any later version).
  7. # See LICENSE for details.
  8. # --- END COPYRIGHT BLOCK ----
  9. """
  10. import os
  11. import pytest
  12. from lib389._constants import DEFAULT_SUFFIX
  13. from lib389.idm.domain import Domain
  14. from lib389.topologies import topology_st as topo
  15. import ldap
  16. pytestmark = pytest.mark.tier1
  17. INVALID = [('test_targattrfilters_1',
  18. f'(targattrfilters ="add=title:title=fred),del=cn:(cn!=harry)")'
  19. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  20. ('test_targattrfilters_2',
  21. f'(targattrfilters ="add=:(title=fred),del=cn:(cn!=harry)")'
  22. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  23. ('test_targattrfilters_3',
  24. f'(targattrfilters ="add=:(title=fred),del=cn:(cn!=harry))'
  25. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  26. ('test_targattrfilters_4',
  27. f'(targattrfilters ="add=title:(title=fred),=cn:(cn!=harry")'
  28. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  29. ('test_targattrfilters_5',
  30. f'(targattrfilters ="add=title:(|(title=fred)(cn=harry)),del=cn:(cn=harry)")'
  31. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  32. ('test_targattrfilters_6',
  33. f'(targattrfilters ="add=title:(|(title=fred)(title=harry)),del=cn:(title=harry)")'
  34. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  35. ('test_targattrfilters_7',
  36. f'(targattrfilters ="add=title:(cn=architect), '
  37. f'del=title:(title=architect) && l:(l=cn=Meylan,dc=example,dc=com")")'
  38. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  39. ('test_targattrfilters_8',
  40. f'(targattrfilters ="add=title:(cn=architect)")'
  41. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  42. ('test_targattrfilters_9',
  43. f'(targattrfilters ="add=title:(cn=arch*)")'
  44. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  45. ('test_targattrfilters_10',
  46. f'(targattrfilters ="add=title:(cn >= 1)")'
  47. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  48. ('test_targattrfilters_11',
  49. f'(targattrfilters ="add=title:(cn <= 1)")'
  50. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  51. ('test_targattrfilters_12',
  52. f'(targattrfilters ="add=title:(cn ~= 1)")'
  53. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  54. ('test_targattrfilters_13',
  55. f'(targattrfilters ="add=title:(!(cn ~= 1))")'
  56. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  57. ('test_targattrfilters_14',
  58. f'(targattrfilters ="add=title:(&(cn=fred)(cn ~= 1))")'
  59. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  60. ('test_targattrfilters_15',
  61. f'(targattrfilters ="add=title:(|(cn=fred)(cn ~= 1))")'
  62. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  63. ('test_targattrfilters_16',
  64. f'(targattrfilters ="add=title:(&(|(title=fred)(title=harry))(cn ~= 1))")'
  65. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  66. ('test_targattrfilters_17',
  67. f'\(targattrfilters ="add=title:(&(|(&(title=harry)(title=fred))'
  68. f'(title=harry))(title ~= 1))")'
  69. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  70. ('test_targattrfilters_19',
  71. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  72. f'(targetattr=*)'
  73. f'(version 3.0; acl "Name of the ACI"; deny(write)gropdn="ldap:///anyone";)'),
  74. ('test_targattrfilters_21',
  75. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  76. f'(targetattr=*)'
  77. f'(version 3.0; acl "Name of the ACI"; deny(rite)userdn="ldap:///anyone";)'),
  78. ('test_targattrfilters_22',
  79. f'(targt = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  80. f'(targetattr=*)'
  81. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  82. ('test_targattrfilters_23',
  83. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  84. f'(targetattr=*)'
  85. f'(version 3.0; acl "Name of the ACI"; absolute (all)userdn="ldap:///anyone";)'),
  86. ('test_Missing_acl_mispel',
  87. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  88. f'(targetattr=*)'
  89. f'(version 3.0; alc "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  90. ('test_Missing_acl_string',
  91. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  92. f'(targetattr=*)'
  93. f'(version 3.0; "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  94. ('test_Wrong_version_string',
  95. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  96. f'(targetattr=*)'
  97. f'(version 2.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  98. ('test_Missing_version_string',
  99. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  100. f'(targetattr=*)'
  101. f'(; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  102. ('test_Authenticate_statement',
  103. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  104. f'(targetattr != "uid")'
  105. f'(targetattr=*)(version 3.0; acl "Name of the ACI"; deny absolute (all)'
  106. f'userdn="ldap:///anyone";)'),
  107. ('test_Multiple_targets',
  108. f'(target = ldap:///ou=Product Development,{DEFAULT_SUFFIX})'
  109. f'(target = ldap:///ou=Product Testing,{DEFAULT_SUFFIX})(targetattr=*)'
  110. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  111. ('test_Target_set_to_self',
  112. f'(target = ldap:///self)(targetattr=*)'
  113. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  114. ('test_target_set_with_ldap_instead_of_ldap',
  115. f'(target = ldap:\\\{DEFAULT_SUFFIX})(targetattr=*)'
  116. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  117. ('test_target_set_with_more_than_three',
  118. f'(target = ldap:////{DEFAULT_SUFFIX})(targetattr=*)'
  119. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  120. ('test_target_set_with_less_than_three',
  121. f'(target = ldap://{DEFAULT_SUFFIX})(targetattr=*)'
  122. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  123. ('test_bind_rule_set_with_less_than_three',
  124. f'(target = ldap:///{DEFAULT_SUFFIX})(targetattr=*)'
  125. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:/anyone";)'),
  126. ('test_Use_semicolon_instead_of_comma_in_permission',
  127. f'(target = ldap:///{DEFAULT_SUFFIX})(targetattr=*)'
  128. f'(version 3.0; acl "Name of the ACI"; deny '
  129. f'(read; search; compare; write)userdn="ldap:///anyone";)'),
  130. ('test_Use_double_equal_instead_of_equal_in_the_target',
  131. f'(target == ldap:///{DEFAULT_SUFFIX})(targetattr=*)'
  132. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  133. ('test_use_double_equal_instead_of_equal_in_user_and_group_access',
  134. f'(target = ldap:///{DEFAULT_SUFFIX})'
  135. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)'
  136. f'userdn == "ldap:///anyone";)'),
  137. ('test_donot_cote_the_name_of_the_aci',
  138. f'(target = ldap:///{DEFAULT_SUFFIX})'
  139. f'(version 3.0; acl Name of the ACI ; deny absolute (all)userdn = "ldap:///anyone";)'),
  140. ('test_extra_parentheses_case_1',
  141. f'( )(target = ldap:///{DEFAULT_SUFFIX}) (targetattr=*)'
  142. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn = "ldap:///anyone";)'),
  143. ('test_extra_parentheses_case_2',
  144. f'(((((target = ldap:///{DEFAULT_SUFFIX})(targetattr=*)'
  145. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)'
  146. f'userdn == "ldap:///anyone";)'),
  147. ('test_extra_parentheses_case_3',
  148. f'(((target = ldap:///{DEFAULT_SUFFIX}) (targetattr=*)'
  149. f'(version 3.0; acl "Name of the ACI"; deny absolute '
  150. f'(all)userdn = "ldap:///anyone";)))'),
  151. ('test_no_semicolon_at_the_end_of_the_aci',
  152. f'(target = ldap:///{DEFAULT_SUFFIX}) (targetattr=*)'
  153. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn = "ldap:///anyone")'),
  154. ('test_a_character_different_of_a_semicolon_at_the_end_of_the_aci',
  155. f'(target = ldap:///{DEFAULT_SUFFIX}) (targetattr=*)'
  156. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn = "ldap:///anyone"%)'),
  157. ('test_bad_filter',
  158. f'(target = ldap:///{DEFAULT_SUFFIX}) '
  159. f'(targetattr="cn |&| sn |(|) uid")'
  160. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn = "ldap:///anyone";)'),
  161. ('test_Use_double_equal_instead_of_equal_in_the_targattrfilters',
  162. f'(target = ldap:///{DEFAULT_SUFFIX})(targattrfilters== "add=title:(title=architect)")'
  163. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  164. ('test_Use_double_equal_instead_of_equal_inside_the_targattrfilters',
  165. f'(target = ldap:///{DEFAULT_SUFFIX})(targattrfilters="add==title:(title==architect)")'
  166. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),]
  167. FAILED = [('test_targattrfilters_18',
  168. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  169. f'(targetattr=*)'
  170. f'(version 3.0; acl "Name of the ACI"; deny(write)userdn="ldap:///{"123" * 300}";)'),
  171. ('test_targattrfilters_20',
  172. f'(target = ldap:///cn=Jeff Vedder,ou=Product Development,{DEFAULT_SUFFIX})'
  173. f'(targetattr=*)'
  174. f'(version 3.0; acl "Name of the ACI"; deny(write)userdns="ldap:///anyone";)'),
  175. ('test_bind_rule_set_with_more_than_three',
  176. f'(target = ldap:///{DEFAULT_SUFFIX})(targetattr=*)'
  177. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)'
  178. f'userdn="ldap:////////anyone";)'),
  179. ('test_Use_double_equal_instead_of_equal_in_the_targetattr',
  180. f'(target = ldap:///{DEFAULT_SUFFIX})(targetattr==*)'
  181. f'(version 3.0; acl "Name of the ACI"; deny absolute (all)userdn="ldap:///anyone";)'),
  182. ('test_Use_double_equal_instead_of_equal_in_the_targetfilter',
  183. f'(target = ldap:///{DEFAULT_SUFFIX})(targetfilter==*)'
  184. f'(version 3.0; acl "Name of the ACI"; deny absolute '
  185. f'(all)userdn="ldap:///anyone";)'), ]
  186. @pytest.mark.xfail(reason='https://bugzilla.redhat.com/show_bug.cgi?id=1691473')
  187. @pytest.mark.parametrize("real_value", [a[1] for a in FAILED],
  188. ids=[a[0] for a in FAILED])
  189. def test_aci_invalid_syntax_fail(topo, real_value):
  190. """
  191. Try to set wrong ACI syntax.
  192. :id: d544d09a-6ed1-11e8-8872-8c16451d917b
  193. :setup: Standalone Instance
  194. :steps:
  195. 1. Create ACI
  196. 2. Try to setup the ACI with Instance
  197. :expectedresults:
  198. 1. It should pass
  199. 2. It should not pass
  200. """
  201. domain = Domain(topo.standalone, DEFAULT_SUFFIX)
  202. with pytest.raises(ldap.INVALID_SYNTAX):
  203. domain.add("aci", real_value)
  204. @pytest.mark.parametrize("real_value", [a[1] for a in INVALID],
  205. ids=[a[0] for a in INVALID])
  206. def test_aci_invalid_syntax(topo, real_value):
  207. """
  208. Try to set wrong ACI syntax.
  209. :id: d544d09a-6ed1-11e8-8872-8c16451d917b
  210. :setup: Standalone Instance
  211. :steps:
  212. 1. Create ACI
  213. 2. Try to setup the ACI with Instance
  214. :expectedresults:
  215. 1. It should pass
  216. 2. It should not pass
  217. """
  218. domain = Domain(topo.standalone, DEFAULT_SUFFIX)
  219. with pytest.raises(ldap.INVALID_SYNTAX):
  220. domain.add("aci", real_value)
  221. def test_target_set_above_the_entry_test(topo):
  222. """
  223. Try to set wrong ACI syntax.
  224. :id: d544d09a-6ed1-11e8-8872-8c16451d917b
  225. :setup: Standalone Instance
  226. :steps:
  227. 1. Create ACI
  228. 2. Try to setup the ACI with Instance
  229. :expectedresults:
  230. 1. It should pass
  231. 2. It should not pass
  232. """
  233. domain = Domain(topo.standalone, "ou=People,{}".format(DEFAULT_SUFFIX))
  234. with pytest.raises(ldap.INVALID_SYNTAX):
  235. domain.add("aci", f'(target = ldap:///{DEFAULT_SUFFIX})'
  236. f'(targetattr=*)(version 3.0; acl "Name of the ACI"; deny absolute '
  237. f'(all)userdn="ldap:///anyone";)')
  238. if __name__ == "__main__":
  239. CURRENT_FILE = os.path.realpath(__file__)
  240. pytest.main("-s -v %s" % CURRENT_FILE)