tests/test-trusted.py
changeset 28934 c4040a35b5d9
parent 28913 b4048ce003fb
child 30559 d83ca854fa21
equal deleted inserted replaced
28933:6262f0215d08 28934:c4040a35b5d9
     1 # Since it's not easy to write a test that portably deals
     1 # Since it's not easy to write a test that portably deals
     2 # with files from different users/groups, we cheat a bit by
     2 # with files from different users/groups, we cheat a bit by
     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
     5 from __future__ import absolute_import, print_function
     6 
     6 
     7 import os
     7 import os
     8 from mercurial import (
     8 from mercurial import (
     9     error,
     9     error,
    10     ui as uimod,
    10     ui as uimod,
    61     kind = ('different', 'same')
    61     kind = ('different', 'same')
    62     who = ('', 'user', 'group', 'user and the group')
    62     who = ('', 'user', 'group', 'user and the group')
    63     trusted = who[(user in tusers) + 2*(group in tgroups)]
    63     trusted = who[(user in tusers) + 2*(group in tgroups)]
    64     if trusted:
    64     if trusted:
    65         trusted = ', but we trust the ' + trusted
    65         trusted = ', but we trust the ' + trusted
    66     print '# %s user, %s group%s' % (kind[user == cuser], kind[group == cgroup],
    66     print('# %s user, %s group%s' % (kind[user == cuser], kind[group == cgroup],
    67                                      trusted)
    67                                      trusted))
    68 
    68 
    69     u = uimod.ui()
    69     u = uimod.ui()
    70     u.setconfig('ui', 'debug', str(bool(debug)))
    70     u.setconfig('ui', 'debug', str(bool(debug)))
    71     u.setconfig('ui', 'report_untrusted', str(bool(report)))
    71     u.setconfig('ui', 'report_untrusted', str(bool(report)))
    72     u.readconfig('.hg/hgrc')
    72     u.readconfig('.hg/hgrc')
    73     if silent:
    73     if silent:
    74         return u
    74         return u
    75     print 'trusted'
    75     print('trusted')
    76     for name, path in u.configitems('paths'):
    76     for name, path in u.configitems('paths'):
    77         print '   ', name, '=', path
    77         print('   ', name, '=', path)
    78     print 'untrusted'
    78     print('untrusted')
    79     for name, path in u.configitems('paths', untrusted=True):
    79     for name, path in u.configitems('paths', untrusted=True):
    80         print '.',
    80         print('.', end=' ')
    81         u.config('paths', name) # warning with debug=True
    81         u.config('paths', name) # warning with debug=True
    82         print '.',
    82         print('.', end=' ')
    83         u.config('paths', name, untrusted=True) # no warnings
    83         u.config('paths', name, untrusted=True) # no warnings
    84         print name, '=', path
    84         print(name, '=', path)
    85     print
    85     print()
    86 
    86 
    87     return u
    87     return u
    88 
    88 
    89 os.mkdir('repo')
    89 os.mkdir('repo')
    90 os.chdir('repo')
    90 os.chdir('repo')
   111 # ... but we trust the group
   111 # ... but we trust the group
   112 testui(user='abc', group='def', tgroups=['def'])
   112 testui(user='abc', group='def', tgroups=['def'])
   113 # ... but we trust the user and the group
   113 # ... but we trust the user and the group
   114 testui(user='abc', group='def', tusers=['abc'], tgroups=['def'])
   114 testui(user='abc', group='def', tusers=['abc'], tgroups=['def'])
   115 # ... but we trust all users
   115 # ... but we trust all users
   116 print '# we trust all users'
   116 print('# we trust all users')
   117 testui(user='abc', group='def', tusers=['*'])
   117 testui(user='abc', group='def', tusers=['*'])
   118 # ... but we trust all groups
   118 # ... but we trust all groups
   119 print '# we trust all groups'
   119 print('# we trust all groups')
   120 testui(user='abc', group='def', tgroups=['*'])
   120 testui(user='abc', group='def', tgroups=['*'])
   121 # ... but we trust the whole universe
   121 # ... but we trust the whole universe
   122 print '# we trust all users and groups'
   122 print('# we trust all users and groups')
   123 testui(user='abc', group='def', tusers=['*'], tgroups=['*'])
   123 testui(user='abc', group='def', tusers=['*'], tgroups=['*'])
   124 # ... check that users and groups are in different namespaces
   124 # ... check that users and groups are in different namespaces
   125 print "# we don't get confused by users and groups with the same name"
   125 print("# we don't get confused by users and groups with the same name")
   126 testui(user='abc', group='def', tusers=['def'], tgroups=['abc'])
   126 testui(user='abc', group='def', tusers=['def'], tgroups=['abc'])
   127 # ... lists of user names work
   127 # ... lists of user names work
   128 print "# list of user names"
   128 print("# list of user names")
   129 testui(user='abc', group='def', tusers=['foo', 'xyz', 'abc', 'bleh'],
   129 testui(user='abc', group='def', tusers=['foo', 'xyz', 'abc', 'bleh'],
   130        tgroups=['bar', 'baz', 'qux'])
   130        tgroups=['bar', 'baz', 'qux'])
   131 # ... lists of group names work
   131 # ... lists of group names work
   132 print "# list of group names"
   132 print("# list of group names")
   133 testui(user='abc', group='def', tusers=['foo', 'xyz', 'bleh'],
   133 testui(user='abc', group='def', tusers=['foo', 'xyz', 'bleh'],
   134        tgroups=['bar', 'def', 'baz', 'qux'])
   134        tgroups=['bar', 'def', 'baz', 'qux'])
   135 
   135 
   136 print "# Can't figure out the name of the user running this process"
   136 print("# Can't figure out the name of the user running this process")
   137 testui(user='abc', group='def', cuser=None)
   137 testui(user='abc', group='def', cuser=None)
   138 
   138 
   139 print "# prints debug warnings"
   139 print("# prints debug warnings")
   140 u = testui(user='abc', group='def', cuser='foo', debug=True)
   140 u = testui(user='abc', group='def', cuser='foo', debug=True)
   141 
   141 
   142 print "# report_untrusted enabled without debug hides warnings"
   142 print("# report_untrusted enabled without debug hides warnings")
   143 u = testui(user='abc', group='def', cuser='foo', report=False)
   143 u = testui(user='abc', group='def', cuser='foo', report=False)
   144 
   144 
   145 print "# report_untrusted enabled with debug shows warnings"
   145 print("# report_untrusted enabled with debug shows warnings")
   146 u = testui(user='abc', group='def', cuser='foo', debug=True, report=False)
   146 u = testui(user='abc', group='def', cuser='foo', debug=True, report=False)
   147 
   147 
   148 print "# ui.readconfig sections"
   148 print("# ui.readconfig sections")
   149 filename = 'foobar'
   149 filename = 'foobar'
   150 f = open(filename, 'w')
   150 f = open(filename, 'w')
   151 f.write('[foobar]\n')
   151 f.write('[foobar]\n')
   152 f.write('baz = quux\n')
   152 f.write('baz = quux\n')
   153 f.close()
   153 f.close()
   154 u.readconfig(filename, sections=['foobar'])
   154 u.readconfig(filename, sections=['foobar'])
   155 print u.config('foobar', 'baz')
   155 print(u.config('foobar', 'baz'))
   156 
   156 
   157 print
   157 print()
   158 print "# read trusted, untrusted, new ui, trusted"
   158 print("# read trusted, untrusted, new ui, trusted")
   159 u = uimod.ui()
   159 u = uimod.ui()
   160 u.setconfig('ui', 'debug', 'on')
   160 u.setconfig('ui', 'debug', 'on')
   161 u.readconfig(filename)
   161 u.readconfig(filename)
   162 u2 = u.copy()
   162 u2 = u.copy()
   163 def username(uid=None):
   163 def username(uid=None):
   164     return 'foo'
   164     return 'foo'
   165 util.username = username
   165 util.username = username
   166 u2.readconfig('.hg/hgrc')
   166 u2.readconfig('.hg/hgrc')
   167 print 'trusted:'
   167 print('trusted:')
   168 print u2.config('foobar', 'baz')
   168 print(u2.config('foobar', 'baz'))
   169 print 'untrusted:'
   169 print('untrusted:')
   170 print u2.config('foobar', 'baz', untrusted=True)
   170 print(u2.config('foobar', 'baz', untrusted=True))
   171 
   171 
   172 print
   172 print()
   173 print "# error handling"
   173 print("# error handling")
   174 
   174 
   175 def assertraises(f, exc=error.Abort):
   175 def assertraises(f, exc=error.Abort):
   176     try:
   176     try:
   177         f()
   177         f()
   178     except exc as inst:
   178     except exc as inst:
   179         print 'raised', inst.__class__.__name__
   179         print('raised', inst.__class__.__name__)
   180     else:
   180     else:
   181         print 'no exception?!'
   181         print('no exception?!')
   182 
   182 
   183 print "# file doesn't exist"
   183 print("# file doesn't exist")
   184 os.unlink('.hg/hgrc')
   184 os.unlink('.hg/hgrc')
   185 assert not os.path.exists('.hg/hgrc')
   185 assert not os.path.exists('.hg/hgrc')
   186 testui(debug=True, silent=True)
   186 testui(debug=True, silent=True)
   187 testui(user='abc', group='def', debug=True, silent=True)
   187 testui(user='abc', group='def', debug=True, silent=True)
   188 
   188 
   189 print
   189 print()
   190 print "# parse error"
   190 print("# parse error")
   191 f = open('.hg/hgrc', 'w')
   191 f = open('.hg/hgrc', 'w')
   192 f.write('foo')
   192 f.write('foo')
   193 f.close()
   193 f.close()
   194 
   194 
   195 try:
   195 try:
   196     testui(user='abc', group='def', silent=True)
   196     testui(user='abc', group='def', silent=True)
   197 except error.ParseError as inst:
   197 except error.ParseError as inst:
   198     print inst
   198     print(inst)
   199 
   199 
   200 try:
   200 try:
   201     testui(debug=True, silent=True)
   201     testui(debug=True, silent=True)
   202 except error.ParseError as inst:
   202 except error.ParseError as inst:
   203     print inst
   203     print(inst)