test_client.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 cStringIO import StringIO
  19. from dulwich import (
  20. client,
  21. )
  22. from dulwich.client import (
  23. TraditionalGitClient,
  24. TCPGitClient,
  25. SubprocessGitClient,
  26. SSHGitClient,
  27. HttpGitClient,
  28. ReportStatusParser,
  29. SendPackError,
  30. UpdateRefsError,
  31. get_transport_and_path,
  32. )
  33. from dulwich.tests import (
  34. TestCase,
  35. )
  36. from dulwich.protocol import (
  37. TCP_GIT_PORT,
  38. Protocol,
  39. )
  40. from dulwich.pack import (
  41. write_pack_objects,
  42. )
  43. from dulwich.objects import (
  44. Commit,
  45. Tree
  46. )
  47. class DummyClient(TraditionalGitClient):
  48. def __init__(self, can_read, read, write):
  49. self.can_read = can_read
  50. self.read = read
  51. self.write = write
  52. TraditionalGitClient.__init__(self)
  53. def _connect(self, service, path):
  54. return Protocol(self.read, self.write), self.can_read
  55. # TODO(durin42): add unit-level tests of GitClient
  56. class GitClientTests(TestCase):
  57. def setUp(self):
  58. super(GitClientTests, self).setUp()
  59. self.rout = StringIO()
  60. self.rin = StringIO()
  61. self.client = DummyClient(lambda x: True, self.rin.read,
  62. self.rout.write)
  63. def test_caps(self):
  64. self.assertEqual(set(['multi_ack', 'side-band-64k', 'ofs-delta',
  65. 'thin-pack', 'multi_ack_detailed']),
  66. set(self.client._fetch_capabilities))
  67. self.assertEqual(set(['ofs-delta', 'report-status', 'side-band-64k']),
  68. set(self.client._send_capabilities))
  69. def test_archive_ack(self):
  70. self.rin.write(
  71. '0009NACK\n'
  72. '0000')
  73. self.rin.seek(0)
  74. self.client.archive('bla', 'HEAD', None, None)
  75. self.assertEqual(self.rout.getvalue(), '0011argument HEAD0000')
  76. def test_fetch_empty(self):
  77. self.rin.write('0000')
  78. self.rin.seek(0)
  79. self.client.fetch_pack('/', lambda heads: [], None, None)
  80. def test_fetch_pack_none(self):
  81. self.rin.write(
  82. '008855dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 HEAD.multi_ack '
  83. 'thin-pack side-band side-band-64k ofs-delta shallow no-progress '
  84. 'include-tag\n'
  85. '0000')
  86. self.rin.seek(0)
  87. self.client.fetch_pack('bla', lambda heads: [], None, None, None)
  88. self.assertEqual(self.rout.getvalue(), '0000')
  89. def test_get_transport_and_path_tcp(self):
  90. client, path = get_transport_and_path('git://foo.com/bar/baz')
  91. self.assertTrue(isinstance(client, TCPGitClient))
  92. self.assertEqual('foo.com', client._host)
  93. self.assertEqual(TCP_GIT_PORT, client._port)
  94. self.assertEqual('/bar/baz', path)
  95. def test_get_transport_and_path_tcp_port(self):
  96. client, path = get_transport_and_path('git://foo.com:1234/bar/baz')
  97. self.assertTrue(isinstance(client, TCPGitClient))
  98. self.assertEqual('foo.com', client._host)
  99. self.assertEqual(1234, client._port)
  100. self.assertEqual('/bar/baz', path)
  101. def test_get_transport_and_path_ssh_explicit(self):
  102. client, path = get_transport_and_path('git+ssh://foo.com/bar/baz')
  103. self.assertTrue(isinstance(client, SSHGitClient))
  104. self.assertEqual('foo.com', client.host)
  105. self.assertEqual(None, client.port)
  106. self.assertEqual(None, client.username)
  107. self.assertEqual('bar/baz', path)
  108. def test_get_transport_and_path_ssh_port_explicit(self):
  109. client, path = get_transport_and_path(
  110. 'git+ssh://foo.com:1234/bar/baz')
  111. self.assertTrue(isinstance(client, SSHGitClient))
  112. self.assertEqual('foo.com', client.host)
  113. self.assertEqual(1234, client.port)
  114. self.assertEqual('bar/baz', path)
  115. def test_get_transport_and_path_ssh_abspath_explicit(self):
  116. client, path = get_transport_and_path('git+ssh://foo.com//bar/baz')
  117. self.assertTrue(isinstance(client, SSHGitClient))
  118. self.assertEqual('foo.com', client.host)
  119. self.assertEqual(None, client.port)
  120. self.assertEqual(None, client.username)
  121. self.assertEqual('/bar/baz', path)
  122. def test_get_transport_and_path_ssh_port_abspath_explicit(self):
  123. client, path = get_transport_and_path(
  124. 'git+ssh://foo.com:1234//bar/baz')
  125. self.assertTrue(isinstance(client, SSHGitClient))
  126. self.assertEqual('foo.com', client.host)
  127. self.assertEqual(1234, client.port)
  128. self.assertEqual('/bar/baz', path)
  129. def test_get_transport_and_path_ssh_implicit(self):
  130. client, path = get_transport_and_path('foo:/bar/baz')
  131. self.assertTrue(isinstance(client, SSHGitClient))
  132. self.assertEqual('foo', client.host)
  133. self.assertEqual(None, client.port)
  134. self.assertEqual(None, client.username)
  135. self.assertEqual('/bar/baz', path)
  136. def test_get_transport_and_path_ssh_host(self):
  137. client, path = get_transport_and_path('foo.com:/bar/baz')
  138. self.assertTrue(isinstance(client, SSHGitClient))
  139. self.assertEqual('foo.com', client.host)
  140. self.assertEqual(None, client.port)
  141. self.assertEqual(None, client.username)
  142. self.assertEqual('/bar/baz', path)
  143. def test_get_transport_and_path_ssh_user_host(self):
  144. client, path = get_transport_and_path('user@foo.com:/bar/baz')
  145. self.assertTrue(isinstance(client, SSHGitClient))
  146. self.assertEqual('foo.com', client.host)
  147. self.assertEqual(None, client.port)
  148. self.assertEqual('user', client.username)
  149. self.assertEqual('/bar/baz', path)
  150. def test_get_transport_and_path_ssh_relpath(self):
  151. client, path = get_transport_and_path('foo:bar/baz')
  152. self.assertTrue(isinstance(client, SSHGitClient))
  153. self.assertEqual('foo', client.host)
  154. self.assertEqual(None, client.port)
  155. self.assertEqual(None, client.username)
  156. self.assertEqual('bar/baz', path)
  157. def test_get_transport_and_path_ssh_host_relpath(self):
  158. client, path = get_transport_and_path('foo.com:bar/baz')
  159. self.assertTrue(isinstance(client, SSHGitClient))
  160. self.assertEqual('foo.com', client.host)
  161. self.assertEqual(None, client.port)
  162. self.assertEqual(None, client.username)
  163. self.assertEqual('bar/baz', path)
  164. def test_get_transport_and_path_ssh_user_host_relpath(self):
  165. client, path = get_transport_and_path('user@foo.com:bar/baz')
  166. self.assertTrue(isinstance(client, SSHGitClient))
  167. self.assertEqual('foo.com', client.host)
  168. self.assertEqual(None, client.port)
  169. self.assertEqual('user', client.username)
  170. self.assertEqual('bar/baz', path)
  171. def test_get_transport_and_path_subprocess(self):
  172. client, path = get_transport_and_path('foo.bar/baz')
  173. self.assertTrue(isinstance(client, SubprocessGitClient))
  174. self.assertEqual('foo.bar/baz', path)
  175. def test_get_transport_and_path_error(self):
  176. # Need to use a known urlparse.uses_netloc URL scheme to get the
  177. # expected parsing of the URL on Python versions less than 2.6.5
  178. self.assertRaises(ValueError, get_transport_and_path,
  179. 'prospero://bar/baz')
  180. def test_get_transport_and_path_http(self):
  181. url = 'https://github.com/jelmer/dulwich'
  182. client, path = get_transport_and_path(url)
  183. self.assertTrue(isinstance(client, HttpGitClient))
  184. self.assertEqual('/jelmer/dulwich', path)
  185. def test_send_pack_no_sideband64k_with_update_ref_error(self):
  186. # No side-bank-64k reported by server shouldn't try to parse
  187. # side band data
  188. pkts = ['55dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 capabilities^{}'
  189. '\x00 report-status delete-refs ofs-delta\n',
  190. '',
  191. "unpack ok",
  192. "ng refs/foo/bar pre-receive hook declined",
  193. '']
  194. for pkt in pkts:
  195. if pkt == '':
  196. self.rin.write("0000")
  197. else:
  198. self.rin.write("%04x%s" % (len(pkt)+4, pkt))
  199. self.rin.seek(0)
  200. tree = Tree()
  201. commit = Commit()
  202. commit.tree = tree
  203. commit.parents = []
  204. commit.author = commit.committer = 'test user'
  205. commit.commit_time = commit.author_time = 1174773719
  206. commit.commit_timezone = commit.author_timezone = 0
  207. commit.encoding = 'UTF-8'
  208. commit.message = 'test message'
  209. def determine_wants(refs):
  210. return {'refs/foo/bar': commit.id, }
  211. def generate_pack_contents(have, want):
  212. return [(commit, None), (tree, ''), ]
  213. self.assertRaises(UpdateRefsError,
  214. self.client.send_pack, "blah",
  215. determine_wants, generate_pack_contents)
  216. def test_send_pack_none(self):
  217. self.rin.write(
  218. '0078310ca9477129b8586fa2afc779c1f57cf64bba6c '
  219. 'refs/heads/master\x00 report-status delete-refs '
  220. 'side-band-64k quiet ofs-delta\n'
  221. '0000')
  222. self.rin.seek(0)
  223. def determine_wants(refs):
  224. return {
  225. 'refs/heads/master': '310ca9477129b8586fa2afc779c1f57cf64bba6c'
  226. }
  227. def generate_pack_contents(have, want):
  228. return {}
  229. self.client.send_pack('/', determine_wants, generate_pack_contents)
  230. self.assertEqual(self.rout.getvalue(), '0000')
  231. def test_send_pack_delete_only(self):
  232. self.rin.write(
  233. '0063310ca9477129b8586fa2afc779c1f57cf64bba6c '
  234. 'refs/heads/master\x00report-status delete-refs ofs-delta\n'
  235. '0000000eunpack ok\n'
  236. '0019ok refs/heads/master\n'
  237. '0000')
  238. self.rin.seek(0)
  239. def determine_wants(refs):
  240. return {'refs/heads/master': '0' * 40}
  241. def generate_pack_contents(have, want):
  242. return {}
  243. self.client.send_pack('/', determine_wants, generate_pack_contents)
  244. self.assertEqual(
  245. self.rout.getvalue(),
  246. '007f310ca9477129b8586fa2afc779c1f57cf64bba6c '
  247. '0000000000000000000000000000000000000000 '
  248. 'refs/heads/master\x00report-status ofs-delta0000')
  249. def test_send_pack_new_ref_only(self):
  250. self.rin.write(
  251. '0063310ca9477129b8586fa2afc779c1f57cf64bba6c '
  252. 'refs/heads/master\x00report-status delete-refs ofs-delta\n'
  253. '0000000eunpack ok\n'
  254. '0019ok refs/heads/blah12\n'
  255. '0000')
  256. self.rin.seek(0)
  257. def determine_wants(refs):
  258. return {
  259. 'refs/heads/blah12':
  260. '310ca9477129b8586fa2afc779c1f57cf64bba6c',
  261. 'refs/heads/master': '310ca9477129b8586fa2afc779c1f57cf64bba6c'
  262. }
  263. def generate_pack_contents(have, want):
  264. return {}
  265. f = StringIO()
  266. empty_pack = write_pack_objects(f, {})
  267. self.client.send_pack('/', determine_wants, generate_pack_contents)
  268. self.assertEqual(
  269. self.rout.getvalue(),
  270. '007f0000000000000000000000000000000000000000 '
  271. '310ca9477129b8586fa2afc779c1f57cf64bba6c '
  272. 'refs/heads/blah12\x00report-status ofs-delta0000%s'
  273. % f.getvalue())
  274. def test_send_pack_new_ref(self):
  275. self.rin.write(
  276. '0064310ca9477129b8586fa2afc779c1f57cf64bba6c '
  277. 'refs/heads/master\x00 report-status delete-refs ofs-delta\n'
  278. '0000000eunpack ok\n'
  279. '0019ok refs/heads/blah12\n'
  280. '0000')
  281. self.rin.seek(0)
  282. tree = Tree()
  283. commit = Commit()
  284. commit.tree = tree
  285. commit.parents = []
  286. commit.author = commit.committer = 'test user'
  287. commit.commit_time = commit.author_time = 1174773719
  288. commit.commit_timezone = commit.author_timezone = 0
  289. commit.encoding = 'UTF-8'
  290. commit.message = 'test message'
  291. def determine_wants(refs):
  292. return {
  293. 'refs/heads/blah12': commit.id,
  294. 'refs/heads/master': '310ca9477129b8586fa2afc779c1f57cf64bba6c'
  295. }
  296. def generate_pack_contents(have, want):
  297. return [(commit, None), (tree, ''), ]
  298. f = StringIO()
  299. pack = write_pack_objects(f, generate_pack_contents(None, None))
  300. self.client.send_pack('/', determine_wants, generate_pack_contents)
  301. self.assertEqual(
  302. self.rout.getvalue(),
  303. '007f0000000000000000000000000000000000000000 %s '
  304. 'refs/heads/blah12\x00report-status ofs-delta0000%s'
  305. % (commit.id, f.getvalue()))
  306. def test_send_pack_no_deleteref_delete_only(self):
  307. pkts = ['310ca9477129b8586fa2afc779c1f57cf64bba6c refs/heads/master'
  308. '\x00 report-status ofs-delta\n',
  309. '',
  310. '']
  311. for pkt in pkts:
  312. if pkt == '':
  313. self.rin.write("0000")
  314. else:
  315. self.rin.write("%04x%s" % (len(pkt)+4, pkt))
  316. self.rin.seek(0)
  317. def determine_wants(refs):
  318. return {'refs/heads/master': '0' * 40}
  319. def generate_pack_contents(have, want):
  320. return {}
  321. self.assertRaises(UpdateRefsError,
  322. self.client.send_pack, "/",
  323. determine_wants, generate_pack_contents)
  324. self.assertEqual(self.rout.getvalue(), '0000')
  325. class TestSSHVendor(object):
  326. def __init__(self):
  327. self.host = None
  328. self.command = ""
  329. self.username = None
  330. self.port = None
  331. def connect_ssh(self, host, command, username=None, port=None):
  332. self.host = host
  333. self.command = command
  334. self.username = username
  335. self.port = port
  336. class Subprocess: pass
  337. setattr(Subprocess, 'read', lambda: None)
  338. setattr(Subprocess, 'write', lambda: None)
  339. setattr(Subprocess, 'can_read', lambda: None)
  340. return Subprocess()
  341. class SSHGitClientTests(TestCase):
  342. def setUp(self):
  343. super(SSHGitClientTests, self).setUp()
  344. self.server = TestSSHVendor()
  345. self.real_vendor = client.get_ssh_vendor
  346. client.get_ssh_vendor = lambda: self.server
  347. self.client = SSHGitClient('git.samba.org')
  348. def tearDown(self):
  349. super(SSHGitClientTests, self).tearDown()
  350. client.get_ssh_vendor = self.real_vendor
  351. def test_default_command(self):
  352. self.assertEqual('git-upload-pack',
  353. self.client._get_cmd_path('upload-pack'))
  354. def test_alternative_command_path(self):
  355. self.client.alternative_paths['upload-pack'] = (
  356. '/usr/lib/git/git-upload-pack')
  357. self.assertEqual('/usr/lib/git/git-upload-pack',
  358. self.client._get_cmd_path('upload-pack'))
  359. def test_connect(self):
  360. server = self.server
  361. client = self.client
  362. client.username = "username"
  363. client.port = 1337
  364. client._connect("command", "/path/to/repo")
  365. self.assertEquals("username", server.username)
  366. self.assertEquals(1337, server.port)
  367. self.assertEquals(["git-command '/path/to/repo'"], server.command)
  368. client._connect("relative-command", "/~/path/to/repo")
  369. self.assertEquals(["git-relative-command '~/path/to/repo'"],
  370. server.command)
  371. class ReportStatusParserTests(TestCase):
  372. def test_invalid_pack(self):
  373. parser = ReportStatusParser()
  374. parser.handle_packet("unpack error - foo bar")
  375. parser.handle_packet("ok refs/foo/bar")
  376. parser.handle_packet(None)
  377. self.assertRaises(SendPackError, parser.check)
  378. def test_update_refs_error(self):
  379. parser = ReportStatusParser()
  380. parser.handle_packet("unpack ok")
  381. parser.handle_packet("ng refs/foo/bar need to pull")
  382. parser.handle_packet(None)
  383. self.assertRaises(UpdateRefsError, parser.check)
  384. def test_ok(self):
  385. parser = ReportStatusParser()
  386. parser.handle_packet("unpack ok")
  387. parser.handle_packet("ok refs/foo/bar")
  388. parser.handle_packet(None)
  389. parser.check()