tests/test-trusted.py
author Alexis S. L. Carvalho <alexis@cecm.usp.br>
Tue, 22 Aug 2006 20:45:03 -0300
changeset 3013 494521a3f142
permissions -rwxr-xr-x
Only read .hg/hgrc files from trusted users/groups The list of trusted users and groups is specified in the [trusted] section of a hgrc; the current user is always trusted; "*" can be used to trust all users/groups. Global hgrc files are always read. On Windows (and other systems that don't have the pwd and grp modules), all .hg/hgrc files are read.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3013
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     1
#!/usr/bin/env python
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     2
# Since it's not easy to write a test that portably deals
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     3
# with files from different users/groups, we cheat a bit by
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     4
# monkey-patching some functions in the util module
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     5
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     6
import os
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     7
from mercurial import ui, util
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     8
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     9
hgrc = os.environ['HGRCPATH']
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    10
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    11
def testui(user='foo', group='bar', tusers=(), tgroups=(),
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    12
           cuser='foo', cgroup='bar'):
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    13
    # user, group => owners of the file
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    14
    # tusers, tgroups => trusted users/groups
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    15
    # cuser, cgroup => user/group of the current process
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    16
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    17
    # write a global hgrc with the list of trusted users/groups and
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    18
    # some setting so that we can be sure it was read
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    19
    f = open(hgrc, 'w')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    20
    f.write('[paths]\n')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    21
    f.write('global = /some/path\n\n')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    22
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    23
    if tusers or tgroups:
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    24
        f.write('[trusted]\n')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    25
        if tusers:
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    26
            f.write('users = %s\n' % ', '.join(tusers))
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    27
        if tgroups:
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    28
            f.write('groups = %s\n' % ', '.join(tgroups))
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    29
    f.close()
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    30
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    31
    # override the functions that give names to uids and gids
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    32
    def username(uid=None):
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    33
        if uid is None:
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    34
            return cuser
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    35
        return user
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    36
    util.username = username
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    37
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    38
    def groupname(gid=None):
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    39
        if gid is None:
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    40
            return 'bar'
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    41
        return group
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    42
    util.groupname = groupname
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    43
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    44
    # try to read everything
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    45
    #print '# File belongs to user %s, group %s' % (user, group)
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    46
    #print '# trusted users = %s; trusted groups = %s' % (tusers, tgroups)
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    47
    kind = ('different', 'same')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    48
    who = ('', 'user', 'group', 'user and the group')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    49
    trusted = who[(user in tusers) + 2*(group in tgroups)]
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    50
    if trusted:
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    51
        trusted = ', but we trust the ' + trusted
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    52
    print '# %s user, %s group%s' % (kind[user == cuser], kind[group == cgroup],
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    53
                                     trusted)
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    54
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    55
    parentui = ui.ui()
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    56
    u = ui.ui(parentui=parentui)
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    57
    u.readconfig('.hg/hgrc')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    58
    for name, path in u.configitems('paths'):
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    59
        print name, '=', path
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    60
    print
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    61
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    62
    return u
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    63
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    64
os.mkdir('repo')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    65
os.chdir('repo')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    66
os.mkdir('.hg')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    67
f = open('.hg/hgrc', 'w')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    68
f.write('[paths]\n')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    69
f.write('local = /another/path\n\n')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    70
f.close()
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    71
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    72
#print '# Everything is run by user foo, group bar\n'
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    73
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    74
# same user, same group
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    75
testui()
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    76
# same user, different group
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    77
testui(group='def')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    78
# different user, same group
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    79
testui(user='abc')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    80
# ... but we trust the group
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    81
testui(user='abc', tgroups=['bar'])
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    82
# different user, different group
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    83
testui(user='abc', group='def')
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    84
# ... but we trust the user
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    85
testui(user='abc', group='def', tusers=['abc'])
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    86
# ... but we trust the group
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    87
testui(user='abc', group='def', tgroups=['def'])
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    88
# ... but we trust the user and the group
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    89
testui(user='abc', group='def', tusers=['abc'], tgroups=['def'])
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    90
# ... but we trust all users
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    91
print '# we trust all users'
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    92
testui(user='abc', group='def', tusers=['*'])
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    93
# ... but we trust all groups
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    94
print '# we trust all groups'
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    95
testui(user='abc', group='def', tgroups=['*'])
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    96
# ... but we trust the whole universe
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    97
print '# we trust all users and groups'
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    98
testui(user='abc', group='def', tusers=['*'], tgroups=['*'])
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    99
# ... check that users and groups are in different namespaces
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   100
print "# we don't get confused by users and groups with the same name"
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   101
testui(user='abc', group='def', tusers=['def'], tgroups=['abc'])
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   102
# ... lists of user names work
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   103
print "# list of user names"
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   104
testui(user='abc', group='def', tusers=['foo', 'xyz', 'abc', 'bleh'],
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   105
       tgroups=['bar', 'baz', 'qux'])
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   106
# ... lists of group names work
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   107
print "# list of group names"
494521a3f142 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', tusers=['foo', 'xyz', 'bleh'],
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   109
       tgroups=['bar', 'def', 'baz', 'qux'])
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   110
494521a3f142 Only read .hg/hgrc files from trusted users/groups
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
   111
print "# Can't figure out the name of the user running this process"
494521a3f142 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', cuser=None)