bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
4763.2.4
by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry. |
1 |
# Copyright (C) 2007-2010 Canonical Ltd
|
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
2 |
#
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
|
4183.7.1
by Sabin Iacob
update FSF mailing address |
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
16 |
|
|
6622.1.34
by Jelmer Vernooij
Rename brzlib => breezy. |
17 |
from breezy.tests import TestCase |
18 |
from breezy.errors import SSHVendorNotFound, UnknownSSH |
|
19 |
from breezy.transport.ssh import ( |
|
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
20 |
OpenSSHSubprocessVendor, |
|
2221.5.16
by Dmitry Vasiliev
Added comments for test_cached_vendor |
21 |
PLinkSubprocessVendor, |
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
22 |
SSHCorpSubprocessVendor, |
|
5444.2.2
by Matthew Gordon
Added tests for GNU lsh support. |
23 |
LSHSubprocessVendor, |
|
2221.5.16
by Dmitry Vasiliev
Added comments for test_cached_vendor |
24 |
SSHVendorManager, |
|
6753.1.1
by Jelmer Vernooij
Refuse to connect to ssh hostnames starting with a dash. Fixes LP:1710979 |
25 |
StrangeHostname, |
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
26 |
)
|
27 |
||
28 |
||
29 |
class TestSSHVendorManager(SSHVendorManager): |
|
30 |
||
31 |
_ssh_version_string = "" |
|
32 |
||
33 |
def set_ssh_version_string(self, version): |
|
34 |
self._ssh_version_string = version |
|
35 |
||
36 |
def _get_ssh_version_string(self, args): |
|
37 |
return self._ssh_version_string |
|
38 |
||
39 |
||
40 |
class SSHVendorManagerTests(TestCase): |
|
41 |
||
42 |
def test_register_vendor(self): |
|
43 |
manager = TestSSHVendorManager() |
|
44 |
self.assertRaises(SSHVendorNotFound, manager.get_vendor, {}) |
|
|
2221.5.6
by Dmitry Vasiliev
Changed tests to make sure the vendor manager returns the same object as was registered |
45 |
vendor = object() |
46 |
manager.register_vendor("vendor", vendor) |
|
|
6622.1.28
by Jelmer Vernooij
More renames; commands in output, environment variables. |
47 |
self.assertIs(manager.get_vendor({"BRZ_SSH": "vendor"}), vendor) |
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
48 |
|
49 |
def test_default_vendor(self): |
|
50 |
manager = TestSSHVendorManager() |
|
51 |
self.assertRaises(SSHVendorNotFound, manager.get_vendor, {}) |
|
|
2221.5.6
by Dmitry Vasiliev
Changed tests to make sure the vendor manager returns the same object as was registered |
52 |
vendor = object() |
53 |
manager.register_default_vendor(vendor) |
|
54 |
self.assertIs(manager.get_vendor({}), vendor) |
|
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
55 |
|
56 |
def test_get_vendor_by_environment(self): |
|
57 |
manager = TestSSHVendorManager() |
|
58 |
self.assertRaises(SSHVendorNotFound, manager.get_vendor, {}) |
|
59 |
self.assertRaises(UnknownSSH, |
|
|
6622.1.28
by Jelmer Vernooij
More renames; commands in output, environment variables. |
60 |
manager.get_vendor, {"BRZ_SSH": "vendor"}) |
|
2221.5.6
by Dmitry Vasiliev
Changed tests to make sure the vendor manager returns the same object as was registered |
61 |
vendor = object() |
62 |
manager.register_vendor("vendor", vendor) |
|
|
6622.1.28
by Jelmer Vernooij
More renames; commands in output, environment variables. |
63 |
self.assertIs(manager.get_vendor({"BRZ_SSH": "vendor"}), vendor) |
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
64 |
|
65 |
def test_get_vendor_by_inspection_openssh(self): |
|
66 |
manager = TestSSHVendorManager() |
|
67 |
self.assertRaises(SSHVendorNotFound, manager.get_vendor, {}) |
|
68 |
manager.set_ssh_version_string("OpenSSH") |
|
69 |
self.assertIsInstance(manager.get_vendor({}), OpenSSHSubprocessVendor) |
|
70 |
||
71 |
def test_get_vendor_by_inspection_sshcorp(self): |
|
72 |
manager = TestSSHVendorManager() |
|
73 |
self.assertRaises(SSHVendorNotFound, manager.get_vendor, {}) |
|
74 |
manager.set_ssh_version_string("SSH Secure Shell") |
|
75 |
self.assertIsInstance(manager.get_vendor({}), SSHCorpSubprocessVendor) |
|
76 |
||
|
5444.2.2
by Matthew Gordon
Added tests for GNU lsh support. |
77 |
def test_get_vendor_by_inspection_lsh(self): |
78 |
manager = TestSSHVendorManager() |
|
79 |
self.assertRaises(SSHVendorNotFound, manager.get_vendor, {}) |
|
80 |
manager.set_ssh_version_string("lsh") |
|
81 |
self.assertIsInstance(manager.get_vendor({}), LSHSubprocessVendor) |
|
82 |
||
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
83 |
def test_get_vendor_by_inspection_plink(self): |
84 |
manager = TestSSHVendorManager() |
|
85 |
self.assertRaises(SSHVendorNotFound, manager.get_vendor, {}) |
|
86 |
manager.set_ssh_version_string("plink") |
|
|
4636.3.1
by Alexander Belchenko
Disabled auto-detection of plink SSH client to avoid many problems. On Windows paramiko wil be used as default. Nevertheless user can force usage of plink via env variable BZR_SSH=plink |
87 |
# Auto-detect of plink vendor disabled, on Windows recommended
|
88 |
# default ssh-client is paramiko
|
|
89 |
# see https://bugs.launchpad.net/bugs/414743
|
|
90 |
#~self.assertIsInstance(manager.get_vendor({}), PLinkSubprocessVendor)
|
|
91 |
self.assertRaises(SSHVendorNotFound, manager.get_vendor, {}) |
|
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
92 |
|
93 |
def test_cached_vendor(self): |
|
94 |
manager = TestSSHVendorManager() |
|
95 |
self.assertRaises(SSHVendorNotFound, manager.get_vendor, {}) |
|
|
2221.5.6
by Dmitry Vasiliev
Changed tests to make sure the vendor manager returns the same object as was registered |
96 |
vendor = object() |
97 |
manager.register_vendor("vendor", vendor) |
|
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
98 |
self.assertRaises(SSHVendorNotFound, manager.get_vendor, {}) |
|
2221.5.16
by Dmitry Vasiliev
Added comments for test_cached_vendor |
99 |
# Once the vendor is found the result is cached (mainly because of the
|
100 |
# 'get_vendor' sometimes can be an expensive operation) and later
|
|
101 |
# invocations of the 'get_vendor' just returns the cached value.
|
|
|
6622.1.28
by Jelmer Vernooij
More renames; commands in output, environment variables. |
102 |
self.assertIs(manager.get_vendor({"BRZ_SSH": "vendor"}), vendor) |
|
2221.5.6
by Dmitry Vasiliev
Changed tests to make sure the vendor manager returns the same object as was registered |
103 |
self.assertIs(manager.get_vendor({}), vendor) |
|
2221.5.16
by Dmitry Vasiliev
Added comments for test_cached_vendor |
104 |
# The cache can be cleared by the 'clear_cache' method
|
105 |
manager.clear_cache() |
|
106 |
self.assertRaises(SSHVendorNotFound, manager.get_vendor, {}) |
|
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
107 |
|
|
2221.5.17
by Dmitry Vasiliev
Added comments for test_get_vendor_search_order |
108 |
def test_get_vendor_search_order(self): |
109 |
# The 'get_vendor' method search for SSH vendors as following:
|
|
110 |
#
|
|
111 |
# 1. Check previously cached value
|
|
|
6622.1.28
by Jelmer Vernooij
More renames; commands in output, environment variables. |
112 |
# 2. Check BRZ_SSH environment variable
|
|
2221.5.17
by Dmitry Vasiliev
Added comments for test_get_vendor_search_order |
113 |
# 3. Check the system for known SSH vendors
|
114 |
# 4. Fall back to the default vendor if registered
|
|
115 |
#
|
|
116 |
# Let's now check the each check method in the reverse order
|
|
117 |
# clearing the cache between each invocation:
|
|
118 |
||
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
119 |
manager = TestSSHVendorManager() |
|
2221.5.17
by Dmitry Vasiliev
Added comments for test_get_vendor_search_order |
120 |
# At first no vendors are found
|
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
121 |
self.assertRaises(SSHVendorNotFound, manager.get_vendor, {}) |
122 |
||
|
2221.5.17
by Dmitry Vasiliev
Added comments for test_get_vendor_search_order |
123 |
# If the default vendor is registered it will be returned
|
|
2221.5.6
by Dmitry Vasiliev
Changed tests to make sure the vendor manager returns the same object as was registered |
124 |
default_vendor = object() |
125 |
manager.register_default_vendor(default_vendor) |
|
126 |
self.assertIs(manager.get_vendor({}), default_vendor) |
|
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
127 |
|
|
2221.5.17
by Dmitry Vasiliev
Added comments for test_get_vendor_search_order |
128 |
# If the known vendor is found in the system it will be returned
|
|
2221.5.8
by Dmitry Vasiliev
Added SSHVendorManager.clear_cache() method |
129 |
manager.clear_cache() |
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
130 |
manager.set_ssh_version_string("OpenSSH") |
131 |
self.assertIsInstance(manager.get_vendor({}), OpenSSHSubprocessVendor) |
|
132 |
||
|
6622.1.28
by Jelmer Vernooij
More renames; commands in output, environment variables. |
133 |
# If the BRZ_SSH environment variable is found it will be treated as
|
|
2221.5.17
by Dmitry Vasiliev
Added comments for test_get_vendor_search_order |
134 |
# the vendor name
|
|
2221.5.8
by Dmitry Vasiliev
Added SSHVendorManager.clear_cache() method |
135 |
manager.clear_cache() |
|
2221.5.6
by Dmitry Vasiliev
Changed tests to make sure the vendor manager returns the same object as was registered |
136 |
vendor = object() |
137 |
manager.register_vendor("vendor", vendor) |
|
|
6622.1.28
by Jelmer Vernooij
More renames; commands in output, environment variables. |
138 |
self.assertIs(manager.get_vendor({"BRZ_SSH": "vendor"}), vendor) |
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
139 |
|
|
2221.5.17
by Dmitry Vasiliev
Added comments for test_get_vendor_search_order |
140 |
# Last cached value always checked first
|
|
2221.5.6
by Dmitry Vasiliev
Changed tests to make sure the vendor manager returns the same object as was registered |
141 |
self.assertIs(manager.get_vendor({}), vendor) |
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
142 |
|
|
4595.17.1
by Martin
Add ability to give a path to a particular ssh client in BZR_SSH envvar |
143 |
def test_get_vendor_from_path_win32_plink(self): |
144 |
manager = TestSSHVendorManager() |
|
145 |
manager.set_ssh_version_string("plink: Release 0.60") |
|
146 |
plink_path = "C:/Program Files/PuTTY/plink.exe" |
|
|
6622.1.28
by Jelmer Vernooij
More renames; commands in output, environment variables. |
147 |
vendor = manager.get_vendor({"BRZ_SSH": plink_path}) |
|
4595.17.1
by Martin
Add ability to give a path to a particular ssh client in BZR_SSH envvar |
148 |
self.assertIsInstance(vendor, PLinkSubprocessVendor) |
149 |
args = vendor._get_vendor_specific_argv("user", "host", 22, ["bzr"]) |
|
150 |
self.assertEqual(args[0], plink_path) |
|
151 |
||
152 |
def test_get_vendor_from_path_nix_openssh(self): |
|
153 |
manager = TestSSHVendorManager() |
|
154 |
manager.set_ssh_version_string( |
|
155 |
"OpenSSH_5.1p1 Debian-5, OpenSSL, 0.9.8g 19 Oct 2007") |
|
156 |
openssh_path = "/usr/bin/ssh" |
|
|
6622.1.28
by Jelmer Vernooij
More renames; commands in output, environment variables. |
157 |
vendor = manager.get_vendor({"BRZ_SSH": openssh_path}) |
|
4595.17.1
by Martin
Add ability to give a path to a particular ssh client in BZR_SSH envvar |
158 |
self.assertIsInstance(vendor, OpenSSHSubprocessVendor) |
159 |
args = vendor._get_vendor_specific_argv("user", "host", 22, ["bzr"]) |
|
160 |
self.assertEqual(args[0], openssh_path) |
|
161 |
||
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
162 |
|
163 |
class SubprocessVendorsTests(TestCase): |
|
164 |
||
|
6753.1.1
by Jelmer Vernooij
Refuse to connect to ssh hostnames starting with a dash. Fixes LP:1710979 |
165 |
def test_openssh_command_tricked(self): |
166 |
vendor = OpenSSHSubprocessVendor() |
|
167 |
self.assertEqual( |
|
168 |
vendor._get_vendor_specific_argv( |
|
169 |
"user", "-oProxyCommand=blah", 100, command=["bzr"]), |
|
170 |
["ssh", "-oForwardX11=no", "-oForwardAgent=no", |
|
171 |
"-oClearAllForwardings=yes", |
|
172 |
"-oNoHostAuthenticationForLocalhost=yes", |
|
173 |
"-p", "100", |
|
174 |
"-l", "user", |
|
175 |
"--", |
|
176 |
"-oProxyCommand=blah", "bzr"]) |
|
177 |
||
|
2221.5.3
by Dmitry Vasiliev
Fixed plink's arguments order. Added tests for such a case. |
178 |
def test_openssh_command_arguments(self): |
179 |
vendor = OpenSSHSubprocessVendor() |
|
180 |
self.assertEqual( |
|
181 |
vendor._get_vendor_specific_argv( |
|
182 |
"user", "host", 100, command=["bzr"]), |
|
183 |
["ssh", "-oForwardX11=no", "-oForwardAgent=no", |
|
|
5459.4.1
by Neil Martinsen-Burrell
dont force openssh to use protocol=2 |
184 |
"-oClearAllForwardings=yes", |
|
2221.5.3
by Dmitry Vasiliev
Fixed plink's arguments order. Added tests for such a case. |
185 |
"-oNoHostAuthenticationForLocalhost=yes", |
186 |
"-p", "100", |
|
187 |
"-l", "user", |
|
|
6753.1.1
by Jelmer Vernooij
Refuse to connect to ssh hostnames starting with a dash. Fixes LP:1710979 |
188 |
"--", |
|
2221.5.3
by Dmitry Vasiliev
Fixed plink's arguments order. Added tests for such a case. |
189 |
"host", "bzr"] |
190 |
)
|
|
191 |
||
192 |
def test_openssh_subsystem_arguments(self): |
|
193 |
vendor = OpenSSHSubprocessVendor() |
|
194 |
self.assertEqual( |
|
195 |
vendor._get_vendor_specific_argv( |
|
196 |
"user", "host", 100, subsystem="sftp"), |
|
197 |
["ssh", "-oForwardX11=no", "-oForwardAgent=no", |
|
|
5459.4.1
by Neil Martinsen-Burrell
dont force openssh to use protocol=2 |
198 |
"-oClearAllForwardings=yes", |
|
2221.5.3
by Dmitry Vasiliev
Fixed plink's arguments order. Added tests for such a case. |
199 |
"-oNoHostAuthenticationForLocalhost=yes", |
200 |
"-p", "100", |
|
201 |
"-l", "user", |
|
|
6753.1.1
by Jelmer Vernooij
Refuse to connect to ssh hostnames starting with a dash. Fixes LP:1710979 |
202 |
"-s", "--", "host", "sftp"] |
|
2221.5.3
by Dmitry Vasiliev
Fixed plink's arguments order. Added tests for such a case. |
203 |
)
|
204 |
||
|
6753.1.1
by Jelmer Vernooij
Refuse to connect to ssh hostnames starting with a dash. Fixes LP:1710979 |
205 |
def test_openssh_command_tricked(self): |
206 |
vendor = SSHCorpSubprocessVendor() |
|
207 |
self.assertRaises( |
|
208 |
StrangeHostname, |
|
209 |
vendor._get_vendor_specific_argv, |
|
210 |
"user", "-oProxyCommand=host", 100, command=["bzr"]) |
|
211 |
||
|
2221.5.3
by Dmitry Vasiliev
Fixed plink's arguments order. Added tests for such a case. |
212 |
def test_sshcorp_command_arguments(self): |
213 |
vendor = SSHCorpSubprocessVendor() |
|
214 |
self.assertEqual( |
|
215 |
vendor._get_vendor_specific_argv( |
|
216 |
"user", "host", 100, command=["bzr"]), |
|
217 |
["ssh", "-x", |
|
218 |
"-p", "100", |
|
219 |
"-l", "user", |
|
220 |
"host", "bzr"] |
|
221 |
)
|
|
222 |
||
223 |
def test_sshcorp_subsystem_arguments(self): |
|
224 |
vendor = SSHCorpSubprocessVendor() |
|
225 |
self.assertEqual( |
|
226 |
vendor._get_vendor_specific_argv( |
|
227 |
"user", "host", 100, subsystem="sftp"), |
|
228 |
["ssh", "-x", |
|
229 |
"-p", "100", |
|
230 |
"-l", "user", |
|
231 |
"-s", "sftp", "host"] |
|
232 |
)
|
|
233 |
||
|
6753.1.1
by Jelmer Vernooij
Refuse to connect to ssh hostnames starting with a dash. Fixes LP:1710979 |
234 |
def test_lsh_command_tricked(self): |
235 |
vendor = LSHSubprocessVendor() |
|
236 |
self.assertRaises( |
|
237 |
StrangeHostname, |
|
238 |
vendor._get_vendor_specific_argv, |
|
239 |
"user", "-oProxyCommand=host", 100, command=["bzr"]) |
|
240 |
||
|
5444.2.2
by Matthew Gordon
Added tests for GNU lsh support. |
241 |
def test_lsh_command_arguments(self): |
242 |
vendor = LSHSubprocessVendor() |
|
243 |
self.assertEqual( |
|
244 |
vendor._get_vendor_specific_argv( |
|
245 |
"user", "host", 100, command=["bzr"]), |
|
246 |
["lsh", |
|
247 |
"-p", "100", |
|
248 |
"-l", "user", |
|
249 |
"host", "bzr"] |
|
250 |
)
|
|
251 |
||
252 |
def test_lsh_subsystem_arguments(self): |
|
253 |
vendor = LSHSubprocessVendor() |
|
254 |
self.assertEqual( |
|
255 |
vendor._get_vendor_specific_argv( |
|
256 |
"user", "host", 100, subsystem="sftp"), |
|
257 |
["lsh", |
|
258 |
"-p", "100", |
|
259 |
"-l", "user", |
|
260 |
"--subsystem", "sftp", "host"] |
|
261 |
)
|
|
262 |
||
|
6753.1.1
by Jelmer Vernooij
Refuse to connect to ssh hostnames starting with a dash. Fixes LP:1710979 |
263 |
def test_plink_command_tricked(self): |
264 |
vendor = PLinkSubprocessVendor() |
|
265 |
self.assertRaises( |
|
266 |
StrangeHostname, |
|
267 |
vendor._get_vendor_specific_argv, |
|
268 |
"user", "-oProxyCommand=host", 100, command=["bzr"]) |
|
269 |
||
|
2221.5.3
by Dmitry Vasiliev
Fixed plink's arguments order. Added tests for such a case. |
270 |
def test_plink_command_arguments(self): |
271 |
vendor = PLinkSubprocessVendor() |
|
272 |
self.assertEqual( |
|
273 |
vendor._get_vendor_specific_argv( |
|
274 |
"user", "host", 100, command=["bzr"]), |
|
|
3220.1.2
by Dmitry Vasiliev
Re-enabled auto-detection of plink vendor and fixed tests |
275 |
["plink", "-x", "-a", "-ssh", "-2", "-batch", |
|
2221.5.3
by Dmitry Vasiliev
Fixed plink's arguments order. Added tests for such a case. |
276 |
"-P", "100", |
277 |
"-l", "user", |
|
278 |
"host", "bzr"] |
|
279 |
)
|
|
280 |
||
281 |
def test_plink_subsystem_arguments(self): |
|
282 |
vendor = PLinkSubprocessVendor() |
|
283 |
self.assertEqual( |
|
284 |
vendor._get_vendor_specific_argv( |
|
285 |
"user", "host", 100, subsystem="sftp"), |
|
|
3220.1.2
by Dmitry Vasiliev
Re-enabled auto-detection of plink vendor and fixed tests |
286 |
["plink", "-x", "-a", "-ssh", "-2", "-batch", |
|
2221.5.3
by Dmitry Vasiliev
Fixed plink's arguments order. Added tests for such a case. |
287 |
"-P", "100", |
288 |
"-l", "user", |
|
289 |
"-s", "host", "sftp"] |
|
|
2221.5.1
by Dmitry Vasiliev
Added support for Putty's SSH implementation |
290 |
)
|