-
Notifications
You must be signed in to change notification settings - Fork 2
chore(xtest): replace cipherTexts caches with session-scoped fixture #442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
046d2ba
chore(xtest): replace cipherTexts caches with session-scoped fixture
dmihalcik-virtru c1dda70
refactor(xtest): convert remaining secpmlkem tests to encrypted_tdf
dmihalcik-virtru 1b1e9d0
fixup
dmihalcik-virtru b299d16
fixup remove overly verbose text
dmihalcik-virtru 1f985f5
fixup ruff format
dmihalcik-virtru 4416611
refactor(xtest): derive rt_file paths from ct_file instead of manual …
dmihalcik-virtru 6871439
fixup remove unused tmp_dir fixture parameters
dmihalcik-virtru dbd484b
fixup more
dmihalcik-virtru 794e5d1
refactor(xtest): address PR review issues
dmihalcik-virtru dd4ff16
fixup ruff format
dmihalcik-virtru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| """Session-scoped factory fixture for memoized TDF encryption. | ||
|
|
||
| The cache key is derived from encryption input parameters; on-disk filenames also embed | ||
| the requesting test's name and a short hash for debuggability. | ||
| """ | ||
|
|
||
| import hashlib | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| import tdfs | ||
|
|
||
|
|
||
| class EncryptFactory: | ||
| """Memoized TDF encryption factory bound to the current test. | ||
|
|
||
| Call to encrypt (results are cached by input parameters). Use rt_file() to | ||
| generate a test-unique decrypted output path derived from the ciphertext. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| label: str, | ||
| pt_file: Path, | ||
| tmp_dir: Path, | ||
| cache: dict[tuple, Path], | ||
| ) -> None: | ||
| self._label = label | ||
| self._pt_file = pt_file | ||
| self._tmp_dir = tmp_dir | ||
| self._cache = cache | ||
|
|
||
| def __call__( | ||
| self, | ||
| encrypt_sdk: tdfs.SDK, | ||
| *, | ||
| container: tdfs.container_type = "ztdf", | ||
| attr_values: list[str] | None = None, | ||
| target_mode: tdfs.container_version | None = None, | ||
| az: str = "", | ||
| mime_type: str = "text/plain", | ||
| ) -> Path: | ||
| attr_key = tuple(attr_values) if attr_values is not None else None | ||
| key = (str(encrypt_sdk), container, target_mode, attr_key, az, mime_type) | ||
| cached = self._cache.get(key) | ||
| if cached is not None: | ||
| return cached | ||
| digest = hashlib.sha1(repr(key).encode()).hexdigest()[:8] | ||
| ct_file = ( | ||
| self._tmp_dir / f"ct-{self._label}-{encrypt_sdk}-{container}-{digest}.tdf" | ||
| ) | ||
| encrypt_sdk.encrypt( | ||
| self._pt_file, | ||
| ct_file, | ||
| mime_type=mime_type, | ||
| container=container, | ||
| attr_values=attr_values, | ||
| assert_value=az, | ||
| target_mode=target_mode, | ||
| ) | ||
| assert ct_file.is_file() | ||
| self._cache[key] = ct_file | ||
| return ct_file | ||
|
|
||
| def rt_file(self, ct_file: Path, decrypt_sdk: tdfs.SDK, variant: str = "") -> Path: | ||
| """Return a test-unique path for the decrypted output. | ||
|
|
||
| Embeds the current test label so tests that share a cached ciphertext | ||
| don't collide on their output files. | ||
| """ | ||
| variant_part = f"-{variant}" if variant else "" | ||
| return ct_file.with_name( | ||
| f"{ct_file.stem}-{decrypt_sdk}-{self._label}{variant_part}.untdf" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def _encryption_cache() -> dict[tuple, Path]: | ||
| """Session-wide cache mapping input-tuple keys to encrypted Paths.""" | ||
| return {} | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def encrypted_tdf( | ||
| request: pytest.FixtureRequest, | ||
| pt_file: Path, | ||
| tmp_dir: Path, | ||
| _encryption_cache: dict[tuple, Path], | ||
| ) -> EncryptFactory: | ||
| """Return a memoized encrypt-to-TDF factory for the current test. | ||
|
|
||
| Two callers with identical inputs share a ciphertext; differing inputs | ||
| produce distinct ciphertexts. Use rt_file() to get a test-unique output path. | ||
| """ | ||
| label = request.node.originalname or request.node.name | ||
| return EncryptFactory(label, pt_file, tmp_dir, _encryption_cache) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.