From f4eba5f8fcc684c79e04c3e5f8d1b7fcaa74b4cb Mon Sep 17 00:00:00 2001 From: Marc Schoolderman Date: Mon, 29 Jul 2024 16:58:30 +0200 Subject: [PATCH] add error case and a full-up nested relation test Signed-off-by: Marc Schoolderman --- examples/cli-test-relations.sh | 55 ++++++++++++++++++++++++++++++++++ examples/src/cli.rs | 39 ++++++++++++++---------- tsp/src/async_store.rs | 6 ++-- tsp/src/store.rs | 4 +++ 4 files changed, 85 insertions(+), 19 deletions(-) create mode 100755 examples/cli-test-relations.sh diff --git a/examples/cli-test-relations.sh b/examples/cli-test-relations.sh new file mode 100755 index 0000000..ed7474b --- /dev/null +++ b/examples/cli-test-relations.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# install using +cargo install --path . + +echo "---- cleanup the database" +rm -f marlon.sqlite marc.sqlite + +echo "---- create a new sender identity" +tsp --database marlon create --alias marlon marlon + +echo "---- create a new receiver identity" +tsp --database marc create --alias marc marc + +echo "---- verify the address of the receiver" +tsp --database marlon verify --alias marc did:web:tsp-test.org:user:marc + +echo "---- establish an outer relation: send and receive an initial hello" +sleep 2 && tsp --database marlon request -s marlon -r marc & + +received=$(tsp --yes --database marc receive --one marc) +vid=$(echo "$received" | cut -f1) +thread_id=$(echo "$received" | cut -f2) + +tsp --database marc set-alias marlon "$vid" + +echo "---- confirm the outer relation: send and process the reply" +sleep 2 && tsp --database marc accept -s marc -r marlon --thread-id "$thread_id" & +tsp --database marlon receive --one marlon + +echo "---- send and process a direct message" +sleep 2 && echo -n "Oh hello Marc" | tsp --database marlon send -s marlon -r marc & + +[ "$(tsp --database marc receive --one marc)" == "Oh hello Marc" ] || exit 5 + +echo "---- establish a nested relationship: send and receive nested hello" +sleep 2 && tsp --database marc request -s marc -r marlon --nested > /tmp/vid & + +received=$(tsp --database marlon receive --one marlon) +nested_marc=$(echo "$received" | cut -f1) +thread_id=$(echo "$received" | cut -f2) + +[ "$nested_marc" == `cat /tmp/vid` ] || exit 5 + +echo "---- confirm a nested relationship: send and receive nested reply" + +sleep 2 && tsp --database marlon accept -s marlon -r "$nested_marc" --nested --thread-id "$thread_id" & +nested_marlon=$(tsp --database marc receive --one marc) + +echo "---- send and process a nested message" +sleep 2 && echo "Oh hello Nested Marc" | tsp --database marlon send -s "$nested_marlon" -r "$nested_marc" & +[ "$(tsp --database marc receive --one marc)" == "Oh hello Nested Marc" ] || exit 5 + +echo "---- cleanup databases" +rm -f marc.sqlite marlon.sqlite /tmp/vid diff --git a/examples/src/cli.rs b/examples/src/cli.rs index e1b6c0e..0d778d2 100644 --- a/examples/src/cli.rs +++ b/examples/src/cli.rs @@ -235,6 +235,20 @@ fn print_message(message: &[u8]) { println!(); } +fn prompt(message: String) -> bool { + use std::io::{self, BufRead, Write}; + print!("{message}? [y/n] "); + io::stdout().flush().expect("I/O error"); + let mut line = String::new(); + io::stdin() + .lock() + .read_line(&mut line) + .expect("could not read reply"); + line = line.to_uppercase(); + + matches!(line.trim(), "Y" | "YES") +} + async fn run() -> Result<(), Error> { let args = Cli::parse(); @@ -466,6 +480,7 @@ async fn run() -> Result<(), Error> { nested_vid: Some(vid), } => { info!("received accept nested relationship from '{vid}' (new identity for {sender})"); + println!("{vid}"); } ReceivedTspMessage::CancelRelationship { sender } => { info!("received cancel relationship from {sender}"); @@ -474,6 +489,9 @@ async fn run() -> Result<(), Error> { sender, next_hop, .. } => { info!("messaging forwarding request from {sender} to {next_hop}",); + if args.yes || prompt(format!("do you want to forward this message?")) { + todo!() + } } ReceivedTspMessage::NewIdentifier { sender, new_vid } => { info!("received request for new identifier '{new_vid}' from {sender}"); @@ -494,24 +512,12 @@ async fn run() -> Result<(), Error> { unknown_vid, payload, } => { - use std::io::{self, BufRead, Write}; info!("message involving unknown party {}", unknown_vid); - let user_affirms = args.yes || { - print!( - "do you want to read a message from '{}' [y/n]? ", - unknown_vid - ); - io::stdout().flush().expect("I/O error"); - let mut line = String::new(); - io::stdin() - .lock() - .read_line(&mut line) - .expect("could not read reply"); - line = line.to_uppercase(); - - matches!(line.trim(), "Y" | "YES") - }; + let user_affirms = args.yes + || prompt(format!( + "do you want to read a message from '{unknown_vid}'" + )); if user_affirms { trace!("processing pending message"); @@ -588,6 +594,7 @@ async fn run() -> Result<(), Error> { "sent a nested relationship request to {receiver_vid} with new identity '{}'", vid.identifier() ); + println!("{}", vid.identifier()); } Err(e) => { tracing::error!( diff --git a/tsp/src/async_store.rs b/tsp/src/async_store.rs index f16be9a..cbe5e87 100644 --- a/tsp/src/async_store.rs +++ b/tsp/src/async_store.rs @@ -331,15 +331,15 @@ impl AsyncStore { /// Pass along a in-transit routed TSP `opaque_message` that is not meant for us, given earlier resolved VIDs. /// The message is routed through the route that has been established with `receiver`. - pub async fn forward_routed_message( + pub async fn forward_routed_message>( &self, next_hop: &str, - path: Vec<&[u8]>, + path: Vec, opaque_message: &[u8], ) -> Result { let (transport, message) = self.inner - .forward_routed_message(next_hop, path, opaque_message)?; + .forward_routed_message(next_hop, path.iter().map(|x| x.as_ref()).collect(), opaque_message)?; crate::transport::send_message(&transport, &message).await?; diff --git a/tsp/src/store.rs b/tsp/src/store.rs index 992eba4..826be4f 100644 --- a/tsp/src/store.rs +++ b/tsp/src/store.rs @@ -363,6 +363,10 @@ impl Store { return Err(VidError::ResolveVid("missing parent for inner VID").into()); }; + if parent_sender != sender.identifier() && inner_sender != sender.identifier() { + return Err(VidError::ResolveVid("incorrect sender VID").into()); + } + let inner_sender = self.get_private_vid(inner_sender)?; let inner_message = crate::crypto::sign( &*inner_sender,