2
0

test_web.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 public 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. receive_pack_handler_cls = app.handlers[b"git-receive-pack"]
  73. caps = receive_pack_handler_cls.capabilities()
  74. self.assertNotIn(b"side-band-64k", caps)
  75. def _make_app(self, backend):
  76. app = make_wsgi_chain(backend, handlers=self._handlers())
  77. to_check = app
  78. # peel back layers until we're at the base application
  79. while not issubclass(to_check.__class__, HTTPGitApplication):
  80. to_check = to_check.app
  81. self._check_app(to_check)
  82. return app
  83. def patch_capabilities(handler, caps_removed):
  84. # Patch a handler's capabilities by specifying a list of them to be
  85. # removed, and return the original classmethod for restoration.
  86. original_capabilities = handler.capabilities
  87. filtered_capabilities = [
  88. i for i in original_capabilities() if i not in caps_removed
  89. ]
  90. def capabilities(cls):
  91. return filtered_capabilities
  92. handler.capabilities = classmethod(capabilities)
  93. return original_capabilities
  94. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  95. class SmartWebSideBand64kTestCase(SmartWebTestCase):
  96. """Test cases for smart HTTP server with side-band-64k support."""
  97. # side-band-64k in git-receive-pack was introduced in git 1.7.0.2
  98. min_git_version = (1, 7, 0, 2)
  99. def setUp(self) -> None:
  100. self.o_uph_cap = patch_capabilities(UploadPackHandler, (b"no-done",))
  101. self.o_rph_cap = patch_capabilities(ReceivePackHandler, (b"no-done",))
  102. super().setUp()
  103. def tearDown(self) -> None:
  104. super().tearDown()
  105. UploadPackHandler.capabilities = self.o_uph_cap
  106. ReceivePackHandler.capabilities = self.o_rph_cap
  107. def _handlers(self) -> None:
  108. return None # default handlers include side-band-64k
  109. def _check_app(self, app) -> None:
  110. receive_pack_handler_cls = app.handlers[b"git-receive-pack"]
  111. caps = receive_pack_handler_cls.capabilities()
  112. self.assertIn(b"side-band-64k", caps)
  113. self.assertNotIn(b"no-done", caps)
  114. class SmartWebSideBand64kNoDoneTestCase(SmartWebTestCase):
  115. """Test cases for smart HTTP server with side-band-64k and no-done
  116. support.
  117. """
  118. # no-done was introduced in git 1.7.4
  119. min_git_version = (1, 7, 4)
  120. def _handlers(self) -> None:
  121. return None # default handlers include side-band-64k
  122. def _check_app(self, app) -> None:
  123. receive_pack_handler_cls = app.handlers[b"git-receive-pack"]
  124. caps = receive_pack_handler_cls.capabilities()
  125. self.assertIn(b"side-band-64k", caps)
  126. self.assertIn(b"no-done", caps)
  127. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  128. class DumbWebTestCase(WebTests, CompatTestCase):
  129. """Test cases for dumb HTTP server."""
  130. def _make_app(self, backend):
  131. return make_wsgi_chain(backend, dumb=True)
  132. def test_push_to_dulwich(self) -> NoReturn:
  133. # Note: remove this if dulwich implements dumb web pushing.
  134. raise SkipTest("Dumb web pushing not supported.")
  135. def test_push_to_dulwich_remove_branch(self) -> NoReturn:
  136. # Note: remove this if dumb pushing is supported
  137. raise SkipTest("Dumb web pushing not supported.")
  138. def test_new_shallow_clone_from_dulwich(self) -> NoReturn:
  139. # Note: remove this if C git and dulwich implement dumb web shallow
  140. # clones.
  141. raise SkipTest("Dumb web shallow cloning not supported.")
  142. def test_shallow_clone_from_git_is_identical(self) -> NoReturn:
  143. # Note: remove this if C git and dulwich implement dumb web shallow
  144. # clones.
  145. raise SkipTest("Dumb web shallow cloning not supported.")
  146. def test_fetch_same_depth_into_shallow_clone_from_dulwich(self) -> NoReturn:
  147. # Note: remove this if C git and dulwich implement dumb web shallow
  148. # clones.
  149. raise SkipTest("Dumb web shallow cloning not supported.")
  150. def test_fetch_full_depth_into_shallow_clone_from_dulwich(self) -> NoReturn:
  151. # Note: remove this if C git and dulwich implement dumb web shallow
  152. # clones.
  153. raise SkipTest("Dumb web shallow cloning not supported.")
  154. def test_push_to_dulwich_issue_88_standard(self) -> NoReturn:
  155. raise SkipTest("Dumb web pushing not supported.")