45 lines
1.9 KiB
Python
45 lines
1.9 KiB
Python
"""Nonempty native recovery receipts must retain their incarnation and floor."""
|
|
import copy
|
|
from pathlib import Path
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
from c01_provision import validate_anchor
|
|
|
|
|
|
class RecoveryAnchorTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.binding = {'incarnation': [17] * 32}
|
|
self.anchor = dict(incarnation=[17] * 32, revision=41,
|
|
watermark=1793680000, digest=[29] * 32)
|
|
|
|
def test_nonempty_recovery_retains_exact_receipt(self):
|
|
before = copy.deepcopy(self.anchor)
|
|
validate_anchor(self.anchor, self.binding, initial=False)
|
|
self.assertEqual(before, self.anchor)
|
|
with self.assertRaises(ValueError):
|
|
validate_anchor(self.anchor, self.binding, initial=True)
|
|
|
|
def test_new_store_still_requires_initial_floor(self):
|
|
self.anchor.update(revision=1, watermark=0)
|
|
validate_anchor(self.anchor, self.binding, initial=True)
|
|
for field, value in (('revision', 2), ('watermark', 1)):
|
|
changed = dict(self.anchor, **{field: value})
|
|
with self.assertRaises(ValueError):
|
|
validate_anchor(changed, self.binding, initial=True)
|
|
|
|
def test_malformed_or_foreign_receipts_refuse(self):
|
|
mutations = [('incarnation', [18] * 32), ('digest', [0] * 32),
|
|
('digest', [True] * 32), ('revision', 0), ('revision', True),
|
|
('revision', 2**64), ('watermark', -1), ('watermark', 2**64),
|
|
('watermark', False), ('extra', 1)]
|
|
for field, value in mutations:
|
|
with self.subTest(field=field, value=value):
|
|
with self.assertRaises(ValueError):
|
|
validate_anchor(dict(self.anchor, **{field: value}), self.binding, initial=False)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|