From 7d6bb9ebf96fb402531a30f6a76705bc90518357 Mon Sep 17 00:00:00 2001 From: ennucore Date: Sat, 28 Mar 2020 21:08:17 +0300 Subject: [PATCH] Tests, Tunnel methods, Transport.receive --- src/ironforce.rs | 1 - src/lib.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++--- src/message.rs | 2 -- src/transport.rs | 33 ++++++++++++++-------------- src/tunnel.rs | 13 ++++++----- src/way.rs | 3 ++- 6 files changed, 81 insertions(+), 28 deletions(-) diff --git a/src/ironforce.rs b/src/ironforce.rs index 237fa7c..155ccf6 100644 --- a/src/ironforce.rs +++ b/src/ironforce.rs @@ -10,7 +10,6 @@ pub struct IronForce { key_pack: Keys, } -#[allow(dead_code)] impl IronForce { pub fn new() -> IronForce { IronForce { transport: Transport::new(), key_pack: Keys::gen() } diff --git a/src/lib.rs b/src/lib.rs index 1ca6b28..a3b5fa6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![no_std] +#![allow(dead_code)] extern crate alloc; #[cfg(feature = "std")] @@ -17,7 +18,7 @@ mod tunnel; mod tests { mod iron_force { #[test] - fn creation_works() { + fn creation() { use crate::ironforce::IronForce; IronForce::new(); } @@ -48,7 +49,7 @@ mod tests { } #[test] - fn test_invalid_sign() { + fn invalid_sign() { let key_pack = Keys::gen(); let mut msg = Message::new( MsgType::MultiCast, Vec::::new(), &key_pack, @@ -58,7 +59,7 @@ mod tests { msg.hash = [0; 64].to_vec(); msg.sign = crate::message::Sign::Signed( key_pack.get_public(), - key_pack.sign(&msg.content_hash) + key_pack.sign(&msg.content_hash), ); assert!(!&msg.verify()); } @@ -71,4 +72,54 @@ mod tests { msg.get_hash(); } } + + mod tunnel { + use crate::tunnel::Tunnel; + use crate::crypto::PublicKey; + + fn key(i: u8) -> PublicKey { + PublicKey { key: [i; 32] } + } + + fn create_test_tunnel() -> Tunnel { + Tunnel::from_vec([ + key(0), key(1), key(2), key(3), key(4) + ].to_vec()) + } + + #[test] + fn creation() { + create_test_tunnel(); + } + + #[test] + fn test_next() { + let tunnel = create_test_tunnel(); + assert_eq!(tunnel.next(&key(1), &key(3)), Some(key(2))); + assert_eq!(tunnel.next(&key(2), &key(0)), Some(key(1))); + assert_eq!(tunnel.next(&key(2), &key(2)), None); + } + + #[test] + fn is_in_tunnel() { + let tunnel = create_test_tunnel(); + assert!(tunnel.is_in_tunnel(&key(0))); + assert!(tunnel.is_in_tunnel(&key(1))); + assert!(tunnel.is_in_tunnel(&key(4))); + assert!(!tunnel.is_in_tunnel(&key(5))); + } + + #[test] + fn tunnel_to() { + let tunnel = create_test_tunnel(); + let me = &key(2); + assert_eq!(tunnel.tunnel_to(me, &key(5)), None); + assert_eq!(tunnel.tunnel_to(me, &key(3)), Some( + Tunnel::from_vec([key(2), key(3)].to_vec()) + )); + assert_eq!(tunnel.tunnel_to(me, &key(0)), Some( + Tunnel::from_vec([key(0), key(1), key(2)].to_vec()) + )); + } + } } diff --git a/src/message.rs b/src/message.rs index 5349519..01b65c8 100644 --- a/src/message.rs +++ b/src/message.rs @@ -52,7 +52,6 @@ impl MessageContent { to_vec(&self).expect("Message content serialization failed") } - #[allow(dead_code)] pub fn deserialize(serialized: Vec) -> Self { from_bytes(&serialized).expect("Message content deserialization failed") } @@ -102,7 +101,6 @@ impl Message { to_vec(&self).expect("Message serialization failed") } - #[allow(dead_code)] pub fn deserialize(serialized: Vec) -> Self { from_bytes(&serialized).expect("Message deserialization failed") } diff --git a/src/transport.rs b/src/transport.rs index ea06f0a..5e2d999 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -1,17 +1,15 @@ -use crate::message::{Message, MsgType}; +use crate::message::Message; +#[cfg(not(feature = "std"))] use crate::message::MsgType; use crate::crypto::PublicKey; - -#[cfg(feature = "std")] -use crate::way::Way; +#[cfg(feature = "std")] use crate::way::Way; #[cfg(feature = "std")] use std::thread; #[cfg(feature = "std")] use std::sync::{Arc, Mutex}; - use alloc::vec::Vec; -#[cfg(feature = "std")] -use alloc::boxed::Box; +#[cfg(feature = "std")] use alloc::boxed::Box; + #[cfg(not(feature = "std"))] pub struct Transport {} @@ -19,7 +17,7 @@ pub struct Transport {} #[cfg(feature = "std")] pub struct Transport { pub ways: Vec>, - pub msg_pool: Vec, + pub msg_pool: Vec<(Message, PublicKey)>, } #[cfg(not(feature = "std"))] @@ -32,7 +30,6 @@ impl Transport { // todo } - #[allow(dead_code)] pub fn receive(&self) -> (Message, PublicKey) { (Message::new(MsgType::Service, Vec::::new(), @@ -43,7 +40,7 @@ impl Transport { #[cfg(feature = "std")] impl Transport { pub fn new() -> Self { - let transport = Self { ways: Vec::>::new(), msg_pool: Vec::::new() }; + let transport = Self { ways: Vec::>::new(), msg_pool: Vec::<(Message, PublicKey)>::new() }; transport } @@ -53,17 +50,21 @@ impl Transport { } } - pub fn receive(&self) -> (Message, PublicKey) { - (Message::new(MsgType::MultiCast, - Vec::::new(), - &crate::crypto::Keys::gen()), PublicKey { key: [1u8; 32] }) + pub fn receive(&mut self) -> (Message, PublicKey) { + if self.msg_pool.len() != 0 { + let msg = self.msg_pool[0].clone(); + self.msg_pool = self.msg_pool[1..].to_vec(); + return msg; + } + return self.receive(); // recursion: infinite loop + // until we get a message (see exit condition above) } fn way_receiving_loop(&mut self, i: i32) { let way = &self.ways[i as usize]; loop { - let mut msg = way.receive(); - self.msg_pool.push(msg); + let (msg, key) = way.receive(); + self.msg_pool.push((msg, key)); } } diff --git a/src/tunnel.rs b/src/tunnel.rs index 38f750e..63dbf3a 100644 --- a/src/tunnel.rs +++ b/src/tunnel.rs @@ -32,8 +32,8 @@ impl Tunnel { .find(|&node| node.1 == to) { Some(en) => { match my_index < en.0 { - true => { Some(Tunnel::from_vec(Vec::::from(self.nodes.clone()[en.0..].to_vec()))) } - false => { Some(Tunnel::from_vec(Vec::::from(self.nodes.clone()[..=en.0].to_vec()))) } + true => { Some(Tunnel::from_vec(Vec::::from(self.nodes.clone()[my_index..=en.0].to_vec()))) } + false => { Some(Tunnel::from_vec(Vec::::from(self.nodes.clone()[en.0..=my_index].to_vec()))) } } } None => { @@ -42,7 +42,6 @@ impl Tunnel { } } - #[allow(dead_code)] pub fn next(&self, me: &PublicKey, to: &PublicKey) -> Option { let my_index = self.nodes .iter() @@ -52,9 +51,13 @@ impl Tunnel { .iter() .enumerate() .find(|&node| node.1 == to).unwrap().0; - if to_index <= my_index { + if to_index == my_index { return None; } - Option::from(self.nodes[my_index + 1].clone()) + return if to_index < my_index { + Option::from(self.nodes[my_index - 1].clone()) + } else { + Option::from(self.nodes[my_index + 1].clone()) + }; } } diff --git a/src/way.rs b/src/way.rs index 4c5045a..4eb18da 100644 --- a/src/way.rs +++ b/src/way.rs @@ -1,6 +1,7 @@ use crate::message::Message; +use crate::crypto::PublicKey; pub trait Way { fn send(&self, msg: &Message); - fn receive(&self) -> Message; + fn receive(&self) -> (Message, PublicKey); }