test_client.py 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  1. # test_client.py -- Tests for the git protocol, client side
  2. # Copyright (C) 2009 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. from io import BytesIO
  21. import base64
  22. import sys
  23. import shutil
  24. import tempfile
  25. import warnings
  26. try:
  27. from urllib import quote as urlquote
  28. except ImportError:
  29. from urllib.parse import quote as urlquote
  30. try:
  31. import urlparse
  32. except ImportError:
  33. import urllib.parse as urlparse
  34. import urllib3
  35. import dulwich
  36. from dulwich import (
  37. client,
  38. )
  39. from dulwich.client import (
  40. InvalidWants,
  41. LocalGitClient,
  42. TraditionalGitClient,
  43. TCPGitClient,
  44. SSHGitClient,
  45. HttpGitClient,
  46. ReportStatusParser,
  47. SendPackError,
  48. StrangeHostname,
  49. SubprocessSSHVendor,
  50. PLinkSSHVendor,
  51. UpdateRefsError,
  52. check_wants,
  53. default_urllib3_manager,
  54. get_transport_and_path,
  55. get_transport_and_path_from_url,
  56. parse_rsync_url,
  57. )
  58. from dulwich.config import (
  59. ConfigDict,
  60. )
  61. from dulwich.tests import (
  62. TestCase,
  63. )
  64. from dulwich.protocol import (
  65. TCP_GIT_PORT,
  66. Protocol,
  67. )
  68. from dulwich.pack import (
  69. pack_objects_to_data,
  70. write_pack_data,
  71. write_pack_objects,
  72. )
  73. from dulwich.objects import (
  74. Commit,
  75. Tree
  76. )
  77. from dulwich.repo import (
  78. MemoryRepo,
  79. Repo,
  80. )
  81. from dulwich.tests import skipIf
  82. from dulwich.tests.utils import (
  83. open_repo,
  84. tear_down_repo,
  85. setup_warning_catcher,
  86. )
  87. class DummyClient(TraditionalGitClient):
  88. def __init__(self, can_read, read, write):
  89. self.can_read = can_read
  90. self.read = read
  91. self.write = write
  92. TraditionalGitClient.__init__(self)
  93. def _connect(self, service, path):
  94. return Protocol(self.read, self.write), self.can_read, None
  95. class DummyPopen():
  96. def __init__(self, *args, **kwards):
  97. self.stdin = BytesIO(b"stdin")
  98. self.stdout = BytesIO(b"stdout")
  99. self.stderr = BytesIO(b"stderr")
  100. self.returncode = 0
  101. self.args = args
  102. self.kwargs = kwards
  103. def communicate(self, *args, **kwards):
  104. return ('Running', '')
  105. def wait(self, *args, **kwards):
  106. return False
  107. # TODO(durin42): add unit-level tests of GitClient
  108. class GitClientTests(TestCase):
  109. def setUp(self):
  110. super(GitClientTests, self).setUp()
  111. self.rout = BytesIO()
  112. self.rin = BytesIO()
  113. self.client = DummyClient(lambda x: True, self.rin.read,
  114. self.rout.write)
  115. def test_caps(self):
  116. agent_cap = (
  117. 'agent=dulwich/%d.%d.%d' % dulwich.__version__).encode('ascii')
  118. self.assertEqual(set([b'multi_ack', b'side-band-64k', b'ofs-delta',
  119. b'thin-pack', b'multi_ack_detailed', b'shallow',
  120. agent_cap]),
  121. set(self.client._fetch_capabilities))
  122. self.assertEqual(set([b'ofs-delta', b'report-status', b'side-band-64k',
  123. agent_cap]),
  124. set(self.client._send_capabilities))
  125. def test_archive_ack(self):
  126. self.rin.write(
  127. b'0009NACK\n'
  128. b'0000')
  129. self.rin.seek(0)
  130. self.client.archive(b'bla', b'HEAD', None, None)
  131. self.assertEqual(self.rout.getvalue(), b'0011argument HEAD0000')
  132. def test_fetch_empty(self):
  133. self.rin.write(b'0000')
  134. self.rin.seek(0)
  135. def check_heads(heads):
  136. self.assertEqual(heads, {})
  137. return []
  138. ret = self.client.fetch_pack(b'/', check_heads, None, None)
  139. self.assertEqual({}, ret.refs)
  140. self.assertEqual({}, ret.symrefs)
  141. def test_fetch_pack_ignores_magic_ref(self):
  142. self.rin.write(
  143. b'00000000000000000000000000000000000000000000 capabilities^{}'
  144. b'\x00 multi_ack '
  145. b'thin-pack side-band side-band-64k ofs-delta shallow no-progress '
  146. b'include-tag\n'
  147. b'0000')
  148. self.rin.seek(0)
  149. def check_heads(heads):
  150. self.assertEqual({}, heads)
  151. return []
  152. ret = self.client.fetch_pack(b'bla', check_heads, None, None, None)
  153. self.assertEqual({}, ret.refs)
  154. self.assertEqual({}, ret.symrefs)
  155. self.assertEqual(self.rout.getvalue(), b'0000')
  156. def test_fetch_pack_none(self):
  157. self.rin.write(
  158. b'008855dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 HEAD\x00multi_ack '
  159. b'thin-pack side-band side-band-64k ofs-delta shallow no-progress '
  160. b'include-tag\n'
  161. b'0000')
  162. self.rin.seek(0)
  163. ret = self.client.fetch_pack(
  164. b'bla', lambda heads: [], None, None, None)
  165. self.assertEqual(
  166. {b'HEAD': b'55dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7'},
  167. ret.refs)
  168. self.assertEqual({}, ret.symrefs)
  169. self.assertEqual(self.rout.getvalue(), b'0000')
  170. def test_fetch_pack_sha_not_in_ref(self):
  171. self.rin.write(
  172. b'008855dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 HEAD\x00multi_ack '
  173. b'thin-pack side-band side-band-64k ofs-delta shallow no-progress '
  174. b'include-tag\n'
  175. b'0000')
  176. self.rin.seek(0)
  177. self.assertRaises(
  178. InvalidWants, self.client.fetch_pack,
  179. b'bla',
  180. lambda heads: ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'],
  181. None, None,
  182. None)
  183. def test_send_pack_no_sideband64k_with_update_ref_error(self):
  184. # No side-bank-64k reported by server shouldn't try to parse
  185. # side band data
  186. pkts = [b'55dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 capabilities^{}'
  187. b'\x00 report-status delete-refs ofs-delta\n',
  188. b'',
  189. b"unpack ok",
  190. b"ng refs/foo/bar pre-receive hook declined",
  191. b'']
  192. for pkt in pkts:
  193. if pkt == b'':
  194. self.rin.write(b"0000")
  195. else:
  196. self.rin.write(("%04x" % (len(pkt)+4)).encode('ascii') + pkt)
  197. self.rin.seek(0)
  198. tree = Tree()
  199. commit = Commit()
  200. commit.tree = tree
  201. commit.parents = []
  202. commit.author = commit.committer = b'test user'
  203. commit.commit_time = commit.author_time = 1174773719
  204. commit.commit_timezone = commit.author_timezone = 0
  205. commit.encoding = b'UTF-8'
  206. commit.message = b'test message'
  207. def update_refs(refs):
  208. return {b'refs/foo/bar': commit.id, }
  209. def generate_pack_data(have, want, ofs_delta=False):
  210. return pack_objects_to_data([(commit, None), (tree, ''), ])
  211. self.assertRaises(UpdateRefsError,
  212. self.client.send_pack, "blah",
  213. update_refs, generate_pack_data)
  214. def test_send_pack_none(self):
  215. self.rin.write(
  216. b'0078310ca9477129b8586fa2afc779c1f57cf64bba6c '
  217. b'refs/heads/master\x00 report-status delete-refs '
  218. b'side-band-64k quiet ofs-delta\n'
  219. b'0000')
  220. self.rin.seek(0)
  221. def update_refs(refs):
  222. return {
  223. b'refs/heads/master':
  224. b'310ca9477129b8586fa2afc779c1f57cf64bba6c'
  225. }
  226. def generate_pack_data(have, want, ofs_delta=False):
  227. return 0, []
  228. self.client.send_pack(b'/', update_refs, generate_pack_data)
  229. self.assertEqual(self.rout.getvalue(), b'0000')
  230. def test_send_pack_keep_and_delete(self):
  231. self.rin.write(
  232. b'0063310ca9477129b8586fa2afc779c1f57cf64bba6c '
  233. b'refs/heads/master\x00report-status delete-refs ofs-delta\n'
  234. b'003f310ca9477129b8586fa2afc779c1f57cf64bba6c refs/heads/keepme\n'
  235. b'0000000eunpack ok\n'
  236. b'0019ok refs/heads/master\n'
  237. b'0000')
  238. self.rin.seek(0)
  239. def update_refs(refs):
  240. return {b'refs/heads/master': b'0' * 40}
  241. def generate_pack_data(have, want, ofs_delta=False):
  242. return 0, []
  243. self.client.send_pack(b'/', update_refs, generate_pack_data)
  244. self.assertIn(
  245. self.rout.getvalue(),
  246. [b'007f310ca9477129b8586fa2afc779c1f57cf64bba6c '
  247. b'0000000000000000000000000000000000000000 '
  248. b'refs/heads/master\x00report-status ofs-delta0000',
  249. b'007f310ca9477129b8586fa2afc779c1f57cf64bba6c '
  250. b'0000000000000000000000000000000000000000 '
  251. b'refs/heads/master\x00ofs-delta report-status0000'])
  252. def test_send_pack_delete_only(self):
  253. self.rin.write(
  254. b'0063310ca9477129b8586fa2afc779c1f57cf64bba6c '
  255. b'refs/heads/master\x00report-status delete-refs ofs-delta\n'
  256. b'0000000eunpack ok\n'
  257. b'0019ok refs/heads/master\n'
  258. b'0000')
  259. self.rin.seek(0)
  260. def update_refs(refs):
  261. return {b'refs/heads/master': b'0' * 40}
  262. def generate_pack_data(have, want, ofs_delta=False):
  263. return 0, []
  264. self.client.send_pack(b'/', update_refs, generate_pack_data)
  265. self.assertIn(
  266. self.rout.getvalue(),
  267. [b'007f310ca9477129b8586fa2afc779c1f57cf64bba6c '
  268. b'0000000000000000000000000000000000000000 '
  269. b'refs/heads/master\x00report-status ofs-delta0000',
  270. b'007f310ca9477129b8586fa2afc779c1f57cf64bba6c '
  271. b'0000000000000000000000000000000000000000 '
  272. b'refs/heads/master\x00ofs-delta report-status0000'])
  273. def test_send_pack_new_ref_only(self):
  274. self.rin.write(
  275. b'0063310ca9477129b8586fa2afc779c1f57cf64bba6c '
  276. b'refs/heads/master\x00report-status delete-refs ofs-delta\n'
  277. b'0000000eunpack ok\n'
  278. b'0019ok refs/heads/blah12\n'
  279. b'0000')
  280. self.rin.seek(0)
  281. def update_refs(refs):
  282. return {
  283. b'refs/heads/blah12':
  284. b'310ca9477129b8586fa2afc779c1f57cf64bba6c',
  285. b'refs/heads/master':
  286. b'310ca9477129b8586fa2afc779c1f57cf64bba6c'
  287. }
  288. def generate_pack_data(have, want, ofs_delta=False):
  289. return 0, []
  290. f = BytesIO()
  291. write_pack_objects(f, {})
  292. self.client.send_pack('/', update_refs, generate_pack_data)
  293. self.assertIn(
  294. self.rout.getvalue(),
  295. [b'007f0000000000000000000000000000000000000000 '
  296. b'310ca9477129b8586fa2afc779c1f57cf64bba6c '
  297. b'refs/heads/blah12\x00report-status ofs-delta0000' +
  298. f.getvalue(),
  299. b'007f0000000000000000000000000000000000000000 '
  300. b'310ca9477129b8586fa2afc779c1f57cf64bba6c '
  301. b'refs/heads/blah12\x00ofs-delta report-status0000' +
  302. f.getvalue()])
  303. def test_send_pack_new_ref(self):
  304. self.rin.write(
  305. b'0064310ca9477129b8586fa2afc779c1f57cf64bba6c '
  306. b'refs/heads/master\x00 report-status delete-refs ofs-delta\n'
  307. b'0000000eunpack ok\n'
  308. b'0019ok refs/heads/blah12\n'
  309. b'0000')
  310. self.rin.seek(0)
  311. tree = Tree()
  312. commit = Commit()
  313. commit.tree = tree
  314. commit.parents = []
  315. commit.author = commit.committer = b'test user'
  316. commit.commit_time = commit.author_time = 1174773719
  317. commit.commit_timezone = commit.author_timezone = 0
  318. commit.encoding = b'UTF-8'
  319. commit.message = b'test message'
  320. def update_refs(refs):
  321. return {
  322. b'refs/heads/blah12': commit.id,
  323. b'refs/heads/master':
  324. b'310ca9477129b8586fa2afc779c1f57cf64bba6c'
  325. }
  326. def generate_pack_data(have, want, ofs_delta=False):
  327. return pack_objects_to_data([(commit, None), (tree, b''), ])
  328. f = BytesIO()
  329. write_pack_data(f, *generate_pack_data(None, None))
  330. self.client.send_pack(b'/', update_refs, generate_pack_data)
  331. self.assertIn(
  332. self.rout.getvalue(),
  333. [b'007f0000000000000000000000000000000000000000 ' + commit.id +
  334. b' refs/heads/blah12\x00report-status ofs-delta0000' +
  335. f.getvalue(),
  336. b'007f0000000000000000000000000000000000000000 ' + commit.id +
  337. b' refs/heads/blah12\x00ofs-delta report-status0000' +
  338. f.getvalue()])
  339. def test_send_pack_no_deleteref_delete_only(self):
  340. pkts = [b'310ca9477129b8586fa2afc779c1f57cf64bba6c refs/heads/master'
  341. b'\x00 report-status ofs-delta\n',
  342. b'',
  343. b'']
  344. for pkt in pkts:
  345. if pkt == b'':
  346. self.rin.write(b"0000")
  347. else:
  348. self.rin.write(("%04x" % (len(pkt)+4)).encode('ascii') + pkt)
  349. self.rin.seek(0)
  350. def update_refs(refs):
  351. return {b'refs/heads/master': b'0' * 40}
  352. def generate_pack_data(have, want, ofs_delta=False):
  353. return 0, []
  354. self.assertRaises(UpdateRefsError,
  355. self.client.send_pack, b"/",
  356. update_refs, generate_pack_data)
  357. self.assertEqual(self.rout.getvalue(), b'0000')
  358. class TestGetTransportAndPath(TestCase):
  359. def test_tcp(self):
  360. c, path = get_transport_and_path('git://foo.com/bar/baz')
  361. self.assertTrue(isinstance(c, TCPGitClient))
  362. self.assertEqual('foo.com', c._host)
  363. self.assertEqual(TCP_GIT_PORT, c._port)
  364. self.assertEqual('/bar/baz', path)
  365. def test_tcp_port(self):
  366. c, path = get_transport_and_path('git://foo.com:1234/bar/baz')
  367. self.assertTrue(isinstance(c, TCPGitClient))
  368. self.assertEqual('foo.com', c._host)
  369. self.assertEqual(1234, c._port)
  370. self.assertEqual('/bar/baz', path)
  371. def test_git_ssh_explicit(self):
  372. c, path = get_transport_and_path('git+ssh://foo.com/bar/baz')
  373. self.assertTrue(isinstance(c, SSHGitClient))
  374. self.assertEqual('foo.com', c.host)
  375. self.assertEqual(None, c.port)
  376. self.assertEqual(None, c.username)
  377. self.assertEqual('/bar/baz', path)
  378. def test_ssh_explicit(self):
  379. c, path = get_transport_and_path('ssh://foo.com/bar/baz')
  380. self.assertTrue(isinstance(c, SSHGitClient))
  381. self.assertEqual('foo.com', c.host)
  382. self.assertEqual(None, c.port)
  383. self.assertEqual(None, c.username)
  384. self.assertEqual('/bar/baz', path)
  385. def test_ssh_port_explicit(self):
  386. c, path = get_transport_and_path(
  387. 'git+ssh://foo.com:1234/bar/baz')
  388. self.assertTrue(isinstance(c, SSHGitClient))
  389. self.assertEqual('foo.com', c.host)
  390. self.assertEqual(1234, c.port)
  391. self.assertEqual('/bar/baz', path)
  392. def test_username_and_port_explicit_unknown_scheme(self):
  393. c, path = get_transport_and_path(
  394. 'unknown://git@server:7999/dply/stuff.git')
  395. self.assertTrue(isinstance(c, SSHGitClient))
  396. self.assertEqual('unknown', c.host)
  397. self.assertEqual('//git@server:7999/dply/stuff.git', path)
  398. def test_username_and_port_explicit(self):
  399. c, path = get_transport_and_path(
  400. 'ssh://git@server:7999/dply/stuff.git')
  401. self.assertTrue(isinstance(c, SSHGitClient))
  402. self.assertEqual('git', c.username)
  403. self.assertEqual('server', c.host)
  404. self.assertEqual(7999, c.port)
  405. self.assertEqual('/dply/stuff.git', path)
  406. def test_ssh_abspath_doubleslash(self):
  407. c, path = get_transport_and_path('git+ssh://foo.com//bar/baz')
  408. self.assertTrue(isinstance(c, SSHGitClient))
  409. self.assertEqual('foo.com', c.host)
  410. self.assertEqual(None, c.port)
  411. self.assertEqual(None, c.username)
  412. self.assertEqual('//bar/baz', path)
  413. def test_ssh_port(self):
  414. c, path = get_transport_and_path(
  415. 'git+ssh://foo.com:1234/bar/baz')
  416. self.assertTrue(isinstance(c, SSHGitClient))
  417. self.assertEqual('foo.com', c.host)
  418. self.assertEqual(1234, c.port)
  419. self.assertEqual('/bar/baz', path)
  420. def test_ssh_implicit(self):
  421. c, path = get_transport_and_path('foo:/bar/baz')
  422. self.assertTrue(isinstance(c, SSHGitClient))
  423. self.assertEqual('foo', c.host)
  424. self.assertEqual(None, c.port)
  425. self.assertEqual(None, c.username)
  426. self.assertEqual('/bar/baz', path)
  427. def test_ssh_host(self):
  428. c, path = get_transport_and_path('foo.com:/bar/baz')
  429. self.assertTrue(isinstance(c, SSHGitClient))
  430. self.assertEqual('foo.com', c.host)
  431. self.assertEqual(None, c.port)
  432. self.assertEqual(None, c.username)
  433. self.assertEqual('/bar/baz', path)
  434. def test_ssh_user_host(self):
  435. c, path = get_transport_and_path('user@foo.com:/bar/baz')
  436. self.assertTrue(isinstance(c, SSHGitClient))
  437. self.assertEqual('foo.com', c.host)
  438. self.assertEqual(None, c.port)
  439. self.assertEqual('user', c.username)
  440. self.assertEqual('/bar/baz', path)
  441. def test_ssh_relpath(self):
  442. c, path = get_transport_and_path('foo:bar/baz')
  443. self.assertTrue(isinstance(c, SSHGitClient))
  444. self.assertEqual('foo', c.host)
  445. self.assertEqual(None, c.port)
  446. self.assertEqual(None, c.username)
  447. self.assertEqual('bar/baz', path)
  448. def test_ssh_host_relpath(self):
  449. c, path = get_transport_and_path('foo.com:bar/baz')
  450. self.assertTrue(isinstance(c, SSHGitClient))
  451. self.assertEqual('foo.com', c.host)
  452. self.assertEqual(None, c.port)
  453. self.assertEqual(None, c.username)
  454. self.assertEqual('bar/baz', path)
  455. def test_ssh_user_host_relpath(self):
  456. c, path = get_transport_and_path('user@foo.com:bar/baz')
  457. self.assertTrue(isinstance(c, SSHGitClient))
  458. self.assertEqual('foo.com', c.host)
  459. self.assertEqual(None, c.port)
  460. self.assertEqual('user', c.username)
  461. self.assertEqual('bar/baz', path)
  462. def test_local(self):
  463. c, path = get_transport_and_path('foo.bar/baz')
  464. self.assertTrue(isinstance(c, LocalGitClient))
  465. self.assertEqual('foo.bar/baz', path)
  466. @skipIf(sys.platform != 'win32', 'Behaviour only happens on windows.')
  467. def test_local_abs_windows_path(self):
  468. c, path = get_transport_and_path('C:\\foo.bar\\baz')
  469. self.assertTrue(isinstance(c, LocalGitClient))
  470. self.assertEqual('C:\\foo.bar\\baz', path)
  471. def test_error(self):
  472. # Need to use a known urlparse.uses_netloc URL scheme to get the
  473. # expected parsing of the URL on Python versions less than 2.6.5
  474. c, path = get_transport_and_path('prospero://bar/baz')
  475. self.assertTrue(isinstance(c, SSHGitClient))
  476. def test_http(self):
  477. url = 'https://github.com/jelmer/dulwich'
  478. c, path = get_transport_and_path(url)
  479. self.assertTrue(isinstance(c, HttpGitClient))
  480. self.assertEqual('/jelmer/dulwich', path)
  481. def test_http_auth(self):
  482. url = 'https://user:passwd@github.com/jelmer/dulwich'
  483. c, path = get_transport_and_path(url)
  484. self.assertTrue(isinstance(c, HttpGitClient))
  485. self.assertEqual('/jelmer/dulwich', path)
  486. self.assertEqual('user', c._username)
  487. self.assertEqual('passwd', c._password)
  488. def test_http_auth_with_username(self):
  489. url = 'https://github.com/jelmer/dulwich'
  490. c, path = get_transport_and_path(
  491. url, username='user2', password='blah')
  492. self.assertTrue(isinstance(c, HttpGitClient))
  493. self.assertEqual('/jelmer/dulwich', path)
  494. self.assertEqual('user2', c._username)
  495. self.assertEqual('blah', c._password)
  496. def test_http_auth_with_username_and_in_url(self):
  497. url = 'https://user:passwd@github.com/jelmer/dulwich'
  498. c, path = get_transport_and_path(
  499. url, username='user2', password='blah')
  500. self.assertTrue(isinstance(c, HttpGitClient))
  501. self.assertEqual('/jelmer/dulwich', path)
  502. self.assertEqual('user', c._username)
  503. self.assertEqual('passwd', c._password)
  504. def test_http_no_auth(self):
  505. url = 'https://github.com/jelmer/dulwich'
  506. c, path = get_transport_and_path(url)
  507. self.assertTrue(isinstance(c, HttpGitClient))
  508. self.assertEqual('/jelmer/dulwich', path)
  509. self.assertIs(None, c._username)
  510. self.assertIs(None, c._password)
  511. class TestGetTransportAndPathFromUrl(TestCase):
  512. def test_tcp(self):
  513. c, path = get_transport_and_path_from_url('git://foo.com/bar/baz')
  514. self.assertTrue(isinstance(c, TCPGitClient))
  515. self.assertEqual('foo.com', c._host)
  516. self.assertEqual(TCP_GIT_PORT, c._port)
  517. self.assertEqual('/bar/baz', path)
  518. def test_tcp_port(self):
  519. c, path = get_transport_and_path_from_url('git://foo.com:1234/bar/baz')
  520. self.assertTrue(isinstance(c, TCPGitClient))
  521. self.assertEqual('foo.com', c._host)
  522. self.assertEqual(1234, c._port)
  523. self.assertEqual('/bar/baz', path)
  524. def test_ssh_explicit(self):
  525. c, path = get_transport_and_path_from_url('git+ssh://foo.com/bar/baz')
  526. self.assertTrue(isinstance(c, SSHGitClient))
  527. self.assertEqual('foo.com', c.host)
  528. self.assertEqual(None, c.port)
  529. self.assertEqual(None, c.username)
  530. self.assertEqual('/bar/baz', path)
  531. def test_ssh_port_explicit(self):
  532. c, path = get_transport_and_path_from_url(
  533. 'git+ssh://foo.com:1234/bar/baz')
  534. self.assertTrue(isinstance(c, SSHGitClient))
  535. self.assertEqual('foo.com', c.host)
  536. self.assertEqual(1234, c.port)
  537. self.assertEqual('/bar/baz', path)
  538. def test_ssh_homepath(self):
  539. c, path = get_transport_and_path_from_url(
  540. 'git+ssh://foo.com/~/bar/baz')
  541. self.assertTrue(isinstance(c, SSHGitClient))
  542. self.assertEqual('foo.com', c.host)
  543. self.assertEqual(None, c.port)
  544. self.assertEqual(None, c.username)
  545. self.assertEqual('/~/bar/baz', path)
  546. def test_ssh_port_homepath(self):
  547. c, path = get_transport_and_path_from_url(
  548. 'git+ssh://foo.com:1234/~/bar/baz')
  549. self.assertTrue(isinstance(c, SSHGitClient))
  550. self.assertEqual('foo.com', c.host)
  551. self.assertEqual(1234, c.port)
  552. self.assertEqual('/~/bar/baz', path)
  553. def test_ssh_host_relpath(self):
  554. self.assertRaises(
  555. ValueError, get_transport_and_path_from_url,
  556. 'foo.com:bar/baz')
  557. def test_ssh_user_host_relpath(self):
  558. self.assertRaises(
  559. ValueError, get_transport_and_path_from_url,
  560. 'user@foo.com:bar/baz')
  561. def test_local_path(self):
  562. self.assertRaises(
  563. ValueError, get_transport_and_path_from_url,
  564. 'foo.bar/baz')
  565. def test_error(self):
  566. # Need to use a known urlparse.uses_netloc URL scheme to get the
  567. # expected parsing of the URL on Python versions less than 2.6.5
  568. self.assertRaises(
  569. ValueError, get_transport_and_path_from_url,
  570. 'prospero://bar/baz')
  571. def test_http(self):
  572. url = 'https://github.com/jelmer/dulwich'
  573. c, path = get_transport_and_path_from_url(url)
  574. self.assertTrue(isinstance(c, HttpGitClient))
  575. self.assertEqual('https://github.com', c.get_url(b'/'))
  576. self.assertEqual('/jelmer/dulwich', path)
  577. def test_http_port(self):
  578. url = 'https://github.com:9090/jelmer/dulwich'
  579. c, path = get_transport_and_path_from_url(url)
  580. self.assertEqual('https://github.com:9090', c.get_url(b'/'))
  581. self.assertTrue(isinstance(c, HttpGitClient))
  582. self.assertEqual('/jelmer/dulwich', path)
  583. def test_file(self):
  584. c, path = get_transport_and_path_from_url('file:///home/jelmer/foo')
  585. self.assertTrue(isinstance(c, LocalGitClient))
  586. self.assertEqual('/home/jelmer/foo', path)
  587. class TestSSHVendor(object):
  588. def __init__(self):
  589. self.host = None
  590. self.command = ""
  591. self.username = None
  592. self.port = None
  593. self.password = None
  594. self.key_filename = None
  595. def run_command(self, host, command, username=None, port=None,
  596. password=None, key_filename=None):
  597. self.host = host
  598. self.command = command
  599. self.username = username
  600. self.port = port
  601. self.password = password
  602. self.key_filename = key_filename
  603. class Subprocess:
  604. pass
  605. setattr(Subprocess, 'read', lambda: None)
  606. setattr(Subprocess, 'write', lambda: None)
  607. setattr(Subprocess, 'close', lambda: None)
  608. setattr(Subprocess, 'can_read', lambda: None)
  609. return Subprocess()
  610. class SSHGitClientTests(TestCase):
  611. def setUp(self):
  612. super(SSHGitClientTests, self).setUp()
  613. self.server = TestSSHVendor()
  614. self.real_vendor = client.get_ssh_vendor
  615. client.get_ssh_vendor = lambda: self.server
  616. self.client = SSHGitClient('git.samba.org')
  617. def tearDown(self):
  618. super(SSHGitClientTests, self).tearDown()
  619. client.get_ssh_vendor = self.real_vendor
  620. def test_get_url(self):
  621. path = '/tmp/repo.git'
  622. c = SSHGitClient('git.samba.org')
  623. url = c.get_url(path)
  624. self.assertEqual('ssh://git.samba.org/tmp/repo.git', url)
  625. def test_get_url_with_username_and_port(self):
  626. path = '/tmp/repo.git'
  627. c = SSHGitClient('git.samba.org', port=2222, username='user')
  628. url = c.get_url(path)
  629. self.assertEqual('ssh://user@git.samba.org:2222/tmp/repo.git', url)
  630. def test_default_command(self):
  631. self.assertEqual(
  632. b'git-upload-pack',
  633. self.client._get_cmd_path(b'upload-pack'))
  634. def test_alternative_command_path(self):
  635. self.client.alternative_paths[b'upload-pack'] = (
  636. b'/usr/lib/git/git-upload-pack')
  637. self.assertEqual(
  638. b'/usr/lib/git/git-upload-pack',
  639. self.client._get_cmd_path(b'upload-pack'))
  640. def test_alternative_command_path_spaces(self):
  641. self.client.alternative_paths[b'upload-pack'] = (
  642. b'/usr/lib/git/git-upload-pack -ibla')
  643. self.assertEqual(b"/usr/lib/git/git-upload-pack -ibla",
  644. self.client._get_cmd_path(b'upload-pack'))
  645. def test_connect(self):
  646. server = self.server
  647. client = self.client
  648. client.username = b"username"
  649. client.port = 1337
  650. client._connect(b"command", b"/path/to/repo")
  651. self.assertEqual(b"username", server.username)
  652. self.assertEqual(1337, server.port)
  653. self.assertEqual("git-command '/path/to/repo'", server.command)
  654. client._connect(b"relative-command", b"/~/path/to/repo")
  655. self.assertEqual("git-relative-command '~/path/to/repo'",
  656. server.command)
  657. class ReportStatusParserTests(TestCase):
  658. def test_invalid_pack(self):
  659. parser = ReportStatusParser()
  660. parser.handle_packet(b"unpack error - foo bar")
  661. parser.handle_packet(b"ok refs/foo/bar")
  662. parser.handle_packet(None)
  663. self.assertRaises(SendPackError, parser.check)
  664. def test_update_refs_error(self):
  665. parser = ReportStatusParser()
  666. parser.handle_packet(b"unpack ok")
  667. parser.handle_packet(b"ng refs/foo/bar need to pull")
  668. parser.handle_packet(None)
  669. self.assertRaises(UpdateRefsError, parser.check)
  670. def test_ok(self):
  671. parser = ReportStatusParser()
  672. parser.handle_packet(b"unpack ok")
  673. parser.handle_packet(b"ok refs/foo/bar")
  674. parser.handle_packet(None)
  675. parser.check()
  676. class LocalGitClientTests(TestCase):
  677. def test_get_url(self):
  678. path = "/tmp/repo.git"
  679. c = LocalGitClient()
  680. url = c.get_url(path)
  681. self.assertEqual('file:///tmp/repo.git', url)
  682. def test_fetch_into_empty(self):
  683. c = LocalGitClient()
  684. t = MemoryRepo()
  685. s = open_repo('a.git')
  686. self.addCleanup(tear_down_repo, s)
  687. self.assertEqual(s.get_refs(), c.fetch(s.path, t).refs)
  688. def test_fetch_empty(self):
  689. c = LocalGitClient()
  690. s = open_repo('a.git')
  691. self.addCleanup(tear_down_repo, s)
  692. out = BytesIO()
  693. walker = {}
  694. ret = c.fetch_pack(
  695. s.path, lambda heads: [], graph_walker=walker, pack_data=out.write)
  696. self.assertEqual({
  697. b'HEAD': b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  698. b'refs/heads/master': b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  699. b'refs/tags/mytag': b'28237f4dc30d0d462658d6b937b08a0f0b6ef55a',
  700. b'refs/tags/mytag-packed':
  701. b'b0931cadc54336e78a1d980420e3268903b57a50'
  702. }, ret.refs)
  703. self.assertEqual(
  704. {b'HEAD': b'refs/heads/master'},
  705. ret.symrefs)
  706. self.assertEqual(
  707. b"PACK\x00\x00\x00\x02\x00\x00\x00\x00\x02\x9d\x08"
  708. b"\x82;\xd8\xa8\xea\xb5\x10\xadj\xc7\\\x82<\xfd>\xd3\x1e",
  709. out.getvalue())
  710. def test_fetch_pack_none(self):
  711. c = LocalGitClient()
  712. s = open_repo('a.git')
  713. self.addCleanup(tear_down_repo, s)
  714. out = BytesIO()
  715. walker = MemoryRepo().get_graph_walker()
  716. ret = c.fetch_pack(
  717. s.path,
  718. lambda heads: [b"a90fa2d900a17e99b433217e988c4eb4a2e9a097"],
  719. graph_walker=walker, pack_data=out.write)
  720. self.assertEqual({b'HEAD': b'refs/heads/master'}, ret.symrefs)
  721. self.assertEqual({
  722. b'HEAD': b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  723. b'refs/heads/master': b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  724. b'refs/tags/mytag': b'28237f4dc30d0d462658d6b937b08a0f0b6ef55a',
  725. b'refs/tags/mytag-packed':
  726. b'b0931cadc54336e78a1d980420e3268903b57a50'
  727. }, ret.refs)
  728. # Hardcoding is not ideal, but we'll fix that some other day..
  729. self.assertTrue(out.getvalue().startswith(
  730. b'PACK\x00\x00\x00\x02\x00\x00\x00\x07'))
  731. def test_send_pack_without_changes(self):
  732. local = open_repo('a.git')
  733. self.addCleanup(tear_down_repo, local)
  734. target = open_repo('a.git')
  735. self.addCleanup(tear_down_repo, target)
  736. self.send_and_verify(b"master", local, target)
  737. def test_send_pack_with_changes(self):
  738. local = open_repo('a.git')
  739. self.addCleanup(tear_down_repo, local)
  740. target_path = tempfile.mkdtemp()
  741. self.addCleanup(shutil.rmtree, target_path)
  742. with Repo.init_bare(target_path) as target:
  743. self.send_and_verify(b"master", local, target)
  744. def test_get_refs(self):
  745. local = open_repo('refs.git')
  746. self.addCleanup(tear_down_repo, local)
  747. client = LocalGitClient()
  748. refs = client.get_refs(local.path)
  749. self.assertDictEqual(local.refs.as_dict(), refs)
  750. def send_and_verify(self, branch, local, target):
  751. """Send branch from local to remote repository and verify it worked."""
  752. client = LocalGitClient()
  753. ref_name = b"refs/heads/" + branch
  754. new_refs = client.send_pack(target.path,
  755. lambda _: {ref_name: local.refs[ref_name]},
  756. local.object_store.generate_pack_data)
  757. self.assertEqual(local.refs[ref_name], new_refs[ref_name])
  758. obj_local = local.get_object(new_refs[ref_name])
  759. obj_target = target.get_object(new_refs[ref_name])
  760. self.assertEqual(obj_local, obj_target)
  761. class HttpGitClientTests(TestCase):
  762. @staticmethod
  763. def b64encode(s):
  764. """Python 2/3 compatible Base64 encoder. Returns string."""
  765. try:
  766. return base64.b64encode(s)
  767. except TypeError:
  768. return base64.b64encode(s.encode('latin1')).decode('ascii')
  769. def test_get_url(self):
  770. base_url = 'https://github.com/jelmer/dulwich'
  771. path = '/jelmer/dulwich'
  772. c = HttpGitClient(base_url)
  773. url = c.get_url(path)
  774. self.assertEqual('https://github.com/jelmer/dulwich', url)
  775. def test_get_url_bytes_path(self):
  776. base_url = 'https://github.com/jelmer/dulwich'
  777. path_bytes = b'/jelmer/dulwich'
  778. c = HttpGitClient(base_url)
  779. url = c.get_url(path_bytes)
  780. self.assertEqual('https://github.com/jelmer/dulwich', url)
  781. def test_get_url_with_username_and_passwd(self):
  782. base_url = 'https://github.com/jelmer/dulwich'
  783. path = '/jelmer/dulwich'
  784. c = HttpGitClient(base_url, username='USERNAME', password='PASSWD')
  785. url = c.get_url(path)
  786. self.assertEqual('https://github.com/jelmer/dulwich', url)
  787. def test_init_username_passwd_set(self):
  788. url = 'https://github.com/jelmer/dulwich'
  789. c = HttpGitClient(url, config=None, username='user', password='passwd')
  790. self.assertEqual('user', c._username)
  791. self.assertEqual('passwd', c._password)
  792. basic_auth = c.pool_manager.headers['authorization']
  793. auth_string = '%s:%s' % ('user', 'passwd')
  794. b64_credentials = self.b64encode(auth_string)
  795. expected_basic_auth = 'Basic %s' % b64_credentials
  796. self.assertEqual(basic_auth, expected_basic_auth)
  797. def test_init_no_username_passwd(self):
  798. url = 'https://github.com/jelmer/dulwich'
  799. c = HttpGitClient(url, config=None)
  800. self.assertIs(None, c._username)
  801. self.assertIs(None, c._password)
  802. self.assertNotIn('authorization', c.pool_manager.headers)
  803. def test_from_parsedurl_on_url_with_quoted_credentials(self):
  804. original_username = 'john|the|first'
  805. quoted_username = urlquote(original_username)
  806. original_password = 'Ya#1$2%3'
  807. quoted_password = urlquote(original_password)
  808. url = 'https://{username}:{password}@github.com/jelmer/dulwich'.format(
  809. username=quoted_username,
  810. password=quoted_password
  811. )
  812. c = HttpGitClient.from_parsedurl(urlparse.urlparse(url))
  813. self.assertEqual(original_username, c._username)
  814. self.assertEqual(original_password, c._password)
  815. basic_auth = c.pool_manager.headers['authorization']
  816. auth_string = '%s:%s' % (original_username, original_password)
  817. b64_credentials = self.b64encode(auth_string)
  818. expected_basic_auth = 'Basic %s' % str(b64_credentials)
  819. self.assertEqual(basic_auth, expected_basic_auth)
  820. class TCPGitClientTests(TestCase):
  821. def test_get_url(self):
  822. host = 'github.com'
  823. path = '/jelmer/dulwich'
  824. c = TCPGitClient(host)
  825. url = c.get_url(path)
  826. self.assertEqual('git://github.com/jelmer/dulwich', url)
  827. def test_get_url_with_port(self):
  828. host = 'github.com'
  829. path = '/jelmer/dulwich'
  830. port = 9090
  831. c = TCPGitClient(host, port=port)
  832. url = c.get_url(path)
  833. self.assertEqual('git://github.com:9090/jelmer/dulwich', url)
  834. class DefaultUrllib3ManagerTest(TestCase):
  835. def test_no_config(self):
  836. manager = default_urllib3_manager(config=None)
  837. self.assertEqual(manager.connection_pool_kw['cert_reqs'],
  838. 'CERT_REQUIRED')
  839. def test_config_no_proxy(self):
  840. manager = default_urllib3_manager(config=ConfigDict())
  841. self.assertNotIsInstance(manager, urllib3.ProxyManager)
  842. def test_config_ssl(self):
  843. config = ConfigDict()
  844. config.set(b'http', b'sslVerify', b'true')
  845. manager = default_urllib3_manager(config=config)
  846. self.assertEqual(manager.connection_pool_kw['cert_reqs'],
  847. 'CERT_REQUIRED')
  848. def test_config_no_ssl(self):
  849. config = ConfigDict()
  850. config.set(b'http', b'sslVerify', b'false')
  851. manager = default_urllib3_manager(config=config)
  852. self.assertEqual(manager.connection_pool_kw['cert_reqs'],
  853. 'CERT_NONE')
  854. def test_config_proxy(self):
  855. config = ConfigDict()
  856. config.set(b'http', b'proxy', b'http://localhost:3128/')
  857. manager = default_urllib3_manager(config=config)
  858. self.assertIsInstance(manager, urllib3.ProxyManager)
  859. self.assertTrue(hasattr(manager, 'proxy'))
  860. self.assertEqual(manager.proxy.scheme, 'http')
  861. self.assertEqual(manager.proxy.host, 'localhost')
  862. self.assertEqual(manager.proxy.port, 3128)
  863. def test_config_no_verify_ssl(self):
  864. manager = default_urllib3_manager(config=None, cert_reqs="CERT_NONE")
  865. self.assertEqual(manager.connection_pool_kw['cert_reqs'], 'CERT_NONE')
  866. class SubprocessSSHVendorTests(TestCase):
  867. def setUp(self):
  868. # Monkey Patch client subprocess popen
  869. self._orig_popen = dulwich.client.subprocess.Popen
  870. dulwich.client.subprocess.Popen = DummyPopen
  871. def tearDown(self):
  872. dulwich.client.subprocess.Popen = self._orig_popen
  873. def test_run_command_dashes(self):
  874. vendor = SubprocessSSHVendor()
  875. self.assertRaises(StrangeHostname, vendor.run_command, '--weird-host',
  876. 'git-clone-url')
  877. def test_run_command_password(self):
  878. vendor = SubprocessSSHVendor()
  879. self.assertRaises(NotImplementedError, vendor.run_command, 'host',
  880. 'git-clone-url', password='12345')
  881. def test_run_command_password_and_privkey(self):
  882. vendor = SubprocessSSHVendor()
  883. self.assertRaises(NotImplementedError, vendor.run_command,
  884. 'host', 'git-clone-url',
  885. password='12345', key_filename='/tmp/id_rsa')
  886. def test_run_command_with_port_username_and_privkey(self):
  887. expected = ['ssh', '-x', '-p', '2200',
  888. '-i', '/tmp/id_rsa', 'user@host', 'git-clone-url']
  889. vendor = SubprocessSSHVendor()
  890. command = vendor.run_command(
  891. 'host', 'git-clone-url',
  892. username='user', port='2200',
  893. key_filename='/tmp/id_rsa')
  894. args = command.proc.args
  895. self.assertListEqual(expected, args[0])
  896. class PLinkSSHVendorTests(TestCase):
  897. def setUp(self):
  898. # Monkey Patch client subprocess popen
  899. self._orig_popen = dulwich.client.subprocess.Popen
  900. dulwich.client.subprocess.Popen = DummyPopen
  901. def tearDown(self):
  902. dulwich.client.subprocess.Popen = self._orig_popen
  903. def test_run_command_dashes(self):
  904. vendor = PLinkSSHVendor()
  905. self.assertRaises(StrangeHostname, vendor.run_command, '--weird-host',
  906. 'git-clone-url')
  907. def test_run_command_password_and_privkey(self):
  908. vendor = PLinkSSHVendor()
  909. warnings.simplefilter("always", UserWarning)
  910. self.addCleanup(warnings.resetwarnings)
  911. warnings_list, restore_warnings = setup_warning_catcher()
  912. self.addCleanup(restore_warnings)
  913. command = vendor.run_command(
  914. 'host', 'git-clone-url', password='12345',
  915. key_filename='/tmp/id_rsa')
  916. expected_warning = UserWarning(
  917. 'Invoking PLink with a password exposes the password in the '
  918. 'process list.')
  919. for w in warnings_list:
  920. if (type(w) == type(expected_warning) and
  921. w.args == expected_warning.args):
  922. break
  923. else:
  924. raise AssertionError(
  925. 'Expected warning %r not in %r' %
  926. (expected_warning, warnings_list))
  927. args = command.proc.args
  928. if sys.platform == 'win32':
  929. binary = ['plink.exe', '-ssh']
  930. else:
  931. binary = ['plink', '-ssh']
  932. expected = binary + [
  933. '-pw', '12345', '-i', '/tmp/id_rsa', 'host', 'git-clone-url']
  934. self.assertListEqual(expected, args[0])
  935. def test_run_command_password(self):
  936. if sys.platform == 'win32':
  937. binary = ['plink.exe', '-ssh']
  938. else:
  939. binary = ['plink', '-ssh']
  940. expected = binary + ['-pw', '12345', 'host', 'git-clone-url']
  941. vendor = PLinkSSHVendor()
  942. warnings.simplefilter("always", UserWarning)
  943. self.addCleanup(warnings.resetwarnings)
  944. warnings_list, restore_warnings = setup_warning_catcher()
  945. self.addCleanup(restore_warnings)
  946. command = vendor.run_command('host', 'git-clone-url', password='12345')
  947. expected_warning = UserWarning(
  948. 'Invoking PLink with a password exposes the password in the '
  949. 'process list.')
  950. for w in warnings_list:
  951. if (type(w) == type(expected_warning) and
  952. w.args == expected_warning.args):
  953. break
  954. else:
  955. raise AssertionError(
  956. 'Expected warning %r not in %r' %
  957. (expected_warning, warnings_list))
  958. args = command.proc.args
  959. self.assertListEqual(expected, args[0])
  960. def test_run_command_with_port_username_and_privkey(self):
  961. if sys.platform == 'win32':
  962. binary = ['plink.exe', '-ssh']
  963. else:
  964. binary = ['plink', '-ssh']
  965. expected = binary + [
  966. '-P', '2200', '-i', '/tmp/id_rsa',
  967. 'user@host', 'git-clone-url']
  968. vendor = PLinkSSHVendor()
  969. command = vendor.run_command(
  970. 'host', 'git-clone-url',
  971. username='user', port='2200',
  972. key_filename='/tmp/id_rsa')
  973. args = command.proc.args
  974. self.assertListEqual(expected, args[0])
  975. class RsyncUrlTests(TestCase):
  976. def test_simple(self):
  977. self.assertEqual(
  978. parse_rsync_url('foo:bar/path'),
  979. (None, 'foo', 'bar/path'))
  980. self.assertEqual(
  981. parse_rsync_url('user@foo:bar/path'),
  982. ('user', 'foo', 'bar/path'))
  983. def test_path(self):
  984. self.assertRaises(ValueError, parse_rsync_url, '/path')
  985. class CheckWantsTests(TestCase):
  986. def test_fine(self):
  987. check_wants(
  988. [b'2f3dc7a53fb752a6961d3a56683df46d4d3bf262'],
  989. {b'refs/heads/blah': b'2f3dc7a53fb752a6961d3a56683df46d4d3bf262'})
  990. def test_missing(self):
  991. self.assertRaises(
  992. InvalidWants, check_wants,
  993. [b'2f3dc7a53fb752a6961d3a56683df46d4d3bf262'],
  994. {b'refs/heads/blah': b'3f3dc7a53fb752a6961d3a56683df46d4d3bf262'})
  995. def test_annotated(self):
  996. self.assertRaises(
  997. InvalidWants, check_wants,
  998. [b'2f3dc7a53fb752a6961d3a56683df46d4d3bf262'],
  999. {b'refs/heads/blah': b'3f3dc7a53fb752a6961d3a56683df46d4d3bf262',
  1000. b'refs/heads/blah^{}':
  1001. b'2f3dc7a53fb752a6961d3a56683df46d4d3bf262'})