comparison tests/sshprotoext.py @ 35930:83d67257ba90

tests: add low-level SSH protocol tests We don't really have good low-level tests for the behavior of the SSH wire protocol. This commit attempts to establish some. The added tests consist of a mixture of starting a server with `hg serve --stdio` and sending bytes to it and using `hg debugpeer` to go through the official client code. Having insight into what raw bytes are exchanged as well as what the peer does is useful. We also introduce a test extension for modifying the behavior of the SSH server and peer. For example, we change the server to not recognize the "hello" command, simulating behavior of <0.9.1 servers. These tests are generally useful to have. But the impetus for creating them now is they will be needed for verifying behavior of old clients and servers when a new SSH protocol is introduced. Differential Revision: https://phab.mercurial-scm.org/D2026 # no-check-commit because of serve_forever()
author Gregory Szorc <gregory.szorc@gmail.com>
date Sun, 04 Feb 2018 14:02:41 -0800
parents
children a9cffd14aa04
comparison
equal deleted inserted replaced
35929:5f029d03cf71 35930:83d67257ba90
1 # sshprotoext.py - Extension to test behavior of SSH protocol
2 #
3 # Copyright 2018 Gregory Szorc <gregory.szorc@gmail.com>
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7
8 # This extension replaces the SSH server started via `hg serve --stdio`.
9 # The server behaves differently depending on environment variables.
10
11 from __future__ import absolute_import
12
13 from mercurial import (
14 error,
15 registrar,
16 sshpeer,
17 wireproto,
18 wireprotoserver,
19 )
20
21 configtable = {}
22 configitem = registrar.configitem(configtable)
23
24 configitem('sshpeer', 'mode', default=None)
25 configitem('sshpeer', 'handshake-mode', default=None)
26
27 class bannerserver(wireprotoserver.sshserver):
28 """Server that sends a banner to stdout."""
29 def serve_forever(self):
30 for i in range(10):
31 self._fout.write(b'banner: line %d\n' % i)
32
33 super(bannerserver, self).serve_forever()
34
35 class prehelloserver(wireprotoserver.sshserver):
36 """Tests behavior when connecting to <0.9.1 servers.
37
38 The ``hello`` wire protocol command was introduced in Mercurial
39 0.9.1. Modern clients send the ``hello`` command when connecting
40 to SSH servers. This mock server tests behavior of the handshake
41 when ``hello`` is not supported.
42 """
43 def serve_forever(self):
44 l = self._fin.readline()
45 assert l == b'hello\n'
46 # Respond to unknown commands with an empty reply.
47 self._sendresponse(b'')
48 l = self._fin.readline()
49 assert l == b'between\n'
50 rsp = wireproto.dispatch(self._repo, self, b'between')
51 self._handlers[rsp.__class__](self, rsp)
52
53 super(prehelloserver, self).serve_forever()
54
55 class extrahandshakecommandspeer(sshpeer.sshpeer):
56 """An ssh peer that sends extra commands as part of initial handshake."""
57 # There isn't a good hook point. So we wrap _callstream() and inject
58 # logic when the peer says "hello".
59 def _callstream(self, cmd, **args):
60 if cmd != b'hello':
61 return super(extrahandshakecommandspeer, self)._callstream(cmd,
62 **args)
63
64 mode = self._ui.config(b'sshpeer', b'handshake-mode')
65 if mode == b'pre-no-args':
66 self._callstream(b'no-args')
67 return super(extrahandshakecommandspeer, self)._callstream(
68 cmd, **args)
69 elif mode == b'pre-multiple-no-args':
70 self._callstream(b'unknown1')
71 self._callstream(b'unknown2')
72 self._callstream(b'unknown3')
73 return super(extrahandshakecommandspeer, self)._callstream(
74 cmd, **args)
75 else:
76 raise error.ProgrammingError(b'unknown HANDSHAKECOMMANDMODE: %s' %
77 mode)
78
79 def registercommands():
80 def dummycommand(repo, proto):
81 raise error.ProgrammingError('this should never be called')
82
83 wireproto.wireprotocommand(b'no-args', b'')(dummycommand)
84 wireproto.wireprotocommand(b'unknown1', b'')(dummycommand)
85 wireproto.wireprotocommand(b'unknown2', b'')(dummycommand)
86 wireproto.wireprotocommand(b'unknown3', b'')(dummycommand)
87
88 def extsetup(ui):
89 # It's easier for tests to define the server behavior via environment
90 # variables than config options. This is because `hg serve --stdio`
91 # has to be invoked with a certain form for security reasons and
92 # `dummyssh` can't just add `--config` flags to the command line.
93 servermode = ui.environ.get(b'SSHSERVERMODE')
94
95 if servermode == b'banner':
96 wireprotoserver.sshserver = bannerserver
97 elif servermode == b'no-hello':
98 wireprotoserver.sshserver = prehelloserver
99 elif servermode:
100 raise error.ProgrammingError(b'unknown server mode: %s' % servermode)
101
102 peermode = ui.config(b'sshpeer', b'mode')
103
104 if peermode == b'extra-handshake-commands':
105 sshpeer.sshpeer = extrahandshakecommandspeer
106 registercommands()
107 elif peermode:
108 raise error.ProgrammingError(b'unknown peer mode: %s' % peermode)