From d3eeaeb94ced98b6ce9de5354267a43cedcd42e6 Mon Sep 17 00:00:00 2001 From: Folkert Date: Tue, 25 Jun 2024 15:06:05 +0200 Subject: [PATCH] nested test case in store.rs Signed-off-by: Folkert --- tsp-python/src/lib.rs | 27 ++++++ tsp-python/test.py | 69 +++++++++++++++ tsp-python/tsp.py | 6 ++ tsp/src/store.rs | 194 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 296 insertions(+) diff --git a/tsp-python/src/lib.rs b/tsp-python/src/lib.rs index 23d1e83..2fe8b12 100644 --- a/tsp-python/src/lib.rs +++ b/tsp-python/src/lib.rs @@ -126,6 +126,33 @@ impl Store { Ok((url.to_string(), bytes)) } + fn make_nested_relationship_request( + &self, + parent_sender: String, + receiver: String, + ) -> PyResult<((String, Vec), OwnedVid)> { + let ((url, bytes), vid) = self + .0 + .make_nested_relationship_request(&parent_sender, &receiver) + .map_err(py_exception)?; + + Ok(((url.to_string(), bytes), OwnedVid(vid))) + } + + fn make_nested_relationship_accept( + &self, + sender: String, + receiver: String, + thread_id: [u8; 32], + ) -> PyResult<((String, Vec), OwnedVid)> { + let ((url, bytes), vid) = self + .0 + .make_nested_relationship_accept(&sender, &receiver, thread_id) + .map_err(py_exception)?; + + Ok(((url.to_string(), bytes), OwnedVid(vid))) + } + fn forward_routed_message( &self, next_hop: String, diff --git a/tsp-python/test.py b/tsp-python/test.py index b8d30d2..edef7d5 100644 --- a/tsp-python/test.py +++ b/tsp-python/test.py @@ -191,7 +191,76 @@ def test_routed(self): case other: self.fail(f"unexpected message type {other}") + def test_nested_automatic(self): + a_store = Store() + b_store = Store() + + a = new_vid() + b = new_vid() + a_store.add_private_vid(a) + b_store.add_private_vid(b) + + a_store.add_verified_vid(b) + b_store.add_verified_vid(a) + + url, sealed = a_store.make_relationship_request(a.identifier(), b.identifier(), None) + self.assertEqual(url, "tcp://127.0.0.1:1337") + + received = b_store.open_message(sealed) + match received: + case RequestRelationship(sender, _route, _nested_vid, thread_id): + self.assertEqual(sender, a.identifier()) + + case other: + self.fail(f"unexpected message type {other}") + + url, sealed = b_store.make_relationship_accept(b.identifier(), a.identifier(), thread_id, None) + self.assertEqual(url, "tcp://127.0.0.1:1337") + + received = a_store.open_message(sealed) + match received: + case AcceptRelationship(sender, _nested_vid): + self.assertEqual(sender, b.identifier()) + + case other: + self.fail(f"unexpected message type {other}") + + (_url, sealed), nested_a = a_store.make_nested_relationship_request(a.identifier(), b.identifier()) + + match b_store.open_message(sealed): + case RequestRelationship(sender, _route, nested_vid_1, thread_id): + self.assertEqual(sender, a.identifier()) + + case other: + self.fail(f"unexpected message type {other}") + + (_url, sealed), nested_b = b_store.make_nested_relationship_accept(b.identifier(), nested_vid_1, thread_id) + + match a_store.open_message(sealed): + case AcceptRelationship(sender, nested_vid_2): + self.assertEqual(sender, b.identifier()) + + case other: + self.fail(f"unexpected message type {other}") + + self.assertEqual(nested_a.identifier(), nested_vid_1); + self.assertEqual(nested_b.identifier(), nested_vid_2); + + hello_world = b"hello world"; + + _url, sealed = a_store.seal_message(nested_a.identifier(), nested_b.identifier(), None, hello_world) + + received = b_store.open_message(sealed) + + match received: + case GenericMessage(sender, _, received_message, message_type): + self.assertEqual(sender, nested_a.identifier()) + self.assertEqual(received_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 e26fe3b..ae668f4 100644 --- a/tsp-python/tsp.py +++ b/tsp-python/tsp.py @@ -37,6 +37,12 @@ def make_relationship_accept(self, *args, **kwargs): def make_relationship_cancel(self, *args, **kwargs): return self.inner.make_relationship_cancel(*args, **kwargs) + def make_nested_relationship_request(self, *args, **kwargs): + return self.inner.make_nested_relationship_request(*args, **kwargs) + + def make_nested_relationship_accept(self, *args, **kwargs): + return self.inner.make_nested_relationship_accept(*args, **kwargs) + def forward_routed_message(self, *args, **kwargs): return self.inner.forward_routed_message(*args, **kwargs) diff --git a/tsp/src/store.rs b/tsp/src/store.rs index e25d40d..a921b1a 100644 --- a/tsp/src/store.rs +++ b/tsp/src/store.rs @@ -1285,4 +1285,198 @@ mod test { assert_eq!(message, hello_world); assert_eq!(message_type, MessageType::SignedAndEncrypted); } + + #[test] + #[wasm_bindgen_test] + fn test_nested_manual() { + let a_store = Store::new(); + let b_store = Store::new(); + + let a = new_vid(); + let b = new_vid(); + + let nested_a = new_vid(); + let nested_b = new_vid(); + + a_store.add_private_vid(a.clone()).unwrap(); + a_store.add_private_vid(nested_a.clone()).unwrap(); + + b_store.add_private_vid(b.clone()).unwrap(); + b_store.add_private_vid(nested_b.clone()).unwrap(); + + a_store.add_verified_vid(b.clone()).unwrap(); + a_store.add_verified_vid(nested_b.clone()).unwrap(); + + b_store.add_verified_vid(a.clone()).unwrap(); + b_store.add_verified_vid(nested_a.clone()).unwrap(); + + a_store + .set_parent_for_vid(nested_b.identifier(), Some(b.identifier())) + .unwrap(); + + a_store + .set_relation_for_vid(nested_b.identifier(), Some(nested_a.identifier())) + .unwrap(); + + a_store + .set_parent_for_vid(nested_a.identifier(), Some(a.identifier())) + .unwrap(); + + let hello_world = b"hello world"; + + let (_url, sealed) = a_store + .seal_message( + nested_a.identifier(), + nested_b.identifier(), + None, + hello_world, + ) + .unwrap(); + + let received = b_store.open_message(&mut sealed.clone()).unwrap(); + + let ReceivedTspMessage::GenericMessage { + sender, + nonconfidential_data, + message, + message_type, + } = received + else { + panic!() + }; + + assert_eq!(sender, nested_a.identifier()); + assert!(nonconfidential_data.is_none()); + assert_eq!(message, hello_world); + assert_eq!(message_type, MessageType::SignedAndEncrypted); + } + + #[test] + #[wasm_bindgen_test] + fn test_nested_automatic_setup() { + let a_store = Store::new(); + let b_store = Store::new(); + + let a = new_vid(); + let b = new_vid(); + + a_store.add_private_vid(a.clone()).unwrap(); + b_store.add_private_vid(b.clone()).unwrap(); + + a_store.add_verified_vid(b.clone()).unwrap(); + b_store.add_verified_vid(a.clone()).unwrap(); + + let (_url, mut sealed) = a_store + .make_relationship_request(a.identifier(), b.identifier(), None) + .unwrap(); + + let received = b_store.open_message(&mut sealed).unwrap(); + + let ReceivedTspMessage::RequestRelationship { + sender: _, + route: _, + nested_vid: None, + thread_id, + } = received + else { + panic!() + }; + + let (_url, mut sealed) = b_store + .make_relationship_accept(b.identifier(), a.identifier(), thread_id, None) + .unwrap(); + + let received = a_store.open_message(&mut sealed).unwrap(); + + let ReceivedTspMessage::AcceptRelationship { .. } = received else { + panic!() + }; + + let ((_url, mut sealed), nested_a) = a_store + .make_nested_relationship_request(a.identifier(), b.identifier()) + .unwrap(); + + let received = b_store.open_message(&mut sealed).unwrap(); + + let ReceivedTspMessage::RequestRelationship { + sender: _, + route: _, + nested_vid: Some(ref nested_vid_1), + thread_id, + } = received + else { + panic!() + }; + + let ((_url, mut sealed), nested_b) = b_store + .make_nested_relationship_accept(b.identifier(), nested_vid_1, thread_id) + .unwrap(); + + let received = a_store.open_message(&mut sealed).unwrap(); + + let ReceivedTspMessage::AcceptRelationship { + sender: _, + nested_vid: Some(ref nested_vid_2), + } = received + else { + panic!() + }; + + assert_eq!(nested_a.identifier(), nested_vid_1); + assert_eq!(nested_b.identifier(), nested_vid_2); + + assert_eq!( + a_store + .get_vid(nested_a.identifier()) + .unwrap() + .get_parent_vid(), + Some(a.identifier()) + ); + + assert_eq!( + b_store + .get_vid(nested_b.identifier()) + .unwrap() + .get_parent_vid(), + Some(b.identifier()) + ); + + assert_eq!( + b_store.get_vid(nested_vid_1).unwrap().get_parent_vid(), + Some(a.identifier()) + ); + + assert_eq!( + a_store.get_vid(nested_vid_2).unwrap().get_parent_vid(), + Some(b.identifier()) + ); + + let hello_world = b"hello world"; + + let (_url, sealed) = a_store + .seal_message( + nested_a.identifier(), + nested_b.identifier(), + None, + hello_world, + ) + .unwrap(); + + let received = b_store.open_message(&mut sealed.clone()).unwrap(); + + let ReceivedTspMessage::GenericMessage { + sender, + nonconfidential_data, + message, + message_type, + } = received + else { + panic!() + }; + + assert_eq!(sender, nested_a.identifier()); + assert!(nonconfidential_data.is_none()); + assert_eq!(message, hello_world); + assert_eq!(message_type, MessageType::SignedAndEncrypted); + } }