PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/test-trusted.py

https://bitbucket.org/mirror/mercurial/
Python | 197 lines | 144 code | 26 blank | 27 comment | 17 complexity | 7605b9747fcbe59108528e1592933e74 MD5 | raw file
Possible License(s): GPL-2.0
  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. import os
  5. from mercurial import ui, util, error
  6. hgrc = os.environ['HGRCPATH']
  7. f = open(hgrc)
  8. basehgrc = f.read()
  9. f.close()
  10. def testui(user='foo', group='bar', tusers=(), tgroups=(),
  11. cuser='foo', cgroup='bar', debug=False, silent=False,
  12. report=True):
  13. # user, group => owners of the file
  14. # tusers, tgroups => trusted users/groups
  15. # cuser, cgroup => user/group of the current process
  16. # write a global hgrc with the list of trusted users/groups and
  17. # some setting so that we can be sure it was read
  18. f = open(hgrc, 'w')
  19. f.write(basehgrc)
  20. f.write('\n[paths]\n')
  21. f.write('global = /some/path\n\n')
  22. if tusers or tgroups:
  23. f.write('[trusted]\n')
  24. if tusers:
  25. f.write('users = %s\n' % ', '.join(tusers))
  26. if tgroups:
  27. f.write('groups = %s\n' % ', '.join(tgroups))
  28. f.close()
  29. # override the functions that give names to uids and gids
  30. def username(uid=None):
  31. if uid is None:
  32. return cuser
  33. return user
  34. util.username = username
  35. def groupname(gid=None):
  36. if gid is None:
  37. return 'bar'
  38. return group
  39. util.groupname = groupname
  40. def isowner(st):
  41. return user == cuser
  42. util.isowner = isowner
  43. # try to read everything
  44. #print '# File belongs to user %s, group %s' % (user, group)
  45. #print '# trusted users = %s; trusted groups = %s' % (tusers, tgroups)
  46. kind = ('different', 'same')
  47. who = ('', 'user', 'group', 'user and the group')
  48. trusted = who[(user in tusers) + 2*(group in tgroups)]
  49. if trusted:
  50. trusted = ', but we trust the ' + trusted
  51. print '# %s user, %s group%s' % (kind[user == cuser], kind[group == cgroup],
  52. trusted)
  53. u = ui.ui()
  54. u.setconfig('ui', 'debug', str(bool(debug)))
  55. u.setconfig('ui', 'report_untrusted', str(bool(report)))
  56. u.readconfig('.hg/hgrc')
  57. if silent:
  58. return u
  59. print 'trusted'
  60. for name, path in u.configitems('paths'):
  61. print ' ', name, '=', path
  62. print 'untrusted'
  63. for name, path in u.configitems('paths', untrusted=True):
  64. print '.',
  65. u.config('paths', name) # warning with debug=True
  66. print '.',
  67. u.config('paths', name, untrusted=True) # no warnings
  68. print name, '=', path
  69. print
  70. return u
  71. os.mkdir('repo')
  72. os.chdir('repo')
  73. os.mkdir('.hg')
  74. f = open('.hg/hgrc', 'w')
  75. f.write('[paths]\n')
  76. f.write('local = /another/path\n\n')
  77. f.close()
  78. #print '# Everything is run by user foo, group bar\n'
  79. # same user, same group
  80. testui()
  81. # same user, different group
  82. testui(group='def')
  83. # different user, same group
  84. testui(user='abc')
  85. # ... but we trust the group
  86. testui(user='abc', tgroups=['bar'])
  87. # different user, different group
  88. testui(user='abc', group='def')
  89. # ... but we trust the user
  90. testui(user='abc', group='def', tusers=['abc'])
  91. # ... but we trust the group
  92. testui(user='abc', group='def', tgroups=['def'])
  93. # ... but we trust the user and the group
  94. testui(user='abc', group='def', tusers=['abc'], tgroups=['def'])
  95. # ... but we trust all users
  96. print '# we trust all users'
  97. testui(user='abc', group='def', tusers=['*'])
  98. # ... but we trust all groups
  99. print '# we trust all groups'
  100. testui(user='abc', group='def', tgroups=['*'])
  101. # ... but we trust the whole universe
  102. print '# we trust all users and groups'
  103. testui(user='abc', group='def', tusers=['*'], tgroups=['*'])
  104. # ... check that users and groups are in different namespaces
  105. print "# we don't get confused by users and groups with the same name"
  106. testui(user='abc', group='def', tusers=['def'], tgroups=['abc'])
  107. # ... lists of user names work
  108. print "# list of user names"
  109. testui(user='abc', group='def', tusers=['foo', 'xyz', 'abc', 'bleh'],
  110. tgroups=['bar', 'baz', 'qux'])
  111. # ... lists of group names work
  112. print "# list of group names"
  113. testui(user='abc', group='def', tusers=['foo', 'xyz', 'bleh'],
  114. tgroups=['bar', 'def', 'baz', 'qux'])
  115. print "# Can't figure out the name of the user running this process"
  116. testui(user='abc', group='def', cuser=None)
  117. print "# prints debug warnings"
  118. u = testui(user='abc', group='def', cuser='foo', debug=True)
  119. print "# report_untrusted enabled without debug hides warnings"
  120. u = testui(user='abc', group='def', cuser='foo', report=False)
  121. print "# report_untrusted enabled with debug shows warnings"
  122. u = testui(user='abc', group='def', cuser='foo', debug=True, report=False)
  123. print "# ui.readconfig sections"
  124. filename = 'foobar'
  125. f = open(filename, 'w')
  126. f.write('[foobar]\n')
  127. f.write('baz = quux\n')
  128. f.close()
  129. u.readconfig(filename, sections=['foobar'])
  130. print u.config('foobar', 'baz')
  131. print
  132. print "# read trusted, untrusted, new ui, trusted"
  133. u = ui.ui()
  134. u.setconfig('ui', 'debug', 'on')
  135. u.readconfig(filename)
  136. u2 = u.copy()
  137. def username(uid=None):
  138. return 'foo'
  139. util.username = username
  140. u2.readconfig('.hg/hgrc')
  141. print 'trusted:'
  142. print u2.config('foobar', 'baz')
  143. print 'untrusted:'
  144. print u2.config('foobar', 'baz', untrusted=True)
  145. print
  146. print "# error handling"
  147. def assertraises(f, exc=util.Abort):
  148. try:
  149. f()
  150. except exc, inst:
  151. print 'raised', inst.__class__.__name__
  152. else:
  153. print 'no exception?!'
  154. print "# file doesn't exist"
  155. os.unlink('.hg/hgrc')
  156. assert not os.path.exists('.hg/hgrc')
  157. testui(debug=True, silent=True)
  158. testui(user='abc', group='def', debug=True, silent=True)
  159. print
  160. print "# parse error"
  161. f = open('.hg/hgrc', 'w')
  162. f.write('foo')
  163. f.close()
  164. try:
  165. testui(user='abc', group='def', silent=True)
  166. except error.ParseError, inst:
  167. print inst
  168. try:
  169. testui(debug=True, silent=True)
  170. except error.ParseError, inst:
  171. print inst