Skip to content

Commit

Permalink
python routed test case
Browse files Browse the repository at this point in the history
Signed-off-by: Folkert <[email protected]>
  • Loading branch information
folkertdev committed Jun 25, 2024
1 parent 34a89a2 commit e97dfde
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 5 deletions.
32 changes: 32 additions & 0 deletions tsp-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> 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<String>) -> 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,
Expand Down Expand Up @@ -109,6 +126,21 @@ impl Store {
Ok((url.to_string(), bytes))
}

fn forward_routed_message(
&self,
next_hop: String,
route: Vec<Vec<u8>>,
opaque_payload: Vec<u8>,
) -> PyResult<(String, Vec<u8>)> {
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<u8>) -> PyResult<FlatReceivedTspMessage> {
self.0
.open_message(&mut message)
Expand Down
93 changes: 92 additions & 1 deletion tsp-python/test.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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()
26 changes: 22 additions & 4 deletions tsp-python/tsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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):
Expand All @@ -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!")
Expand All @@ -58,7 +70,7 @@ class GenericMessage(ReceivedTspMessage):
sender: str
nonconfidential_data: str
message: str
message_type: str
message_type: str

@dataclass
class AcceptRelationship(ReceivedTspMessage):
Expand All @@ -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

0 comments on commit e97dfde

Please sign in to comment.