Spaces:
Sleeping
Sleeping
File size: 3,582 Bytes
caf8059 | 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 | import hashlib
import io
import unittest
import uuid
from pathlib import Path
from src.adapters.artifact_store.resources import ResourceClient, ResourceError, NoRedirect
class Response(io.BytesIO):
def __init__(self, value):
super().__init__(value)
self.headers = {"Content-Length": str(len(value))}
class Opener:
def __init__(self, data=b"verified artifact", failure=False):
self.data, self.failure, self.calls = data, failure, 0
def open(self, request, timeout):
self.calls += 1
if self.failure:
raise OSError("test-only network failure")
return Response(self.data)
class ResourceTests(unittest.TestCase):
def setUp(self):
self.root = Path(__file__).resolve().parents[3] / ".local/test-runs/resources" / uuid.uuid4().hex
self.opener = Opener()
self.client = ResourceClient(self.root, opener=self.opener)
def test_usage_counts_bytes_and_cache_without_retransmission(self):
first = self.client.fetch("maven-central", "example/a/1/a-1.pom", logical_ref="example:a:1")
self.client.fetch("maven-central", "example/a/1/a-1.pom", logical_ref="example:a:1")
summary = self.client.summary()[0]
self.assertEqual(summary["request_count"], 2)
self.assertEqual(summary["cache_hits"], 1)
self.assertEqual(summary["downloads"], 1)
self.assertEqual(summary["transferred_bytes"], len(self.opener.data))
self.assertEqual(first.read_bytes(), self.opener.data)
def test_unapproved_repository_and_paths_denied_before_network(self):
for repository, path in [("unapproved", "file"), ("maven-central", "../secret"), ("maven-central", "a?token=secret"), ("maven-central", "https://evil.test/file"), ("maven-central", "%2e%2e/file")]:
with self.subTest(path=path), self.assertRaises(ResourceError):
self.client.fetch(repository, path, logical_ref="test")
self.assertEqual(self.opener.calls, 0)
def test_size_digest_and_corrupt_cache_rejected(self):
with self.assertRaises(ResourceError):
self.client.fetch("maven-central", "large", logical_ref="large", max_bytes=2)
with self.assertRaises(ResourceError):
self.client.fetch("maven-central", "wrong", logical_ref="wrong", expected_sha256="0" * 64)
path = self.client.fetch("maven-central", "ok", logical_ref="ok")
path.write_bytes(b"tampered")
with self.assertRaises(ResourceError):
self.client.fetch("maven-central", "ok", logical_ref="ok", expected_sha256=hashlib.sha256(self.opener.data).hexdigest())
def test_intranet_endpoint_replaces_logical_repository_only(self):
client = ResourceClient(self.root / "intranet", {"maven-central": "https://maven.intranet.example/repository/approved/"}, self.opener)
self.assertEqual(client.resolve("maven-central", "g/a/1/a-1.jar"), "https://maven.intranet.example/repository/approved/g/a/1/a-1.jar")
self.assertIsNone(NoRedirect().redirect_request(None, None, 302, "", {}, "https://elsewhere.invalid"))
def test_fetch_failure_is_audited_without_secret_exception_text(self):
client = ResourceClient(self.root / "failure", opener=Opener(failure=True))
with self.assertRaises(ResourceError) as exc:
client.fetch("maven-central", "missing", logical_ref="missing")
self.assertEqual(str(exc.exception), "RESOURCE_FETCH_FAILED")
self.assertEqual(client.summary()[0]["transferred_bytes"], 0)
if __name__ == "__main__":
unittest.main()
|