test_client.py 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  1. # test_client.py -- Tests for the git protocol, client side
  2. # Copyright (C) 2009 Jelmer Vernooij <jelmer@jelmer.uk>
  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. import base64
  21. import os
  22. import shutil
  23. import sys
  24. import tempfile
  25. import warnings
  26. from io import BytesIO
  27. from typing import Dict
  28. from unittest.mock import patch
  29. from urllib.parse import quote as urlquote
  30. from urllib.parse import urlparse
  31. import dulwich
  32. from dulwich import client
  33. from dulwich.client import (
  34. FetchPackResult,
  35. GitProtocolError,
  36. HangupException,
  37. HttpGitClient,
  38. InvalidWants,
  39. LocalGitClient,
  40. PLinkSSHVendor,
  41. ReportStatusParser,
  42. SendPackError,
  43. SSHGitClient,
  44. StrangeHostname,
  45. SubprocessSSHVendor,
  46. TCPGitClient,
  47. TraditionalGitClient,
  48. _extract_symrefs_and_agent,
  49. _remote_error_from_stderr,
  50. check_wants,
  51. default_urllib3_manager,
  52. get_credentials_from_store,
  53. get_transport_and_path,
  54. get_transport_and_path_from_url,
  55. parse_rsync_url,
  56. )
  57. from dulwich.config import ConfigDict
  58. from dulwich.objects import Commit, Tree
  59. from dulwich.pack import pack_objects_to_data, write_pack_data, write_pack_objects
  60. from dulwich.protocol import DEFAULT_GIT_PROTOCOL_VERSION_FETCH, TCP_GIT_PORT, Protocol
  61. from dulwich.repo import MemoryRepo, Repo
  62. from dulwich.tests.utils import open_repo, setup_warning_catcher, tear_down_repo
  63. from . import TestCase, skipIf
  64. class DummyClient(TraditionalGitClient):
  65. def __init__(self, can_read, read, write) -> None:
  66. self.can_read = can_read
  67. self.read = read
  68. self.write = write
  69. TraditionalGitClient.__init__(self)
  70. def _connect(self, service, path, protocol_version=None):
  71. return Protocol(self.read, self.write), self.can_read, None
  72. class DummyPopen:
  73. def __init__(self, *args, **kwards) -> None:
  74. self.stdin = BytesIO(b"stdin")
  75. self.stdout = BytesIO(b"stdout")
  76. self.stderr = BytesIO(b"stderr")
  77. self.returncode = 0
  78. self.args = args
  79. self.kwargs = kwards
  80. def communicate(self, *args, **kwards):
  81. return ("Running", "")
  82. def wait(self, *args, **kwards):
  83. return False
  84. # TODO(durin42): add unit-level tests of GitClient
  85. class GitClientTests(TestCase):
  86. def setUp(self):
  87. super().setUp()
  88. self.rout = BytesIO()
  89. self.rin = BytesIO()
  90. self.client = DummyClient(lambda x: True, self.rin.read, self.rout.write)
  91. def test_caps(self):
  92. agent_cap = ("agent=dulwich/%d.%d.%d" % dulwich.__version__).encode("ascii")
  93. self.assertEqual(
  94. {
  95. b"multi_ack",
  96. b"side-band-64k",
  97. b"ofs-delta",
  98. b"thin-pack",
  99. b"multi_ack_detailed",
  100. b"shallow",
  101. agent_cap,
  102. },
  103. set(self.client._fetch_capabilities),
  104. )
  105. self.assertEqual(
  106. {
  107. b"delete-refs",
  108. b"ofs-delta",
  109. b"report-status",
  110. b"side-band-64k",
  111. agent_cap,
  112. },
  113. set(self.client._send_capabilities),
  114. )
  115. def test_archive_ack(self):
  116. self.rin.write(b"0009NACK\n" b"0000")
  117. self.rin.seek(0)
  118. self.client.archive(b"bla", b"HEAD", None, None)
  119. self.assertEqual(self.rout.getvalue(), b"0011argument HEAD0000")
  120. def test_fetch_empty(self):
  121. self.rin.write(b"0000")
  122. self.rin.seek(0)
  123. def check_heads(heads, **kwargs):
  124. self.assertEqual(heads, {})
  125. return []
  126. ret = self.client.fetch_pack(b"/", check_heads, None, None)
  127. self.assertEqual({}, ret.refs)
  128. self.assertEqual({}, ret.symrefs)
  129. def test_fetch_pack_ignores_magic_ref(self):
  130. self.rin.write(
  131. b"00000000000000000000000000000000000000000000 capabilities^{}"
  132. b"\x00 multi_ack "
  133. b"thin-pack side-band side-band-64k ofs-delta shallow no-progress "
  134. b"include-tag\n"
  135. b"0000"
  136. )
  137. self.rin.seek(0)
  138. def check_heads(heads, **kwargs):
  139. self.assertEqual({}, heads)
  140. return []
  141. ret = self.client.fetch_pack(b"bla", check_heads, None, None, None)
  142. self.assertEqual({}, ret.refs)
  143. self.assertEqual({}, ret.symrefs)
  144. self.assertEqual(self.rout.getvalue(), b"0000")
  145. def test_fetch_pack_none(self):
  146. self.rin.write(
  147. b"008855dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 HEAD\x00multi_ack "
  148. b"thin-pack side-band side-band-64k ofs-delta shallow no-progress "
  149. b"include-tag\n"
  150. b"0000"
  151. )
  152. self.rin.seek(0)
  153. ret = self.client.fetch_pack(
  154. b"bla", lambda heads, **kwargs: [], None, None, None
  155. )
  156. self.assertEqual(
  157. {b"HEAD": b"55dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7"}, ret.refs
  158. )
  159. self.assertEqual({}, ret.symrefs)
  160. self.assertEqual(self.rout.getvalue(), b"0000")
  161. def test_send_pack_no_sideband64k_with_update_ref_error(self) -> None:
  162. # No side-bank-64k reported by server shouldn't try to parse
  163. # side band data
  164. pkts = [
  165. b"55dcc6bf963f922e1ed5c4bbaaefcfacef57b1d7 capabilities^{}"
  166. b"\x00 report-status delete-refs ofs-delta\n",
  167. b"",
  168. b"unpack ok",
  169. b"ng refs/foo/bar pre-receive hook declined",
  170. b"",
  171. ]
  172. for pkt in pkts:
  173. if pkt == b"":
  174. self.rin.write(b"0000")
  175. else:
  176. self.rin.write(("%04x" % (len(pkt) + 4)).encode("ascii") + pkt)
  177. self.rin.seek(0)
  178. tree = Tree()
  179. commit = Commit()
  180. commit.tree = tree
  181. commit.parents = []
  182. commit.author = commit.committer = b"test user"
  183. commit.commit_time = commit.author_time = 1174773719
  184. commit.commit_timezone = commit.author_timezone = 0
  185. commit.encoding = b"UTF-8"
  186. commit.message = b"test message"
  187. def update_refs(refs):
  188. return {
  189. b"refs/foo/bar": commit.id,
  190. }
  191. def generate_pack_data(have, want, ofs_delta=False, progress=None):
  192. return pack_objects_to_data(
  193. [
  194. (commit, None),
  195. (tree, b""),
  196. ]
  197. )
  198. result = self.client.send_pack("blah", update_refs, generate_pack_data)
  199. self.assertEqual(
  200. {b"refs/foo/bar": "pre-receive hook declined"}, result.ref_status
  201. )
  202. self.assertEqual({b"refs/foo/bar": commit.id}, result.refs)
  203. def test_send_pack_none(self):
  204. # Set ref to current value
  205. self.rin.write(
  206. b"0078310ca9477129b8586fa2afc779c1f57cf64bba6c "
  207. b"refs/heads/master\x00 report-status delete-refs "
  208. b"side-band-64k quiet ofs-delta\n"
  209. b"0000"
  210. )
  211. self.rin.seek(0)
  212. def update_refs(refs):
  213. return {b"refs/heads/master": b"310ca9477129b8586fa2afc779c1f57cf64bba6c"}
  214. def generate_pack_data(have, want, ofs_delta=False, progress=None):
  215. return 0, []
  216. self.client.send_pack(b"/", update_refs, generate_pack_data)
  217. self.assertEqual(self.rout.getvalue(), b"0000")
  218. def test_send_pack_keep_and_delete(self):
  219. self.rin.write(
  220. b"0063310ca9477129b8586fa2afc779c1f57cf64bba6c "
  221. b"refs/heads/master\x00report-status delete-refs ofs-delta\n"
  222. b"003f310ca9477129b8586fa2afc779c1f57cf64bba6c refs/heads/keepme\n"
  223. b"0000000eunpack ok\n"
  224. b"0019ok refs/heads/master\n"
  225. b"0000"
  226. )
  227. self.rin.seek(0)
  228. def update_refs(refs):
  229. return {b"refs/heads/master": b"0" * 40}
  230. def generate_pack_data(have, want, ofs_delta=False, progress=None):
  231. return 0, []
  232. self.client.send_pack(b"/", update_refs, generate_pack_data)
  233. self.assertEqual(
  234. self.rout.getvalue(),
  235. b"008b310ca9477129b8586fa2afc779c1f57cf64bba6c "
  236. b"0000000000000000000000000000000000000000 "
  237. b"refs/heads/master\x00delete-refs ofs-delta report-status0000",
  238. )
  239. def test_send_pack_delete_only(self):
  240. self.rin.write(
  241. b"0063310ca9477129b8586fa2afc779c1f57cf64bba6c "
  242. b"refs/heads/master\x00report-status delete-refs ofs-delta\n"
  243. b"0000000eunpack ok\n"
  244. b"0019ok refs/heads/master\n"
  245. b"0000"
  246. )
  247. self.rin.seek(0)
  248. def update_refs(refs):
  249. return {b"refs/heads/master": b"0" * 40}
  250. def generate_pack_data(have, want, ofs_delta=False, progress=None):
  251. return 0, []
  252. self.client.send_pack(b"/", update_refs, generate_pack_data)
  253. self.assertEqual(
  254. self.rout.getvalue(),
  255. b"008b310ca9477129b8586fa2afc779c1f57cf64bba6c "
  256. b"0000000000000000000000000000000000000000 "
  257. b"refs/heads/master\x00delete-refs ofs-delta report-status0000",
  258. )
  259. def test_send_pack_new_ref_only(self):
  260. self.rin.write(
  261. b"0063310ca9477129b8586fa2afc779c1f57cf64bba6c "
  262. b"refs/heads/master\x00report-status delete-refs ofs-delta\n"
  263. b"0000000eunpack ok\n"
  264. b"0019ok refs/heads/blah12\n"
  265. b"0000"
  266. )
  267. self.rin.seek(0)
  268. def update_refs(refs):
  269. return {
  270. b"refs/heads/blah12": b"310ca9477129b8586fa2afc779c1f57cf64bba6c",
  271. b"refs/heads/master": b"310ca9477129b8586fa2afc779c1f57cf64bba6c",
  272. }
  273. def generate_pack_data(have, want, ofs_delta=False, progress=None):
  274. return 0, []
  275. f = BytesIO()
  276. write_pack_objects(f.write, [])
  277. self.client.send_pack("/", update_refs, generate_pack_data)
  278. self.assertEqual(
  279. self.rout.getvalue(),
  280. b"008b0000000000000000000000000000000000000000 "
  281. b"310ca9477129b8586fa2afc779c1f57cf64bba6c "
  282. b"refs/heads/blah12\x00delete-refs ofs-delta report-status0000"
  283. + f.getvalue(),
  284. )
  285. def test_send_pack_new_ref(self):
  286. self.rin.write(
  287. b"0064310ca9477129b8586fa2afc779c1f57cf64bba6c "
  288. b"refs/heads/master\x00 report-status delete-refs ofs-delta\n"
  289. b"0000000eunpack ok\n"
  290. b"0019ok refs/heads/blah12\n"
  291. b"0000"
  292. )
  293. self.rin.seek(0)
  294. tree = Tree()
  295. commit = Commit()
  296. commit.tree = tree
  297. commit.parents = []
  298. commit.author = commit.committer = b"test user"
  299. commit.commit_time = commit.author_time = 1174773719
  300. commit.commit_timezone = commit.author_timezone = 0
  301. commit.encoding = b"UTF-8"
  302. commit.message = b"test message"
  303. def update_refs(refs):
  304. return {
  305. b"refs/heads/blah12": commit.id,
  306. b"refs/heads/master": b"310ca9477129b8586fa2afc779c1f57cf64bba6c",
  307. }
  308. def generate_pack_data(have, want, ofs_delta=False, progress=None):
  309. return pack_objects_to_data(
  310. [
  311. (commit, None),
  312. (tree, b""),
  313. ]
  314. )
  315. f = BytesIO()
  316. count, records = generate_pack_data(None, None)
  317. write_pack_data(f.write, records, num_records=count)
  318. self.client.send_pack(b"/", update_refs, generate_pack_data)
  319. self.assertEqual(
  320. self.rout.getvalue(),
  321. b"008b0000000000000000000000000000000000000000 "
  322. + commit.id
  323. + b" refs/heads/blah12\x00delete-refs ofs-delta report-status0000"
  324. + f.getvalue(),
  325. )
  326. def test_send_pack_no_deleteref_delete_only(self):
  327. pkts = [
  328. b"310ca9477129b8586fa2afc779c1f57cf64bba6c refs/heads/master"
  329. b"\x00 report-status ofs-delta\n",
  330. b"",
  331. b"",
  332. ]
  333. for pkt in pkts:
  334. if pkt == b"":
  335. self.rin.write(b"0000")
  336. else:
  337. self.rin.write(("%04x" % (len(pkt) + 4)).encode("ascii") + pkt)
  338. self.rin.seek(0)
  339. def update_refs(refs):
  340. return {b"refs/heads/master": b"0" * 40}
  341. def generate_pack_data(have, want, ofs_delta=False, progress=None):
  342. return 0, []
  343. result = self.client.send_pack(b"/", update_refs, generate_pack_data)
  344. self.assertEqual(
  345. result.ref_status,
  346. {b"refs/heads/master": "remote does not support deleting refs"},
  347. )
  348. self.assertEqual(
  349. result.refs,
  350. {b"refs/heads/master": b"310ca9477129b8586fa2afc779c1f57cf64bba6c"},
  351. )
  352. self.assertEqual(self.rout.getvalue(), b"0000")
  353. class TestGetTransportAndPath(TestCase):
  354. def test_tcp(self):
  355. c, path = get_transport_and_path("git://foo.com/bar/baz")
  356. self.assertIsInstance(c, TCPGitClient)
  357. self.assertEqual("foo.com", c._host)
  358. self.assertEqual(TCP_GIT_PORT, c._port)
  359. self.assertEqual("/bar/baz", path)
  360. def test_tcp_port(self):
  361. c, path = get_transport_and_path("git://foo.com:1234/bar/baz")
  362. self.assertIsInstance(c, TCPGitClient)
  363. self.assertEqual("foo.com", c._host)
  364. self.assertEqual(1234, c._port)
  365. self.assertEqual("/bar/baz", path)
  366. def test_git_ssh_explicit(self):
  367. c, path = get_transport_and_path("git+ssh://foo.com/bar/baz")
  368. self.assertIsInstance(c, SSHGitClient)
  369. self.assertEqual("foo.com", c.host)
  370. self.assertEqual(None, c.port)
  371. self.assertEqual(None, c.username)
  372. self.assertEqual("/bar/baz", path)
  373. def test_ssh_explicit(self):
  374. c, path = get_transport_and_path("ssh://foo.com/bar/baz")
  375. self.assertIsInstance(c, SSHGitClient)
  376. self.assertEqual("foo.com", c.host)
  377. self.assertEqual(None, c.port)
  378. self.assertEqual(None, c.username)
  379. self.assertEqual("/bar/baz", path)
  380. def test_ssh_port_explicit(self):
  381. c, path = get_transport_and_path("git+ssh://foo.com:1234/bar/baz")
  382. self.assertIsInstance(c, SSHGitClient)
  383. self.assertEqual("foo.com", c.host)
  384. self.assertEqual(1234, c.port)
  385. self.assertEqual("/bar/baz", path)
  386. def test_username_and_port_explicit_unknown_scheme(self):
  387. c, path = get_transport_and_path("unknown://git@server:7999/dply/stuff.git")
  388. self.assertIsInstance(c, SSHGitClient)
  389. self.assertEqual("unknown", c.host)
  390. self.assertEqual("//git@server:7999/dply/stuff.git", path)
  391. def test_username_and_port_explicit(self):
  392. c, path = get_transport_and_path("ssh://git@server:7999/dply/stuff.git")
  393. self.assertIsInstance(c, SSHGitClient)
  394. self.assertEqual("git", c.username)
  395. self.assertEqual("server", c.host)
  396. self.assertEqual(7999, c.port)
  397. self.assertEqual("/dply/stuff.git", path)
  398. def test_ssh_abspath_doubleslash(self):
  399. c, path = get_transport_and_path("git+ssh://foo.com//bar/baz")
  400. self.assertIsInstance(c, SSHGitClient)
  401. self.assertEqual("foo.com", c.host)
  402. self.assertEqual(None, c.port)
  403. self.assertEqual(None, c.username)
  404. self.assertEqual("//bar/baz", path)
  405. def test_ssh_port(self):
  406. c, path = get_transport_and_path("git+ssh://foo.com:1234/bar/baz")
  407. self.assertIsInstance(c, SSHGitClient)
  408. self.assertEqual("foo.com", c.host)
  409. self.assertEqual(1234, c.port)
  410. self.assertEqual("/bar/baz", path)
  411. def test_ssh_implicit(self):
  412. c, path = get_transport_and_path("foo:/bar/baz")
  413. self.assertIsInstance(c, SSHGitClient)
  414. self.assertEqual("foo", c.host)
  415. self.assertEqual(None, c.port)
  416. self.assertEqual(None, c.username)
  417. self.assertEqual("/bar/baz", path)
  418. def test_ssh_host(self):
  419. c, path = get_transport_and_path("foo.com:/bar/baz")
  420. self.assertIsInstance(c, SSHGitClient)
  421. self.assertEqual("foo.com", c.host)
  422. self.assertEqual(None, c.port)
  423. self.assertEqual(None, c.username)
  424. self.assertEqual("/bar/baz", path)
  425. def test_ssh_user_host(self):
  426. c, path = get_transport_and_path("user@foo.com:/bar/baz")
  427. self.assertIsInstance(c, SSHGitClient)
  428. self.assertEqual("foo.com", c.host)
  429. self.assertEqual(None, c.port)
  430. self.assertEqual("user", c.username)
  431. self.assertEqual("/bar/baz", path)
  432. def test_ssh_relpath(self):
  433. c, path = get_transport_and_path("foo:bar/baz")
  434. self.assertIsInstance(c, SSHGitClient)
  435. self.assertEqual("foo", c.host)
  436. self.assertEqual(None, c.port)
  437. self.assertEqual(None, c.username)
  438. self.assertEqual("bar/baz", path)
  439. def test_ssh_host_relpath(self):
  440. c, path = get_transport_and_path("foo.com:bar/baz")
  441. self.assertIsInstance(c, SSHGitClient)
  442. self.assertEqual("foo.com", c.host)
  443. self.assertEqual(None, c.port)
  444. self.assertEqual(None, c.username)
  445. self.assertEqual("bar/baz", path)
  446. def test_ssh_user_host_relpath(self):
  447. c, path = get_transport_and_path("user@foo.com:bar/baz")
  448. self.assertIsInstance(c, SSHGitClient)
  449. self.assertEqual("foo.com", c.host)
  450. self.assertEqual(None, c.port)
  451. self.assertEqual("user", c.username)
  452. self.assertEqual("bar/baz", path)
  453. def test_local(self):
  454. c, path = get_transport_and_path("foo.bar/baz")
  455. self.assertIsInstance(c, LocalGitClient)
  456. self.assertEqual("foo.bar/baz", path)
  457. @skipIf(sys.platform != "win32", "Behaviour only happens on windows.")
  458. def test_local_abs_windows_path(self):
  459. c, path = get_transport_and_path("C:\\foo.bar\\baz")
  460. self.assertIsInstance(c, LocalGitClient)
  461. self.assertEqual("C:\\foo.bar\\baz", path)
  462. def test_error(self):
  463. # Need to use a known urlparse.uses_netloc URL scheme to get the
  464. # expected parsing of the URL on Python versions less than 2.6.5
  465. c, path = get_transport_and_path("prospero://bar/baz")
  466. self.assertIsInstance(c, SSHGitClient)
  467. def test_http(self):
  468. url = "https://github.com/jelmer/dulwich"
  469. c, path = get_transport_and_path(url)
  470. self.assertIsInstance(c, HttpGitClient)
  471. self.assertEqual("/jelmer/dulwich", path)
  472. def test_http_auth(self):
  473. url = "https://user:passwd@github.com/jelmer/dulwich"
  474. c, path = get_transport_and_path(url)
  475. self.assertIsInstance(c, HttpGitClient)
  476. self.assertEqual("/jelmer/dulwich", path)
  477. self.assertEqual("user", c._username)
  478. self.assertEqual("passwd", c._password)
  479. def test_http_auth_with_username(self):
  480. url = "https://github.com/jelmer/dulwich"
  481. c, path = get_transport_and_path(url, username="user2", password="blah")
  482. self.assertIsInstance(c, HttpGitClient)
  483. self.assertEqual("/jelmer/dulwich", path)
  484. self.assertEqual("user2", c._username)
  485. self.assertEqual("blah", c._password)
  486. def test_http_auth_with_username_and_in_url(self):
  487. url = "https://user:passwd@github.com/jelmer/dulwich"
  488. c, path = get_transport_and_path(url, username="user2", password="blah")
  489. self.assertIsInstance(c, HttpGitClient)
  490. self.assertEqual("/jelmer/dulwich", path)
  491. self.assertEqual("user", c._username)
  492. self.assertEqual("passwd", c._password)
  493. def test_http_no_auth(self):
  494. url = "https://github.com/jelmer/dulwich"
  495. c, path = get_transport_and_path(url)
  496. self.assertIsInstance(c, HttpGitClient)
  497. self.assertEqual("/jelmer/dulwich", path)
  498. self.assertIs(None, c._username)
  499. self.assertIs(None, c._password)
  500. class TestGetTransportAndPathFromUrl(TestCase):
  501. def test_tcp(self):
  502. c, path = get_transport_and_path_from_url("git://foo.com/bar/baz")
  503. self.assertIsInstance(c, TCPGitClient)
  504. self.assertEqual("foo.com", c._host)
  505. self.assertEqual(TCP_GIT_PORT, c._port)
  506. self.assertEqual("/bar/baz", path)
  507. def test_tcp_port(self):
  508. c, path = get_transport_and_path_from_url("git://foo.com:1234/bar/baz")
  509. self.assertIsInstance(c, TCPGitClient)
  510. self.assertEqual("foo.com", c._host)
  511. self.assertEqual(1234, c._port)
  512. self.assertEqual("/bar/baz", path)
  513. def test_ssh_explicit(self):
  514. c, path = get_transport_and_path_from_url("git+ssh://foo.com/bar/baz")
  515. self.assertIsInstance(c, SSHGitClient)
  516. self.assertEqual("foo.com", c.host)
  517. self.assertEqual(None, c.port)
  518. self.assertEqual(None, c.username)
  519. self.assertEqual("/bar/baz", path)
  520. def test_ssh_port_explicit(self):
  521. c, path = get_transport_and_path_from_url("git+ssh://foo.com:1234/bar/baz")
  522. self.assertIsInstance(c, SSHGitClient)
  523. self.assertEqual("foo.com", c.host)
  524. self.assertEqual(1234, c.port)
  525. self.assertEqual("/bar/baz", path)
  526. def test_ssh_homepath(self):
  527. c, path = get_transport_and_path_from_url("git+ssh://foo.com/~/bar/baz")
  528. self.assertIsInstance(c, SSHGitClient)
  529. self.assertEqual("foo.com", c.host)
  530. self.assertEqual(None, c.port)
  531. self.assertEqual(None, c.username)
  532. self.assertEqual("/~/bar/baz", path)
  533. def test_ssh_port_homepath(self):
  534. c, path = get_transport_and_path_from_url("git+ssh://foo.com:1234/~/bar/baz")
  535. self.assertIsInstance(c, SSHGitClient)
  536. self.assertEqual("foo.com", c.host)
  537. self.assertEqual(1234, c.port)
  538. self.assertEqual("/~/bar/baz", path)
  539. def test_ssh_host_relpath(self):
  540. self.assertRaises(
  541. ValueError, get_transport_and_path_from_url, "foo.com:bar/baz"
  542. )
  543. def test_ssh_user_host_relpath(self):
  544. self.assertRaises(
  545. ValueError, get_transport_and_path_from_url, "user@foo.com:bar/baz"
  546. )
  547. def test_local_path(self):
  548. self.assertRaises(ValueError, get_transport_and_path_from_url, "foo.bar/baz")
  549. def test_error(self):
  550. # Need to use a known urlparse.uses_netloc URL scheme to get the
  551. # expected parsing of the URL on Python versions less than 2.6.5
  552. self.assertRaises(
  553. ValueError, get_transport_and_path_from_url, "prospero://bar/baz"
  554. )
  555. def test_http(self):
  556. url = "https://github.com/jelmer/dulwich"
  557. c, path = get_transport_and_path_from_url(url)
  558. self.assertIsInstance(c, HttpGitClient)
  559. self.assertEqual("https://github.com", c.get_url(b"/"))
  560. self.assertEqual("/jelmer/dulwich", path)
  561. def test_http_port(self):
  562. url = "https://github.com:9090/jelmer/dulwich"
  563. c, path = get_transport_and_path_from_url(url)
  564. self.assertEqual("https://github.com:9090", c.get_url(b"/"))
  565. self.assertIsInstance(c, HttpGitClient)
  566. self.assertEqual("/jelmer/dulwich", path)
  567. @patch("os.name", "posix")
  568. @patch("sys.platform", "linux")
  569. def test_file(self):
  570. c, path = get_transport_and_path_from_url("file:///home/jelmer/foo")
  571. self.assertIsInstance(c, LocalGitClient)
  572. self.assertEqual("/home/jelmer/foo", path)
  573. @patch("os.name", "nt")
  574. @patch("sys.platform", "win32")
  575. def test_file_win(self):
  576. # `_win32_url_to_path` uses urllib.request.url2pathname, which is set to
  577. # `ntutl2path.url2pathname` when `os.name==nt`
  578. from nturl2path import url2pathname
  579. with patch("dulwich.client.url2pathname", url2pathname):
  580. expected = "C:\\foo.bar\\baz"
  581. for file_url in [
  582. "file:C:/foo.bar/baz",
  583. "file:/C:/foo.bar/baz",
  584. "file://C:/foo.bar/baz",
  585. "file://C://foo.bar//baz",
  586. "file:///C:/foo.bar/baz",
  587. ]:
  588. c, path = get_transport_and_path(file_url)
  589. self.assertIsInstance(c, LocalGitClient)
  590. self.assertEqual(path, expected)
  591. for remote_url in [
  592. "file://host.example.com/C:/foo.bar/baz"
  593. "file://host.example.com/C:/foo.bar/baz"
  594. "file:////host.example/foo.bar/baz",
  595. ]:
  596. with self.assertRaises(NotImplementedError):
  597. c, path = get_transport_and_path(remote_url)
  598. class TestSSHVendor:
  599. def __init__(self) -> None:
  600. self.host = None
  601. self.command = ""
  602. self.username = None
  603. self.port = None
  604. self.password = None
  605. self.key_filename = None
  606. def run_command(
  607. self,
  608. host,
  609. command,
  610. username=None,
  611. port=None,
  612. password=None,
  613. key_filename=None,
  614. ssh_command=None,
  615. protocol_version=None,
  616. ):
  617. self.host = host
  618. self.command = command
  619. self.username = username
  620. self.port = port
  621. self.password = password
  622. self.key_filename = key_filename
  623. self.ssh_command = ssh_command
  624. self.protocol_version = protocol_version
  625. class Subprocess:
  626. pass
  627. Subprocess.read = lambda: None
  628. Subprocess.write = lambda: None
  629. Subprocess.close = lambda: None
  630. Subprocess.can_read = lambda: None
  631. return Subprocess()
  632. class SSHGitClientTests(TestCase):
  633. def setUp(self):
  634. super().setUp()
  635. self.server = TestSSHVendor()
  636. self.real_vendor = client.get_ssh_vendor
  637. client.get_ssh_vendor = lambda: self.server
  638. self.client = SSHGitClient("git.samba.org")
  639. def tearDown(self):
  640. super().tearDown()
  641. client.get_ssh_vendor = self.real_vendor
  642. def test_get_url(self):
  643. path = "/tmp/repo.git"
  644. c = SSHGitClient("git.samba.org")
  645. url = c.get_url(path)
  646. self.assertEqual("ssh://git.samba.org/tmp/repo.git", url)
  647. def test_get_url_with_username_and_port(self):
  648. path = "/tmp/repo.git"
  649. c = SSHGitClient("git.samba.org", port=2222, username="user")
  650. url = c.get_url(path)
  651. self.assertEqual("ssh://user@git.samba.org:2222/tmp/repo.git", url)
  652. def test_default_command(self):
  653. self.assertEqual(b"git-upload-pack", self.client._get_cmd_path(b"upload-pack"))
  654. def test_alternative_command_path(self):
  655. self.client.alternative_paths[b"upload-pack"] = b"/usr/lib/git/git-upload-pack"
  656. self.assertEqual(
  657. b"/usr/lib/git/git-upload-pack",
  658. self.client._get_cmd_path(b"upload-pack"),
  659. )
  660. def test_alternative_command_path_spaces(self):
  661. self.client.alternative_paths[b"upload-pack"] = (
  662. b"/usr/lib/git/git-upload-pack -ibla"
  663. )
  664. self.assertEqual(
  665. b"/usr/lib/git/git-upload-pack -ibla",
  666. self.client._get_cmd_path(b"upload-pack"),
  667. )
  668. def test_connect(self):
  669. server = self.server
  670. client = self.client
  671. client.username = b"username"
  672. client.port = 1337
  673. client._connect(b"command", b"/path/to/repo")
  674. self.assertEqual(b"username", server.username)
  675. self.assertEqual(1337, server.port)
  676. self.assertEqual("git-command '/path/to/repo'", server.command)
  677. client._connect(b"relative-command", b"/~/path/to/repo")
  678. self.assertEqual("git-relative-command '~/path/to/repo'", server.command)
  679. def test_ssh_command_precedence(self):
  680. self.overrideEnv("GIT_SSH", "/path/to/ssh")
  681. test_client = SSHGitClient("git.samba.org")
  682. self.assertEqual(test_client.ssh_command, "/path/to/ssh")
  683. self.overrideEnv("GIT_SSH_COMMAND", "/path/to/ssh -o Option=Value")
  684. test_client = SSHGitClient("git.samba.org")
  685. self.assertEqual(test_client.ssh_command, "/path/to/ssh -o Option=Value")
  686. test_client = SSHGitClient("git.samba.org", ssh_command="ssh -o Option1=Value1")
  687. self.assertEqual(test_client.ssh_command, "ssh -o Option1=Value1")
  688. class ReportStatusParserTests(TestCase):
  689. def test_invalid_pack(self):
  690. parser = ReportStatusParser()
  691. parser.handle_packet(b"unpack error - foo bar")
  692. parser.handle_packet(b"ok refs/foo/bar")
  693. parser.handle_packet(None)
  694. self.assertRaises(SendPackError, list, parser.check())
  695. def test_update_refs_error(self):
  696. parser = ReportStatusParser()
  697. parser.handle_packet(b"unpack ok")
  698. parser.handle_packet(b"ng refs/foo/bar need to pull")
  699. parser.handle_packet(None)
  700. self.assertEqual([(b"refs/foo/bar", "need to pull")], list(parser.check()))
  701. def test_ok(self):
  702. parser = ReportStatusParser()
  703. parser.handle_packet(b"unpack ok")
  704. parser.handle_packet(b"ok refs/foo/bar")
  705. parser.handle_packet(None)
  706. self.assertEqual([(b"refs/foo/bar", None)], list(parser.check()))
  707. class LocalGitClientTests(TestCase):
  708. def test_get_url(self):
  709. path = "/tmp/repo.git"
  710. c = LocalGitClient()
  711. url = c.get_url(path)
  712. self.assertEqual("file:///tmp/repo.git", url)
  713. def test_fetch_into_empty(self):
  714. c = LocalGitClient()
  715. target = tempfile.mkdtemp()
  716. self.addCleanup(shutil.rmtree, target)
  717. t = Repo.init_bare(target)
  718. self.addCleanup(t.close)
  719. s = open_repo("a.git")
  720. self.addCleanup(tear_down_repo, s)
  721. self.assertEqual(s.get_refs(), c.fetch(s.path, t).refs)
  722. def test_clone(self):
  723. c = LocalGitClient()
  724. s = open_repo("a.git")
  725. self.addCleanup(tear_down_repo, s)
  726. target = tempfile.mkdtemp()
  727. self.addCleanup(shutil.rmtree, target)
  728. result_repo = c.clone(s.path, target, mkdir=False)
  729. self.addCleanup(result_repo.close)
  730. expected = dict(s.get_refs())
  731. expected[b"refs/remotes/origin/HEAD"] = expected[b"HEAD"]
  732. expected[b"refs/remotes/origin/master"] = expected[b"refs/heads/master"]
  733. self.assertEqual(expected, result_repo.get_refs())
  734. def test_fetch_empty(self):
  735. c = LocalGitClient()
  736. s = open_repo("a.git")
  737. self.addCleanup(tear_down_repo, s)
  738. out = BytesIO()
  739. walker = {}
  740. ret = c.fetch_pack(
  741. s.path, lambda heads, **kwargs: [], graph_walker=walker, pack_data=out.write
  742. )
  743. self.assertEqual(
  744. {
  745. b"HEAD": b"a90fa2d900a17e99b433217e988c4eb4a2e9a097",
  746. b"refs/heads/master": b"a90fa2d900a17e99b433217e988c4eb4a2e9a097",
  747. b"refs/tags/mytag": b"28237f4dc30d0d462658d6b937b08a0f0b6ef55a",
  748. b"refs/tags/mytag-packed": b"b0931cadc54336e78a1d980420e3268903b57a50",
  749. },
  750. ret.refs,
  751. )
  752. self.assertEqual({b"HEAD": b"refs/heads/master"}, ret.symrefs)
  753. self.assertEqual(
  754. b"PACK\x00\x00\x00\x02\x00\x00\x00\x00\x02\x9d\x08"
  755. b"\x82;\xd8\xa8\xea\xb5\x10\xadj\xc7\\\x82<\xfd>\xd3\x1e",
  756. out.getvalue(),
  757. )
  758. def test_fetch_pack_none(self):
  759. c = LocalGitClient()
  760. s = open_repo("a.git")
  761. self.addCleanup(tear_down_repo, s)
  762. out = BytesIO()
  763. walker = MemoryRepo().get_graph_walker()
  764. ret = c.fetch_pack(
  765. s.path,
  766. lambda heads, **kwargs: [b"a90fa2d900a17e99b433217e988c4eb4a2e9a097"],
  767. graph_walker=walker,
  768. pack_data=out.write,
  769. )
  770. self.assertEqual({b"HEAD": b"refs/heads/master"}, ret.symrefs)
  771. self.assertEqual(
  772. {
  773. b"HEAD": b"a90fa2d900a17e99b433217e988c4eb4a2e9a097",
  774. b"refs/heads/master": b"a90fa2d900a17e99b433217e988c4eb4a2e9a097",
  775. b"refs/tags/mytag": b"28237f4dc30d0d462658d6b937b08a0f0b6ef55a",
  776. b"refs/tags/mytag-packed": b"b0931cadc54336e78a1d980420e3268903b57a50",
  777. },
  778. ret.refs,
  779. )
  780. # Hardcoding is not ideal, but we'll fix that some other day..
  781. self.assertTrue(
  782. out.getvalue().startswith(b"PACK\x00\x00\x00\x02\x00\x00\x00\x07")
  783. )
  784. def test_send_pack_without_changes(self):
  785. local = open_repo("a.git")
  786. self.addCleanup(tear_down_repo, local)
  787. target = open_repo("a.git")
  788. self.addCleanup(tear_down_repo, target)
  789. self.send_and_verify(b"master", local, target)
  790. def test_send_pack_with_changes(self):
  791. local = open_repo("a.git")
  792. self.addCleanup(tear_down_repo, local)
  793. target_path = tempfile.mkdtemp()
  794. self.addCleanup(shutil.rmtree, target_path)
  795. with Repo.init_bare(target_path) as target:
  796. self.send_and_verify(b"master", local, target)
  797. def test_get_refs(self):
  798. local = open_repo("refs.git")
  799. self.addCleanup(tear_down_repo, local)
  800. client = LocalGitClient()
  801. refs = client.get_refs(local.path)
  802. self.assertDictEqual(local.refs.as_dict(), refs)
  803. def send_and_verify(self, branch, local, target):
  804. """Send branch from local to remote repository and verify it worked."""
  805. client = LocalGitClient()
  806. ref_name = b"refs/heads/" + branch
  807. result = client.send_pack(
  808. target.path,
  809. lambda _: {ref_name: local.refs[ref_name]},
  810. local.generate_pack_data,
  811. )
  812. self.assertEqual(local.refs[ref_name], result.refs[ref_name])
  813. self.assertIs(None, result.agent)
  814. self.assertEqual({}, result.ref_status)
  815. obj_local = local.get_object(result.refs[ref_name])
  816. obj_target = target.get_object(result.refs[ref_name])
  817. self.assertEqual(obj_local, obj_target)
  818. class HttpGitClientTests(TestCase):
  819. def test_get_url(self):
  820. base_url = "https://github.com/jelmer/dulwich"
  821. path = "/jelmer/dulwich"
  822. c = HttpGitClient(base_url)
  823. url = c.get_url(path)
  824. self.assertEqual("https://github.com/jelmer/dulwich", url)
  825. def test_get_url_bytes_path(self):
  826. base_url = "https://github.com/jelmer/dulwich"
  827. path_bytes = b"/jelmer/dulwich"
  828. c = HttpGitClient(base_url)
  829. url = c.get_url(path_bytes)
  830. self.assertEqual("https://github.com/jelmer/dulwich", url)
  831. def test_get_url_with_username_and_passwd(self):
  832. base_url = "https://github.com/jelmer/dulwich"
  833. path = "/jelmer/dulwich"
  834. c = HttpGitClient(base_url, username="USERNAME", password="PASSWD")
  835. url = c.get_url(path)
  836. self.assertEqual("https://github.com/jelmer/dulwich", url)
  837. def test_init_username_passwd_set(self):
  838. url = "https://github.com/jelmer/dulwich"
  839. c = HttpGitClient(url, config=None, username="user", password="passwd")
  840. self.assertEqual("user", c._username)
  841. self.assertEqual("passwd", c._password)
  842. basic_auth = c.pool_manager.headers["authorization"]
  843. auth_string = "{}:{}".format("user", "passwd")
  844. b64_credentials = base64.b64encode(auth_string.encode("latin1"))
  845. expected_basic_auth = "Basic {}".format(b64_credentials.decode("latin1"))
  846. self.assertEqual(basic_auth, expected_basic_auth)
  847. def test_init_username_set_no_password(self):
  848. url = "https://github.com/jelmer/dulwich"
  849. c = HttpGitClient(url, config=None, username="user")
  850. self.assertEqual("user", c._username)
  851. self.assertIsNone(c._password)
  852. basic_auth = c.pool_manager.headers["authorization"]
  853. auth_string = b"user:"
  854. b64_credentials = base64.b64encode(auth_string)
  855. expected_basic_auth = f"Basic {b64_credentials.decode('ascii')}"
  856. self.assertEqual(basic_auth, expected_basic_auth)
  857. def test_init_no_username_passwd(self):
  858. url = "https://github.com/jelmer/dulwich"
  859. c = HttpGitClient(url, config=None)
  860. self.assertIs(None, c._username)
  861. self.assertIs(None, c._password)
  862. self.assertNotIn("authorization", c.pool_manager.headers)
  863. def test_from_parsedurl_username_only(self):
  864. username = "user"
  865. url = f"https://{username}@github.com/jelmer/dulwich"
  866. c = HttpGitClient.from_parsedurl(urlparse(url))
  867. self.assertEqual(c._username, username)
  868. self.assertEqual(c._password, None)
  869. basic_auth = c.pool_manager.headers["authorization"]
  870. auth_string = username.encode("ascii") + b":"
  871. b64_credentials = base64.b64encode(auth_string)
  872. expected_basic_auth = f"Basic {b64_credentials.decode('ascii')}"
  873. self.assertEqual(basic_auth, expected_basic_auth)
  874. def test_from_parsedurl_on_url_with_quoted_credentials(self):
  875. original_username = "john|the|first"
  876. quoted_username = urlquote(original_username)
  877. original_password = "Ya#1$2%3"
  878. quoted_password = urlquote(original_password)
  879. url = f"https://{quoted_username}:{quoted_password}@github.com/jelmer/dulwich"
  880. c = HttpGitClient.from_parsedurl(urlparse(url))
  881. self.assertEqual(original_username, c._username)
  882. self.assertEqual(original_password, c._password)
  883. basic_auth = c.pool_manager.headers["authorization"]
  884. auth_string = f"{original_username}:{original_password}"
  885. b64_credentials = base64.b64encode(auth_string.encode("latin1"))
  886. expected_basic_auth = "Basic {}".format(b64_credentials.decode("latin1"))
  887. self.assertEqual(basic_auth, expected_basic_auth)
  888. def test_url_redirect_location(self):
  889. from urllib3.response import HTTPResponse
  890. test_data = {
  891. "https://gitlab.com/inkscape/inkscape/": {
  892. "location": "https://gitlab.com/inkscape/inkscape.git/",
  893. "redirect_url": "https://gitlab.com/inkscape/inkscape.git/",
  894. "refs_data": (
  895. b"001e# service=git-upload-pack\n00000032"
  896. b"fb2bebf4919a011f0fd7cec085443d0031228e76 "
  897. b"HEAD\n0000"
  898. ),
  899. },
  900. "https://github.com/jelmer/dulwich/": {
  901. "location": "https://github.com/jelmer/dulwich/",
  902. "redirect_url": "https://github.com/jelmer/dulwich/",
  903. "refs_data": (
  904. b"001e# service=git-upload-pack\n00000032"
  905. b"3ff25e09724aa4d86ea5bca7d5dd0399a3c8bfcf "
  906. b"HEAD\n0000"
  907. ),
  908. },
  909. # check for absolute-path URI reference as location
  910. "https://codeberg.org/ashwinvis/radicale-sh.git/": {
  911. "location": "/ashwinvis/radicale-auth-sh/",
  912. "redirect_url": "https://codeberg.org/ashwinvis/radicale-auth-sh/",
  913. "refs_data": (
  914. b"001e# service=git-upload-pack\n00000032"
  915. b"470f8603768b608fc988675de2fae8f963c21158 "
  916. b"HEAD\n0000"
  917. ),
  918. },
  919. }
  920. tail = "info/refs?service=git-upload-pack"
  921. # we need to mock urllib3.PoolManager as this test will fail
  922. # otherwise without an active internet connection
  923. class PoolManagerMock:
  924. def __init__(self) -> None:
  925. self.headers: Dict[str, str] = {}
  926. def request(
  927. self,
  928. method,
  929. url,
  930. fields=None,
  931. headers=None,
  932. redirect=True,
  933. preload_content=True,
  934. ):
  935. base_url = url[: -len(tail)]
  936. redirect_base_url = test_data[base_url]["location"]
  937. redirect_url = redirect_base_url + tail
  938. headers = {
  939. "Content-Type": "application/x-git-upload-pack-advertisement"
  940. }
  941. body = test_data[base_url]["refs_data"]
  942. # urllib3 handles automatic redirection by default
  943. status = 200
  944. request_url = redirect_url
  945. # simulate urllib3 behavior when redirect parameter is False
  946. if redirect is False:
  947. request_url = url
  948. if redirect_base_url != base_url:
  949. body = b""
  950. headers["location"] = test_data[base_url]["location"]
  951. status = 301
  952. return HTTPResponse(
  953. body=BytesIO(body),
  954. headers=headers,
  955. request_method=method,
  956. request_url=request_url,
  957. preload_content=preload_content,
  958. status=status,
  959. )
  960. pool_manager = PoolManagerMock()
  961. for base_url in test_data.keys():
  962. # instantiate HttpGitClient with mocked pool manager
  963. c = HttpGitClient(base_url, pool_manager=pool_manager, config=None)
  964. # call method that detects url redirection
  965. _, _, processed_url, _, _ = c._discover_references(
  966. b"git-upload-pack", base_url
  967. )
  968. # send the same request as the method above without redirection
  969. resp = c.pool_manager.request("GET", base_url + tail, redirect=False)
  970. # check expected behavior of urllib3
  971. redirect_location = resp.get_redirect_location()
  972. if resp.status == 200:
  973. self.assertFalse(redirect_location)
  974. if redirect_location:
  975. # check that url redirection has been correctly detected
  976. self.assertEqual(processed_url, test_data[base_url]["redirect_url"])
  977. else:
  978. # check also the no redirection case
  979. self.assertEqual(processed_url, base_url)
  980. def test_smart_request_content_type_with_directive_check(self):
  981. from urllib3.response import HTTPResponse
  982. # we need to mock urllib3.PoolManager as this test will fail
  983. # otherwise without an active internet connection
  984. class PoolManagerMock:
  985. def __init__(self) -> None:
  986. self.headers: Dict[str, str] = {}
  987. def request(
  988. self,
  989. method,
  990. url,
  991. fields=None,
  992. headers=None,
  993. redirect=True,
  994. preload_content=True,
  995. ):
  996. return HTTPResponse(
  997. headers={
  998. "Content-Type": "application/x-git-upload-pack-result; charset=utf-8"
  999. },
  1000. request_method=method,
  1001. request_url=url,
  1002. preload_content=preload_content,
  1003. status=200,
  1004. )
  1005. clone_url = "https://hacktivis.me/git/blog.git/"
  1006. client = HttpGitClient(clone_url, pool_manager=PoolManagerMock(), config=None)
  1007. self.assertTrue(client._smart_request("git-upload-pack", clone_url, data=None))
  1008. def test_urllib3_protocol_error(self):
  1009. from urllib3.exceptions import ProtocolError
  1010. from urllib3.response import HTTPResponse
  1011. error_msg = "protocol error"
  1012. # we need to mock urllib3.PoolManager as this test will fail
  1013. # otherwise without an active internet connection
  1014. class PoolManagerMock:
  1015. def __init__(self) -> None:
  1016. self.headers: Dict[str, str] = {}
  1017. def request(
  1018. self,
  1019. method,
  1020. url,
  1021. fields=None,
  1022. headers=None,
  1023. redirect=True,
  1024. preload_content=True,
  1025. ):
  1026. response = HTTPResponse(
  1027. headers={"Content-Type": "application/x-git-upload-pack-result"},
  1028. request_method=method,
  1029. request_url=url,
  1030. preload_content=preload_content,
  1031. status=200,
  1032. )
  1033. def read(self):
  1034. raise ProtocolError(error_msg)
  1035. # override HTTPResponse.read to throw urllib3.exceptions.ProtocolError
  1036. response.read = read
  1037. return response
  1038. def check_heads(heads, **kwargs):
  1039. self.assertEqual(heads, {})
  1040. return []
  1041. clone_url = "https://git.example.org/user/project.git/"
  1042. client = HttpGitClient(clone_url, pool_manager=PoolManagerMock(), config=None)
  1043. with self.assertRaises(GitProtocolError, msg=error_msg):
  1044. client.fetch_pack(b"/", check_heads, None, None)
  1045. class TCPGitClientTests(TestCase):
  1046. def test_get_url(self):
  1047. host = "github.com"
  1048. path = "/jelmer/dulwich"
  1049. c = TCPGitClient(host)
  1050. url = c.get_url(path)
  1051. self.assertEqual("git://github.com/jelmer/dulwich", url)
  1052. def test_get_url_with_port(self):
  1053. host = "github.com"
  1054. path = "/jelmer/dulwich"
  1055. port = 9090
  1056. c = TCPGitClient(host, port=port)
  1057. url = c.get_url(path)
  1058. self.assertEqual("git://github.com:9090/jelmer/dulwich", url)
  1059. class DefaultUrllib3ManagerTest(TestCase):
  1060. def test_no_config(self):
  1061. manager = default_urllib3_manager(config=None)
  1062. self.assertEqual(manager.connection_pool_kw["cert_reqs"], "CERT_REQUIRED")
  1063. def test_config_no_proxy(self):
  1064. import urllib3
  1065. manager = default_urllib3_manager(config=ConfigDict())
  1066. self.assertNotIsInstance(manager, urllib3.ProxyManager)
  1067. self.assertIsInstance(manager, urllib3.PoolManager)
  1068. def test_config_no_proxy_custom_cls(self):
  1069. import urllib3
  1070. class CustomPoolManager(urllib3.PoolManager):
  1071. pass
  1072. manager = default_urllib3_manager(
  1073. config=ConfigDict(), pool_manager_cls=CustomPoolManager
  1074. )
  1075. self.assertIsInstance(manager, CustomPoolManager)
  1076. def test_config_ssl(self):
  1077. config = ConfigDict()
  1078. config.set(b"http", b"sslVerify", b"true")
  1079. manager = default_urllib3_manager(config=config)
  1080. self.assertEqual(manager.connection_pool_kw["cert_reqs"], "CERT_REQUIRED")
  1081. def test_config_no_ssl(self):
  1082. config = ConfigDict()
  1083. config.set(b"http", b"sslVerify", b"false")
  1084. manager = default_urllib3_manager(config=config)
  1085. self.assertEqual(manager.connection_pool_kw["cert_reqs"], "CERT_NONE")
  1086. def test_config_proxy(self):
  1087. import urllib3
  1088. config = ConfigDict()
  1089. config.set(b"http", b"proxy", b"http://localhost:3128/")
  1090. manager = default_urllib3_manager(config=config)
  1091. self.assertIsInstance(manager, urllib3.ProxyManager)
  1092. self.assertTrue(hasattr(manager, "proxy"))
  1093. self.assertEqual(manager.proxy.scheme, "http")
  1094. self.assertEqual(manager.proxy.host, "localhost")
  1095. self.assertEqual(manager.proxy.port, 3128)
  1096. def test_environment_proxy(self):
  1097. import urllib3
  1098. config = ConfigDict()
  1099. self.overrideEnv("http_proxy", "http://myproxy:8080")
  1100. manager = default_urllib3_manager(config=config)
  1101. self.assertIsInstance(manager, urllib3.ProxyManager)
  1102. self.assertTrue(hasattr(manager, "proxy"))
  1103. self.assertEqual(manager.proxy.scheme, "http")
  1104. self.assertEqual(manager.proxy.host, "myproxy")
  1105. self.assertEqual(manager.proxy.port, 8080)
  1106. def test_environment_empty_proxy(self):
  1107. import urllib3
  1108. config = ConfigDict()
  1109. self.overrideEnv("http_proxy", "")
  1110. manager = default_urllib3_manager(config=config)
  1111. self.assertNotIsInstance(manager, urllib3.ProxyManager)
  1112. self.assertIsInstance(manager, urllib3.PoolManager)
  1113. def test_environment_no_proxy_1(self):
  1114. import urllib3
  1115. config = ConfigDict()
  1116. self.overrideEnv("http_proxy", "http://myproxy:8080")
  1117. self.overrideEnv("no_proxy", "xyz,abc.def.gh,abc.gh")
  1118. base_url = "http://xyz.abc.def.gh:8080/path/port"
  1119. manager = default_urllib3_manager(config=config, base_url=base_url)
  1120. self.assertNotIsInstance(manager, urllib3.ProxyManager)
  1121. self.assertIsInstance(manager, urllib3.PoolManager)
  1122. def test_environment_no_proxy_2(self):
  1123. import urllib3
  1124. config = ConfigDict()
  1125. self.overrideEnv("http_proxy", "http://myproxy:8080")
  1126. self.overrideEnv("no_proxy", "xyz,abc.def.gh,abc.gh,ample.com")
  1127. base_url = "http://ample.com/path/port"
  1128. manager = default_urllib3_manager(config=config, base_url=base_url)
  1129. self.assertNotIsInstance(manager, urllib3.ProxyManager)
  1130. self.assertIsInstance(manager, urllib3.PoolManager)
  1131. def test_environment_no_proxy_3(self):
  1132. import urllib3
  1133. config = ConfigDict()
  1134. self.overrideEnv("http_proxy", "http://myproxy:8080")
  1135. self.overrideEnv("no_proxy", "xyz,abc.def.gh,abc.gh,ample.com")
  1136. base_url = "http://ample.com:80/path/port"
  1137. manager = default_urllib3_manager(config=config, base_url=base_url)
  1138. self.assertNotIsInstance(manager, urllib3.ProxyManager)
  1139. self.assertIsInstance(manager, urllib3.PoolManager)
  1140. def test_environment_no_proxy_4(self):
  1141. import urllib3
  1142. config = ConfigDict()
  1143. self.overrideEnv("http_proxy", "http://myproxy:8080")
  1144. self.overrideEnv("no_proxy", "xyz,abc.def.gh,abc.gh,ample.com")
  1145. base_url = "http://www.ample.com/path/port"
  1146. manager = default_urllib3_manager(config=config, base_url=base_url)
  1147. self.assertNotIsInstance(manager, urllib3.ProxyManager)
  1148. self.assertIsInstance(manager, urllib3.PoolManager)
  1149. def test_environment_no_proxy_5(self):
  1150. import urllib3
  1151. config = ConfigDict()
  1152. self.overrideEnv("http_proxy", "http://myproxy:8080")
  1153. self.overrideEnv("no_proxy", "xyz,abc.def.gh,abc.gh,ample.com")
  1154. base_url = "http://www.example.com/path/port"
  1155. manager = default_urllib3_manager(config=config, base_url=base_url)
  1156. self.assertIsInstance(manager, urllib3.ProxyManager)
  1157. self.assertTrue(hasattr(manager, "proxy"))
  1158. self.assertEqual(manager.proxy.scheme, "http")
  1159. self.assertEqual(manager.proxy.host, "myproxy")
  1160. self.assertEqual(manager.proxy.port, 8080)
  1161. def test_environment_no_proxy_6(self):
  1162. import urllib3
  1163. config = ConfigDict()
  1164. self.overrideEnv("http_proxy", "http://myproxy:8080")
  1165. self.overrideEnv("no_proxy", "xyz,abc.def.gh,abc.gh,ample.com")
  1166. base_url = "http://ample.com.org/path/port"
  1167. manager = default_urllib3_manager(config=config, base_url=base_url)
  1168. self.assertIsInstance(manager, urllib3.ProxyManager)
  1169. self.assertTrue(hasattr(manager, "proxy"))
  1170. self.assertEqual(manager.proxy.scheme, "http")
  1171. self.assertEqual(manager.proxy.host, "myproxy")
  1172. self.assertEqual(manager.proxy.port, 8080)
  1173. def test_environment_no_proxy_ipv4_address_1(self):
  1174. import urllib3
  1175. config = ConfigDict()
  1176. self.overrideEnv("http_proxy", "http://myproxy:8080")
  1177. self.overrideEnv("no_proxy", "xyz,abc.def.gh,192.168.0.10,ample.com")
  1178. base_url = "http://192.168.0.10/path/port"
  1179. manager = default_urllib3_manager(config=config, base_url=base_url)
  1180. self.assertNotIsInstance(manager, urllib3.ProxyManager)
  1181. self.assertIsInstance(manager, urllib3.PoolManager)
  1182. def test_environment_no_proxy_ipv4_address_2(self):
  1183. import urllib3
  1184. config = ConfigDict()
  1185. self.overrideEnv("http_proxy", "http://myproxy:8080")
  1186. self.overrideEnv("no_proxy", "xyz,abc.def.gh,192.168.0.10,ample.com")
  1187. base_url = "http://192.168.0.10:8888/path/port"
  1188. manager = default_urllib3_manager(config=config, base_url=base_url)
  1189. self.assertNotIsInstance(manager, urllib3.ProxyManager)
  1190. self.assertIsInstance(manager, urllib3.PoolManager)
  1191. def test_environment_no_proxy_ipv4_address_3(self):
  1192. import urllib3
  1193. config = ConfigDict()
  1194. self.overrideEnv("http_proxy", "http://myproxy:8080")
  1195. self.overrideEnv(
  1196. "no_proxy", "xyz,abc.def.gh,ff80:1::/64,192.168.0.0/24,ample.com"
  1197. )
  1198. base_url = "http://192.168.0.10/path/port"
  1199. manager = default_urllib3_manager(config=config, base_url=base_url)
  1200. self.assertNotIsInstance(manager, urllib3.ProxyManager)
  1201. self.assertIsInstance(manager, urllib3.PoolManager)
  1202. def test_environment_no_proxy_ipv6_address_1(self):
  1203. import urllib3
  1204. config = ConfigDict()
  1205. self.overrideEnv("http_proxy", "http://myproxy:8080")
  1206. self.overrideEnv("no_proxy", "xyz,abc.def.gh,ff80:1::affe,ample.com")
  1207. base_url = "http://[ff80:1::affe]/path/port"
  1208. manager = default_urllib3_manager(config=config, base_url=base_url)
  1209. self.assertNotIsInstance(manager, urllib3.ProxyManager)
  1210. self.assertIsInstance(manager, urllib3.PoolManager)
  1211. def test_environment_no_proxy_ipv6_address_2(self):
  1212. import urllib3
  1213. config = ConfigDict()
  1214. self.overrideEnv("http_proxy", "http://myproxy:8080")
  1215. self.overrideEnv("no_proxy", "xyz,abc.def.gh,ff80:1::affe,ample.com")
  1216. base_url = "http://[ff80:1::affe]:1234/path/port"
  1217. manager = default_urllib3_manager(config=config, base_url=base_url)
  1218. self.assertNotIsInstance(manager, urllib3.ProxyManager)
  1219. self.assertIsInstance(manager, urllib3.PoolManager)
  1220. def test_environment_no_proxy_ipv6_address_3(self):
  1221. import urllib3
  1222. config = ConfigDict()
  1223. self.overrideEnv("http_proxy", "http://myproxy:8080")
  1224. self.overrideEnv(
  1225. "no_proxy", "xyz,abc.def.gh,192.168.0.0/24,ff80:1::/64,ample.com"
  1226. )
  1227. base_url = "http://[ff80:1::affe]/path/port"
  1228. manager = default_urllib3_manager(config=config, base_url=base_url)
  1229. self.assertNotIsInstance(manager, urllib3.ProxyManager)
  1230. self.assertIsInstance(manager, urllib3.PoolManager)
  1231. def test_config_proxy_custom_cls(self):
  1232. import urllib3
  1233. class CustomProxyManager(urllib3.ProxyManager):
  1234. pass
  1235. config = ConfigDict()
  1236. config.set(b"http", b"proxy", b"http://localhost:3128/")
  1237. manager = default_urllib3_manager(
  1238. config=config, proxy_manager_cls=CustomProxyManager
  1239. )
  1240. self.assertIsInstance(manager, CustomProxyManager)
  1241. def test_config_proxy_creds(self):
  1242. import urllib3
  1243. config = ConfigDict()
  1244. config.set(b"http", b"proxy", b"http://jelmer:example@localhost:3128/")
  1245. manager = default_urllib3_manager(config=config)
  1246. assert isinstance(manager, urllib3.ProxyManager)
  1247. self.assertEqual(
  1248. manager.proxy_headers, {"proxy-authorization": "Basic amVsbWVyOmV4YW1wbGU="}
  1249. )
  1250. def test_config_no_verify_ssl(self):
  1251. manager = default_urllib3_manager(config=None, cert_reqs="CERT_NONE")
  1252. self.assertEqual(manager.connection_pool_kw["cert_reqs"], "CERT_NONE")
  1253. class SubprocessSSHVendorTests(TestCase):
  1254. def setUp(self):
  1255. # Monkey Patch client subprocess popen
  1256. self._orig_popen = dulwich.client.subprocess.Popen
  1257. dulwich.client.subprocess.Popen = DummyPopen
  1258. def tearDown(self):
  1259. dulwich.client.subprocess.Popen = self._orig_popen
  1260. def test_run_command_dashes(self):
  1261. vendor = SubprocessSSHVendor()
  1262. self.assertRaises(
  1263. StrangeHostname,
  1264. vendor.run_command,
  1265. "--weird-host",
  1266. "git-clone-url",
  1267. )
  1268. def test_run_command_password(self):
  1269. vendor = SubprocessSSHVendor()
  1270. self.assertRaises(
  1271. NotImplementedError,
  1272. vendor.run_command,
  1273. "host",
  1274. "git-clone-url",
  1275. password="12345",
  1276. )
  1277. def test_run_command_password_and_privkey(self):
  1278. vendor = SubprocessSSHVendor()
  1279. self.assertRaises(
  1280. NotImplementedError,
  1281. vendor.run_command,
  1282. "host",
  1283. "git-clone-url",
  1284. password="12345",
  1285. key_filename="/tmp/id_rsa",
  1286. )
  1287. def test_run_command_with_port_username_and_privkey(self):
  1288. expected = [
  1289. "ssh",
  1290. "-x",
  1291. "-p",
  1292. "2200",
  1293. "-i",
  1294. "/tmp/id_rsa",
  1295. ]
  1296. if DEFAULT_GIT_PROTOCOL_VERSION_FETCH:
  1297. expected += [
  1298. "-o",
  1299. f"SetEnv GIT_PROTOCOL=version={DEFAULT_GIT_PROTOCOL_VERSION_FETCH}",
  1300. ]
  1301. expected += [
  1302. "user@host",
  1303. "git-clone-url",
  1304. ]
  1305. vendor = SubprocessSSHVendor()
  1306. command = vendor.run_command(
  1307. "host",
  1308. "git-clone-url",
  1309. username="user",
  1310. port="2200",
  1311. key_filename="/tmp/id_rsa",
  1312. )
  1313. args = command.proc.args
  1314. self.assertListEqual(expected, args[0])
  1315. def test_run_with_ssh_command(self):
  1316. expected = [
  1317. "/path/to/ssh",
  1318. "-o",
  1319. "Option=Value",
  1320. "-x",
  1321. ]
  1322. if DEFAULT_GIT_PROTOCOL_VERSION_FETCH:
  1323. expected += [
  1324. "-o",
  1325. f"SetEnv GIT_PROTOCOL=version={DEFAULT_GIT_PROTOCOL_VERSION_FETCH}",
  1326. ]
  1327. expected += [
  1328. "host",
  1329. "git-clone-url",
  1330. ]
  1331. vendor = SubprocessSSHVendor()
  1332. command = vendor.run_command(
  1333. "host",
  1334. "git-clone-url",
  1335. ssh_command="/path/to/ssh -o Option=Value",
  1336. )
  1337. args = command.proc.args
  1338. self.assertListEqual(expected, args[0])
  1339. class PLinkSSHVendorTests(TestCase):
  1340. def setUp(self):
  1341. # Monkey Patch client subprocess popen
  1342. self._orig_popen = dulwich.client.subprocess.Popen
  1343. dulwich.client.subprocess.Popen = DummyPopen
  1344. def tearDown(self):
  1345. dulwich.client.subprocess.Popen = self._orig_popen
  1346. def test_run_command_dashes(self):
  1347. vendor = PLinkSSHVendor()
  1348. self.assertRaises(
  1349. StrangeHostname,
  1350. vendor.run_command,
  1351. "--weird-host",
  1352. "git-clone-url",
  1353. )
  1354. def test_run_command_password_and_privkey(self):
  1355. vendor = PLinkSSHVendor()
  1356. warnings.simplefilter("always", UserWarning)
  1357. self.addCleanup(warnings.resetwarnings)
  1358. warnings_list, restore_warnings = setup_warning_catcher()
  1359. self.addCleanup(restore_warnings)
  1360. command = vendor.run_command(
  1361. "host",
  1362. "git-clone-url",
  1363. password="12345",
  1364. key_filename="/tmp/id_rsa",
  1365. )
  1366. expected_warning = UserWarning(
  1367. "Invoking PLink with a password exposes the password in the "
  1368. "process list."
  1369. )
  1370. for w in warnings_list:
  1371. if type(w) is type(expected_warning) and w.args == expected_warning.args:
  1372. break
  1373. else:
  1374. raise AssertionError(
  1375. f"Expected warning {expected_warning!r} not in {warnings_list!r}"
  1376. )
  1377. args = command.proc.args
  1378. if sys.platform == "win32":
  1379. binary = ["plink.exe", "-ssh"]
  1380. else:
  1381. binary = ["plink", "-ssh"]
  1382. expected = [
  1383. *binary,
  1384. "-pw",
  1385. "12345",
  1386. "-i",
  1387. "/tmp/id_rsa",
  1388. "host",
  1389. "git-clone-url",
  1390. ]
  1391. self.assertListEqual(expected, args[0])
  1392. def test_run_command_password(self):
  1393. if sys.platform == "win32":
  1394. binary = ["plink.exe", "-ssh"]
  1395. else:
  1396. binary = ["plink", "-ssh"]
  1397. expected = [*binary, "-pw", "12345", "host", "git-clone-url"]
  1398. vendor = PLinkSSHVendor()
  1399. warnings.simplefilter("always", UserWarning)
  1400. self.addCleanup(warnings.resetwarnings)
  1401. warnings_list, restore_warnings = setup_warning_catcher()
  1402. self.addCleanup(restore_warnings)
  1403. command = vendor.run_command("host", "git-clone-url", password="12345")
  1404. expected_warning = UserWarning(
  1405. "Invoking PLink with a password exposes the password in the "
  1406. "process list."
  1407. )
  1408. for w in warnings_list:
  1409. if type(w) is type(expected_warning) and w.args == expected_warning.args:
  1410. break
  1411. else:
  1412. raise AssertionError(
  1413. f"Expected warning {expected_warning!r} not in {warnings_list!r}"
  1414. )
  1415. args = command.proc.args
  1416. self.assertListEqual(expected, args[0])
  1417. def test_run_command_with_port_username_and_privkey(self):
  1418. if sys.platform == "win32":
  1419. binary = ["plink.exe", "-ssh"]
  1420. else:
  1421. binary = ["plink", "-ssh"]
  1422. expected = [
  1423. *binary,
  1424. "-P",
  1425. "2200",
  1426. "-i",
  1427. "/tmp/id_rsa",
  1428. "user@host",
  1429. "git-clone-url",
  1430. ]
  1431. vendor = PLinkSSHVendor()
  1432. command = vendor.run_command(
  1433. "host",
  1434. "git-clone-url",
  1435. username="user",
  1436. port="2200",
  1437. key_filename="/tmp/id_rsa",
  1438. )
  1439. args = command.proc.args
  1440. self.assertListEqual(expected, args[0])
  1441. def test_run_with_ssh_command(self):
  1442. expected = [
  1443. "/path/to/plink",
  1444. "-ssh",
  1445. "host",
  1446. "git-clone-url",
  1447. ]
  1448. vendor = PLinkSSHVendor()
  1449. command = vendor.run_command(
  1450. "host",
  1451. "git-clone-url",
  1452. ssh_command="/path/to/plink",
  1453. )
  1454. args = command.proc.args
  1455. self.assertListEqual(expected, args[0])
  1456. class RsyncUrlTests(TestCase):
  1457. def test_simple(self):
  1458. self.assertEqual(parse_rsync_url("foo:bar/path"), (None, "foo", "bar/path"))
  1459. self.assertEqual(
  1460. parse_rsync_url("user@foo:bar/path"), ("user", "foo", "bar/path")
  1461. )
  1462. def test_path(self):
  1463. self.assertRaises(ValueError, parse_rsync_url, "/path")
  1464. class CheckWantsTests(TestCase):
  1465. def test_fine(self):
  1466. check_wants(
  1467. [b"2f3dc7a53fb752a6961d3a56683df46d4d3bf262"],
  1468. {b"refs/heads/blah": b"2f3dc7a53fb752a6961d3a56683df46d4d3bf262"},
  1469. )
  1470. def test_missing(self):
  1471. self.assertRaises(
  1472. InvalidWants,
  1473. check_wants,
  1474. [b"2f3dc7a53fb752a6961d3a56683df46d4d3bf262"],
  1475. {b"refs/heads/blah": b"3f3dc7a53fb752a6961d3a56683df46d4d3bf262"},
  1476. )
  1477. def test_annotated(self):
  1478. self.assertRaises(
  1479. InvalidWants,
  1480. check_wants,
  1481. [b"2f3dc7a53fb752a6961d3a56683df46d4d3bf262"],
  1482. {
  1483. b"refs/heads/blah": b"3f3dc7a53fb752a6961d3a56683df46d4d3bf262",
  1484. b"refs/heads/blah^{}": b"2f3dc7a53fb752a6961d3a56683df46d4d3bf262",
  1485. },
  1486. )
  1487. class FetchPackResultTests(TestCase):
  1488. def test_eq(self):
  1489. self.assertEqual(
  1490. FetchPackResult(
  1491. {b"refs/heads/master": b"2f3dc7a53fb752a6961d3a56683df46d4d3bf262"},
  1492. {},
  1493. b"user/agent",
  1494. ),
  1495. FetchPackResult(
  1496. {b"refs/heads/master": b"2f3dc7a53fb752a6961d3a56683df46d4d3bf262"},
  1497. {},
  1498. b"user/agent",
  1499. ),
  1500. )
  1501. class GitCredentialStoreTests(TestCase):
  1502. @classmethod
  1503. def setUpClass(cls):
  1504. with tempfile.NamedTemporaryFile(delete=False) as f:
  1505. f.write(b"https://user:pass@example.org\n")
  1506. cls.fname = f.name
  1507. @classmethod
  1508. def tearDownClass(cls):
  1509. os.unlink(cls.fname)
  1510. def test_nonmatching_scheme(self):
  1511. self.assertEqual(
  1512. get_credentials_from_store(b"http", b"example.org", fnames=[self.fname]),
  1513. None,
  1514. )
  1515. def test_nonmatching_hostname(self):
  1516. self.assertEqual(
  1517. get_credentials_from_store(b"https", b"noentry.org", fnames=[self.fname]),
  1518. None,
  1519. )
  1520. def test_match_without_username(self):
  1521. self.assertEqual(
  1522. get_credentials_from_store(b"https", b"example.org", fnames=[self.fname]),
  1523. (b"user", b"pass"),
  1524. )
  1525. def test_match_with_matching_username(self):
  1526. self.assertEqual(
  1527. get_credentials_from_store(
  1528. b"https", b"example.org", b"user", fnames=[self.fname]
  1529. ),
  1530. (b"user", b"pass"),
  1531. )
  1532. def test_no_match_with_nonmatching_username(self):
  1533. self.assertEqual(
  1534. get_credentials_from_store(
  1535. b"https", b"example.org", b"otheruser", fnames=[self.fname]
  1536. ),
  1537. None,
  1538. )
  1539. class RemoteErrorFromStderrTests(TestCase):
  1540. def test_nothing(self):
  1541. self.assertEqual(_remote_error_from_stderr(None), HangupException())
  1542. def test_error_line(self):
  1543. b = BytesIO(
  1544. b"""\
  1545. This is some random output.
  1546. ERROR: This is the actual error
  1547. with a tail
  1548. """
  1549. )
  1550. self.assertEqual(
  1551. _remote_error_from_stderr(b),
  1552. GitProtocolError("This is the actual error"),
  1553. )
  1554. def test_no_error_line(self):
  1555. b = BytesIO(
  1556. b"""\
  1557. This is output without an error line.
  1558. And this line is just random noise, too.
  1559. """
  1560. )
  1561. self.assertEqual(
  1562. _remote_error_from_stderr(b),
  1563. HangupException(
  1564. [
  1565. b"This is output without an error line.",
  1566. b"And this line is just random noise, too.",
  1567. ]
  1568. ),
  1569. )
  1570. class TestExtractAgentAndSymrefs(TestCase):
  1571. def test_extract_agent_and_symrefs(self):
  1572. (symrefs, agent) = _extract_symrefs_and_agent(
  1573. [b"agent=git/2.31.1", b"symref=HEAD:refs/heads/master"]
  1574. )
  1575. self.assertEqual(agent, b"git/2.31.1")
  1576. self.assertEqual(symrefs, {b"HEAD": b"refs/heads/master"})