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