68 lines
3.3 KiB
Python
68 lines
3.3 KiB
Python
"""Origin HMAC continuation tokens allow bounded same-owner replay until revision changes."""
|
|
from pathlib import Path
|
|
import sys
|
|
import unittest
|
|
from unittest.mock import Mock
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
import scenarios
|
|
|
|
|
|
class RawContinuationReplayTests(unittest.TestCase):
|
|
def runner(self, *, changed_repeat=False, error_rows=False, unchanged_origin=False):
|
|
runner = object.__new__(scenarios.ScenarioRunner)
|
|
runner.primary = Mock(side_effect=lambda pair: pair + "-1")
|
|
runner.node_id = Mock(side_effect=lambda node: node + ":4053")
|
|
runner.manifest = {"fixture": {"browse_roots": {"raw": "modbus/plc", "uns": "equipment"}}}
|
|
revision = [0]
|
|
runner.mutate = Mock(side_effect=lambda **_: revision.__setitem__(0, revision[0] + 1))
|
|
runner.save = Mock()
|
|
def check(valid, message):
|
|
if not valid:
|
|
raise scenarios.ScenarioError(message)
|
|
runner.check = Mock(side_effect=check)
|
|
uses, issued = {}, {}
|
|
def serving():
|
|
return {"nodeId": "source-1:4053", "incarnation": "unit", "authorityEpoch": 1,
|
|
"catalogEpoch": 1 if unchanged_origin else revision[0], "servingGeneration": 1}
|
|
def probe(node, operation, realm, **fields):
|
|
if operation == "capabilities":
|
|
return {"serving": serving()}
|
|
if operation == "browse":
|
|
cursor = "cursor-" + str(len(issued))
|
|
issued[cursor] = revision[0]
|
|
return {"complete": False, "continuation": cursor, "items": [{"id": "first"}], "serving": serving()}
|
|
cursor = fields["continuation"]
|
|
uses[cursor] = uses.get(cursor, 0) + 1
|
|
if issued[cursor] == revision[0]:
|
|
self.assertEqual(fields.get("expected_state", "Good"), "Good")
|
|
return {"status": "Good", "items": [{"id": "changed" if changed_repeat and uses[cursor] == 2 else "second"}],
|
|
"serving": serving(), "limits": {"maxPageItems": 1}, "complete": True,
|
|
"operations": "Browse, Metadata, Read", "compact": True, "readiness": "Ready"}
|
|
self.assertEqual(fields["expected_state"], "InvalidContinuation")
|
|
return {"status": "InvalidContinuation", "items": [{"id": "leaked"}] if error_rows else []}
|
|
runner.probe = Mock(side_effect=probe)
|
|
return runner
|
|
|
|
def test_d05_accepts_same_ordered_origin_page_replay_then_requires_invalid_after_revision(self):
|
|
runner = self.runner()
|
|
runner.scenario_D05()
|
|
continuations = [call for call in runner.probe.call_args_list if call.args[1] == "continuation"]
|
|
self.assertEqual(len(continuations), 12)
|
|
|
|
def test_d05_rejects_different_replayed_items(self):
|
|
with self.assertRaises(scenarios.ScenarioError):
|
|
self.runner(changed_repeat=True).scenario_D05()
|
|
|
|
def test_d05_rejects_items_on_invalid_continuation(self):
|
|
with self.assertRaises(scenarios.ScenarioError):
|
|
self.runner(error_rows=True).scenario_D05()
|
|
|
|
def test_d05_requires_an_actual_origin_serving_revision_change(self):
|
|
with self.assertRaises(scenarios.ScenarioError):
|
|
self.runner(unchanged_origin=True).scenario_D05()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|