test_unix.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- test-case-name: twisted.conch.test.test_unix -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. from __future__ import absolute_import
  5. from zope.interface import implementer
  6. from twisted.internet.interfaces import IReactorProcess
  7. from twisted.python.reflect import requireModule
  8. from twisted.trial import unittest
  9. from .test_session import StubConnection, StubClient
  10. unix = requireModule('twisted.conch.unix')
  11. @implementer(IReactorProcess)
  12. class MockProcessSpawner(object):
  13. """
  14. An L{IReactorProcess} that logs calls to C{spawnProcess}.
  15. """
  16. def __init__(self):
  17. self._spawnProcessCalls = []
  18. def spawnProcess(self, processProtocol, executable, args=(), env={},
  19. path=None, uid=None, gid=None, usePTY=0, childFDs=None):
  20. """
  21. Log a call to C{spawnProcess}. Do not actually spawn a process.
  22. """
  23. self._spawnProcessCalls.append(
  24. {'processProtocol': processProtocol,
  25. 'executable': executable,
  26. 'args': args,
  27. 'env': env,
  28. 'path': path,
  29. 'uid': uid,
  30. 'gid': gid,
  31. 'usePTY': usePTY,
  32. 'childFDs': childFDs})
  33. class StubUnixConchUser(object):
  34. """
  35. Enough of UnixConchUser to exercise SSHSessionForUnixConchUser in the
  36. tests below.
  37. """
  38. def __init__(self, homeDirectory):
  39. self._homeDirectory = homeDirectory
  40. self.conn = StubConnection(transport=StubClient())
  41. def getUserGroupId(self):
  42. return (None, None)
  43. def getHomeDir(self):
  44. return self._homeDirectory
  45. def getShell(self):
  46. pass
  47. class TestSSHSessionForUnixConchUser(unittest.TestCase):
  48. if unix is None:
  49. skip = "Unix system required"
  50. def testExecCommandEnvironment(self):
  51. """
  52. C{execCommand} sets the C{HOME} environment variable to the avatar's home
  53. directory.
  54. """
  55. mockReactor = MockProcessSpawner()
  56. homeDirectory = "/made/up/path/"
  57. avatar = StubUnixConchUser(homeDirectory)
  58. session = unix.SSHSessionForUnixConchUser(avatar, reactor=mockReactor)
  59. protocol = None
  60. command = ["not-actually-executed"]
  61. session.execCommand(protocol, command)
  62. [call] = mockReactor._spawnProcessCalls
  63. self.assertEqual(homeDirectory, call['env']['HOME'])