tests/test-trusted.py
author Mads Kiilerich <mads@kiilerich.com>
Mon, 11 Sep 2017 00:42:22 +0200
branchstable
changeset 34089 e05e50fbdeaf
parent 31857 08fbc97d1364
child 34858 85a2db47ad50
permissions -rw-r--r--
mq: test coverage of how [diff] configuration influence can break mq patches
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     1
# Since it's not easy to write a test that portably deals
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     2
# with files from different users/groups, we cheat a bit by
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     3
# monkey-patching some functions in the util module
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     4
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
     5
from __future__ import absolute_import, print_function
28913
b4048ce003fb tests: make test-trusted use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26587
diff changeset
     6
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     7
import os
28913
b4048ce003fb tests: make test-trusted use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26587
diff changeset
     8
from mercurial import (
b4048ce003fb tests: make test-trusted use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26587
diff changeset
     9
    error,
b4048ce003fb tests: make test-trusted use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26587
diff changeset
    10
    ui as uimod,
b4048ce003fb tests: make test-trusted use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26587
diff changeset
    11
    util,
b4048ce003fb tests: make test-trusted use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26587
diff changeset
    12
)
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    13
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    14
hgrc = os.environ['HGRCPATH']
5523
5db730475d6d tests/*: avoid losing the original settings from $HGRCPATH
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4516
diff changeset
    15
f = open(hgrc)
5db730475d6d tests/*: avoid losing the original settings from $HGRCPATH
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4516
diff changeset
    16
basehgrc = f.read()
5db730475d6d tests/*: avoid losing the original settings from $HGRCPATH
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4516
diff changeset
    17
f.close()
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    18
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    19
def testui(user='foo', group='bar', tusers=(), tgroups=(),
13493
95b0d4c1c9e1 ui: always report untrusted hgrc files when debug enabled
Ry4an Brase <ry4an-hg@ry4an.org>
parents: 11291
diff changeset
    20
           cuser='foo', cgroup='bar', debug=False, silent=False,
95b0d4c1c9e1 ui: always report untrusted hgrc files when debug enabled
Ry4an Brase <ry4an-hg@ry4an.org>
parents: 11291
diff changeset
    21
           report=True):
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    22
    # user, group => owners of the file
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    23
    # tusers, tgroups => trusted users/groups
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    24
    # cuser, cgroup => user/group of the current process
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    25
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    26
    # write a global hgrc with the list of trusted users/groups and
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    27
    # some setting so that we can be sure it was read
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    28
    f = open(hgrc, 'w')
5523
5db730475d6d tests/*: avoid losing the original settings from $HGRCPATH
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4516
diff changeset
    29
    f.write(basehgrc)
5db730475d6d tests/*: avoid losing the original settings from $HGRCPATH
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4516
diff changeset
    30
    f.write('\n[paths]\n')
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    31
    f.write('global = /some/path\n\n')
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    32
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    33
    if tusers or tgroups:
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    34
        f.write('[trusted]\n')
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    35
        if tusers:
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    36
            f.write('users = %s\n' % ', '.join(tusers))
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    37
        if tgroups:
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    38
            f.write('groups = %s\n' % ', '.join(tgroups))
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    39
    f.close()
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    40
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    41
    # override the functions that give names to uids and gids
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    42
    def username(uid=None):
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    43
        if uid is None:
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    44
            return cuser
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    45
        return user
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    46
    util.username = username
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    47
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    48
    def groupname(gid=None):
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    49
        if gid is None:
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    50
            return 'bar'
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    51
        return group
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    52
    util.groupname = groupname
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    53
8657
3fa92c618624 posix: do not use fstat in isowner
Martin Geisler <mg@lazybytes.net>
parents: 8190
diff changeset
    54
    def isowner(st):
3677
1a0fa3914c46 Avoid looking up usernames if the current user owns the .hgrc file
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3552
diff changeset
    55
        return user == cuser
1a0fa3914c46 Avoid looking up usernames if the current user owns the .hgrc file
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3552
diff changeset
    56
    util.isowner = isowner
1a0fa3914c46 Avoid looking up usernames if the current user owns the .hgrc file
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3552
diff changeset
    57
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    58
    # try to read everything
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    59
    #print '# File belongs to user %s, group %s' % (user, group)
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    60
    #print '# trusted users = %s; trusted groups = %s' % (tusers, tgroups)
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    61
    kind = ('different', 'same')
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    62
    who = ('', 'user', 'group', 'user and the group')
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    63
    trusted = who[(user in tusers) + 2*(group in tgroups)]
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    64
    if trusted:
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    65
        trusted = ', but we trust the ' + trusted
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
    66
    print('# %s user, %s group%s' % (kind[user == cuser], kind[group == cgroup],
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
    67
                                     trusted))
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    68
30559
d83ca854fa21 ui: factor out ui.load() to create a ui without loading configs (API)
Yuya Nishihara <yuya@tcha.org>
parents: 28934
diff changeset
    69
    u = uimod.ui.load()
8190
9b8ac5fb7760 ui: kill most users of parentui name and arg, replace with .copy()
Matt Mackall <mpm@selenic.com>
parents: 8144
diff changeset
    70
    u.setconfig('ui', 'debug', str(bool(debug)))
13493
95b0d4c1c9e1 ui: always report untrusted hgrc files when debug enabled
Ry4an Brase <ry4an-hg@ry4an.org>
parents: 11291
diff changeset
    71
    u.setconfig('ui', 'report_untrusted', str(bool(report)))
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    72
    u.readconfig('.hg/hgrc')
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
    73
    if silent:
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
    74
        return u
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
    75
    print('trusted')
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    76
    for name, path in u.configitems('paths'):
31857
08fbc97d1364 tests: print Unix style paths in *.py tests
Matt Harbison <matt_harbison@yahoo.com>
parents: 31472
diff changeset
    77
        print('   ', name, '=', util.pconvert(path))
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
    78
    print('untrusted')
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
    79
    for name, path in u.configitems('paths', untrusted=True):
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
    80
        print('.', end=' ')
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
    81
        u.config('paths', name) # warning with debug=True
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
    82
        print('.', end=' ')
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
    83
        u.config('paths', name, untrusted=True) # no warnings
31857
08fbc97d1364 tests: print Unix style paths in *.py tests
Matt Harbison <matt_harbison@yahoo.com>
parents: 31472
diff changeset
    84
        print(name, '=', util.pconvert(path))
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
    85
    print()
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    86
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    87
    return u
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    88
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    89
os.mkdir('repo')
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    90
os.chdir('repo')
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    91
os.mkdir('.hg')
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    92
f = open('.hg/hgrc', 'w')
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    93
f.write('[paths]\n')
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    94
f.write('local = /another/path\n\n')
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    95
f.close()
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    96
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    97
#print '# Everything is run by user foo, group bar\n'
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    98
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    99
# same user, same group
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   100
testui()
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   101
# same user, different group
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   102
testui(group='def')
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   103
# different user, same group
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   104
testui(user='abc')
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   105
# ... but we trust the group
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   106
testui(user='abc', tgroups=['bar'])
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   107
# different user, different group
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   108
testui(user='abc', group='def')
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   109
# ... but we trust the user
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   110
testui(user='abc', group='def', tusers=['abc'])
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   111
# ... but we trust the group
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   112
testui(user='abc', group='def', tgroups=['def'])
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   113
# ... but we trust the user and the group
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   114
testui(user='abc', group='def', tusers=['abc'], tgroups=['def'])
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   115
# ... but we trust all users
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   116
print('# we trust all users')
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   117
testui(user='abc', group='def', tusers=['*'])
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   118
# ... but we trust all groups
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   119
print('# we trust all groups')
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   120
testui(user='abc', group='def', tgroups=['*'])
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   121
# ... but we trust the whole universe
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   122
print('# we trust all users and groups')
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   123
testui(user='abc', group='def', tusers=['*'], tgroups=['*'])
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   124
# ... check that users and groups are in different namespaces
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   125
print("# we don't get confused by users and groups with the same name")
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   126
testui(user='abc', group='def', tusers=['def'], tgroups=['abc'])
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   127
# ... lists of user names work
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   128
print("# list of user names")
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   129
testui(user='abc', group='def', tusers=['foo', 'xyz', 'abc', 'bleh'],
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   130
       tgroups=['bar', 'baz', 'qux'])
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   131
# ... lists of group names work
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   132
print("# list of group names")
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   133
testui(user='abc', group='def', tusers=['foo', 'xyz', 'bleh'],
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   134
       tgroups=['bar', 'def', 'baz', 'qux'])
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   135
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   136
print("# Can't figure out the name of the user running this process")
3551
3b07e223534b Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   137
testui(user='abc', group='def', cuser=None)
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   138
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   139
print("# prints debug warnings")
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   140
u = testui(user='abc', group='def', cuser='foo', debug=True)
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   141
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   142
print("# report_untrusted enabled without debug hides warnings")
13493
95b0d4c1c9e1 ui: always report untrusted hgrc files when debug enabled
Ry4an Brase <ry4an-hg@ry4an.org>
parents: 11291
diff changeset
   143
u = testui(user='abc', group='def', cuser='foo', report=False)
95b0d4c1c9e1 ui: always report untrusted hgrc files when debug enabled
Ry4an Brase <ry4an-hg@ry4an.org>
parents: 11291
diff changeset
   144
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   145
print("# report_untrusted enabled with debug shows warnings")
13493
95b0d4c1c9e1 ui: always report untrusted hgrc files when debug enabled
Ry4an Brase <ry4an-hg@ry4an.org>
parents: 11291
diff changeset
   146
u = testui(user='abc', group='def', cuser='foo', debug=True, report=False)
95b0d4c1c9e1 ui: always report untrusted hgrc files when debug enabled
Ry4an Brase <ry4an-hg@ry4an.org>
parents: 11291
diff changeset
   147
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   148
print("# ui.readconfig sections")
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   149
filename = 'foobar'
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   150
f = open(filename, 'w')
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   151
f.write('[foobar]\n')
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   152
f.write('baz = quux\n')
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   153
f.close()
19872
681f7b9213a4 check-code: check for spaces around = for named parameters
Mads Kiilerich <madski@unity3d.com>
parents: 13493
diff changeset
   154
u.readconfig(filename, sections=['foobar'])
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   155
print(u.config('foobar', 'baz'))
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   156
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   157
print()
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   158
print("# read trusted, untrusted, new ui, trusted")
30559
d83ca854fa21 ui: factor out ui.load() to create a ui without loading configs (API)
Yuya Nishihara <yuya@tcha.org>
parents: 28934
diff changeset
   159
u = uimod.ui.load()
8136
6b5522cb2ad2 ui: refactor option setting
Matt Mackall <mpm@selenic.com>
parents: 5523
diff changeset
   160
u.setconfig('ui', 'debug', 'on')
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   161
u.readconfig(filename)
8190
9b8ac5fb7760 ui: kill most users of parentui name and arg, replace with .copy()
Matt Mackall <mpm@selenic.com>
parents: 8144
diff changeset
   162
u2 = u.copy()
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   163
def username(uid=None):
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   164
    return 'foo'
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   165
util.username = username
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   166
u2.readconfig('.hg/hgrc')
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   167
print('trusted:')
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   168
print(u2.config('foobar', 'baz'))
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   169
print('untrusted:')
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   170
print(u2.config('foobar', 'baz', untrusted=True))
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   171
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   172
print()
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   173
print("# error handling")
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   174
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25660
diff changeset
   175
def assertraises(f, exc=error.Abort):
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   176
    try:
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   177
        f()
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19872
diff changeset
   178
    except exc as inst:
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   179
        print('raised', inst.__class__.__name__)
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   180
    else:
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   181
        print('no exception?!')
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   182
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   183
print("# file doesn't exist")
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   184
os.unlink('.hg/hgrc')
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   185
assert not os.path.exists('.hg/hgrc')
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   186
testui(debug=True, silent=True)
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   187
testui(user='abc', group='def', debug=True, silent=True)
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   188
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   189
print()
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   190
print("# parse error")
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   191
f = open('.hg/hgrc', 'w')
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents: 8142
diff changeset
   192
f.write('foo')
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   193
f.close()
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   194
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents: 8142
diff changeset
   195
try:
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents: 8142
diff changeset
   196
    testui(user='abc', group='def', silent=True)
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19872
diff changeset
   197
except error.ParseError as inst:
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   198
    print(inst)
3552
9b52239dc740 save settings from untrusted config files in a separate configparser
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3551
diff changeset
   199
8144
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents: 8142
diff changeset
   200
try:
fca54469480e ui: introduce new config parser
Matt Mackall <mpm@selenic.com>
parents: 8142
diff changeset
   201
    testui(debug=True, silent=True)
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19872
diff changeset
   202
except error.ParseError as inst:
28934
c4040a35b5d9 tests: make test-trusted use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28913
diff changeset
   203
    print(inst)
31472
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   204
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   205
print()
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   206
print('# access typed information')
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   207
with open('.hg/hgrc', 'w') as f:
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   208
    f.write('''\
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   209
[foo]
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   210
sub=main
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   211
sub:one=one
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   212
sub:two=two
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   213
path=monty/python
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   214
bool=true
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   215
int=42
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   216
bytes=81mb
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   217
list=spam,ham,eggs
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   218
''')
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   219
u = testui(user='abc', group='def', cuser='foo', silent=True)
31857
08fbc97d1364 tests: print Unix style paths in *.py tests
Matt Harbison <matt_harbison@yahoo.com>
parents: 31472
diff changeset
   220
def configpath(section, name, default=None, untrusted=False):
08fbc97d1364 tests: print Unix style paths in *.py tests
Matt Harbison <matt_harbison@yahoo.com>
parents: 31472
diff changeset
   221
    path = u.configpath(section, name, default, untrusted)
08fbc97d1364 tests: print Unix style paths in *.py tests
Matt Harbison <matt_harbison@yahoo.com>
parents: 31472
diff changeset
   222
    if path is None:
08fbc97d1364 tests: print Unix style paths in *.py tests
Matt Harbison <matt_harbison@yahoo.com>
parents: 31472
diff changeset
   223
        return None
08fbc97d1364 tests: print Unix style paths in *.py tests
Matt Harbison <matt_harbison@yahoo.com>
parents: 31472
diff changeset
   224
    return util.pconvert(path)
08fbc97d1364 tests: print Unix style paths in *.py tests
Matt Harbison <matt_harbison@yahoo.com>
parents: 31472
diff changeset
   225
31472
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   226
print('# suboptions, trusted and untrusted')
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   227
trusted = u.configsuboptions('foo', 'sub')
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   228
untrusted = u.configsuboptions('foo', 'sub', untrusted=True)
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   229
print(
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   230
    (trusted[0], sorted(trusted[1].items())),
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   231
    (untrusted[0], sorted(untrusted[1].items())))
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   232
print('# path, trusted and untrusted')
31857
08fbc97d1364 tests: print Unix style paths in *.py tests
Matt Harbison <matt_harbison@yahoo.com>
parents: 31472
diff changeset
   233
print(configpath('foo', 'path'), configpath('foo', 'path', untrusted=True))
31472
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   234
print('# bool, trusted and untrusted')
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   235
print(u.configbool('foo', 'bool'), u.configbool('foo', 'bool', untrusted=True))
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   236
print('# int, trusted and untrusted')
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   237
print(
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   238
    u.configint('foo', 'int', 0),
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   239
    u.configint('foo', 'int', 0, untrusted=True))
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   240
print('# bytes, trusted and untrusted')
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   241
print(
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   242
    u.configbytes('foo', 'bytes', 0),
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   243
    u.configbytes('foo', 'bytes', 0, untrusted=True))
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   244
print('# list, trusted and untrusted')
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   245
print(
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   246
    u.configlist('foo', 'list', []),
75e4bae56068 config: honour the trusted flag in ui.configbytes
Martijn Pieters <mjpieters@fb.com>
parents: 30559
diff changeset
   247
    u.configlist('foo', 'list', [], untrusted=True))