Skip to content

Commit e28bb9b

Browse files
committed
test: Add tests for corrupt FileCache
1 parent a3f6437 commit e28bb9b

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/poetry/utils/cache.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import dataclasses
55
import hashlib
66
import json
7+
import logging
78
import shutil
89
import time
910

@@ -18,6 +19,8 @@
1819
MAX_DATE = 9999999999
1920
T = TypeVar("T")
2021

22+
logger = logging.getLogger(__name__)
23+
2124

2225
def decode(string: bytes, encodings: list[str] | None = None) -> str:
2326
"""
@@ -172,7 +175,14 @@ def _get_payload(self, key: str) -> T | None:
172175
return None
173176

174177
with open(path, "rb") as f:
175-
payload = self._deserialize(f.read())
178+
file_content = f.read()
179+
180+
try:
181+
payload = self._deserialize(file_content)
182+
except (json.JSONDecodeError, ValueError):
183+
self.forget(key)
184+
logger.warning("Corrupt cache file was detected and cleaned up.")
185+
return None
176186

177187
if payload.expired:
178188
self.forget(key)

tests/utils/test_cache.py

+62
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import shutil
4+
35
from typing import TYPE_CHECKING
46
from typing import Any
57
from typing import TypeVar
@@ -192,3 +194,63 @@ def test_cachy_compatibility(
192194

193195
assert cachy_file_cache.get("key3") == test_str
194196
assert cachy_file_cache.get("key4") == test_obj
197+
198+
199+
def test_missing_cache_file(
200+
poetry_file_cache: FileCache, mocker: MockerFixture
201+
) -> None:
202+
poetry_file_cache.put("key1", "value")
203+
204+
key1_path = (
205+
poetry_file_cache.path
206+
/ "81/74/09/96/87/a2/66/21/8174099687a26621f4e2cdd7cc03b3dacedb3fb962255b1aafd033cabe831530" # noqa: E501
207+
)
208+
assert key1_path.exists()
209+
key1_path.unlink() # corrupt cache by removing a key file
210+
211+
assert poetry_file_cache.get("key1") is None
212+
213+
214+
def test_missing_cache_path(
215+
poetry_file_cache: FileCache, mocker: MockerFixture
216+
) -> None:
217+
poetry_file_cache.put("key1", "value")
218+
219+
key1_partial_path = poetry_file_cache.path / "81/74/09/96/87/a2/"
220+
assert key1_partial_path.exists()
221+
shutil.rmtree(
222+
key1_partial_path
223+
) # corrupt cache by removing a subdirectory containting a key file
224+
225+
assert poetry_file_cache.get("key1") is None
226+
227+
228+
@pytest.mark.parametrize(
229+
"corrupt_payload",
230+
[
231+
"", # empty file
232+
b"\x00", # null
233+
"99999999", # truncated file
234+
'999999a999"value"', # corrupt lifetime
235+
b'9999999999"va\xd8\x00"', # invalid unicode
236+
"fil3systemFa!led", # garbage file
237+
],
238+
)
239+
def test_detect_corrupted_cache_key_file(
240+
corrupt_payload: str | bytes, poetry_file_cache: FileCache, mocker: MockerFixture
241+
) -> None:
242+
poetry_file_cache.put("key1", "value")
243+
244+
key1_path = (
245+
poetry_file_cache.path
246+
/ "81/74/09/96/87/a2/66/21/8174099687a26621f4e2cdd7cc03b3dacedb3fb962255b1aafd033cabe831530" # noqa: E501
247+
)
248+
assert key1_path.exists()
249+
250+
# original content: 9999999999"value"
251+
252+
write_modes = {str: "w", bytes: "wb"}
253+
with open(key1_path, write_modes[type(corrupt_payload)]) as f:
254+
f.write(corrupt_payload) # write corrupt data
255+
256+
assert poetry_file_cache.get("key1") is None

0 commit comments

Comments
 (0)