1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
# test_dumb.py -- Tests for dumb HTTP git repositories
# Copyright (C) 2025 Dulwich contributors
#
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
# General Public License as published by the Free Software Foundation; version 2.0
# or (at your option) any later version. You can redistribute it and/or
# modify it under the terms of either of these two licenses.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# You should have received a copy of the licenses; if not, see
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
# License, Version 2.0.
#
"""Tests for dumb HTTP git repositories."""
import zlib
from collections.abc import Callable, Mapping
from unittest import TestCase
from unittest.mock import Mock
from dulwich.dumb import DumbHTTPObjectStore, DumbRemoteHTTPRepo
from dulwich.errors import NotGitRepository
from dulwich.objects import ZERO_SHA, Blob, Commit, ShaFile, Tag, Tree, sha_to_hex
class MockResponse:
def __init__(
self,
status: int = 200,
content: bytes = b"",
headers: dict[str, str] | None = None,
) -> None:
self.status = status
self.content = content
self.headers = headers or {}
self.closed = False
def close(self) -> None:
self.closed = True
class DumbHTTPObjectStoreTests(TestCase):
"""Tests for DumbHTTPObjectStore."""
def setUp(self) -> None:
self.base_url = "https://example.com/repo.git/"
self.responses: dict[str, dict[str, int | bytes]] = {}
self.store = DumbHTTPObjectStore(self.base_url, self._mock_http_request)
def _mock_http_request(
self, url: str, headers: dict[str, str]
) -> tuple[MockResponse, Callable[[int | None], bytes]]:
"""Mock HTTP request function."""
if url in self.responses:
resp_data = self.responses[url]
resp = MockResponse(
int(resp_data.get("status", 200)), bytes(resp_data.get("content", b""))
)
# Create a mock read function that behaves like urllib3's read
content = resp.content
offset = [0] # Use list to make it mutable in closure
def read_func(size: int | None = None) -> bytes:
if offset[0] >= len(content):
return b""
if size is None:
result = content[offset[0] :]
offset[0] = len(content)
else:
result = content[offset[0] : offset[0] + size]
offset[0] += size
return result
return resp, read_func
else:
resp = MockResponse(404)
return resp, lambda size: b""
def _add_response(self, path: str, content: bytes, status: int = 200) -> None:
"""Add a mock response for a given path."""
url = self.base_url + path
self.responses[url] = {"status": status, "content": content}
def _make_object(self, obj: ShaFile) -> bytes:
"""Create compressed git object data."""
type_name = {
Blob.type_num: b"blob",
Tree.type_num: b"tree",
Commit.type_num: b"commit",
Tag.type_num: b"tag",
}[obj.type_num]
content = obj.as_raw_string()
header = type_name + b" " + str(len(content)).encode() + b"\x00"
return zlib.compress(header + content)
def test_fetch_loose_object_blob(self) -> None:
# Create a blob object
blob = Blob()
blob.data = b"Hello, world!"
hex_sha = blob.id
# Add mock response
path = f"objects/{hex_sha[:2].decode('ascii')}/{hex_sha[2:].decode('ascii')}"
self._add_response(path, self._make_object(blob))
# Fetch the object
type_num, content = self.store._fetch_loose_object(blob.id)
self.assertEqual(Blob.type_num, type_num)
self.assertEqual(b"Hello, world!", content)
def test_fetch_loose_object_not_found(self) -> None:
hex_sha = b"1" * 40
self.assertRaises(KeyError, self.store._fetch_loose_object, hex_sha)
def test_fetch_loose_object_invalid_format(self) -> None:
sha = b"1" * 20
hex_sha = sha_to_hex(sha)
path = f"objects/{hex_sha[:2].decode('ascii')}/{hex_sha[2:].decode('ascii')}"
# Add invalid compressed data
self._add_response(path, b"invalid data")
self.assertRaises(Exception, self.store._fetch_loose_object, sha)
def test_load_packs_empty(self) -> None:
# No packs file
self.store._load_packs()
self.assertEqual([], self.store._packs)
def test_load_packs_with_entries(self) -> None:
packs_content = b"""P pack-1234567890abcdef1234567890abcdef12345678.pack
P pack-abcdef1234567890abcdef1234567890abcdef12.pack
"""
self._add_response("objects/info/packs", packs_content)
self.store._load_packs()
assert self.store._packs is not None
self.assertEqual(2, len(self.store._packs))
self.assertEqual(
"pack-1234567890abcdef1234567890abcdef12345678", self.store._packs[0][0]
)
self.assertEqual(
"pack-abcdef1234567890abcdef1234567890abcdef12", self.store._packs[1][0]
)
def test_get_raw_from_cache(self) -> None:
sha = b"1" * 40
self.store._cached_objects[sha] = (Blob.type_num, b"cached content")
type_num, content = self.store.get_raw(sha)
self.assertEqual(Blob.type_num, type_num)
self.assertEqual(b"cached content", content)
def test_contains_loose(self) -> None:
# Create a blob object
blob = Blob()
blob.data = b"Test blob"
hex_sha = blob.id
# Add mock response
path = f"objects/{hex_sha[:2].decode('ascii')}/{hex_sha[2:].decode('ascii')}"
self._add_response(path, self._make_object(blob))
self.assertTrue(self.store.contains_loose(hex_sha))
self.assertFalse(self.store.contains_loose(ZERO_SHA))
def test_add_object_not_implemented(self) -> None:
blob = Blob()
blob.data = b"test"
self.assertRaises(NotImplementedError, self.store.add_object, blob)
def test_add_objects_not_implemented(self) -> None:
self.assertRaises(NotImplementedError, self.store.add_objects, [])
class DumbRemoteHTTPRepoTests(TestCase):
"""Tests for DumbRemoteHTTPRepo."""
def setUp(self) -> None:
self.base_url = "https://example.com/repo.git/"
self.responses: dict[str, dict[str, int | bytes]] = {}
self.repo = DumbRemoteHTTPRepo(self.base_url, self._mock_http_request)
def _mock_http_request(
self, url: str, headers: dict[str, str]
) -> tuple[MockResponse, Callable[[int | None], bytes]]:
"""Mock HTTP request function."""
if url in self.responses:
resp_data = self.responses[url]
resp = MockResponse(
int(resp_data.get("status", 200)), bytes(resp_data.get("content", b""))
)
# Create a mock read function that behaves like urllib3's read
content = resp.content
offset = [0] # Use list to make it mutable in closure
def read_func(size: int | None = None) -> bytes:
if offset[0] >= len(content):
return b""
if size is None:
result = content[offset[0] :]
offset[0] = len(content)
else:
result = content[offset[0] : offset[0] + size]
offset[0] += size
return result
return resp, read_func
else:
resp = MockResponse(404)
return resp, lambda size: b""
def _add_response(self, path: str, content: bytes, status: int = 200) -> None:
"""Add a mock response for a given path."""
url = self.base_url + path
self.responses[url] = {"status": status, "content": content}
def test_get_refs(self) -> None:
refs_content = b"""0123456789abcdef0123456789abcdef01234567\trefs/heads/master
abcdef0123456789abcdef0123456789abcdef01\trefs/heads/develop
fedcba9876543210fedcba9876543210fedcba98\trefs/tags/v1.0
"""
self._add_response("info/refs", refs_content)
refs = self.repo.get_refs()
self.assertEqual(3, len(refs))
self.assertEqual(
b"0123456789abcdef0123456789abcdef01234567",
refs[b"refs/heads/master"],
)
self.assertEqual(
b"abcdef0123456789abcdef0123456789abcdef01",
refs[b"refs/heads/develop"],
)
self.assertEqual(
b"fedcba9876543210fedcba9876543210fedcba98",
refs[b"refs/tags/v1.0"],
)
def test_get_refs_not_found(self) -> None:
self.assertRaises(NotGitRepository, self.repo.get_refs)
def test_get_peeled(self) -> None:
refs_content = b"0123456789abcdef0123456789abcdef01234567\trefs/heads/master\n"
self._add_response("info/refs", refs_content)
# For dumb HTTP, peeled just returns the ref value
peeled = self.repo.get_peeled(b"refs/heads/master")
self.assertEqual(b"0123456789abcdef0123456789abcdef01234567", peeled)
def test_fetch_pack_data_no_wants(self) -> None:
refs_content = b"0123456789abcdef0123456789abcdef01234567\trefs/heads/master\n"
self._add_response("info/refs", refs_content)
graph_walker = Mock()
def determine_wants(
refs: Mapping[bytes, bytes], depth: int | None = None
) -> list[bytes]:
return []
result = list(self.repo.fetch_pack_data(determine_wants, graph_walker))
self.assertEqual([], result)
def test_fetch_pack_data_with_blob(self) -> None:
# Set up refs
refs_content = b"0123456789abcdef0123456789abcdef01234567\trefs/heads/master\n"
self._add_response("info/refs", refs_content)
# Create a simple blob object
blob = Blob()
blob.data = b"Test content"
blob_sha = blob.id
# Add blob response
self.repo.object_store._cached_objects[blob_sha] = (
Blob.type_num,
blob.as_raw_string(),
)
# Mock graph walker
graph_walker = Mock()
graph_walker.ack.return_value = [] # No existing objects
def determine_wants(
refs: Mapping[bytes, bytes], depth: int | None = None
) -> list[bytes]:
return [blob_sha]
def progress(msg: bytes) -> None:
assert isinstance(msg, bytes)
result = list(
self.repo.fetch_pack_data(determine_wants, graph_walker, progress)
)
self.assertEqual(1, len(result))
self.assertEqual(Blob.type_num, result[0].pack_type_num)
self.assertEqual([blob.as_raw_string()], result[0].obj_chunks)
def test_object_store_property(self) -> None:
self.assertIsInstance(self.repo.object_store, DumbHTTPObjectStore)
self.assertEqual(self.base_url, self.repo.object_store.base_url)
def test_fetch_pack_data_missing_head(self) -> None:
refs_content = b"0123456789abcdef0123456789abcdef01234567\trefs/heads/master\n"
self._add_response("info/refs", refs_content)
self._add_response("HEAD", b"", status=404)
assert self.repo.get_head() is None
|