2
0

test_client.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. # test_client.py -- Compatibilty tests for git client.
  2. # Copyright (C) 2010 Google, Inc.
  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. """Compatibilty tests between the Dulwich client and the cgit server."""
  21. from contextlib import closing
  22. import copy
  23. from io import BytesIO
  24. import os
  25. import select
  26. import signal
  27. import subprocess
  28. import sys
  29. import tarfile
  30. import tempfile
  31. import threading
  32. try:
  33. from urlparse import unquote
  34. except ImportError:
  35. from urllib.parse import unquote
  36. try:
  37. import BaseHTTPServer
  38. import SimpleHTTPServer
  39. except ImportError:
  40. import http.server
  41. BaseHTTPServer = http.server
  42. SimpleHTTPServer = http.server
  43. if sys.platform == 'win32':
  44. import ctypes
  45. from dulwich import (
  46. client,
  47. errors,
  48. file,
  49. index,
  50. protocol,
  51. objects,
  52. repo,
  53. )
  54. from dulwich.tests import (
  55. SkipTest,
  56. expectedFailure,
  57. )
  58. from dulwich.tests.compat.utils import (
  59. CompatTestCase,
  60. check_for_daemon,
  61. import_repo_to_dir,
  62. rmtree_ro,
  63. run_git_or_fail,
  64. _DEFAULT_GIT,
  65. )
  66. class DulwichClientTestBase(object):
  67. """Tests for client/server compatibility."""
  68. def setUp(self):
  69. self.gitroot = os.path.dirname(import_repo_to_dir('server_new.export').rstrip(os.sep))
  70. self.dest = os.path.join(self.gitroot, 'dest')
  71. file.ensure_dir_exists(self.dest)
  72. run_git_or_fail(['init', '--quiet', '--bare'], cwd=self.dest)
  73. def tearDown(self):
  74. rmtree_ro(self.gitroot)
  75. def assertDestEqualsSrc(self):
  76. repo_dir = os.path.join(self.gitroot, 'server_new.export')
  77. dest_repo_dir = os.path.join(self.gitroot, 'dest')
  78. with closing(repo.Repo(repo_dir)) as src:
  79. with closing(repo.Repo(dest_repo_dir)) as dest:
  80. self.assertReposEqual(src, dest)
  81. def _client(self):
  82. raise NotImplementedError()
  83. def _build_path(self):
  84. raise NotImplementedError()
  85. def _do_send_pack(self):
  86. c = self._client()
  87. srcpath = os.path.join(self.gitroot, 'server_new.export')
  88. with closing(repo.Repo(srcpath)) as src:
  89. sendrefs = dict(src.get_refs())
  90. del sendrefs[b'HEAD']
  91. c.send_pack(self._build_path(b'/dest'), lambda _: sendrefs,
  92. src.object_store.generate_pack_contents)
  93. def test_send_pack(self):
  94. self._do_send_pack()
  95. self.assertDestEqualsSrc()
  96. def test_send_pack_nothing_to_send(self):
  97. self._do_send_pack()
  98. self.assertDestEqualsSrc()
  99. # nothing to send, but shouldn't raise either.
  100. self._do_send_pack()
  101. def test_send_without_report_status(self):
  102. c = self._client()
  103. c._send_capabilities.remove(b'report-status')
  104. srcpath = os.path.join(self.gitroot, 'server_new.export')
  105. with closing(repo.Repo(srcpath)) as src:
  106. sendrefs = dict(src.get_refs())
  107. del sendrefs[b'HEAD']
  108. c.send_pack(self._build_path(b'/dest'), lambda _: sendrefs,
  109. src.object_store.generate_pack_contents)
  110. self.assertDestEqualsSrc()
  111. def make_dummy_commit(self, dest):
  112. b = objects.Blob.from_string(b'hi')
  113. dest.object_store.add_object(b)
  114. t = index.commit_tree(dest.object_store, [(b'hi', b.id, 0o100644)])
  115. c = objects.Commit()
  116. c.author = c.committer = b'Foo Bar <foo@example.com>'
  117. c.author_time = c.commit_time = 0
  118. c.author_timezone = c.commit_timezone = 0
  119. c.message = b'hi'
  120. c.tree = t
  121. dest.object_store.add_object(c)
  122. return c.id
  123. def disable_ff_and_make_dummy_commit(self):
  124. # disable non-fast-forward pushes to the server
  125. dest = repo.Repo(os.path.join(self.gitroot, 'dest'))
  126. run_git_or_fail(['config', 'receive.denyNonFastForwards', 'true'],
  127. cwd=dest.path)
  128. commit_id = self.make_dummy_commit(dest)
  129. return dest, commit_id
  130. def compute_send(self, src):
  131. sendrefs = dict(src.get_refs())
  132. del sendrefs[b'HEAD']
  133. return sendrefs, src.object_store.generate_pack_contents
  134. def test_send_pack_one_error(self):
  135. dest, dummy_commit = self.disable_ff_and_make_dummy_commit()
  136. dest.refs[b'refs/heads/master'] = dummy_commit
  137. repo_dir = os.path.join(self.gitroot, 'server_new.export')
  138. with closing(repo.Repo(repo_dir)) as src:
  139. sendrefs, gen_pack = self.compute_send(src)
  140. c = self._client()
  141. try:
  142. c.send_pack(self._build_path(b'/dest'), lambda _: sendrefs, gen_pack)
  143. except errors.UpdateRefsError as e:
  144. self.assertEqual('refs/heads/master failed to update',
  145. e.args[0])
  146. self.assertEqual({b'refs/heads/branch': b'ok',
  147. b'refs/heads/master': b'non-fast-forward'},
  148. e.ref_status)
  149. def test_send_pack_multiple_errors(self):
  150. dest, dummy = self.disable_ff_and_make_dummy_commit()
  151. # set up for two non-ff errors
  152. branch, master = b'refs/heads/branch', b'refs/heads/master'
  153. dest.refs[branch] = dest.refs[master] = dummy
  154. repo_dir = os.path.join(self.gitroot, 'server_new.export')
  155. with closing(repo.Repo(repo_dir)) as src:
  156. sendrefs, gen_pack = self.compute_send(src)
  157. c = self._client()
  158. try:
  159. c.send_pack(self._build_path(b'/dest'), lambda _: sendrefs, gen_pack)
  160. except errors.UpdateRefsError as e:
  161. self.assertIn(str(e),
  162. ['{0}, {1} failed to update'.format(
  163. branch.decode('ascii'), master.decode('ascii')),
  164. '{1}, {0} failed to update'.format(
  165. branch.decode('ascii'), master.decode('ascii'))])
  166. self.assertEqual({branch: b'non-fast-forward',
  167. master: b'non-fast-forward'},
  168. e.ref_status)
  169. def test_archive(self):
  170. c = self._client()
  171. f = BytesIO()
  172. c.archive(self._build_path(b'/server_new.export'), b'HEAD', f.write)
  173. f.seek(0)
  174. tf = tarfile.open(fileobj=f)
  175. self.assertEqual(['baz', 'foo'], tf.getnames())
  176. def test_fetch_pack(self):
  177. c = self._client()
  178. with closing(repo.Repo(os.path.join(self.gitroot, 'dest'))) as dest:
  179. refs = c.fetch(self._build_path(b'/server_new.export'), dest)
  180. for r in refs.items():
  181. dest.refs.set_if_equals(r[0], None, r[1])
  182. self.assertDestEqualsSrc()
  183. def test_incremental_fetch_pack(self):
  184. self.test_fetch_pack()
  185. dest, dummy = self.disable_ff_and_make_dummy_commit()
  186. dest.refs[b'refs/heads/master'] = dummy
  187. c = self._client()
  188. repo_dir = os.path.join(self.gitroot, 'server_new.export')
  189. with closing(repo.Repo(repo_dir)) as dest:
  190. refs = c.fetch(self._build_path(b'/dest'), dest)
  191. for r in refs.items():
  192. dest.refs.set_if_equals(r[0], None, r[1])
  193. self.assertDestEqualsSrc()
  194. def test_fetch_pack_no_side_band_64k(self):
  195. c = self._client()
  196. c._fetch_capabilities.remove(b'side-band-64k')
  197. with closing(repo.Repo(os.path.join(self.gitroot, 'dest'))) as dest:
  198. refs = c.fetch(self._build_path(b'/server_new.export'), dest)
  199. for r in refs.items():
  200. dest.refs.set_if_equals(r[0], None, r[1])
  201. self.assertDestEqualsSrc()
  202. def test_fetch_pack_zero_sha(self):
  203. # zero sha1s are already present on the client, and should
  204. # be ignored
  205. c = self._client()
  206. with closing(repo.Repo(os.path.join(self.gitroot, 'dest'))) as dest:
  207. refs = c.fetch(self._build_path(b'/server_new.export'), dest,
  208. lambda refs: [protocol.ZERO_SHA])
  209. for r in refs.items():
  210. dest.refs.set_if_equals(r[0], None, r[1])
  211. def test_send_remove_branch(self):
  212. with closing(repo.Repo(os.path.join(self.gitroot, 'dest'))) as dest:
  213. dummy_commit = self.make_dummy_commit(dest)
  214. dest.refs[b'refs/heads/master'] = dummy_commit
  215. dest.refs[b'refs/heads/abranch'] = dummy_commit
  216. sendrefs = dict(dest.refs)
  217. sendrefs[b'refs/heads/abranch'] = b"00" * 20
  218. del sendrefs[b'HEAD']
  219. gen_pack = lambda have, want: []
  220. c = self._client()
  221. self.assertEqual(dest.refs[b"refs/heads/abranch"], dummy_commit)
  222. c.send_pack(self._build_path(b'/dest'), lambda _: sendrefs, gen_pack)
  223. self.assertFalse(b"refs/heads/abranch" in dest.refs)
  224. def test_get_refs(self):
  225. c = self._client()
  226. refs = c.get_refs(self._build_path(b'/server_new.export'))
  227. repo_dir = os.path.join(self.gitroot, 'server_new.export')
  228. with closing(repo.Repo(repo_dir)) as dest:
  229. self.assertDictEqual(dest.refs.as_dict(), refs)
  230. class DulwichTCPClientTest(CompatTestCase, DulwichClientTestBase):
  231. def setUp(self):
  232. CompatTestCase.setUp(self)
  233. DulwichClientTestBase.setUp(self)
  234. if check_for_daemon(limit=1):
  235. raise SkipTest('git-daemon was already running on port %s' %
  236. protocol.TCP_GIT_PORT)
  237. fd, self.pidfile = tempfile.mkstemp(prefix='dulwich-test-git-client',
  238. suffix=".pid")
  239. os.fdopen(fd).close()
  240. args = [_DEFAULT_GIT, 'daemon', '--verbose', '--export-all',
  241. '--pid-file=%s' % self.pidfile,
  242. '--base-path=%s' % self.gitroot,
  243. '--enable=receive-pack', '--enable=upload-archive',
  244. '--listen=localhost', '--reuseaddr',
  245. self.gitroot]
  246. self.process = subprocess.Popen(
  247. args, cwd=self.gitroot,
  248. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  249. if not check_for_daemon():
  250. raise SkipTest('git-daemon failed to start')
  251. def tearDown(self):
  252. with open(self.pidfile) as f:
  253. pid = int(f.read().strip())
  254. if sys.platform == 'win32':
  255. PROCESS_TERMINATE = 1
  256. handle = ctypes.windll.kernel32.OpenProcess(
  257. PROCESS_TERMINATE, False, pid)
  258. ctypes.windll.kernel32.TerminateProcess(handle, -1)
  259. ctypes.windll.kernel32.CloseHandle(handle)
  260. else:
  261. try:
  262. os.kill(pid, signal.SIGKILL)
  263. os.unlink(self.pidfile)
  264. except (OSError, IOError):
  265. pass
  266. self.process.wait()
  267. self.process.stdout.close()
  268. self.process.stderr.close()
  269. DulwichClientTestBase.tearDown(self)
  270. CompatTestCase.tearDown(self)
  271. def _client(self):
  272. return client.TCPGitClient('localhost')
  273. def _build_path(self, path):
  274. return path
  275. if sys.platform == 'win32':
  276. @expectedFailure
  277. def test_fetch_pack_no_side_band_64k(self):
  278. DulwichClientTestBase.test_fetch_pack_no_side_band_64k(self)
  279. class TestSSHVendor(object):
  280. @staticmethod
  281. def run_command(host, command, username=None, port=None):
  282. cmd, path = command.split(b' ')
  283. cmd = cmd.split(b'-', 1)
  284. path = path.replace(b"'", b"")
  285. p = subprocess.Popen(cmd + [path], bufsize=0, stdin=subprocess.PIPE,
  286. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  287. return client.SubprocessWrapper(p)
  288. class DulwichMockSSHClientTest(CompatTestCase, DulwichClientTestBase):
  289. def setUp(self):
  290. CompatTestCase.setUp(self)
  291. DulwichClientTestBase.setUp(self)
  292. self.real_vendor = client.get_ssh_vendor
  293. client.get_ssh_vendor = TestSSHVendor
  294. def tearDown(self):
  295. DulwichClientTestBase.tearDown(self)
  296. CompatTestCase.tearDown(self)
  297. client.get_ssh_vendor = self.real_vendor
  298. def _client(self):
  299. return client.SSHGitClient('localhost')
  300. def _build_path(self, path):
  301. return self.gitroot.encode(sys.getfilesystemencoding()) + path
  302. class DulwichSubprocessClientTest(CompatTestCase, DulwichClientTestBase):
  303. def setUp(self):
  304. CompatTestCase.setUp(self)
  305. DulwichClientTestBase.setUp(self)
  306. def tearDown(self):
  307. DulwichClientTestBase.tearDown(self)
  308. CompatTestCase.tearDown(self)
  309. def _client(self):
  310. return client.SubprocessGitClient(stderr=subprocess.PIPE)
  311. def _build_path(self, path):
  312. return self.gitroot.encode(sys.getfilesystemencoding()) + path
  313. class GitHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
  314. """HTTP Request handler that calls out to 'git http-backend'."""
  315. # Make rfile unbuffered -- we need to read one line and then pass
  316. # the rest to a subprocess, so we can't use buffered input.
  317. rbufsize = 0
  318. def do_POST(self):
  319. self.run_backend()
  320. def do_GET(self):
  321. self.run_backend()
  322. def send_head(self):
  323. return self.run_backend()
  324. def log_request(self, code='-', size='-'):
  325. # Let's be quiet, the test suite is noisy enough already
  326. pass
  327. def run_backend(self):
  328. """Call out to git http-backend."""
  329. # Based on CGIHTTPServer.CGIHTTPRequestHandler.run_cgi:
  330. # Copyright (c) 2001-2010 Python Software Foundation; All Rights Reserved
  331. # Licensed under the Python Software Foundation License.
  332. rest = self.path
  333. # find an explicit query string, if present.
  334. i = rest.rfind('?')
  335. if i >= 0:
  336. rest, query = rest[:i], rest[i+1:]
  337. else:
  338. query = ''
  339. env = copy.deepcopy(os.environ)
  340. env['SERVER_SOFTWARE'] = self.version_string()
  341. env['SERVER_NAME'] = self.server.server_name
  342. env['GATEWAY_INTERFACE'] = 'CGI/1.1'
  343. env['SERVER_PROTOCOL'] = self.protocol_version
  344. env['SERVER_PORT'] = str(self.server.server_port)
  345. env['GIT_PROJECT_ROOT'] = self.server.root_path
  346. env["GIT_HTTP_EXPORT_ALL"] = "1"
  347. env['REQUEST_METHOD'] = self.command
  348. uqrest = unquote(rest)
  349. env['PATH_INFO'] = uqrest
  350. env['SCRIPT_NAME'] = "/"
  351. if query:
  352. env['QUERY_STRING'] = query
  353. host = self.address_string()
  354. if host != self.client_address[0]:
  355. env['REMOTE_HOST'] = host
  356. env['REMOTE_ADDR'] = self.client_address[0]
  357. authorization = self.headers.get("authorization")
  358. if authorization:
  359. authorization = authorization.split()
  360. if len(authorization) == 2:
  361. import base64, binascii
  362. env['AUTH_TYPE'] = authorization[0]
  363. if authorization[0].lower() == "basic":
  364. try:
  365. authorization = base64.decodestring(authorization[1])
  366. except binascii.Error:
  367. pass
  368. else:
  369. authorization = authorization.split(':')
  370. if len(authorization) == 2:
  371. env['REMOTE_USER'] = authorization[0]
  372. # XXX REMOTE_IDENT
  373. env['CONTENT_TYPE'] = self.headers.get('content-type')
  374. length = self.headers.get('content-length')
  375. if length:
  376. env['CONTENT_LENGTH'] = length
  377. referer = self.headers.get('referer')
  378. if referer:
  379. env['HTTP_REFERER'] = referer
  380. accept = []
  381. for line in self.headers.getallmatchingheaders('accept'):
  382. if line[:1] in "\t\n\r ":
  383. accept.append(line.strip())
  384. else:
  385. accept = accept + line[7:].split(',')
  386. env['HTTP_ACCEPT'] = ','.join(accept)
  387. ua = self.headers.get('user-agent')
  388. if ua:
  389. env['HTTP_USER_AGENT'] = ua
  390. co = self.headers.get('cookie')
  391. if co:
  392. env['HTTP_COOKIE'] = co
  393. # XXX Other HTTP_* headers
  394. # Since we're setting the env in the parent, provide empty
  395. # values to override previously set values
  396. for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH',
  397. 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
  398. env.setdefault(k, "")
  399. self.wfile.write(b"HTTP/1.1 200 Script output follows\r\n")
  400. self.wfile.write(
  401. ("Server: %s\r\n" % self.server.server_name).encode('ascii'))
  402. self.wfile.write(
  403. ("Date: %s\r\n" % self.date_time_string()).encode('ascii'))
  404. decoded_query = query.replace('+', ' ')
  405. try:
  406. nbytes = int(length)
  407. except (TypeError, ValueError):
  408. nbytes = 0
  409. if self.command.lower() == "post" and nbytes > 0:
  410. data = self.rfile.read(nbytes)
  411. else:
  412. data = None
  413. # throw away additional data [see bug #427345]
  414. while select.select([self.rfile._sock], [], [], 0)[0]:
  415. if not self.rfile._sock.recv(1):
  416. break
  417. args = ['http-backend']
  418. if '=' not in decoded_query:
  419. args.append(decoded_query)
  420. stdout = run_git_or_fail(args, input=data, env=env, stderr=subprocess.PIPE)
  421. self.wfile.write(stdout)
  422. class HTTPGitServer(BaseHTTPServer.HTTPServer):
  423. allow_reuse_address = True
  424. def __init__(self, server_address, root_path):
  425. BaseHTTPServer.HTTPServer.__init__(self, server_address, GitHTTPRequestHandler)
  426. self.root_path = root_path
  427. self.server_name = "localhost"
  428. def get_url(self):
  429. return 'http://%s:%s/' % (self.server_name, self.server_port)
  430. class DulwichHttpClientTest(CompatTestCase, DulwichClientTestBase):
  431. min_git_version = (1, 7, 0, 2)
  432. def setUp(self):
  433. CompatTestCase.setUp(self)
  434. DulwichClientTestBase.setUp(self)
  435. self._httpd = HTTPGitServer(("localhost", 0), self.gitroot)
  436. self.addCleanup(self._httpd.shutdown)
  437. threading.Thread(target=self._httpd.serve_forever).start()
  438. run_git_or_fail(['config', 'http.uploadpack', 'true'],
  439. cwd=self.dest)
  440. run_git_or_fail(['config', 'http.receivepack', 'true'],
  441. cwd=self.dest)
  442. def tearDown(self):
  443. DulwichClientTestBase.tearDown(self)
  444. CompatTestCase.tearDown(self)
  445. self._httpd.shutdown()
  446. self._httpd.socket.close()
  447. def _client(self):
  448. return client.HttpGitClient(self._httpd.get_url())
  449. def _build_path(self, path):
  450. if sys.version_info[0] == 3:
  451. return path.decode('ascii')
  452. else:
  453. return path
  454. def test_archive(self):
  455. raise SkipTest("exporting archives not supported over http")