test_client.py 33 KB

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