syntax_test.py 14 KB

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