From e97dfde25038754375d1eaf1674d4396b66a0d85 Mon Sep 17 00:00:00 2001 From: Folkert Date: Tue, 25 Jun 2024 12:06:50 +0200 Subject: [PATCH] python routed test case Signed-off-by: Folkert --- tsp-python/src/lib.rs | 32 +++++++++++++++ tsp-python/test.py | 93 ++++++++++++++++++++++++++++++++++++++++++- tsp-python/tsp.py | 26 ++++++++++-- 3 files changed, 146 insertions(+), 5 deletions(-) diff --git a/tsp-python/src/lib.rs b/tsp-python/src/lib.rs index a8a3531..23d1e83 100644 --- a/tsp-python/src/lib.rs +++ b/tsp-python/src/lib.rs @@ -30,6 +30,23 @@ impl Store { self.0.add_private_vid(vid.0).map_err(py_exception) } + fn add_verified_vid(&self, vid: OwnedVid) -> PyResult<()> { + self.0.add_verified_vid(vid.0).map_err(py_exception) + } + + fn set_relation_for_vid(&self, vid: String, relation_vid: Option) -> PyResult<()> { + self.0 + .set_relation_for_vid(&vid, relation_vid.as_deref()) + .map_err(py_exception) + } + + fn set_route_for_vid(&self, vid: String, route: Vec) -> PyResult<()> { + let borrowed: Vec<_> = route.iter().map(|s| s.as_str()).collect(); + self.0 + .set_route_for_vid(&vid, &borrowed) + .map_err(py_exception) + } + #[pyo3(signature = (sender, receiver, nonconfidential_data, message))] fn seal_message( &self, @@ -109,6 +126,21 @@ impl Store { Ok((url.to_string(), bytes)) } + fn forward_routed_message( + &self, + next_hop: String, + route: Vec>, + opaque_payload: Vec, + ) -> PyResult<(String, Vec)> { + let borrowed_route: Vec<_> = route.iter().map(|v| v.as_slice()).collect(); + let (url, bytes) = self + .0 + .forward_routed_message(&next_hop, borrowed_route, &opaque_payload) + .map_err(py_exception)?; + + Ok((url.to_string(), bytes)) + } + fn open_message(&self, mut message: Vec) -> PyResult { self.0 .open_message(&mut message) diff --git a/tsp-python/test.py b/tsp-python/test.py index b949f94..b8d30d2 100644 --- a/tsp-python/test.py +++ b/tsp-python/test.py @@ -1,5 +1,5 @@ import unittest -from tsp import * +from tsp import * def new_vid(): return OwnedVid.new_did_peer("tcp://127.0.0.1:1337") @@ -102,5 +102,96 @@ def test_make_relationship_cancel(self): case other: self.fail(f"unexpected message type {other}") + def test_routed(self): + a_store = Store() + b_store = Store() + c_store = Store() + d_store = Store() + + nette_a = new_vid() + sneaky_a = new_vid() + + b = new_vid() + + mailbox_c = new_vid() + c = new_vid() + + sneaky_d = new_vid() + nette_d = new_vid() + + a_store.add_private_vid(nette_a) + a_store.add_private_vid(sneaky_a) + b_store.add_private_vid(b) + c_store.add_private_vid(mailbox_c) + c_store.add_private_vid(c) + d_store.add_private_vid(sneaky_d) + d_store.add_private_vid(nette_d) + + a_store.add_verified_vid(b) + a_store.add_verified_vid(sneaky_d) + + b_store.add_verified_vid(nette_a) + b_store.add_verified_vid(c) + + c_store.add_verified_vid(b) + c_store.add_verified_vid(nette_d) + + d_store.add_verified_vid(sneaky_a) + d_store.add_verified_vid(mailbox_c) + + # relations + + a_store.set_relation_for_vid(b.identifier(), nette_a.identifier()) + + a_store.set_relation_for_vid(sneaky_d.identifier(), sneaky_a.identifier()) + + a_store.set_route_for_vid( + sneaky_d.identifier(), + [b.identifier(), c.identifier(), mailbox_c.identifier()], + ) + + b_store.set_relation_for_vid(c.identifier(), b.identifier()) + + c_store.set_relation_for_vid(mailbox_c.identifier(), nette_d.identifier()) + + # that was all the setup, now let's run some things + + hello_world = b"hello world"; + + _, sealed = a_store.seal_message(sneaky_a.identifier(), sneaky_d.identifier(), None, hello_world) + received = b_store.open_message(sealed) + + match received: + case ForwardRequest(sender, next_hop, route, opaque_payload): + self.assertEqual(sender, nette_a.identifier()) + + case other: + self.fail(f"unexpected message type {other}") + + _url, sealed = b_store.forward_routed_message(next_hop, route, opaque_payload) + received = c_store.open_message(sealed) + + match received: + case ForwardRequest(sender, next_hop, route, opaque_payload): + self.assertEqual(sender, b.identifier()) + + case other: + self.fail(f"unexpected message type {other}") + + _url, sealed = c_store.forward_routed_message(next_hop, route, opaque_payload) + received = d_store.open_message(sealed) + + match received: + case GenericMessage(sender, nonconfidential_data, message, message_type): + self.assertEqual(sender, sneaky_a.identifier()) + self.assertEqual(nonconfidential_data, None) + self.assertEqual(message, hello_world) + self.assertEqual(message_type, MessageType.SignedAndEncrypted) + + case other: + self.fail(f"unexpected message type {other}") + + + if __name__ == '__main__': unittest.main() diff --git a/tsp-python/tsp.py b/tsp-python/tsp.py index cd8cfc6..e26fe3b 100644 --- a/tsp-python/tsp.py +++ b/tsp-python/tsp.py @@ -12,6 +12,15 @@ def __init__(self): def add_private_vid(self, *args, **kwargs): return self.inner.add_private_vid(*args, **kwargs) + def add_verified_vid(self, *args, **kwargs): + return self.inner.add_verified_vid(*args, **kwargs) + + def set_relation_for_vid(self, *args, **kwargs): + return self.inner.set_relation_for_vid(*args, **kwargs) + + def set_route_for_vid(self, *args, **kwargs): + return self.inner.set_route_for_vid(*args, **kwargs) + def seal_message(self, *args, **kwargs): return self.inner.seal_message(*args, **kwargs) @@ -21,13 +30,16 @@ def open_message(self, *args, **kwargs): def make_relationship_request(self, *args, **kwargs): return self.inner.make_relationship_request(*args, **kwargs) - + def make_relationship_accept(self, *args, **kwargs): return self.inner.make_relationship_accept(*args, **kwargs) def make_relationship_cancel(self, *args, **kwargs): return self.inner.make_relationship_cancel(*args, **kwargs) + def forward_routed_message(self, *args, **kwargs): + return self.inner.forward_routed_message(*args, **kwargs) + class ReceivedTspMessage: @staticmethod def from_flat(msg: FlatReceivedTspMessage): @@ -45,7 +57,7 @@ def from_flat(msg: FlatReceivedTspMessage): return CancelRelationship(msg.sender) case ReceivedTspMessageVariant.ForwardRequest: - raise ValueError("todo!") + return ForwardRequest(msg.sender, msg.next_hop, msg.route, msg.opaque_payload) case ReceivedTspMessageVariant.PendingMessage: raise ValueError("todo!") @@ -58,7 +70,7 @@ class GenericMessage(ReceivedTspMessage): sender: str nonconfidential_data: str message: str - message_type: str + message_type: str @dataclass class AcceptRelationship(ReceivedTspMessage): @@ -74,5 +86,11 @@ class RequestRelationship(ReceivedTspMessage): sender: str route: str nested_vid: str - thread_id: str + thread_id: str +@dataclass +class ForwardRequest(ReceivedTspMessage): + sender: str + next_hop: str + route: str + opaque_payload: str