test_client.py 32 KB

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