##// END OF EJS Templates
py3: add b'' to config options in test extension...
Gregory Szorc -
r36229:30cc9f97 default
parent child Browse files
Show More
@@ -1,127 +1,127
1 1 # sshprotoext.py - Extension to test behavior of SSH protocol
2 2 #
3 3 # Copyright 2018 Gregory Szorc <gregory.szorc@gmail.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 # This extension replaces the SSH server started via `hg serve --stdio`.
9 9 # The server behaves differently depending on environment variables.
10 10
11 11 from __future__ import absolute_import
12 12
13 13 from mercurial import (
14 14 error,
15 15 extensions,
16 16 registrar,
17 17 sshpeer,
18 18 wireproto,
19 19 wireprotoserver,
20 20 )
21 21
22 22 configtable = {}
23 23 configitem = registrar.configitem(configtable)
24 24
25 configitem('sshpeer', 'mode', default=None)
26 configitem('sshpeer', 'handshake-mode', default=None)
25 configitem(b'sshpeer', b'mode', default=None)
26 configitem(b'sshpeer', b'handshake-mode', default=None)
27 27
28 28 class bannerserver(wireprotoserver.sshserver):
29 29 """Server that sends a banner to stdout."""
30 30 def serve_forever(self):
31 31 for i in range(10):
32 32 self._fout.write(b'banner: line %d\n' % i)
33 33
34 34 super(bannerserver, self).serve_forever()
35 35
36 36 class prehelloserver(wireprotoserver.sshserver):
37 37 """Tests behavior when connecting to <0.9.1 servers.
38 38
39 39 The ``hello`` wire protocol command was introduced in Mercurial
40 40 0.9.1. Modern clients send the ``hello`` command when connecting
41 41 to SSH servers. This mock server tests behavior of the handshake
42 42 when ``hello`` is not supported.
43 43 """
44 44 def serve_forever(self):
45 45 l = self._fin.readline()
46 46 assert l == b'hello\n'
47 47 # Respond to unknown commands with an empty reply.
48 48 wireprotoserver._sshv1respondbytes(self._fout, b'')
49 49 l = self._fin.readline()
50 50 assert l == b'between\n'
51 51 rsp = wireproto.dispatch(self._repo, self._proto, b'between')
52 52 wireprotoserver._sshv1respondbytes(self._fout, rsp.data)
53 53
54 54 super(prehelloserver, self).serve_forever()
55 55
56 56 class upgradev2server(wireprotoserver.sshserver):
57 57 """Tests behavior for clients that issue upgrade to version 2."""
58 58 def serve_forever(self):
59 59 name = wireprotoserver.SSHV2
60 60 l = self._fin.readline()
61 61 assert l.startswith(b'upgrade ')
62 62 token, caps = l[:-1].split(b' ')[1:]
63 63 assert caps == b'proto=%s' % name
64 64
65 65 # Filter hello and between requests.
66 66 l = self._fin.readline()
67 67 assert l == b'hello\n'
68 68 l = self._fin.readline()
69 69 assert l == b'between\n'
70 70 l = self._fin.readline()
71 71 assert l == b'pairs 81\n'
72 72 self._fin.read(81)
73 73
74 74 # Send the upgrade response.
75 75 self._fout.write(b'upgraded %s %s\n' % (token, name))
76 76 servercaps = wireproto.capabilities(self._repo, self._proto)
77 77 rsp = b'capabilities: %s' % servercaps.data
78 78 self._fout.write(b'%d\n' % len(rsp))
79 79 self._fout.write(rsp)
80 80 self._fout.write(b'\n')
81 81 self._fout.flush()
82 82
83 83 super(upgradev2server, self).serve_forever()
84 84
85 85 def performhandshake(orig, ui, stdin, stdout, stderr):
86 86 """Wrapped version of sshpeer._performhandshake to send extra commands."""
87 87 mode = ui.config(b'sshpeer', b'handshake-mode')
88 88 if mode == b'pre-no-args':
89 89 ui.debug(b'sending no-args command\n')
90 90 stdin.write(b'no-args\n')
91 91 stdin.flush()
92 92 return orig(ui, stdin, stdout, stderr)
93 93 elif mode == b'pre-multiple-no-args':
94 94 ui.debug(b'sending unknown1 command\n')
95 95 stdin.write(b'unknown1\n')
96 96 ui.debug(b'sending unknown2 command\n')
97 97 stdin.write(b'unknown2\n')
98 98 ui.debug(b'sending unknown3 command\n')
99 99 stdin.write(b'unknown3\n')
100 100 stdin.flush()
101 101 return orig(ui, stdin, stdout, stderr)
102 102 else:
103 103 raise error.ProgrammingError(b'unknown HANDSHAKECOMMANDMODE: %s' %
104 104 mode)
105 105
106 106 def extsetup(ui):
107 107 # It's easier for tests to define the server behavior via environment
108 108 # variables than config options. This is because `hg serve --stdio`
109 109 # has to be invoked with a certain form for security reasons and
110 110 # `dummyssh` can't just add `--config` flags to the command line.
111 111 servermode = ui.environ.get(b'SSHSERVERMODE')
112 112
113 113 if servermode == b'banner':
114 114 wireprotoserver.sshserver = bannerserver
115 115 elif servermode == b'no-hello':
116 116 wireprotoserver.sshserver = prehelloserver
117 117 elif servermode == b'upgradev2':
118 118 wireprotoserver.sshserver = upgradev2server
119 119 elif servermode:
120 120 raise error.ProgrammingError(b'unknown server mode: %s' % servermode)
121 121
122 122 peermode = ui.config(b'sshpeer', b'mode')
123 123
124 124 if peermode == b'extra-handshake-commands':
125 125 extensions.wrapfunction(sshpeer, '_performhandshake', performhandshake)
126 126 elif peermode:
127 127 raise error.ProgrammingError(b'unknown peer mode: %s' % peermode)
General Comments 0
You need to be logged in to leave comments. Login now