|
6 | 6 |
|
7 | 7 | from codex32._alignment import _IncrementalSyndromes, _View |
8 | 8 | from codex32.correction import ( |
| 9 | + _ALIGNMENT_CACHE_SIZE, |
9 | 10 | _LONG_SPEC, |
10 | 11 | _SHORT_SPEC, |
11 | 12 | _pack_syndromes, |
|
15 | 16 | ) |
16 | 17 |
|
17 | 18 |
|
| 19 | +def test_syndrome_alignment_cache_is_bounded_for_untrusted_hrps(): |
| 20 | + _syndrome_alignment.cache_clear() |
| 21 | + |
| 22 | + for index in range(_ALIGNMENT_CACHE_SIZE + 5): |
| 23 | + _syndrome_alignment(_SHORT_SPEC, f"attacker{index}", 45) |
| 24 | + |
| 25 | + info = _syndrome_alignment.cache_info() |
| 26 | + assert info.maxsize == _ALIGNMENT_CACHE_SIZE |
| 27 | + assert info.currsize == _ALIGNMENT_CACHE_SIZE |
| 28 | + |
| 29 | + |
| 30 | +def test_syndrome_alignment_cache_fits_one_unknown_length_search(): |
| 31 | + observed = 50 |
| 32 | + targets = sorted({observed + delta for delta in (*range(-4, 5), -8, 8)}) |
| 33 | + _syndrome_alignment.cache_clear() |
| 34 | + |
| 35 | + first = [_syndrome_alignment(_SHORT_SPEC, "generic", target) for target in targets] |
| 36 | + before = _syndrome_alignment.cache_info() |
| 37 | + second = [_syndrome_alignment(_SHORT_SPEC, "generic", target) for target in targets] |
| 38 | + after = _syndrome_alignment.cache_info() |
| 39 | + |
| 40 | + assert len(targets) == _ALIGNMENT_CACHE_SIZE |
| 41 | + assert all(left is right for left, right in zip(first, second, strict=True)) |
| 42 | + assert after.misses == before.misses |
| 43 | + assert after.hits == before.hits + len(targets) |
| 44 | + |
| 45 | + |
| 46 | +def test_syndrome_alignment_cache_still_reuses_recent_entries(): |
| 47 | + _syndrome_alignment.cache_clear() |
| 48 | + first = _syndrome_alignment(_SHORT_SPEC, "ms", 45) |
| 49 | + before = _syndrome_alignment.cache_info() |
| 50 | + |
| 51 | + second = _syndrome_alignment(_SHORT_SPEC, "ms", 45) |
| 52 | + |
| 53 | + after = _syndrome_alignment.cache_info() |
| 54 | + assert first is second |
| 55 | + assert after.hits == before.hits + 1 |
| 56 | + |
| 57 | + |
18 | 58 | @pytest.mark.parametrize( |
19 | 59 | ("hrp", "length", "spec"), |
20 | 60 | ( |
|
0 commit comments