test_web.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # test_web.py -- Compatibility tests for the git web server.
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as published by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Compatibility tests between Dulwich and the cgit HTTP server.
  22. warning: these tests should be fairly stable, but when writing/debugging new
  23. tests, deadlocks may freeze the test process such that it cannot be
  24. Ctrl-C'ed. On POSIX systems, you can kill the tests with Ctrl-Z, "kill %".
  25. """
  26. import sys
  27. import threading
  28. from typing import NoReturn
  29. from wsgiref import simple_server
  30. from dulwich.server import DictBackend, ReceivePackHandler, UploadPackHandler
  31. from dulwich.web import (
  32. HTTPGitApplication,
  33. WSGIRequestHandlerLogger,
  34. WSGIServerLogger,
  35. make_wsgi_chain,
  36. )
  37. from .. import SkipTest, skipIf
  38. from .server_utils import NoSideBand64kReceivePackHandler, ServerTests
  39. from .utils import CompatTestCase
  40. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  41. class WebTests(ServerTests):
  42. """Base tests for web server tests.
  43. Contains utility and setUp/tearDown methods, but does non inherit from
  44. TestCase so tests are not automatically run.
  45. """
  46. protocol = "http"
  47. def _start_server(self, repo):
  48. backend = DictBackend({"/": repo})
  49. app = self._make_app(backend)
  50. dul_server = simple_server.make_server(
  51. "localhost",
  52. 0,
  53. app,
  54. server_class=WSGIServerLogger,
  55. handler_class=WSGIRequestHandlerLogger,
  56. )
  57. self.addCleanup(dul_server.shutdown)
  58. self.addCleanup(dul_server.server_close)
  59. threading.Thread(target=dul_server.serve_forever).start()
  60. self._server = dul_server
  61. _, port = dul_server.socket.getsockname()
  62. return port
  63. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  64. class SmartWebTestCase(WebTests, CompatTestCase):
  65. """Test cases for smart HTTP server.
  66. This server test case does not use side-band-64k in git-receive-pack.
  67. """
  68. min_git_version: tuple[int, ...] = (1, 6, 6)
  69. def _handlers(self):
  70. return {b"git-receive-pack": NoSideBand64kReceivePackHandler}
  71. def _check_app(self, app) -> None:
  72. from dulwich.protocol import Protocol
  73. receive_pack_handler_cls = app.handlers[b"git-receive-pack"]
  74. # Create a handler instance to check capabilities
  75. handler = receive_pack_handler_cls(
  76. app.backend,
  77. [b"/"],
  78. Protocol(lambda x: b"", lambda x: None),
  79. )
  80. caps = handler.capabilities()
  81. self.assertNotIn(b"side-band-64k", caps)
  82. def _make_app(self, backend):
  83. app = make_wsgi_chain(backend, handlers=self._handlers())
  84. to_check = app
  85. # peel back layers until we're at the base application
  86. while not issubclass(to_check.__class__, HTTPGitApplication):
  87. to_check = to_check.app
  88. self._check_app(to_check)
  89. return app
  90. def patch_capabilities(handler, caps_removed):
  91. # Patch a handler's capabilities by specifying a list of them to be
  92. # removed, and return the original method for restoration.
  93. original_capabilities = handler.capabilities
  94. def capabilities(self):
  95. # Call original to get base capabilities (including object-format)
  96. base_caps = original_capabilities(self)
  97. # Filter out the capabilities we want to remove
  98. return [i for i in base_caps if i not in caps_removed]
  99. handler.capabilities = capabilities
  100. return original_capabilities
  101. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  102. class SmartWebSideBand64kTestCase(SmartWebTestCase):
  103. """Test cases for smart HTTP server with side-band-64k support."""
  104. # side-band-64k in git-receive-pack was introduced in git 1.7.0.2
  105. min_git_version = (1, 7, 0, 2)
  106. def setUp(self) -> None:
  107. self.o_uph_cap = patch_capabilities(UploadPackHandler, (b"no-done",))
  108. self.o_rph_cap = patch_capabilities(ReceivePackHandler, (b"no-done",))
  109. super().setUp()
  110. def tearDown(self) -> None:
  111. super().tearDown()
  112. UploadPackHandler.capabilities = self.o_uph_cap
  113. ReceivePackHandler.capabilities = self.o_rph_cap
  114. def _handlers(self) -> None:
  115. return None # default handlers include side-band-64k
  116. def _check_app(self, app) -> None:
  117. from dulwich.protocol import Protocol
  118. receive_pack_handler_cls = app.handlers[b"git-receive-pack"]
  119. # Create a handler instance to check capabilities
  120. handler = receive_pack_handler_cls(
  121. app.backend,
  122. [b"/"],
  123. Protocol(lambda x: b"", lambda x: None),
  124. )
  125. caps = handler.capabilities()
  126. self.assertIn(b"side-band-64k", caps)
  127. self.assertNotIn(b"no-done", caps)
  128. class SmartWebSideBand64kNoDoneTestCase(SmartWebTestCase):
  129. """Test cases for smart HTTP server with side-band-64k and no-done
  130. support.
  131. """
  132. # no-done was introduced in git 1.7.4
  133. min_git_version = (1, 7, 4)
  134. def _handlers(self) -> None:
  135. return None # default handlers include side-band-64k
  136. def _check_app(self, app) -> None:
  137. from dulwich.protocol import Protocol
  138. receive_pack_handler_cls = app.handlers[b"git-receive-pack"]
  139. # Create a handler instance to check capabilities
  140. handler = receive_pack_handler_cls(
  141. app.backend,
  142. [b"/"],
  143. Protocol(lambda x: b"", lambda x: None),
  144. )
  145. caps = handler.capabilities()
  146. self.assertIn(b"side-band-64k", caps)
  147. self.assertIn(b"no-done", caps)
  148. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  149. class DumbWebTestCase(WebTests, CompatTestCase):
  150. """Test cases for dumb HTTP server."""
  151. def _make_app(self, backend):
  152. return make_wsgi_chain(backend, dumb=True)
  153. def test_push_to_dulwich(self) -> NoReturn:
  154. # Note: remove this if dulwich implements dumb web pushing.
  155. raise SkipTest("Dumb web pushing not supported.")
  156. def test_push_to_dulwich_remove_branch(self) -> NoReturn:
  157. # Note: remove this if dumb pushing is supported
  158. raise SkipTest("Dumb web pushing not supported.")
  159. def test_new_shallow_clone_from_dulwich(self) -> NoReturn:
  160. # Note: remove this if C git and dulwich implement dumb web shallow
  161. # clones.
  162. raise SkipTest("Dumb web shallow cloning not supported.")
  163. def test_shallow_clone_from_git_is_identical(self) -> NoReturn:
  164. # Note: remove this if C git and dulwich implement dumb web shallow
  165. # clones.
  166. raise SkipTest("Dumb web shallow cloning not supported.")
  167. def test_fetch_same_depth_into_shallow_clone_from_dulwich(self) -> NoReturn:
  168. # Note: remove this if C git and dulwich implement dumb web shallow
  169. # clones.
  170. raise SkipTest("Dumb web shallow cloning not supported.")
  171. def test_fetch_full_depth_into_shallow_clone_from_dulwich(self) -> NoReturn:
  172. # Note: remove this if C git and dulwich implement dumb web shallow
  173. # clones.
  174. raise SkipTest("Dumb web shallow cloning not supported.")
  175. def test_push_to_dulwich_issue_88_standard(self) -> NoReturn:
  176. raise SkipTest("Dumb web pushing not supported.")