Mercurial > public > mercurial-scm > hg-stable
annotate tests/sshprotoext.py @ 35970: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 |
rev | line source |
---|---|
35970
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
1 # sshprotoext.py - Extension to test behavior of SSH protocol |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
2 # |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
3 # Copyright 2018 Gregory Szorc <gregory.szorc@gmail.com> |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
4 # |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
6 # GNU General Public License version 2 or any later version. |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
7 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
8 # This extension replaces the SSH server started via `hg serve --stdio`. |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
9 # The server behaves differently depending on environment variables. |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
10 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
11 from __future__ import absolute_import |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
12 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
13 from mercurial import ( |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
14 error, |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
15 registrar, |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
16 sshpeer, |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
17 wireproto, |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
18 wireprotoserver, |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
19 ) |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
20 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
21 configtable = {} |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
22 configitem = registrar.configitem(configtable) |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
23 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
24 configitem('sshpeer', 'mode', default=None) |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
25 configitem('sshpeer', 'handshake-mode', default=None) |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
26 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
27 class bannerserver(wireprotoserver.sshserver): |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
28 """Server that sends a banner to stdout.""" |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
29 def serve_forever(self): |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
30 for i in range(10): |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
31 self._fout.write(b'banner: line %d\n' % i) |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
32 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
33 super(bannerserver, self).serve_forever() |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
34 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
35 class prehelloserver(wireprotoserver.sshserver): |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
36 """Tests behavior when connecting to <0.9.1 servers. |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
37 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
38 The ``hello`` wire protocol command was introduced in Mercurial |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
39 0.9.1. Modern clients send the ``hello`` command when connecting |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
40 to SSH servers. This mock server tests behavior of the handshake |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
41 when ``hello`` is not supported. |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
42 """ |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
43 def serve_forever(self): |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
44 l = self._fin.readline() |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
45 assert l == b'hello\n' |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
46 # Respond to unknown commands with an empty reply. |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
47 self._sendresponse(b'') |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
48 l = self._fin.readline() |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
49 assert l == b'between\n' |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
50 rsp = wireproto.dispatch(self._repo, self, b'between') |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
51 self._handlers[rsp.__class__](self, rsp) |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
52 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
53 super(prehelloserver, self).serve_forever() |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
54 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
55 class extrahandshakecommandspeer(sshpeer.sshpeer): |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
56 """An ssh peer that sends extra commands as part of initial handshake.""" |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
57 # There isn't a good hook point. So we wrap _callstream() and inject |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
58 # logic when the peer says "hello". |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
59 def _callstream(self, cmd, **args): |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
60 if cmd != b'hello': |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
61 return super(extrahandshakecommandspeer, self)._callstream(cmd, |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
62 **args) |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
63 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
64 mode = self._ui.config(b'sshpeer', b'handshake-mode') |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
65 if mode == b'pre-no-args': |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
66 self._callstream(b'no-args') |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
67 return super(extrahandshakecommandspeer, self)._callstream( |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
68 cmd, **args) |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
69 elif mode == b'pre-multiple-no-args': |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
70 self._callstream(b'unknown1') |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
71 self._callstream(b'unknown2') |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
72 self._callstream(b'unknown3') |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
73 return super(extrahandshakecommandspeer, self)._callstream( |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
74 cmd, **args) |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
75 else: |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
76 raise error.ProgrammingError(b'unknown HANDSHAKECOMMANDMODE: %s' % |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
77 mode) |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
78 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
79 def registercommands(): |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
80 def dummycommand(repo, proto): |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
81 raise error.ProgrammingError('this should never be called') |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
82 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
83 wireproto.wireprotocommand(b'no-args', b'')(dummycommand) |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
84 wireproto.wireprotocommand(b'unknown1', b'')(dummycommand) |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
85 wireproto.wireprotocommand(b'unknown2', b'')(dummycommand) |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
86 wireproto.wireprotocommand(b'unknown3', b'')(dummycommand) |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
87 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
88 def extsetup(ui): |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
89 # It's easier for tests to define the server behavior via environment |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
90 # variables than config options. This is because `hg serve --stdio` |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
91 # has to be invoked with a certain form for security reasons and |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
92 # `dummyssh` can't just add `--config` flags to the command line. |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
93 servermode = ui.environ.get(b'SSHSERVERMODE') |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
94 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
95 if servermode == b'banner': |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
96 wireprotoserver.sshserver = bannerserver |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
97 elif servermode == b'no-hello': |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
98 wireprotoserver.sshserver = prehelloserver |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
99 elif servermode: |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
100 raise error.ProgrammingError(b'unknown server mode: %s' % servermode) |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
101 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
102 peermode = ui.config(b'sshpeer', b'mode') |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
103 |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
104 if peermode == b'extra-handshake-commands': |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
105 sshpeer.sshpeer = extrahandshakecommandspeer |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
106 registercommands() |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
107 elif peermode: |
83d67257ba90
tests: add low-level SSH protocol tests
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
108 raise error.ProgrammingError(b'unknown peer mode: %s' % peermode) |