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