test-trusted.py
261 lines
| 7.9 KiB
| text/x-python
|
PythonLexer
/ tests / test-trusted.py
|
r3551 | # Since it's not easy to write a test that portably deals | ||
# with files from different users/groups, we cheat a bit by | ||||
# monkey-patching some functions in the util module | ||||
|
r28934 | from __future__ import absolute_import, print_function | ||
|
r28913 | |||
|
r3551 | import os | ||
|
r28913 | from mercurial import ( | ||
error, | ||||
ui as uimod, | ||||
util, | ||||
) | ||||
|
r3551 | |||
hgrc = os.environ['HGRCPATH'] | ||||
|
r5523 | f = open(hgrc) | ||
basehgrc = f.read() | ||||
f.close() | ||||
|
r3551 | |||
def testui(user='foo', group='bar', tusers=(), tgroups=(), | ||||
|
r13493 | cuser='foo', cgroup='bar', debug=False, silent=False, | ||
report=True): | ||||
|
r3551 | # user, group => owners of the file | ||
# tusers, tgroups => trusted users/groups | ||||
# cuser, cgroup => user/group of the current process | ||||
# write a global hgrc with the list of trusted users/groups and | ||||
# some setting so that we can be sure it was read | ||||
f = open(hgrc, 'w') | ||||
|
r5523 | f.write(basehgrc) | ||
f.write('\n[paths]\n') | ||||
|
r3551 | f.write('global = /some/path\n\n') | ||
if tusers or tgroups: | ||||
f.write('[trusted]\n') | ||||
if tusers: | ||||
f.write('users = %s\n' % ', '.join(tusers)) | ||||
if tgroups: | ||||
f.write('groups = %s\n' % ', '.join(tgroups)) | ||||
f.close() | ||||
# override the functions that give names to uids and gids | ||||
def username(uid=None): | ||||
if uid is None: | ||||
return cuser | ||||
return user | ||||
util.username = username | ||||
def groupname(gid=None): | ||||
if gid is None: | ||||
return 'bar' | ||||
return group | ||||
util.groupname = groupname | ||||
|
r8657 | def isowner(st): | ||
|
r3677 | return user == cuser | ||
util.isowner = isowner | ||||
|
r3551 | # try to read everything | ||
#print '# File belongs to user %s, group %s' % (user, group) | ||||
#print '# trusted users = %s; trusted groups = %s' % (tusers, tgroups) | ||||
kind = ('different', 'same') | ||||
who = ('', 'user', 'group', 'user and the group') | ||||
trusted = who[(user in tusers) + 2*(group in tgroups)] | ||||
if trusted: | ||||
trusted = ', but we trust the ' + trusted | ||||
|
r28934 | print('# %s user, %s group%s' % (kind[user == cuser], kind[group == cgroup], | ||
trusted)) | ||||
|
r3551 | |||
|
r30559 | u = uimod.ui.load() | ||
|
r34859 | # disable the configuration registration warning | ||
# | ||||
# the purpose of this test is to check the old behavior, not to validate the | ||||
# behavior from registered item. so we silent warning related to unregisted | ||||
# config. | ||||
u.setconfig('devel', 'warn-config-unknown', False, 'test') | ||||
u.setconfig('devel', 'all-warnings', False, 'test') | ||||
|
r8190 | u.setconfig('ui', 'debug', str(bool(debug))) | ||
|
r13493 | u.setconfig('ui', 'report_untrusted', str(bool(report))) | ||
|
r3551 | u.readconfig('.hg/hgrc') | ||
|
r3552 | if silent: | ||
return u | ||||
|
r28934 | print('trusted') | ||
|
r3551 | for name, path in u.configitems('paths'): | ||
|
r31857 | print(' ', name, '=', util.pconvert(path)) | ||
|
r28934 | print('untrusted') | ||
|
r3552 | for name, path in u.configitems('paths', untrusted=True): | ||
|
r28934 | print('.', end=' ') | ||
|
r3552 | u.config('paths', name) # warning with debug=True | ||
|
r28934 | print('.', end=' ') | ||
|
r3552 | u.config('paths', name, untrusted=True) # no warnings | ||
|
r31857 | print(name, '=', util.pconvert(path)) | ||
|
r28934 | print() | ||
|
r3551 | |||
return u | ||||
os.mkdir('repo') | ||||
os.chdir('repo') | ||||
os.mkdir('.hg') | ||||
f = open('.hg/hgrc', 'w') | ||||
f.write('[paths]\n') | ||||
f.write('local = /another/path\n\n') | ||||
f.close() | ||||
#print '# Everything is run by user foo, group bar\n' | ||||
# same user, same group | ||||
testui() | ||||
# same user, different group | ||||
testui(group='def') | ||||
# different user, same group | ||||
testui(user='abc') | ||||
# ... but we trust the group | ||||
testui(user='abc', tgroups=['bar']) | ||||
# different user, different group | ||||
testui(user='abc', group='def') | ||||
# ... but we trust the user | ||||
testui(user='abc', group='def', tusers=['abc']) | ||||
# ... but we trust the group | ||||
testui(user='abc', group='def', tgroups=['def']) | ||||
# ... but we trust the user and the group | ||||
testui(user='abc', group='def', tusers=['abc'], tgroups=['def']) | ||||
# ... but we trust all users | ||||
|
r28934 | print('# we trust all users') | ||
|
r3551 | testui(user='abc', group='def', tusers=['*']) | ||
# ... but we trust all groups | ||||
|
r28934 | print('# we trust all groups') | ||
|
r3551 | testui(user='abc', group='def', tgroups=['*']) | ||
# ... but we trust the whole universe | ||||
|
r28934 | print('# we trust all users and groups') | ||
|
r3551 | testui(user='abc', group='def', tusers=['*'], tgroups=['*']) | ||
# ... check that users and groups are in different namespaces | ||||
|
r28934 | print("# we don't get confused by users and groups with the same name") | ||
|
r3551 | testui(user='abc', group='def', tusers=['def'], tgroups=['abc']) | ||
# ... lists of user names work | ||||
|
r28934 | print("# list of user names") | ||
|
r3551 | testui(user='abc', group='def', tusers=['foo', 'xyz', 'abc', 'bleh'], | ||
tgroups=['bar', 'baz', 'qux']) | ||||
# ... lists of group names work | ||||
|
r28934 | print("# list of group names") | ||
|
r3551 | testui(user='abc', group='def', tusers=['foo', 'xyz', 'bleh'], | ||
tgroups=['bar', 'def', 'baz', 'qux']) | ||||
|
r28934 | print("# Can't figure out the name of the user running this process") | ||
|
r3551 | testui(user='abc', group='def', cuser=None) | ||
|
r3552 | |||
|
r28934 | print("# prints debug warnings") | ||
|
r3552 | u = testui(user='abc', group='def', cuser='foo', debug=True) | ||
|
r28934 | print("# report_untrusted enabled without debug hides warnings") | ||
|
r13493 | u = testui(user='abc', group='def', cuser='foo', report=False) | ||
|
r28934 | print("# report_untrusted enabled with debug shows warnings") | ||
|
r13493 | u = testui(user='abc', group='def', cuser='foo', debug=True, report=False) | ||
|
r28934 | print("# ui.readconfig sections") | ||
|
r3552 | filename = 'foobar' | ||
f = open(filename, 'w') | ||||
f.write('[foobar]\n') | ||||
f.write('baz = quux\n') | ||||
f.close() | ||||
|
r19872 | u.readconfig(filename, sections=['foobar']) | ||
|
r28934 | print(u.config('foobar', 'baz')) | ||
|
r3552 | |||
|
r28934 | print() | ||
print("# read trusted, untrusted, new ui, trusted") | ||||
|
r30559 | u = uimod.ui.load() | ||
|
r34859 | # disable the configuration registration warning | ||
# | ||||
# the purpose of this test is to check the old behavior, not to validate the | ||||
# behavior from registered item. so we silent warning related to unregisted | ||||
# config. | ||||
u.setconfig('devel', 'warn-config-unknown', False, 'test') | ||||
u.setconfig('devel', 'all-warnings', False, 'test') | ||||
|
r8136 | u.setconfig('ui', 'debug', 'on') | ||
|
r3552 | u.readconfig(filename) | ||
|
r8190 | u2 = u.copy() | ||
|
r3552 | def username(uid=None): | ||
return 'foo' | ||||
util.username = username | ||||
u2.readconfig('.hg/hgrc') | ||||
|
r28934 | print('trusted:') | ||
print(u2.config('foobar', 'baz')) | ||||
print('untrusted:') | ||||
print(u2.config('foobar', 'baz', untrusted=True)) | ||||
|
r3552 | |||
|
r28934 | print() | ||
print("# error handling") | ||||
|
r3552 | |||
|
r26587 | def assertraises(f, exc=error.Abort): | ||
|
r3552 | try: | ||
f() | ||||
|
r25660 | except exc as inst: | ||
|
r28934 | print('raised', inst.__class__.__name__) | ||
|
r3552 | else: | ||
|
r28934 | print('no exception?!') | ||
|
r3552 | |||
|
r28934 | print("# file doesn't exist") | ||
|
r3552 | os.unlink('.hg/hgrc') | ||
assert not os.path.exists('.hg/hgrc') | ||||
testui(debug=True, silent=True) | ||||
testui(user='abc', group='def', debug=True, silent=True) | ||||
|
r28934 | print() | ||
print("# parse error") | ||||
|
r3552 | f = open('.hg/hgrc', 'w') | ||
|
r8144 | f.write('foo') | ||
|
r3552 | f.close() | ||
|
r8144 | try: | ||
testui(user='abc', group='def', silent=True) | ||||
|
r25660 | except error.ParseError as inst: | ||
|
r28934 | print(inst) | ||
|
r3552 | |||
|
r8144 | try: | ||
testui(debug=True, silent=True) | ||||
|
r25660 | except error.ParseError as inst: | ||
|
r28934 | print(inst) | ||
|
r31472 | |||
print() | ||||
print('# access typed information') | ||||
with open('.hg/hgrc', 'w') as f: | ||||
f.write('''\ | ||||
[foo] | ||||
sub=main | ||||
sub:one=one | ||||
sub:two=two | ||||
path=monty/python | ||||
bool=true | ||||
int=42 | ||||
bytes=81mb | ||||
list=spam,ham,eggs | ||||
''') | ||||
u = testui(user='abc', group='def', cuser='foo', silent=True) | ||||
|
r31857 | def configpath(section, name, default=None, untrusted=False): | ||
path = u.configpath(section, name, default, untrusted) | ||||
if path is None: | ||||
return None | ||||
return util.pconvert(path) | ||||
|
r31472 | print('# suboptions, trusted and untrusted') | ||
trusted = u.configsuboptions('foo', 'sub') | ||||
untrusted = u.configsuboptions('foo', 'sub', untrusted=True) | ||||
print( | ||||
(trusted[0], sorted(trusted[1].items())), | ||||
(untrusted[0], sorted(untrusted[1].items()))) | ||||
print('# path, trusted and untrusted') | ||||
|
r31857 | print(configpath('foo', 'path'), configpath('foo', 'path', untrusted=True)) | ||
|
r31472 | print('# bool, trusted and untrusted') | ||
print(u.configbool('foo', 'bool'), u.configbool('foo', 'bool', untrusted=True)) | ||||
print('# int, trusted and untrusted') | ||||
print( | ||||
u.configint('foo', 'int', 0), | ||||
u.configint('foo', 'int', 0, untrusted=True)) | ||||
print('# bytes, trusted and untrusted') | ||||
print( | ||||
u.configbytes('foo', 'bytes', 0), | ||||
u.configbytes('foo', 'bytes', 0, untrusted=True)) | ||||
print('# list, trusted and untrusted') | ||||
print( | ||||
u.configlist('foo', 'list', []), | ||||
u.configlist('foo', 'list', [], untrusted=True)) | ||||