Skip to content

Commit

Permalink
add error case and a full-up nested relation test
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Schoolderman <[email protected]>
  • Loading branch information
tweedegolf-marc committed Jul 30, 2024
1 parent 3748337 commit f4eba5f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 19 deletions.
55 changes: 55 additions & 0 deletions examples/cli-test-relations.sh
Original file line number Diff line number Diff line change
@@ -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
39 changes: 23 additions & 16 deletions examples/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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}");
Expand All @@ -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}");
Expand All @@ -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");
Expand Down Expand Up @@ -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!(
Expand Down
6 changes: 3 additions & 3 deletions tsp/src/async_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: AsRef<[u8]>>(
&self,
next_hop: &str,
path: Vec<&[u8]>,
path: Vec<T>,
opaque_message: &[u8],
) -> Result<Url, Error> {
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?;

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

0 comments on commit f4eba5f

Please sign in to comment.