Skip to content

Commit

Permalink
Merge pull request #30 from folkertdev/nested-test-case
Browse files Browse the repository at this point in the history
nested test case in store.rs
  • Loading branch information
tweedegolf-marc authored Jul 1, 2024
2 parents 90f6aa8 + d3eeaeb commit 3e46aa6
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tsp-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>), 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<u8>), 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,
Expand Down
69 changes: 69 additions & 0 deletions tsp-python/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
6 changes: 6 additions & 0 deletions tsp-python/tsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
194 changes: 194 additions & 0 deletions tsp/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 3e46aa6

Please sign in to comment.