test_client.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. # test_client.py -- Tests for the git protocol, client side
  2. # Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
  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 sys
  22. import shutil
  23. import tempfile
  24. try:
  25. from urllib import quote as urlquote
  26. except ImportError:
  27. from urllib.parse import quote as urlquote
  28. try:
  29. import urlparse
  30. except ImportError:
  31. import urllib.parse as urlparse
  32. import dulwich
  33. from dulwich import (
  34. client,
  35. )
  36. from dulwich.client import (
  37. LocalGitClient,
  38. TraditionalGitClient,
  39. TCPGitClient,
  40. SSHGitClient,
  41. HttpGitClient,
  42. ReportStatusParser,
  43. SendPackError,
  44. UpdateRefsError,
  45. get_transport_and_path,
  46. get_transport_and_path_from_url,
  47. )
  48. from dulwich.tests import (
  49. TestCase,
  50. )
  51. from dulwich.protocol import (
  52. TCP_GIT_PORT,
  53. Protocol,
  54. )
  55. from dulwich.pack import (
  56. write_pack_objects,
  57. )
  58. from dulwich.objects import (
  59. Commit,
  60. Tree
  61. )
  62. from dulwich.repo import (
  63. MemoryRepo,
  64. Repo,
  65. )
  66. from dulwich.tests import skipIf
  67. from dulwich.tests.utils import (
  68. open_repo,
  69. tear_down_repo,
  70. )
  71. class DummyClient(TraditionalGitClient):
  72. def __init__(self, can_read, read, write):
  73. self.can_read = can_read
  74. self.read = read
  75. self.write = write
  76. TraditionalGitClient.__init__(self)
  77. def _connect(self, service, path):
  78. return Protocol(self.read, self.write), self.can_read
  79. # TODO(durin42): add unit-level tests of GitClient
  80. class GitClientTests(TestCase):
  81. def setUp(self):
  82. super(GitClientTests, self).setUp()
  83. self.rout = BytesIO()
  84. self.rin = BytesIO()
  85. self.client = DummyClient(lambda x: True, self.rin.read,
  86. self.rout.write)
  87. def test_caps(self):
  88. agent_cap = ('agent=dulwich/%d.%d.%d' % dulwich.__version__).encode('ascii')
  89. self.assertEqual(set([b'multi_ack', b'side-band-64k', b'ofs-delta',
  90. b'thin-pack', b'multi_ack_detailed',
  91. agent_cap]),
  92. set(self.client._fetch_capabilities))
  93. self.assertEqual(set([b'ofs-delta', b'report-status', b'side-band-64k',
  94. agent_cap]),
  95. set(self.client._send_capabilities))
  96. def test_archive_ack(self):
  97. self.rin.write(
  98. b'0009NACK\n'
  99. b'0000')
  100. self.rin.seek(0)
  101. self.client.archive(b'bla', b'HEAD', None, None)
  102. self.assertEqual(self.rout.getvalue(), b'0011argument HEAD0000')
  103. def test_fetch_empty(self):
  104. self.rin.write(b'0000')
  105. self.rin.seek(0)
  106. def check_heads(heads):
  107. self.assertIs(heads, None)
  108. return []
  109. ret = self.client.fetch_pack(b'/', check_heads, None, None)
  110. self.assertIs(None, ret)
  111. def test_fetch_pack_ignores_magic_ref(self):
  112. self.rin.write(
  113. b'00000000000000000000000000000000000000000000 capabilities^{}\x00 multi_ack '
  114. b'thin-pack side-band side-band-64k ofs-delta shallow no-progress '
  115. b'include-tag\n'
  116. b'0000')
  117. self.rin.seek(0)
  118. def check_heads(heads):
  119. self.assertEquals({}, heads)
  120. return []
  121. ret = self.client.fetch_pack(b'bla', check_heads, None, None, None)
  122. self.assertIs(None, ret)
  123. self.assertEqual(self.rout.getvalue(), b'0000')
  124. def test_fetch_pack_none(self):
  125. self.rin.write(
  126. b'008855dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 HEAD.multi_ack '
  127. b'thin-pack side-band side-band-64k ofs-delta shallow no-progress '
  128. b'include-tag\n'
  129. b'0000')
  130. self.rin.seek(0)
  131. self.client.fetch_pack(b'bla', lambda heads: [], None, None, None)
  132. self.assertEqual(self.rout.getvalue(), b'0000')
  133. def test_send_pack_no_sideband64k_with_update_ref_error(self):
  134. # No side-bank-64k reported by server shouldn't try to parse
  135. # side band data
  136. pkts = [b'55dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 capabilities^{}'
  137. b'\x00 report-status delete-refs ofs-delta\n',
  138. b'',
  139. b"unpack ok",
  140. b"ng refs/foo/bar pre-receive hook declined",
  141. b'']
  142. for pkt in pkts:
  143. if pkt == b'':
  144. self.rin.write(b"0000")
  145. else:
  146. self.rin.write(("%04x" % (len(pkt)+4)).encode('ascii') + pkt)
  147. self.rin.seek(0)
  148. tree = Tree()
  149. commit = Commit()
  150. commit.tree = tree
  151. commit.parents = []
  152. commit.author = commit.committer = b'test user'
  153. commit.commit_time = commit.author_time = 1174773719
  154. commit.commit_timezone = commit.author_timezone = 0
  155. commit.encoding = b'UTF-8'
  156. commit.message = b'test message'
  157. def determine_wants(refs):
  158. return {b'refs/foo/bar': commit.id, }
  159. def generate_pack_contents(have, want):
  160. return [(commit, None), (tree, ''), ]
  161. self.assertRaises(UpdateRefsError,
  162. self.client.send_pack, "blah",
  163. determine_wants, generate_pack_contents)
  164. def test_send_pack_none(self):
  165. self.rin.write(
  166. b'0078310ca9477129b8586fa2afc779c1f57cf64bba6c '
  167. b'refs/heads/master\x00 report-status delete-refs '
  168. b'side-band-64k quiet ofs-delta\n'
  169. b'0000')
  170. self.rin.seek(0)
  171. def determine_wants(refs):
  172. return {
  173. b'refs/heads/master': b'310ca9477129b8586fa2afc779c1f57cf64bba6c'
  174. }
  175. def generate_pack_contents(have, want):
  176. return {}
  177. self.client.send_pack(b'/', determine_wants, generate_pack_contents)
  178. self.assertEqual(self.rout.getvalue(), b'0000')
  179. def test_send_pack_keep_and_delete(self):
  180. self.rin.write(
  181. b'0063310ca9477129b8586fa2afc779c1f57cf64bba6c '
  182. b'refs/heads/master\x00report-status delete-refs ofs-delta\n'
  183. b'003f310ca9477129b8586fa2afc779c1f57cf64bba6c refs/heads/keepme\n'
  184. b'0000000eunpack ok\n'
  185. b'0019ok refs/heads/master\n'
  186. b'0000')
  187. self.rin.seek(0)
  188. def determine_wants(refs):
  189. return {b'refs/heads/master': b'0' * 40}
  190. def generate_pack_contents(have, want):
  191. return {}
  192. self.client.send_pack(b'/', determine_wants, generate_pack_contents)
  193. self.assertIn(
  194. self.rout.getvalue(),
  195. [b'007f310ca9477129b8586fa2afc779c1f57cf64bba6c '
  196. b'0000000000000000000000000000000000000000 '
  197. b'refs/heads/master\x00report-status ofs-delta0000',
  198. b'007f310ca9477129b8586fa2afc779c1f57cf64bba6c '
  199. b'0000000000000000000000000000000000000000 '
  200. b'refs/heads/master\x00ofs-delta report-status0000'])
  201. def test_send_pack_delete_only(self):
  202. self.rin.write(
  203. b'0063310ca9477129b8586fa2afc779c1f57cf64bba6c '
  204. b'refs/heads/master\x00report-status delete-refs ofs-delta\n'
  205. b'0000000eunpack ok\n'
  206. b'0019ok refs/heads/master\n'
  207. b'0000')
  208. self.rin.seek(0)
  209. def determine_wants(refs):
  210. return {b'refs/heads/master': b'0' * 40}
  211. def generate_pack_contents(have, want):
  212. return {}
  213. self.client.send_pack(b'/', determine_wants, generate_pack_contents)
  214. self.assertIn(
  215. self.rout.getvalue(),
  216. [b'007f310ca9477129b8586fa2afc779c1f57cf64bba6c '
  217. b'0000000000000000000000000000000000000000 '
  218. b'refs/heads/master\x00report-status ofs-delta0000',
  219. b'007f310ca9477129b8586fa2afc779c1f57cf64bba6c '
  220. b'0000000000000000000000000000000000000000 '
  221. b'refs/heads/master\x00ofs-delta report-status0000'])
  222. def test_send_pack_new_ref_only(self):
  223. self.rin.write(
  224. b'0063310ca9477129b8586fa2afc779c1f57cf64bba6c '
  225. b'refs/heads/master\x00report-status delete-refs ofs-delta\n'
  226. b'0000000eunpack ok\n'
  227. b'0019ok refs/heads/blah12\n'
  228. b'0000')
  229. self.rin.seek(0)
  230. def determine_wants(refs):
  231. return {
  232. b'refs/heads/blah12':
  233. b'310ca9477129b8586fa2afc779c1f57cf64bba6c',
  234. b'refs/heads/master': b'310ca9477129b8586fa2afc779c1f57cf64bba6c'
  235. }
  236. def generate_pack_contents(have, want):
  237. return {}
  238. f = BytesIO()
  239. write_pack_objects(f, {})
  240. self.client.send_pack('/', determine_wants, generate_pack_contents)
  241. self.assertIn(
  242. self.rout.getvalue(),
  243. [b'007f0000000000000000000000000000000000000000 '
  244. b'310ca9477129b8586fa2afc779c1f57cf64bba6c '
  245. b'refs/heads/blah12\x00report-status ofs-delta0000' +
  246. f.getvalue(),
  247. b'007f0000000000000000000000000000000000000000 '
  248. b'310ca9477129b8586fa2afc779c1f57cf64bba6c '
  249. b'refs/heads/blah12\x00ofs-delta report-status0000' +
  250. f.getvalue()])
  251. def test_send_pack_new_ref(self):
  252. self.rin.write(
  253. b'0064310ca9477129b8586fa2afc779c1f57cf64bba6c '
  254. b'refs/heads/master\x00 report-status delete-refs ofs-delta\n'
  255. b'0000000eunpack ok\n'
  256. b'0019ok refs/heads/blah12\n'
  257. b'0000')
  258. self.rin.seek(0)
  259. tree = Tree()
  260. commit = Commit()
  261. commit.tree = tree
  262. commit.parents = []
  263. commit.author = commit.committer = b'test user'
  264. commit.commit_time = commit.author_time = 1174773719
  265. commit.commit_timezone = commit.author_timezone = 0
  266. commit.encoding = b'UTF-8'
  267. commit.message = b'test message'
  268. def determine_wants(refs):
  269. return {
  270. b'refs/heads/blah12': commit.id,
  271. b'refs/heads/master': b'310ca9477129b8586fa2afc779c1f57cf64bba6c'
  272. }
  273. def generate_pack_contents(have, want):
  274. return [(commit, None), (tree, b''), ]
  275. f = BytesIO()
  276. write_pack_objects(f, generate_pack_contents(None, None))
  277. self.client.send_pack(b'/', determine_wants, generate_pack_contents)
  278. self.assertIn(
  279. self.rout.getvalue(),
  280. [b'007f0000000000000000000000000000000000000000 ' + commit.id +
  281. b' refs/heads/blah12\x00report-status ofs-delta0000' + f.getvalue(),
  282. b'007f0000000000000000000000000000000000000000 ' + commit.id +
  283. b' refs/heads/blah12\x00ofs-delta report-status0000' + f.getvalue()])
  284. def test_send_pack_no_deleteref_delete_only(self):
  285. pkts = [b'310ca9477129b8586fa2afc779c1f57cf64bba6c refs/heads/master'
  286. b'\x00 report-status ofs-delta\n',
  287. b'',
  288. b'']
  289. for pkt in pkts:
  290. if pkt == b'':
  291. self.rin.write(b"0000")
  292. else:
  293. self.rin.write(("%04x" % (len(pkt)+4)).encode('ascii') + pkt)
  294. self.rin.seek(0)
  295. def determine_wants(refs):
  296. return {b'refs/heads/master': b'0' * 40}
  297. def generate_pack_contents(have, want):
  298. return {}
  299. self.assertRaises(UpdateRefsError,
  300. self.client.send_pack, b"/",
  301. determine_wants, generate_pack_contents)
  302. self.assertEqual(self.rout.getvalue(), b'0000')
  303. class TestGetTransportAndPath(TestCase):
  304. def test_tcp(self):
  305. c, path = get_transport_and_path('git://foo.com/bar/baz')
  306. self.assertTrue(isinstance(c, TCPGitClient))
  307. self.assertEqual('foo.com', c._host)
  308. self.assertEqual(TCP_GIT_PORT, c._port)
  309. self.assertEqual('/bar/baz', path)
  310. def test_tcp_port(self):
  311. c, path = get_transport_and_path('git://foo.com:1234/bar/baz')
  312. self.assertTrue(isinstance(c, TCPGitClient))
  313. self.assertEqual('foo.com', c._host)
  314. self.assertEqual(1234, c._port)
  315. self.assertEqual('/bar/baz', path)
  316. def test_git_ssh_explicit(self):
  317. c, path = get_transport_and_path('git+ssh://foo.com/bar/baz')
  318. self.assertTrue(isinstance(c, SSHGitClient))
  319. self.assertEqual('foo.com', c.host)
  320. self.assertEqual(None, c.port)
  321. self.assertEqual(None, c.username)
  322. self.assertEqual('/bar/baz', path)
  323. def test_ssh_explicit(self):
  324. c, path = get_transport_and_path('ssh://foo.com/bar/baz')
  325. self.assertTrue(isinstance(c, SSHGitClient))
  326. self.assertEqual('foo.com', c.host)
  327. self.assertEqual(None, c.port)
  328. self.assertEqual(None, c.username)
  329. self.assertEqual('/bar/baz', path)
  330. def test_ssh_port_explicit(self):
  331. c, path = get_transport_and_path(
  332. 'git+ssh://foo.com:1234/bar/baz')
  333. self.assertTrue(isinstance(c, SSHGitClient))
  334. self.assertEqual('foo.com', c.host)
  335. self.assertEqual(1234, c.port)
  336. self.assertEqual('/bar/baz', path)
  337. def test_username_and_port_explicit_unknown_scheme(self):
  338. c, path = get_transport_and_path(
  339. 'unknown://git@server:7999/dply/stuff.git')
  340. self.assertTrue(isinstance(c, SSHGitClient))
  341. self.assertEqual('unknown', c.host)
  342. self.assertEqual('//git@server:7999/dply/stuff.git', path)
  343. def test_username_and_port_explicit(self):
  344. c, path = get_transport_and_path(
  345. 'ssh://git@server:7999/dply/stuff.git')
  346. self.assertTrue(isinstance(c, SSHGitClient))
  347. self.assertEqual('git', c.username)
  348. self.assertEqual('server', c.host)
  349. self.assertEqual(7999, c.port)
  350. self.assertEqual('/dply/stuff.git', path)
  351. def test_ssh_abspath_doubleslash(self):
  352. c, path = get_transport_and_path('git+ssh://foo.com//bar/baz')
  353. self.assertTrue(isinstance(c, SSHGitClient))
  354. self.assertEqual('foo.com', c.host)
  355. self.assertEqual(None, c.port)
  356. self.assertEqual(None, c.username)
  357. self.assertEqual('//bar/baz', path)
  358. def test_ssh_port(self):
  359. c, path = get_transport_and_path(
  360. 'git+ssh://foo.com:1234/bar/baz')
  361. self.assertTrue(isinstance(c, SSHGitClient))
  362. self.assertEqual('foo.com', c.host)
  363. self.assertEqual(1234, c.port)
  364. self.assertEqual('/bar/baz', path)
  365. def test_ssh_implicit(self):
  366. c, path = get_transport_and_path('foo:/bar/baz')
  367. self.assertTrue(isinstance(c, SSHGitClient))
  368. self.assertEqual('foo', c.host)
  369. self.assertEqual(None, c.port)
  370. self.assertEqual(None, c.username)
  371. self.assertEqual('/bar/baz', path)
  372. def test_ssh_host(self):
  373. c, path = get_transport_and_path('foo.com:/bar/baz')
  374. self.assertTrue(isinstance(c, SSHGitClient))
  375. self.assertEqual('foo.com', c.host)
  376. self.assertEqual(None, c.port)
  377. self.assertEqual(None, c.username)
  378. self.assertEqual('/bar/baz', path)
  379. def test_ssh_user_host(self):
  380. c, path = get_transport_and_path('user@foo.com:/bar/baz')
  381. self.assertTrue(isinstance(c, SSHGitClient))
  382. self.assertEqual('foo.com', c.host)
  383. self.assertEqual(None, c.port)
  384. self.assertEqual('user', c.username)
  385. self.assertEqual('/bar/baz', path)
  386. def test_ssh_relpath(self):
  387. c, path = get_transport_and_path('foo:bar/baz')
  388. self.assertTrue(isinstance(c, SSHGitClient))
  389. self.assertEqual('foo', c.host)
  390. self.assertEqual(None, c.port)
  391. self.assertEqual(None, c.username)
  392. self.assertEqual('bar/baz', path)
  393. def test_ssh_host_relpath(self):
  394. c, path = get_transport_and_path('foo.com:bar/baz')
  395. self.assertTrue(isinstance(c, SSHGitClient))
  396. self.assertEqual('foo.com', c.host)
  397. self.assertEqual(None, c.port)
  398. self.assertEqual(None, c.username)
  399. self.assertEqual('bar/baz', path)
  400. def test_ssh_user_host_relpath(self):
  401. c, path = get_transport_and_path('user@foo.com:bar/baz')
  402. self.assertTrue(isinstance(c, SSHGitClient))
  403. self.assertEqual('foo.com', c.host)
  404. self.assertEqual(None, c.port)
  405. self.assertEqual('user', c.username)
  406. self.assertEqual('bar/baz', path)
  407. def test_local(self):
  408. c, path = get_transport_and_path('foo.bar/baz')
  409. self.assertTrue(isinstance(c, LocalGitClient))
  410. self.assertEqual('foo.bar/baz', path)
  411. @skipIf(sys.platform != 'win32', 'Behaviour only happens on windows.')
  412. def test_local_abs_windows_path(self):
  413. c, path = get_transport_and_path('C:\\foo.bar\\baz')
  414. self.assertTrue(isinstance(c, LocalGitClient))
  415. self.assertEqual('C:\\foo.bar\\baz', path)
  416. def test_error(self):
  417. # Need to use a known urlparse.uses_netloc URL scheme to get the
  418. # expected parsing of the URL on Python versions less than 2.6.5
  419. c, path = get_transport_and_path('prospero://bar/baz')
  420. self.assertTrue(isinstance(c, SSHGitClient))
  421. def test_http(self):
  422. url = 'https://github.com/jelmer/dulwich'
  423. c, path = get_transport_and_path(url)
  424. self.assertTrue(isinstance(c, HttpGitClient))
  425. self.assertEqual('/jelmer/dulwich', path)
  426. def test_http_auth(self):
  427. url = 'https://user:passwd@github.com/jelmer/dulwich'
  428. c, path = get_transport_and_path(url)
  429. self.assertTrue(isinstance(c, HttpGitClient))
  430. self.assertEqual('/jelmer/dulwich', path)
  431. self.assertEqual('user', c._username)
  432. self.assertEqual('passwd', c._password)
  433. def test_http_no_auth(self):
  434. url = 'https://github.com/jelmer/dulwich'
  435. c, path = get_transport_and_path(url)
  436. self.assertTrue(isinstance(c, HttpGitClient))
  437. self.assertEqual('/jelmer/dulwich', path)
  438. self.assertIs(None, c._username)
  439. self.assertIs(None, c._password)
  440. class TestGetTransportAndPathFromUrl(TestCase):
  441. def test_tcp(self):
  442. c, path = get_transport_and_path_from_url('git://foo.com/bar/baz')
  443. self.assertTrue(isinstance(c, TCPGitClient))
  444. self.assertEqual('foo.com', c._host)
  445. self.assertEqual(TCP_GIT_PORT, c._port)
  446. self.assertEqual('/bar/baz', path)
  447. def test_tcp_port(self):
  448. c, path = get_transport_and_path_from_url('git://foo.com:1234/bar/baz')
  449. self.assertTrue(isinstance(c, TCPGitClient))
  450. self.assertEqual('foo.com', c._host)
  451. self.assertEqual(1234, c._port)
  452. self.assertEqual('/bar/baz', path)
  453. def test_ssh_explicit(self):
  454. c, path = get_transport_and_path_from_url('git+ssh://foo.com/bar/baz')
  455. self.assertTrue(isinstance(c, SSHGitClient))
  456. self.assertEqual('foo.com', c.host)
  457. self.assertEqual(None, c.port)
  458. self.assertEqual(None, c.username)
  459. self.assertEqual('/bar/baz', path)
  460. def test_ssh_port_explicit(self):
  461. c, path = get_transport_and_path_from_url(
  462. 'git+ssh://foo.com:1234/bar/baz')
  463. self.assertTrue(isinstance(c, SSHGitClient))
  464. self.assertEqual('foo.com', c.host)
  465. self.assertEqual(1234, c.port)
  466. self.assertEqual('/bar/baz', path)
  467. def test_ssh_homepath(self):
  468. c, path = get_transport_and_path_from_url('git+ssh://foo.com/~/bar/baz')
  469. self.assertTrue(isinstance(c, SSHGitClient))
  470. self.assertEqual('foo.com', c.host)
  471. self.assertEqual(None, c.port)
  472. self.assertEqual(None, c.username)
  473. self.assertEqual('/~/bar/baz', path)
  474. def test_ssh_port_homepath(self):
  475. c, path = get_transport_and_path_from_url(
  476. 'git+ssh://foo.com:1234/~/bar/baz')
  477. self.assertTrue(isinstance(c, SSHGitClient))
  478. self.assertEqual('foo.com', c.host)
  479. self.assertEqual(1234, c.port)
  480. self.assertEqual('/~/bar/baz', path)
  481. def test_ssh_host_relpath(self):
  482. self.assertRaises(ValueError, get_transport_and_path_from_url,
  483. 'foo.com:bar/baz')
  484. def test_ssh_user_host_relpath(self):
  485. self.assertRaises(ValueError, get_transport_and_path_from_url,
  486. 'user@foo.com:bar/baz')
  487. def test_local_path(self):
  488. self.assertRaises(ValueError, get_transport_and_path_from_url,
  489. 'foo.bar/baz')
  490. def test_error(self):
  491. # Need to use a known urlparse.uses_netloc URL scheme to get the
  492. # expected parsing of the URL on Python versions less than 2.6.5
  493. self.assertRaises(ValueError, get_transport_and_path_from_url,
  494. 'prospero://bar/baz')
  495. def test_http(self):
  496. url = 'https://github.com/jelmer/dulwich'
  497. c, path = get_transport_and_path_from_url(url)
  498. self.assertTrue(isinstance(c, HttpGitClient))
  499. self.assertEqual('/jelmer/dulwich', path)
  500. def test_file(self):
  501. c, path = get_transport_and_path_from_url('file:///home/jelmer/foo')
  502. self.assertTrue(isinstance(c, LocalGitClient))
  503. self.assertEqual('/home/jelmer/foo', path)
  504. class TestSSHVendor(object):
  505. def __init__(self):
  506. self.host = None
  507. self.command = ""
  508. self.username = None
  509. self.port = None
  510. def run_command(self, host, command, username=None, port=None):
  511. if not isinstance(command, bytes):
  512. raise TypeError(command)
  513. self.host = host
  514. self.command = command
  515. self.username = username
  516. self.port = port
  517. class Subprocess: pass
  518. setattr(Subprocess, 'read', lambda: None)
  519. setattr(Subprocess, 'write', lambda: None)
  520. setattr(Subprocess, 'close', lambda: None)
  521. setattr(Subprocess, 'can_read', lambda: None)
  522. return Subprocess()
  523. class SSHGitClientTests(TestCase):
  524. def setUp(self):
  525. super(SSHGitClientTests, self).setUp()
  526. self.server = TestSSHVendor()
  527. self.real_vendor = client.get_ssh_vendor
  528. client.get_ssh_vendor = lambda: self.server
  529. self.client = SSHGitClient('git.samba.org')
  530. def tearDown(self):
  531. super(SSHGitClientTests, self).tearDown()
  532. client.get_ssh_vendor = self.real_vendor
  533. def test_get_url(self):
  534. path = '/tmp/repo.git'
  535. c = SSHGitClient('git.samba.org')
  536. url = c.get_url(path)
  537. self.assertEqual('ssh://git.samba.org/tmp/repo.git', url)
  538. def test_get_url_with_username_and_port(self):
  539. path = '/tmp/repo.git'
  540. c = SSHGitClient('git.samba.org', port=2222, username='user')
  541. url = c.get_url(path)
  542. self.assertEqual('ssh://user@git.samba.org:2222/tmp/repo.git', url)
  543. def test_default_command(self):
  544. self.assertEqual(b'git-upload-pack',
  545. self.client._get_cmd_path(b'upload-pack'))
  546. def test_alternative_command_path(self):
  547. self.client.alternative_paths[b'upload-pack'] = (
  548. b'/usr/lib/git/git-upload-pack')
  549. self.assertEqual(b'/usr/lib/git/git-upload-pack',
  550. self.client._get_cmd_path(b'upload-pack'))
  551. def test_alternative_command_path_spaces(self):
  552. self.client.alternative_paths[b'upload-pack'] = (
  553. b'/usr/lib/git/git-upload-pack -ibla')
  554. self.assertEqual(b"/usr/lib/git/git-upload-pack -ibla",
  555. self.client._get_cmd_path(b'upload-pack'))
  556. def test_connect(self):
  557. server = self.server
  558. client = self.client
  559. client.username = b"username"
  560. client.port = 1337
  561. client._connect(b"command", b"/path/to/repo")
  562. self.assertEqual(b"username", server.username)
  563. self.assertEqual(1337, server.port)
  564. self.assertEqual(b"git-command '/path/to/repo'", server.command)
  565. client._connect(b"relative-command", b"/~/path/to/repo")
  566. self.assertEqual(b"git-relative-command '~/path/to/repo'",
  567. server.command)
  568. class ReportStatusParserTests(TestCase):
  569. def test_invalid_pack(self):
  570. parser = ReportStatusParser()
  571. parser.handle_packet(b"unpack error - foo bar")
  572. parser.handle_packet(b"ok refs/foo/bar")
  573. parser.handle_packet(None)
  574. self.assertRaises(SendPackError, parser.check)
  575. def test_update_refs_error(self):
  576. parser = ReportStatusParser()
  577. parser.handle_packet(b"unpack ok")
  578. parser.handle_packet(b"ng refs/foo/bar need to pull")
  579. parser.handle_packet(None)
  580. self.assertRaises(UpdateRefsError, parser.check)
  581. def test_ok(self):
  582. parser = ReportStatusParser()
  583. parser.handle_packet(b"unpack ok")
  584. parser.handle_packet(b"ok refs/foo/bar")
  585. parser.handle_packet(None)
  586. parser.check()
  587. class LocalGitClientTests(TestCase):
  588. def test_get_url(self):
  589. path = "/tmp/repo.git"
  590. c = LocalGitClient()
  591. url = c.get_url(path)
  592. self.assertEqual('file:///tmp/repo.git', url)
  593. def test_fetch_into_empty(self):
  594. c = LocalGitClient()
  595. t = MemoryRepo()
  596. s = open_repo('a.git')
  597. self.addCleanup(tear_down_repo, s)
  598. self.assertEqual(s.get_refs(), c.fetch(s.path, t))
  599. def test_fetch_empty(self):
  600. c = LocalGitClient()
  601. s = open_repo('a.git')
  602. self.addCleanup(tear_down_repo, s)
  603. out = BytesIO()
  604. walker = {}
  605. ret = c.fetch_pack(s.path, lambda heads: [], graph_walker=walker,
  606. pack_data=out.write)
  607. self.assertEqual({
  608. b'HEAD': b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  609. b'refs/heads/master': b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  610. b'refs/tags/mytag': b'28237f4dc30d0d462658d6b937b08a0f0b6ef55a',
  611. b'refs/tags/mytag-packed': b'b0931cadc54336e78a1d980420e3268903b57a50'
  612. }, ret)
  613. self.assertEqual(b"PACK\x00\x00\x00\x02\x00\x00\x00\x00\x02\x9d\x08"
  614. b"\x82;\xd8\xa8\xea\xb5\x10\xadj\xc7\\\x82<\xfd>\xd3\x1e", out.getvalue())
  615. def test_fetch_pack_none(self):
  616. c = LocalGitClient()
  617. s = open_repo('a.git')
  618. self.addCleanup(tear_down_repo, s)
  619. out = BytesIO()
  620. walker = MemoryRepo().get_graph_walker()
  621. c.fetch_pack(s.path,
  622. lambda heads: [b"a90fa2d900a17e99b433217e988c4eb4a2e9a097"],
  623. graph_walker=walker, pack_data=out.write)
  624. # Hardcoding is not ideal, but we'll fix that some other day..
  625. self.assertTrue(out.getvalue().startswith(b'PACK\x00\x00\x00\x02\x00\x00\x00\x07'))
  626. def test_send_pack_without_changes(self):
  627. local = open_repo('a.git')
  628. self.addCleanup(tear_down_repo, local)
  629. target = open_repo('a.git')
  630. self.addCleanup(tear_down_repo, target)
  631. self.send_and_verify(b"master", local, target)
  632. def test_send_pack_with_changes(self):
  633. local = open_repo('a.git')
  634. self.addCleanup(tear_down_repo, local)
  635. target_path = tempfile.mkdtemp()
  636. self.addCleanup(shutil.rmtree, target_path)
  637. with Repo.init_bare(target_path) as target:
  638. self.send_and_verify(b"master", local, target)
  639. def test_get_refs(self):
  640. local = open_repo('refs.git')
  641. self.addCleanup(tear_down_repo, local)
  642. client = LocalGitClient()
  643. refs = client.get_refs(local.path)
  644. self.assertDictEqual(local.refs.as_dict(), refs)
  645. def send_and_verify(self, branch, local, target):
  646. """Send a branch from local to remote repository and verify it worked."""
  647. client = LocalGitClient()
  648. ref_name = b"refs/heads/" + branch
  649. new_refs = client.send_pack(target.path,
  650. lambda _: { ref_name: local.refs[ref_name] },
  651. local.object_store.generate_pack_contents)
  652. self.assertEqual(local.refs[ref_name], new_refs[ref_name])
  653. obj_local = local.get_object(new_refs[ref_name])
  654. obj_target = target.get_object(new_refs[ref_name])
  655. self.assertEqual(obj_local, obj_target)
  656. class HttpGitClientTests(TestCase):
  657. def test_get_url(self):
  658. base_url = 'https://github.com/jelmer/dulwich'
  659. path = '/jelmer/dulwich'
  660. c = HttpGitClient(base_url)
  661. url = c.get_url(path)
  662. self.assertEqual('https://github.com/jelmer/dulwich', url)
  663. def test_get_url_with_username_and_passwd(self):
  664. base_url = 'https://github.com/jelmer/dulwich'
  665. path = '/jelmer/dulwich'
  666. c = HttpGitClient(base_url, username='USERNAME', password='PASSWD')
  667. url = c.get_url(path)
  668. self.assertEqual('https://github.com/jelmer/dulwich', url)
  669. def test_init_username_passwd_set(self):
  670. url = 'https://github.com/jelmer/dulwich'
  671. c = HttpGitClient(url, config=None, username='user', password='passwd')
  672. self.assertEqual('user', c._username)
  673. self.assertEqual('passwd', c._password)
  674. [pw_handler] = [
  675. h for h in c.opener.handlers if getattr(h, 'passwd', None) is not None]
  676. self.assertEqual(
  677. ('user', 'passwd'),
  678. pw_handler.passwd.find_user_password(
  679. None, 'https://github.com/jelmer/dulwich'))
  680. def test_init_no_username_passwd(self):
  681. url = 'https://github.com/jelmer/dulwich'
  682. c = HttpGitClient(url, config=None)
  683. self.assertIs(None, c._username)
  684. self.assertIs(None, c._password)
  685. pw_handler = [
  686. h for h in c.opener.handlers if getattr(h, 'passwd', None) is not None]
  687. self.assertEqual(0, len(pw_handler))
  688. def test_from_parsedurl_on_url_with_quoted_credentials(self):
  689. original_username = 'john|the|first'
  690. quoted_username = urlquote(original_username)
  691. original_password = 'Ya#1$2%3'
  692. quoted_password = urlquote(original_password)
  693. url = 'https://{username}:{password}@github.com/jelmer/dulwich'.format(
  694. username=quoted_username,
  695. password=quoted_password
  696. )
  697. c = HttpGitClient.from_parsedurl(urlparse.urlparse(url))
  698. self.assertEqual(original_username, c._username)
  699. self.assertEqual(original_password, c._password)
  700. [pw_handler] = [
  701. h for h in c.opener.handlers if getattr(h, 'passwd', None) is not None]
  702. self.assertEqual(
  703. (original_username, original_password),
  704. pw_handler.passwd.find_user_password(
  705. None, 'https://github.com/jelmer/dulwich'))
  706. class TCPGitClientTests(TestCase):
  707. def test_get_url(self):
  708. host = 'github.com'
  709. path = '/jelmer/dulwich'
  710. c = TCPGitClient(host)
  711. url = c.get_url(path)
  712. self.assertEqual('git://github.com/jelmer/dulwich', url)
  713. def test_get_url_with_port(self):
  714. host = 'github.com'
  715. path = '/jelmer/dulwich'
  716. port = 9090
  717. c = TCPGitClient(host, port=9090)
  718. url = c.get_url(path)
  719. self.assertEqual('git://github.com:9090/jelmer/dulwich', url)