📄 authz_tests.py
字号:
#!/usr/bin/env python## authz_tests.py: testing authentication.## Subversion is a tool for revision control. # See http://subversion.tigris.org for more information.# # ====================================================================# Copyright (c) 2000-2006 CollabNet. All rights reserved.## This software is licensed as described in the file COPYING, which# you should have received as part of this distribution. The terms# are also available at http://subversion.tigris.org/license-1.html.# If newer versions of this license are posted there, you may use a# newer version instead, at your option.######################################################################## General modulesimport os# Our testing moduleimport svntest# (abbreviation)Item = svntest.wc.StateItemXFail = svntest.testcase.XFail####################################################################### Utilities#def write_restrictive_svnserve_conf(repo_dir): "Create a restrictive authz file ( no anynomous access )." fp = open(svntest.main.get_svnserve_conf_file_path(repo_dir), 'w') fp.write("[general]\nanon-access = none\nauth-access = write\n" "password-db = passwd\nauthz-db = authz\n") fp.close()def write_authz_file(sbox, rules, sections=None): """Write an authz file to SBOX, appropriate for the RA method used,with authorizations rules RULES mapping paths to strings containingthe rules. You can add sections SECTIONS (ex. groups, aliases...) with an appropriate list of mappings.""" fp = open(sbox.authz_file, 'w') if sbox.repo_url.startswith("http"): prefix = sbox.name + ":" else: prefix = "" if sections: for p, r in sections.items(): fp.write("[%s]\n%s\n" % (p, r)) for p, r in rules.items(): fp.write("[%s%s]\n%s\n" % (prefix, p, r)) fp.close()def skip_test_when_no_authz_available(): "skip this test when authz is not available" if svntest.main.test_area_url.startswith('file://'): raise svntest.Skip ####################################################################### Tests## Each test must return on success or raise on failure.#----------------------------------------------------------------------# regression test for issue #2486 - part 1: open_rootdef authz_open_root(sbox): "authz issue #2486 - open root" sbox.build() skip_test_when_no_authz_available() fp = open(sbox.authz_file, 'w') fp.write("[/]\n\n[/A]\njrandom = rw\n") fp.close() write_restrictive_svnserve_conf(svntest.main.current_repo_dir) # we have write access in folder /A, but not in root. Test on too # restrictive access needed in open_root by modifying a file in /A wc_dir = sbox.wc_dir mu_path = os.path.join(wc_dir, 'A', 'mu') svntest.main.file_append(mu_path, "hi") # Create expected output tree. expected_output = svntest.wc.State(wc_dir, { 'A/mu' : Item(verb='Sending'), }) # Commit the one file. svntest.actions.run_and_verify_commit(wc_dir, expected_output, None, None, None, None, None, None, mu_path)#----------------------------------------------------------------------# regression test for issue #2486 - part 2: open_directorydef authz_open_directory(sbox): "authz issue #2486 - open directory" sbox.build() skip_test_when_no_authz_available() fp = open(sbox.authz_file, 'w') fp.write("[/]\n*=rw\n[/A/B]\n*=\n[/A/B/E]\njrandom = rw\n") fp.close() write_restrictive_svnserve_conf(svntest.main.current_repo_dir) # we have write access in folder /A/B/E, but not in /A/B. Test on too # restrictive access needed in open_directory by moving file /A/mu to # /A/B/E wc_dir = sbox.wc_dir mu_path = os.path.join(wc_dir, 'A', 'mu') E_path = os.path.join(wc_dir, 'A', 'B', 'E') svntest.main.run_svn(None, 'mv', mu_path, E_path) # Create expected output tree. expected_output = svntest.wc.State(wc_dir, { 'A/mu' : Item(verb='Deleting'), 'A/B/E/mu' : Item(verb='Adding'), }) # Commit the working copy. svntest.actions.run_and_verify_commit(wc_dir, expected_output, None, None, None, None, None, None, wc_dir)def broken_authz_file(sbox): "broken authz files cause errors" sbox.build(create_wc = False) skip_test_when_no_authz_available() # No characters but 'r', 'w', and whitespace are allowed as a value # in an authz rule. fp = open(sbox.authz_file, 'w') fp.write("[/]\njrandom = rw # End-line comments disallowed\n") fp.close() write_restrictive_svnserve_conf(svntest.main.current_repo_dir) out, err = svntest.main.run_svn(1, "delete", "--username", svntest.main.wc_author, "--password", svntest.main.wc_passwd, sbox.repo_url + "/A", "-m", "a log message"); if out: raise svntest.actions.SVNUnexpectedStdout(out) if not err: raise svntest.actions.SVNUnexpectedStderr("Missing stderr")# test whether read access is correctly granted and denieddef authz_read_access(sbox): "test authz for read operations" skip_test_when_no_authz_available() sbox.build("authz_read_access", create_wc = False) write_restrictive_svnserve_conf(svntest.main.current_repo_dir) fp = open(sbox.authz_file, 'w') # For mod_dav_svn's parent path setup we need per-repos permissions in # the authz file... if sbox.repo_url.startswith('http'): fp.write("[authz_read_access:/]\n" + "* = r\n" + "[authz_read_access:/A/B]\n" + "* = \n" + "[authz_read_access:/A/D]\n" + "* = rw\n" + "[authz_read_access:/A/D/G]\n" + "* = rw\n" + svntest.main.wc_author + " = \n" + "[authz_read_access:/A/D/H]\n" + "* = \n" + svntest.main.wc_author + " = rw\n") expected_err = ".*403 Forbidden.*" # Otherwise we can just go with the permissions needed for the source # repository. else: fp.write("[/]\n" + "* = r\n" + "[/A/B]\n" + "* =\n" + "[/A/D]\n" + "* = rw\n" + "[/A/D/G]\n" + "* = rw\n" + svntest.main.wc_author + " =\n" + "[/A/D/H]\n" + "* = \n" + svntest.main.wc_author + " = rw\n") expected_err = ".*svn: Authorization failed.*" fp.close() root_url = svntest.main.current_repo_url A_url = root_url + '/A' B_url = A_url + '/B' C_url = A_url + '/C' E_url = B_url + '/E' mu_url = A_url + '/mu' iota_url = root_url + '/iota' lambda_url = B_url + '/lambda' alpha_url = E_url + '/alpha' D_url = A_url + '/D' G_url = D_url + '/G' pi_url = G_url + '/pi' H_url = D_url + '/H' chi_url = H_url + '/chi' # read a remote file svntest.actions.run_and_verify_svn(None, ["This is the file 'iota'.\n"], [], 'cat', '--username', svntest.main.wc_author, '--password', svntest.main.wc_passwd, iota_url) # read a remote file, readably by user specific exception svntest.actions.run_and_verify_svn(None, ["This is the file 'chi'.\n"], [], 'cat', '--username', svntest.main.wc_author, '--password', svntest.main.wc_passwd, chi_url) # read a remote file, unreadable: should fail svntest.actions.run_and_verify_svn("", None, expected_err, 'cat', '--username', svntest.main.wc_author, '--password', svntest.main.wc_passwd, lambda_url) # read a remote file, unreadable through recursion: should fail svntest.actions.run_and_verify_svn("", None, expected_err, 'cat', '--username', svntest.main.wc_author, '--password', svntest.main.wc_passwd, alpha_url) # read a remote file, user specific authorization is ignored because * = rw svntest.actions.run_and_verify_svn(None, ["This is the file 'pi'.\n"], [], 'cat', '--username', svntest.main.wc_author, '--password', svntest.main.wc_passwd, pi_url) # open a remote folder(ls) svntest.actions.run_and_verify_svn("ls remote root folder", ["A/\n", "iota\n"], [], 'ls', '--username', svntest.main.wc_author, '--password', svntest.main.wc_passwd, root_url) # open a remote folder(ls), unreadable: should fail svntest.actions.run_and_verify_svn("", None, svntest.SVNAnyOutput, 'ls', '--username', svntest.main.wc_author, '--password', svntest.main.wc_passwd, B_url) # open a remote folder(ls), unreadable through recursion: should fail
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -