test_client.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. # test_client.py -- Tests for the git protocol, client side
  2. # Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # or (at your option) any later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. from io import BytesIO
  19. from dulwich import (
  20. client,
  21. )
  22. from dulwich.client import (
  23. LocalGitClient,
  24. TraditionalGitClient,
  25. TCPGitClient,
  26. SubprocessGitClient,
  27. SSHGitClient,
  28. HttpGitClient,
  29. ReportStatusParser,
  30. SendPackError,
  31. UpdateRefsError,
  32. get_transport_and_path,
  33. get_transport_and_path_from_url,
  34. )
  35. from dulwich.tests import (
  36. TestCase,
  37. )
  38. from dulwich.protocol import (
  39. TCP_GIT_PORT,
  40. Protocol,
  41. )
  42. from dulwich.pack import (
  43. write_pack_objects,
  44. )
  45. from dulwich.objects import (
  46. Commit,
  47. Tree
  48. )
  49. from dulwich.repo import MemoryRepo
  50. from dulwich.tests.utils import (
  51. open_repo,
  52. )
  53. class DummyClient(TraditionalGitClient):
  54. def __init__(self, can_read, read, write):
  55. self.can_read = can_read
  56. self.read = read
  57. self.write = write
  58. TraditionalGitClient.__init__(self)
  59. def _connect(self, service, path):
  60. return Protocol(self.read, self.write), self.can_read
  61. # TODO(durin42): add unit-level tests of GitClient
  62. class GitClientTests(TestCase):
  63. def setUp(self):
  64. super(GitClientTests, self).setUp()
  65. self.rout = BytesIO()
  66. self.rin = BytesIO()
  67. self.client = DummyClient(lambda x: True, self.rin.read,
  68. self.rout.write)
  69. def test_caps(self):
  70. self.assertEqual(set(['multi_ack', 'side-band-64k', 'ofs-delta',
  71. 'thin-pack', 'multi_ack_detailed']),
  72. set(self.client._fetch_capabilities))
  73. self.assertEqual(set(['ofs-delta', 'report-status', 'side-band-64k']),
  74. set(self.client._send_capabilities))
  75. def test_archive_ack(self):
  76. self.rin.write(
  77. '0009NACK\n'
  78. '0000')
  79. self.rin.seek(0)
  80. self.client.archive('bla', 'HEAD', None, None)
  81. self.assertEqual(self.rout.getvalue(), '0011argument HEAD0000')
  82. def test_fetch_empty(self):
  83. self.rin.write('0000')
  84. self.rin.seek(0)
  85. self.client.fetch_pack('/', lambda heads: [], None, None)
  86. def test_fetch_pack_none(self):
  87. self.rin.write(
  88. '008855dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 HEAD.multi_ack '
  89. 'thin-pack side-band side-band-64k ofs-delta shallow no-progress '
  90. 'include-tag\n'
  91. '0000')
  92. self.rin.seek(0)
  93. self.client.fetch_pack('bla', lambda heads: [], None, None, None)
  94. self.assertEqual(self.rout.getvalue(), '0000')
  95. def test_send_pack_no_sideband64k_with_update_ref_error(self):
  96. # No side-bank-64k reported by server shouldn't try to parse
  97. # side band data
  98. pkts = ['55dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 capabilities^{}'
  99. '\x00 report-status delete-refs ofs-delta\n',
  100. '',
  101. "unpack ok",
  102. "ng refs/foo/bar pre-receive hook declined",
  103. '']
  104. for pkt in pkts:
  105. if pkt == '':
  106. self.rin.write("0000")
  107. else:
  108. self.rin.write("%04x%s" % (len(pkt)+4, pkt))
  109. self.rin.seek(0)
  110. tree = Tree()
  111. commit = Commit()
  112. commit.tree = tree
  113. commit.parents = []
  114. commit.author = commit.committer = 'test user'
  115. commit.commit_time = commit.author_time = 1174773719
  116. commit.commit_timezone = commit.author_timezone = 0
  117. commit.encoding = 'UTF-8'
  118. commit.message = 'test message'
  119. def determine_wants(refs):
  120. return {'refs/foo/bar': commit.id, }
  121. def generate_pack_contents(have, want):
  122. return [(commit, None), (tree, ''), ]
  123. self.assertRaises(UpdateRefsError,
  124. self.client.send_pack, "blah",
  125. determine_wants, generate_pack_contents)
  126. def test_send_pack_none(self):
  127. self.rin.write(
  128. '0078310ca9477129b8586fa2afc779c1f57cf64bba6c '
  129. 'refs/heads/master\x00 report-status delete-refs '
  130. 'side-band-64k quiet ofs-delta\n'
  131. '0000')
  132. self.rin.seek(0)
  133. def determine_wants(refs):
  134. return {
  135. 'refs/heads/master': '310ca9477129b8586fa2afc779c1f57cf64bba6c'
  136. }
  137. def generate_pack_contents(have, want):
  138. return {}
  139. self.client.send_pack('/', determine_wants, generate_pack_contents)
  140. self.assertEqual(self.rout.getvalue(), '0000')
  141. def test_send_pack_delete_only(self):
  142. self.rin.write(
  143. '0063310ca9477129b8586fa2afc779c1f57cf64bba6c '
  144. 'refs/heads/master\x00report-status delete-refs ofs-delta\n'
  145. '0000000eunpack ok\n'
  146. '0019ok refs/heads/master\n'
  147. '0000')
  148. self.rin.seek(0)
  149. def determine_wants(refs):
  150. return {'refs/heads/master': '0' * 40}
  151. def generate_pack_contents(have, want):
  152. return {}
  153. self.client.send_pack('/', determine_wants, generate_pack_contents)
  154. self.assertEqual(
  155. self.rout.getvalue(),
  156. '007f310ca9477129b8586fa2afc779c1f57cf64bba6c '
  157. '0000000000000000000000000000000000000000 '
  158. 'refs/heads/master\x00report-status ofs-delta0000')
  159. def test_send_pack_new_ref_only(self):
  160. self.rin.write(
  161. '0063310ca9477129b8586fa2afc779c1f57cf64bba6c '
  162. 'refs/heads/master\x00report-status delete-refs ofs-delta\n'
  163. '0000000eunpack ok\n'
  164. '0019ok refs/heads/blah12\n'
  165. '0000')
  166. self.rin.seek(0)
  167. def determine_wants(refs):
  168. return {
  169. 'refs/heads/blah12':
  170. '310ca9477129b8586fa2afc779c1f57cf64bba6c',
  171. 'refs/heads/master': '310ca9477129b8586fa2afc779c1f57cf64bba6c'
  172. }
  173. def generate_pack_contents(have, want):
  174. return {}
  175. f = BytesIO()
  176. empty_pack = write_pack_objects(f, {})
  177. self.client.send_pack('/', determine_wants, generate_pack_contents)
  178. self.assertEqual(
  179. self.rout.getvalue(),
  180. '007f0000000000000000000000000000000000000000 '
  181. '310ca9477129b8586fa2afc779c1f57cf64bba6c '
  182. 'refs/heads/blah12\x00report-status ofs-delta0000%s'
  183. % f.getvalue())
  184. def test_send_pack_new_ref(self):
  185. self.rin.write(
  186. '0064310ca9477129b8586fa2afc779c1f57cf64bba6c '
  187. 'refs/heads/master\x00 report-status delete-refs ofs-delta\n'
  188. '0000000eunpack ok\n'
  189. '0019ok refs/heads/blah12\n'
  190. '0000')
  191. self.rin.seek(0)
  192. tree = Tree()
  193. commit = Commit()
  194. commit.tree = tree
  195. commit.parents = []
  196. commit.author = commit.committer = 'test user'
  197. commit.commit_time = commit.author_time = 1174773719
  198. commit.commit_timezone = commit.author_timezone = 0
  199. commit.encoding = 'UTF-8'
  200. commit.message = 'test message'
  201. def determine_wants(refs):
  202. return {
  203. 'refs/heads/blah12': commit.id,
  204. 'refs/heads/master': '310ca9477129b8586fa2afc779c1f57cf64bba6c'
  205. }
  206. def generate_pack_contents(have, want):
  207. return [(commit, None), (tree, ''), ]
  208. f = BytesIO()
  209. pack = write_pack_objects(f, generate_pack_contents(None, None))
  210. self.client.send_pack('/', determine_wants, generate_pack_contents)
  211. self.assertEqual(
  212. self.rout.getvalue(),
  213. '007f0000000000000000000000000000000000000000 %s '
  214. 'refs/heads/blah12\x00report-status ofs-delta0000%s'
  215. % (commit.id, f.getvalue()))
  216. def test_send_pack_no_deleteref_delete_only(self):
  217. pkts = ['310ca9477129b8586fa2afc779c1f57cf64bba6c refs/heads/master'
  218. '\x00 report-status ofs-delta\n',
  219. '',
  220. '']
  221. for pkt in pkts:
  222. if pkt == '':
  223. self.rin.write("0000")
  224. else:
  225. self.rin.write("%04x%s" % (len(pkt)+4, pkt))
  226. self.rin.seek(0)
  227. def determine_wants(refs):
  228. return {'refs/heads/master': '0' * 40}
  229. def generate_pack_contents(have, want):
  230. return {}
  231. self.assertRaises(UpdateRefsError,
  232. self.client.send_pack, "/",
  233. determine_wants, generate_pack_contents)
  234. self.assertEqual(self.rout.getvalue(), '0000')
  235. class TestGetTransportAndPath(TestCase):
  236. def test_tcp(self):
  237. c, path = get_transport_and_path('git://foo.com/bar/baz')
  238. self.assertTrue(isinstance(c, TCPGitClient))
  239. self.assertEqual('foo.com', c._host)
  240. self.assertEqual(TCP_GIT_PORT, c._port)
  241. self.assertEqual('/bar/baz', path)
  242. def test_tcp_port(self):
  243. c, path = get_transport_and_path('git://foo.com:1234/bar/baz')
  244. self.assertTrue(isinstance(c, TCPGitClient))
  245. self.assertEqual('foo.com', c._host)
  246. self.assertEqual(1234, c._port)
  247. self.assertEqual('/bar/baz', path)
  248. def test_ssh_explicit(self):
  249. c, path = get_transport_and_path('git+ssh://foo.com/bar/baz')
  250. self.assertTrue(isinstance(c, SSHGitClient))
  251. self.assertEqual('foo.com', c.host)
  252. self.assertEqual(None, c.port)
  253. self.assertEqual(None, c.username)
  254. self.assertEqual('bar/baz', path)
  255. def test_ssh_port_explicit(self):
  256. c, path = get_transport_and_path(
  257. 'git+ssh://foo.com:1234/bar/baz')
  258. self.assertTrue(isinstance(c, SSHGitClient))
  259. self.assertEqual('foo.com', c.host)
  260. self.assertEqual(1234, c.port)
  261. self.assertEqual('bar/baz', path)
  262. def test_ssh_abspath_explicit(self):
  263. c, path = get_transport_and_path('git+ssh://foo.com//bar/baz')
  264. self.assertTrue(isinstance(c, SSHGitClient))
  265. self.assertEqual('foo.com', c.host)
  266. self.assertEqual(None, c.port)
  267. self.assertEqual(None, c.username)
  268. self.assertEqual('/bar/baz', path)
  269. def test_ssh_port_abspath_explicit(self):
  270. c, path = get_transport_and_path(
  271. 'git+ssh://foo.com:1234//bar/baz')
  272. self.assertTrue(isinstance(c, SSHGitClient))
  273. self.assertEqual('foo.com', c.host)
  274. self.assertEqual(1234, c.port)
  275. self.assertEqual('/bar/baz', path)
  276. def test_ssh_implicit(self):
  277. c, path = get_transport_and_path('foo:/bar/baz')
  278. self.assertTrue(isinstance(c, SSHGitClient))
  279. self.assertEqual('foo', c.host)
  280. self.assertEqual(None, c.port)
  281. self.assertEqual(None, c.username)
  282. self.assertEqual('/bar/baz', path)
  283. def test_ssh_host(self):
  284. c, path = get_transport_and_path('foo.com:/bar/baz')
  285. self.assertTrue(isinstance(c, SSHGitClient))
  286. self.assertEqual('foo.com', c.host)
  287. self.assertEqual(None, c.port)
  288. self.assertEqual(None, c.username)
  289. self.assertEqual('/bar/baz', path)
  290. def test_ssh_user_host(self):
  291. c, path = get_transport_and_path('user@foo.com:/bar/baz')
  292. self.assertTrue(isinstance(c, SSHGitClient))
  293. self.assertEqual('foo.com', c.host)
  294. self.assertEqual(None, c.port)
  295. self.assertEqual('user', c.username)
  296. self.assertEqual('/bar/baz', path)
  297. def test_ssh_relpath(self):
  298. c, path = get_transport_and_path('foo:bar/baz')
  299. self.assertTrue(isinstance(c, SSHGitClient))
  300. self.assertEqual('foo', c.host)
  301. self.assertEqual(None, c.port)
  302. self.assertEqual(None, c.username)
  303. self.assertEqual('bar/baz', path)
  304. def test_ssh_host_relpath(self):
  305. c, path = get_transport_and_path('foo.com:bar/baz')
  306. self.assertTrue(isinstance(c, SSHGitClient))
  307. self.assertEqual('foo.com', c.host)
  308. self.assertEqual(None, c.port)
  309. self.assertEqual(None, c.username)
  310. self.assertEqual('bar/baz', path)
  311. def test_ssh_user_host_relpath(self):
  312. c, path = get_transport_and_path('user@foo.com:bar/baz')
  313. self.assertTrue(isinstance(c, SSHGitClient))
  314. self.assertEqual('foo.com', c.host)
  315. self.assertEqual(None, c.port)
  316. self.assertEqual('user', c.username)
  317. self.assertEqual('bar/baz', path)
  318. def test_subprocess(self):
  319. c, path = get_transport_and_path('foo.bar/baz')
  320. self.assertTrue(isinstance(c, SubprocessGitClient))
  321. self.assertEqual('foo.bar/baz', path)
  322. def test_error(self):
  323. # Need to use a known urlparse.uses_netloc URL scheme to get the
  324. # expected parsing of the URL on Python versions less than 2.6.5
  325. c, path = get_transport_and_path('prospero://bar/baz')
  326. self.assertTrue(isinstance(c, SSHGitClient))
  327. def test_http(self):
  328. url = 'https://github.com/jelmer/dulwich'
  329. c, path = get_transport_and_path(url)
  330. self.assertTrue(isinstance(c, HttpGitClient))
  331. self.assertEqual('/jelmer/dulwich', path)
  332. class TestGetTransportAndPathFromUrl(TestCase):
  333. def test_tcp(self):
  334. c, path = get_transport_and_path_from_url('git://foo.com/bar/baz')
  335. self.assertTrue(isinstance(c, TCPGitClient))
  336. self.assertEqual('foo.com', c._host)
  337. self.assertEqual(TCP_GIT_PORT, c._port)
  338. self.assertEqual('/bar/baz', path)
  339. def test_tcp_port(self):
  340. c, path = get_transport_and_path_from_url('git://foo.com:1234/bar/baz')
  341. self.assertTrue(isinstance(c, TCPGitClient))
  342. self.assertEqual('foo.com', c._host)
  343. self.assertEqual(1234, c._port)
  344. self.assertEqual('/bar/baz', path)
  345. def test_ssh_explicit(self):
  346. c, path = get_transport_and_path_from_url('git+ssh://foo.com/bar/baz')
  347. self.assertTrue(isinstance(c, SSHGitClient))
  348. self.assertEqual('foo.com', c.host)
  349. self.assertEqual(None, c.port)
  350. self.assertEqual(None, c.username)
  351. self.assertEqual('bar/baz', path)
  352. def test_ssh_port_explicit(self):
  353. c, path = get_transport_and_path_from_url(
  354. 'git+ssh://foo.com:1234/bar/baz')
  355. self.assertTrue(isinstance(c, SSHGitClient))
  356. self.assertEqual('foo.com', c.host)
  357. self.assertEqual(1234, c.port)
  358. self.assertEqual('bar/baz', path)
  359. def test_ssh_abspath_explicit(self):
  360. c, path = get_transport_and_path_from_url('git+ssh://foo.com//bar/baz')
  361. self.assertTrue(isinstance(c, SSHGitClient))
  362. self.assertEqual('foo.com', c.host)
  363. self.assertEqual(None, c.port)
  364. self.assertEqual(None, c.username)
  365. self.assertEqual('/bar/baz', path)
  366. def test_ssh_port_abspath_explicit(self):
  367. c, path = get_transport_and_path_from_url(
  368. 'git+ssh://foo.com:1234//bar/baz')
  369. self.assertTrue(isinstance(c, SSHGitClient))
  370. self.assertEqual('foo.com', c.host)
  371. self.assertEqual(1234, c.port)
  372. self.assertEqual('/bar/baz', path)
  373. def test_ssh_host_relpath(self):
  374. self.assertRaises(ValueError, get_transport_and_path_from_url,
  375. 'foo.com:bar/baz')
  376. def test_ssh_user_host_relpath(self):
  377. self.assertRaises(ValueError, get_transport_and_path_from_url,
  378. 'user@foo.com:bar/baz')
  379. def test_local_path(self):
  380. self.assertRaises(ValueError, get_transport_and_path_from_url,
  381. 'foo.bar/baz')
  382. def test_error(self):
  383. # Need to use a known urlparse.uses_netloc URL scheme to get the
  384. # expected parsing of the URL on Python versions less than 2.6.5
  385. self.assertRaises(ValueError, get_transport_and_path_from_url,
  386. 'prospero://bar/baz')
  387. def test_http(self):
  388. url = 'https://github.com/jelmer/dulwich'
  389. c, path = get_transport_and_path_from_url(url)
  390. self.assertTrue(isinstance(c, HttpGitClient))
  391. self.assertEqual('/jelmer/dulwich', path)
  392. def test_file(self):
  393. c, path = get_transport_and_path_from_url('file:///home/jelmer/foo')
  394. self.assertTrue(isinstance(c, SubprocessGitClient))
  395. self.assertEqual('/home/jelmer/foo', path)
  396. class TestSSHVendor(object):
  397. def __init__(self):
  398. self.host = None
  399. self.command = ""
  400. self.username = None
  401. self.port = None
  402. def run_command(self, host, command, username=None, port=None):
  403. self.host = host
  404. self.command = command
  405. self.username = username
  406. self.port = port
  407. class Subprocess: pass
  408. setattr(Subprocess, 'read', lambda: None)
  409. setattr(Subprocess, 'write', lambda: None)
  410. setattr(Subprocess, 'can_read', lambda: None)
  411. return Subprocess()
  412. class SSHGitClientTests(TestCase):
  413. def setUp(self):
  414. super(SSHGitClientTests, self).setUp()
  415. self.server = TestSSHVendor()
  416. self.real_vendor = client.get_ssh_vendor
  417. client.get_ssh_vendor = lambda: self.server
  418. self.client = SSHGitClient('git.samba.org')
  419. def tearDown(self):
  420. super(SSHGitClientTests, self).tearDown()
  421. client.get_ssh_vendor = self.real_vendor
  422. def test_default_command(self):
  423. self.assertEqual('git-upload-pack',
  424. self.client._get_cmd_path('upload-pack'))
  425. def test_alternative_command_path(self):
  426. self.client.alternative_paths['upload-pack'] = (
  427. '/usr/lib/git/git-upload-pack')
  428. self.assertEqual('/usr/lib/git/git-upload-pack',
  429. self.client._get_cmd_path('upload-pack'))
  430. def test_connect(self):
  431. server = self.server
  432. client = self.client
  433. client.username = "username"
  434. client.port = 1337
  435. client._connect("command", "/path/to/repo")
  436. self.assertEquals("username", server.username)
  437. self.assertEquals(1337, server.port)
  438. self.assertEquals(["git-command '/path/to/repo'"], server.command)
  439. client._connect("relative-command", "/~/path/to/repo")
  440. self.assertEquals(["git-relative-command '~/path/to/repo'"],
  441. server.command)
  442. class ReportStatusParserTests(TestCase):
  443. def test_invalid_pack(self):
  444. parser = ReportStatusParser()
  445. parser.handle_packet("unpack error - foo bar")
  446. parser.handle_packet("ok refs/foo/bar")
  447. parser.handle_packet(None)
  448. self.assertRaises(SendPackError, parser.check)
  449. def test_update_refs_error(self):
  450. parser = ReportStatusParser()
  451. parser.handle_packet("unpack ok")
  452. parser.handle_packet("ng refs/foo/bar need to pull")
  453. parser.handle_packet(None)
  454. self.assertRaises(UpdateRefsError, parser.check)
  455. def test_ok(self):
  456. parser = ReportStatusParser()
  457. parser.handle_packet("unpack ok")
  458. parser.handle_packet("ok refs/foo/bar")
  459. parser.handle_packet(None)
  460. parser.check()
  461. class LocalGitClientTests(TestCase):
  462. def test_fetch_into_empty(self):
  463. c = LocalGitClient()
  464. t = MemoryRepo()
  465. s = open_repo('a.git')
  466. self.assertEquals(s.get_refs(), c.fetch(s.path, t))
  467. def test_fetch_empty(self):
  468. c = LocalGitClient()
  469. s = open_repo('a.git')
  470. out = BytesIO()
  471. walker = {}
  472. c.fetch_pack(s.path, lambda heads: [], graph_walker=walker,
  473. pack_data=out.write)
  474. self.assertEquals("PACK\x00\x00\x00\x02\x00\x00\x00\x00\x02\x9d\x08"
  475. "\x82;\xd8\xa8\xea\xb5\x10\xadj\xc7\\\x82<\xfd>\xd3\x1e", out.getvalue())
  476. def test_fetch_pack_none(self):
  477. c = LocalGitClient()
  478. s = open_repo('a.git')
  479. out = BytesIO()
  480. walker = MemoryRepo().get_graph_walker()
  481. c.fetch_pack(s.path,
  482. lambda heads: ["a90fa2d900a17e99b433217e988c4eb4a2e9a097"],
  483. graph_walker=walker, pack_data=out.write)
  484. # Hardcoding is not ideal, but we'll fix that some other day..
  485. self.assertTrue(out.getvalue().startswith('PACK\x00\x00\x00\x02\x00\x00\x00\x07'))