tests/test-trusted.py
changeset 41352 73ccba60aaa1
parent 34858 85a2db47ad50
child 41497 3028b4073be1
equal deleted inserted replaced
41351:0ae3ddb4fbed 41352:73ccba60aaa1
     3 # monkey-patching some functions in the util module
     3 # monkey-patching some functions in the util module
     4 
     4 
     5 from __future__ import absolute_import, print_function
     5 from __future__ import absolute_import, print_function
     6 
     6 
     7 import os
     7 import os
       
     8 import sys
       
     9 
     8 from mercurial import (
    10 from mercurial import (
     9     error,
    11     error,
       
    12     pycompat,
    10     ui as uimod,
    13     ui as uimod,
    11     util,
    14     util,
    12 )
    15 )
       
    16 from mercurial.utils import stringutil
    13 
    17 
    14 hgrc = os.environ['HGRCPATH']
    18 hgrc = os.environ['HGRCPATH']
    15 f = open(hgrc)
    19 f = open(hgrc, 'rb')
    16 basehgrc = f.read()
    20 basehgrc = f.read()
    17 f.close()
    21 f.close()
    18 
    22 
    19 def testui(user='foo', group='bar', tusers=(), tgroups=(),
    23 def _maybesysstr(v):
    20            cuser='foo', cgroup='bar', debug=False, silent=False,
    24     if isinstance(v, bytes):
       
    25         return pycompat.sysstr(v)
       
    26     return pycompat.sysstr(stringutil.pprint(v))
       
    27 
       
    28 def bprint(*args, **kwargs):
       
    29     print(*[_maybesysstr(a) for a in args],
       
    30           **{k: _maybesysstr(v) for k, v in kwargs.items()})
       
    31     # avoid awkward interleaving with ui object's output
       
    32     sys.stdout.flush()
       
    33 
       
    34 def testui(user=b'foo', group=b'bar', tusers=(), tgroups=(),
       
    35            cuser=b'foo', cgroup=b'bar', debug=False, silent=False,
    21            report=True):
    36            report=True):
    22     # user, group => owners of the file
    37     # user, group => owners of the file
    23     # tusers, tgroups => trusted users/groups
    38     # tusers, tgroups => trusted users/groups
    24     # cuser, cgroup => user/group of the current process
    39     # cuser, cgroup => user/group of the current process
    25 
    40 
    26     # write a global hgrc with the list of trusted users/groups and
    41     # write a global hgrc with the list of trusted users/groups and
    27     # some setting so that we can be sure it was read
    42     # some setting so that we can be sure it was read
    28     f = open(hgrc, 'w')
    43     f = open(hgrc, 'wb')
    29     f.write(basehgrc)
    44     f.write(basehgrc)
    30     f.write('\n[paths]\n')
    45     f.write(b'\n[paths]\n')
    31     f.write('global = /some/path\n\n')
    46     f.write(b'global = /some/path\n\n')
    32 
    47 
    33     if tusers or tgroups:
    48     if tusers or tgroups:
    34         f.write('[trusted]\n')
    49         f.write(b'[trusted]\n')
    35         if tusers:
    50         if tusers:
    36             f.write('users = %s\n' % ', '.join(tusers))
    51             f.write(b'users = %s\n' % b', '.join(tusers))
    37         if tgroups:
    52         if tgroups:
    38             f.write('groups = %s\n' % ', '.join(tgroups))
    53             f.write(b'groups = %s\n' % b', '.join(tgroups))
    39     f.close()
    54     f.close()
    40 
    55 
    41     # override the functions that give names to uids and gids
    56     # override the functions that give names to uids and gids
    42     def username(uid=None):
    57     def username(uid=None):
    43         if uid is None:
    58         if uid is None:
    45         return user
    60         return user
    46     util.username = username
    61     util.username = username
    47 
    62 
    48     def groupname(gid=None):
    63     def groupname(gid=None):
    49         if gid is None:
    64         if gid is None:
    50             return 'bar'
    65             return b'bar'
    51         return group
    66         return group
    52     util.groupname = groupname
    67     util.groupname = groupname
    53 
    68 
    54     def isowner(st):
    69     def isowner(st):
    55         return user == cuser
    70         return user == cuser
    56     util.isowner = isowner
    71     util.isowner = isowner
    57 
    72 
    58     # try to read everything
    73     # try to read everything
    59     #print '# File belongs to user %s, group %s' % (user, group)
    74     #print '# File belongs to user %s, group %s' % (user, group)
    60     #print '# trusted users = %s; trusted groups = %s' % (tusers, tgroups)
    75     #print '# trusted users = %s; trusted groups = %s' % (tusers, tgroups)
    61     kind = ('different', 'same')
    76     kind = (b'different', b'same')
    62     who = ('', 'user', 'group', 'user and the group')
    77     who = (b'', b'user', b'group', b'user and the group')
    63     trusted = who[(user in tusers) + 2*(group in tgroups)]
    78     trusted = who[(user in tusers) + 2*(group in tgroups)]
    64     if trusted:
    79     if trusted:
    65         trusted = ', but we trust the ' + trusted
    80         trusted = b', but we trust the ' + trusted
    66     print('# %s user, %s group%s' % (kind[user == cuser], kind[group == cgroup],
    81     bprint(b'# %s user, %s group%s' % (kind[user == cuser],
    67                                      trusted))
    82                                        kind[group == cgroup],
       
    83                                        trusted))
    68 
    84 
    69     u = uimod.ui.load()
    85     u = uimod.ui.load()
    70     # disable the configuration registration warning
    86     # disable the configuration registration warning
    71     #
    87     #
    72     # the purpose of this test is to check the old behavior, not to validate the
    88     # the purpose of this test is to check the old behavior, not to validate the
    73     # behavior from registered item. so we silent warning related to unregisted
    89     # behavior from registered item. so we silent warning related to unregisted
    74     # config.
    90     # config.
    75     u.setconfig('devel', 'warn-config-unknown', False, 'test')
    91     u.setconfig(b'devel', b'warn-config-unknown', False, b'test')
    76     u.setconfig('devel', 'all-warnings', False, 'test')
    92     u.setconfig(b'devel', b'all-warnings', False, b'test')
    77     u.setconfig('ui', 'debug', str(bool(debug)))
    93     u.setconfig(b'ui', b'debug', pycompat.bytestr(bool(debug)))
    78     u.setconfig('ui', 'report_untrusted', str(bool(report)))
    94     u.setconfig(b'ui', b'report_untrusted', pycompat.bytestr(bool(report)))
    79     u.readconfig('.hg/hgrc')
    95     u.readconfig(b'.hg/hgrc')
    80     if silent:
    96     if silent:
    81         return u
    97         return u
    82     print('trusted')
    98     bprint(b'trusted')
    83     for name, path in u.configitems('paths'):
    99     for name, path in u.configitems(b'paths'):
    84         print('   ', name, '=', util.pconvert(path))
   100         bprint(b'   ', name, b'=', util.pconvert(path))
    85     print('untrusted')
   101     bprint(b'untrusted')
    86     for name, path in u.configitems('paths', untrusted=True):
   102     for name, path in u.configitems(b'paths', untrusted=True):
    87         print('.', end=' ')
   103         bprint(b'.', end=b' ')
    88         u.config('paths', name) # warning with debug=True
   104         u.config(b'paths', name) # warning with debug=True
    89         print('.', end=' ')
   105         bprint(b'.', end=b' ')
    90         u.config('paths', name, untrusted=True) # no warnings
   106         u.config(b'paths', name, untrusted=True) # no warnings
    91         print(name, '=', util.pconvert(path))
   107         bprint(name, b'=', util.pconvert(path))
    92     print()
   108     print()
    93 
   109 
    94     return u
   110     return u
    95 
   111 
    96 os.mkdir('repo')
   112 os.mkdir(b'repo')
    97 os.chdir('repo')
   113 os.chdir(b'repo')
    98 os.mkdir('.hg')
   114 os.mkdir(b'.hg')
    99 f = open('.hg/hgrc', 'w')
   115 f = open(b'.hg/hgrc', 'wb')
   100 f.write('[paths]\n')
   116 f.write(b'[paths]\n')
   101 f.write('local = /another/path\n\n')
   117 f.write(b'local = /another/path\n\n')
   102 f.close()
   118 f.close()
   103 
   119 
   104 #print '# Everything is run by user foo, group bar\n'
   120 #print '# Everything is run by user foo, group bar\n'
   105 
   121 
   106 # same user, same group
   122 # same user, same group
   107 testui()
   123 testui()
   108 # same user, different group
   124 # same user, different group
   109 testui(group='def')
   125 testui(group=b'def')
   110 # different user, same group
   126 # different user, same group
   111 testui(user='abc')
   127 testui(user=b'abc')
   112 # ... but we trust the group
   128 # ... but we trust the group
   113 testui(user='abc', tgroups=['bar'])
   129 testui(user=b'abc', tgroups=[b'bar'])
   114 # different user, different group
   130 # different user, different group
   115 testui(user='abc', group='def')
   131 testui(user=b'abc', group=b'def')
   116 # ... but we trust the user
   132 # ... but we trust the user
   117 testui(user='abc', group='def', tusers=['abc'])
   133 testui(user=b'abc', group=b'def', tusers=[b'abc'])
   118 # ... but we trust the group
   134 # ... but we trust the group
   119 testui(user='abc', group='def', tgroups=['def'])
   135 testui(user=b'abc', group=b'def', tgroups=[b'def'])
   120 # ... but we trust the user and the group
   136 # ... but we trust the user and the group
   121 testui(user='abc', group='def', tusers=['abc'], tgroups=['def'])
   137 testui(user=b'abc', group=b'def', tusers=[b'abc'], tgroups=[b'def'])
   122 # ... but we trust all users
   138 # ... but we trust all users
   123 print('# we trust all users')
   139 bprint(b'# we trust all users')
   124 testui(user='abc', group='def', tusers=['*'])
   140 testui(user=b'abc', group=b'def', tusers=[b'*'])
   125 # ... but we trust all groups
   141 # ... but we trust all groups
   126 print('# we trust all groups')
   142 bprint(b'# we trust all groups')
   127 testui(user='abc', group='def', tgroups=['*'])
   143 testui(user=b'abc', group=b'def', tgroups=[b'*'])
   128 # ... but we trust the whole universe
   144 # ... but we trust the whole universe
   129 print('# we trust all users and groups')
   145 bprint(b'# we trust all users and groups')
   130 testui(user='abc', group='def', tusers=['*'], tgroups=['*'])
   146 testui(user=b'abc', group=b'def', tusers=[b'*'], tgroups=[b'*'])
   131 # ... check that users and groups are in different namespaces
   147 # ... check that users and groups are in different namespaces
   132 print("# we don't get confused by users and groups with the same name")
   148 bprint(b"# we don't get confused by users and groups with the same name")
   133 testui(user='abc', group='def', tusers=['def'], tgroups=['abc'])
   149 testui(user=b'abc', group=b'def', tusers=[b'def'], tgroups=[b'abc'])
   134 # ... lists of user names work
   150 # ... lists of user names work
   135 print("# list of user names")
   151 bprint(b"# list of user names")
   136 testui(user='abc', group='def', tusers=['foo', 'xyz', 'abc', 'bleh'],
   152 testui(user=b'abc', group=b'def', tusers=[b'foo', b'xyz', b'abc', b'bleh'],
   137        tgroups=['bar', 'baz', 'qux'])
   153        tgroups=[b'bar', b'baz', b'qux'])
   138 # ... lists of group names work
   154 # ... lists of group names work
   139 print("# list of group names")
   155 bprint(b"# list of group names")
   140 testui(user='abc', group='def', tusers=['foo', 'xyz', 'bleh'],
   156 testui(user=b'abc', group=b'def', tusers=[b'foo', b'xyz', b'bleh'],
   141        tgroups=['bar', 'def', 'baz', 'qux'])
   157        tgroups=[b'bar', b'def', b'baz', b'qux'])
   142 
   158 
   143 print("# Can't figure out the name of the user running this process")
   159 bprint(b"# Can't figure out the name of the user running this process")
   144 testui(user='abc', group='def', cuser=None)
   160 testui(user=b'abc', group=b'def', cuser=None)
   145 
   161 
   146 print("# prints debug warnings")
   162 bprint(b"# prints debug warnings")
   147 u = testui(user='abc', group='def', cuser='foo', debug=True)
   163 u = testui(user=b'abc', group=b'def', cuser=b'foo', debug=True)
   148 
   164 
   149 print("# report_untrusted enabled without debug hides warnings")
   165 bprint(b"# report_untrusted enabled without debug hides warnings")
   150 u = testui(user='abc', group='def', cuser='foo', report=False)
   166 u = testui(user=b'abc', group=b'def', cuser=b'foo', report=False)
   151 
   167 
   152 print("# report_untrusted enabled with debug shows warnings")
   168 bprint(b"# report_untrusted enabled with debug shows warnings")
   153 u = testui(user='abc', group='def', cuser='foo', debug=True, report=False)
   169 u = testui(user=b'abc', group=b'def', cuser=b'foo', debug=True, report=False)
   154 
   170 
   155 print("# ui.readconfig sections")
   171 bprint(b"# ui.readconfig sections")
   156 filename = 'foobar'
   172 filename = b'foobar'
   157 f = open(filename, 'w')
   173 f = open(filename, 'wb')
   158 f.write('[foobar]\n')
   174 f.write(b'[foobar]\n')
   159 f.write('baz = quux\n')
   175 f.write(b'baz = quux\n')
   160 f.close()
   176 f.close()
   161 u.readconfig(filename, sections=['foobar'])
   177 u.readconfig(filename, sections=[b'foobar'])
   162 print(u.config('foobar', 'baz'))
   178 bprint(u.config(b'foobar', b'baz'))
   163 
   179 
   164 print()
   180 print()
   165 print("# read trusted, untrusted, new ui, trusted")
   181 bprint(b"# read trusted, untrusted, new ui, trusted")
   166 u = uimod.ui.load()
   182 u = uimod.ui.load()
   167 # disable the configuration registration warning
   183 # disable the configuration registration warning
   168 #
   184 #
   169 # the purpose of this test is to check the old behavior, not to validate the
   185 # the purpose of this test is to check the old behavior, not to validate the
   170 # behavior from registered item. so we silent warning related to unregisted
   186 # behavior from registered item. so we silent warning related to unregisted
   171 # config.
   187 # config.
   172 u.setconfig('devel', 'warn-config-unknown', False, 'test')
   188 u.setconfig(b'devel', b'warn-config-unknown', False, b'test')
   173 u.setconfig('devel', 'all-warnings', False, 'test')
   189 u.setconfig(b'devel', b'all-warnings', False, b'test')
   174 u.setconfig('ui', 'debug', 'on')
   190 u.setconfig(b'ui', b'debug', b'on')
   175 u.readconfig(filename)
   191 u.readconfig(filename)
   176 u2 = u.copy()
   192 u2 = u.copy()
   177 def username(uid=None):
   193 def username(uid=None):
   178     return 'foo'
   194     return b'foo'
   179 util.username = username
   195 util.username = username
   180 u2.readconfig('.hg/hgrc')
   196 u2.readconfig(b'.hg/hgrc')
   181 print('trusted:')
   197 bprint(b'trusted:')
   182 print(u2.config('foobar', 'baz'))
   198 bprint(u2.config(b'foobar', b'baz'))
   183 print('untrusted:')
   199 bprint(b'untrusted:')
   184 print(u2.config('foobar', 'baz', untrusted=True))
   200 bprint(u2.config(b'foobar', b'baz', untrusted=True))
   185 
   201 
   186 print()
   202 print()
   187 print("# error handling")
   203 bprint(b"# error handling")
   188 
   204 
   189 def assertraises(f, exc=error.Abort):
   205 def assertraises(f, exc=error.Abort):
   190     try:
   206     try:
   191         f()
   207         f()
   192     except exc as inst:
   208     except exc as inst:
   193         print('raised', inst.__class__.__name__)
   209         bprint(b'raised', inst.__class__.__name__)
   194     else:
   210     else:
   195         print('no exception?!')
   211         bprint(b'no exception?!')
   196 
   212 
   197 print("# file doesn't exist")
   213 bprint(b"# file doesn't exist")
   198 os.unlink('.hg/hgrc')
   214 os.unlink(b'.hg/hgrc')
   199 assert not os.path.exists('.hg/hgrc')
   215 assert not os.path.exists(b'.hg/hgrc')
   200 testui(debug=True, silent=True)
   216 testui(debug=True, silent=True)
   201 testui(user='abc', group='def', debug=True, silent=True)
   217 testui(user=b'abc', group=b'def', debug=True, silent=True)
   202 
   218 
   203 print()
   219 print()
   204 print("# parse error")
   220 bprint(b"# parse error")
   205 f = open('.hg/hgrc', 'w')
   221 f = open(b'.hg/hgrc', 'wb')
   206 f.write('foo')
   222 f.write(b'foo')
   207 f.close()
   223 f.close()
   208 
   224 
   209 try:
   225 try:
   210     testui(user='abc', group='def', silent=True)
   226     testui(user=b'abc', group=b'def', silent=True)
   211 except error.ParseError as inst:
   227 except error.ParseError as inst:
   212     print(inst)
   228     bprint(inst)
   213 
   229 
   214 try:
   230 try:
   215     testui(debug=True, silent=True)
   231     testui(debug=True, silent=True)
   216 except error.ParseError as inst:
   232 except error.ParseError as inst:
   217     print(inst)
   233     bprint(inst)
   218 
   234 
   219 print()
   235 print()
   220 print('# access typed information')
   236 bprint(b'# access typed information')
   221 with open('.hg/hgrc', 'w') as f:
   237 with open(b'.hg/hgrc', 'wb') as f:
   222     f.write('''\
   238     f.write(b'''\
   223 [foo]
   239 [foo]
   224 sub=main
   240 sub=main
   225 sub:one=one
   241 sub:one=one
   226 sub:two=two
   242 sub:two=two
   227 path=monty/python
   243 path=monty/python
   228 bool=true
   244 bool=true
   229 int=42
   245 int=42
   230 bytes=81mb
   246 bytes=81mb
   231 list=spam,ham,eggs
   247 list=spam,ham,eggs
   232 ''')
   248 ''')
   233 u = testui(user='abc', group='def', cuser='foo', silent=True)
   249 u = testui(user=b'abc', group=b'def', cuser=b'foo', silent=True)
   234 def configpath(section, name, default=None, untrusted=False):
   250 def configpath(section, name, default=None, untrusted=False):
   235     path = u.configpath(section, name, default, untrusted)
   251     path = u.configpath(section, name, default, untrusted)
   236     if path is None:
   252     if path is None:
   237         return None
   253         return None
   238     return util.pconvert(path)
   254     return util.pconvert(path)
   239 
   255 
   240 print('# suboptions, trusted and untrusted')
   256 bprint(b'# suboptions, trusted and untrusted')
   241 trusted = u.configsuboptions('foo', 'sub')
   257 trusted = u.configsuboptions(b'foo', b'sub')
   242 untrusted = u.configsuboptions('foo', 'sub', untrusted=True)
   258 untrusted = u.configsuboptions(b'foo', b'sub', untrusted=True)
   243 print(
   259 bprint(
   244     (trusted[0], sorted(trusted[1].items())),
   260     (trusted[0], sorted(trusted[1].items())),
   245     (untrusted[0], sorted(untrusted[1].items())))
   261     (untrusted[0], sorted(untrusted[1].items())))
   246 print('# path, trusted and untrusted')
   262 bprint(b'# path, trusted and untrusted')
   247 print(configpath('foo', 'path'), configpath('foo', 'path', untrusted=True))
   263 bprint(configpath(b'foo', b'path'), configpath(b'foo', b'path', untrusted=True))
   248 print('# bool, trusted and untrusted')
   264 bprint(b'# bool, trusted and untrusted')
   249 print(u.configbool('foo', 'bool'), u.configbool('foo', 'bool', untrusted=True))
   265 bprint(u.configbool(b'foo', b'bool'),
   250 print('# int, trusted and untrusted')
   266        u.configbool(b'foo', b'bool', untrusted=True))
   251 print(
   267 bprint(b'# int, trusted and untrusted')
   252     u.configint('foo', 'int', 0),
   268 bprint(
   253     u.configint('foo', 'int', 0, untrusted=True))
   269     u.configint(b'foo', b'int', 0),
   254 print('# bytes, trusted and untrusted')
   270     u.configint(b'foo', b'int', 0, untrusted=True))
   255 print(
   271 bprint(b'# bytes, trusted and untrusted')
   256     u.configbytes('foo', 'bytes', 0),
   272 bprint(
   257     u.configbytes('foo', 'bytes', 0, untrusted=True))
   273     u.configbytes(b'foo', b'bytes', 0),
   258 print('# list, trusted and untrusted')
   274     u.configbytes(b'foo', b'bytes', 0, untrusted=True))
   259 print(
   275 bprint(b'# list, trusted and untrusted')
   260     u.configlist('foo', 'list', []),
   276 bprint(
   261     u.configlist('foo', 'list', [], untrusted=True))
   277     u.configlist(b'foo', b'list', []),
       
   278     u.configlist(b'foo', b'list', [], untrusted=True))