Browse Source

Tests, Tunnel methods, Transport.receive

master
ennucore 5 years ago
parent
commit
7d6bb9ebf9
  1. 1
      src/ironforce.rs
  2. 57
      src/lib.rs
  3. 2
      src/message.rs
  4. 33
      src/transport.rs
  5. 13
      src/tunnel.rs
  6. 3
      src/way.rs

1
src/ironforce.rs

@ -10,7 +10,6 @@ pub struct IronForce {
key_pack: Keys, key_pack: Keys,
} }
#[allow(dead_code)]
impl IronForce { impl IronForce {
pub fn new() -> IronForce { pub fn new() -> IronForce {
IronForce { transport: Transport::new(), key_pack: Keys::gen() } IronForce { transport: Transport::new(), key_pack: Keys::gen() }

57
src/lib.rs

@ -1,4 +1,5 @@
#![no_std] #![no_std]
#![allow(dead_code)]
extern crate alloc; extern crate alloc;
#[cfg(feature = "std")] #[cfg(feature = "std")]
@ -17,7 +18,7 @@ mod tunnel;
mod tests { mod tests {
mod iron_force { mod iron_force {
#[test] #[test]
fn creation_works() { fn creation() {
use crate::ironforce::IronForce; use crate::ironforce::IronForce;
IronForce::new(); IronForce::new();
} }
@ -48,7 +49,7 @@ mod tests {
} }
#[test] #[test]
fn test_invalid_sign() { fn invalid_sign() {
let key_pack = Keys::gen(); let key_pack = Keys::gen();
let mut msg = Message::new( let mut msg = Message::new(
MsgType::MultiCast, Vec::<u8>::new(), &key_pack, MsgType::MultiCast, Vec::<u8>::new(), &key_pack,
@ -58,7 +59,7 @@ mod tests {
msg.hash = [0; 64].to_vec(); msg.hash = [0; 64].to_vec();
msg.sign = crate::message::Sign::Signed( msg.sign = crate::message::Sign::Signed(
key_pack.get_public(), key_pack.get_public(),
key_pack.sign(&msg.content_hash) key_pack.sign(&msg.content_hash),
); );
assert!(!&msg.verify()); assert!(!&msg.verify());
} }
@ -71,4 +72,54 @@ mod tests {
msg.get_hash(); 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())
));
}
}
} }

2
src/message.rs

@ -52,7 +52,6 @@ impl MessageContent {
to_vec(&self).expect("Message content serialization failed") to_vec(&self).expect("Message content serialization failed")
} }
#[allow(dead_code)]
pub fn deserialize(serialized: Vec<u8>) -> Self { pub fn deserialize(serialized: Vec<u8>) -> Self {
from_bytes(&serialized).expect("Message content deserialization failed") from_bytes(&serialized).expect("Message content deserialization failed")
} }
@ -102,7 +101,6 @@ impl Message {
to_vec(&self).expect("Message serialization failed") to_vec(&self).expect("Message serialization failed")
} }
#[allow(dead_code)]
pub fn deserialize(serialized: Vec<u8>) -> Self { pub fn deserialize(serialized: Vec<u8>) -> Self {
from_bytes(&serialized).expect("Message deserialization failed") from_bytes(&serialized).expect("Message deserialization failed")
} }

33
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; use crate::crypto::PublicKey;
#[cfg(feature = "std")] use crate::way::Way;
#[cfg(feature = "std")]
use crate::way::Way;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use std::thread; use std::thread;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use alloc::vec::Vec; use alloc::vec::Vec;
#[cfg(feature = "std")] #[cfg(feature = "std")] use alloc::boxed::Box;
use alloc::boxed::Box;
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
pub struct Transport {} pub struct Transport {}
@ -19,7 +17,7 @@ pub struct Transport {}
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub struct Transport { pub struct Transport {
pub ways: Vec<Box<dyn Way + Send + Sync>>, pub ways: Vec<Box<dyn Way + Send + Sync>>,
pub msg_pool: Vec<Message>, pub msg_pool: Vec<(Message, PublicKey)>,
} }
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
@ -32,7 +30,6 @@ impl Transport {
// todo // todo
} }
#[allow(dead_code)]
pub fn receive(&self) -> (Message, PublicKey) { pub fn receive(&self) -> (Message, PublicKey) {
(Message::new(MsgType::Service, (Message::new(MsgType::Service,
Vec::<u8>::new(), Vec::<u8>::new(),
@ -43,7 +40,7 @@ impl Transport {
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl Transport { impl Transport {
pub fn new() -> Self { pub fn new() -> Self {
let transport = Self { ways: Vec::<Box<dyn Way + Send + Sync>>::new(), msg_pool: Vec::<Message>::new() }; let transport = Self { ways: Vec::<Box<dyn Way + Send + Sync>>::new(), msg_pool: Vec::<(Message, PublicKey)>::new() };
transport transport
} }
@ -53,17 +50,21 @@ impl Transport {
} }
} }
pub fn receive(&self) -> (Message, PublicKey) { pub fn receive(&mut self) -> (Message, PublicKey) {
(Message::new(MsgType::MultiCast, if self.msg_pool.len() != 0 {
Vec::<u8>::new(), let msg = self.msg_pool[0].clone();
&crate::crypto::Keys::gen()), PublicKey { key: [1u8; 32] }) 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) { fn way_receiving_loop(&mut self, i: i32) {
let way = &self.ways[i as usize]; let way = &self.ways[i as usize];
loop { loop {
let mut msg = way.receive(); let (msg, key) = way.receive();
self.msg_pool.push(msg); self.msg_pool.push((msg, key));
} }
} }

13
src/tunnel.rs

@ -32,8 +32,8 @@ impl Tunnel {
.find(|&node| node.1 == to) { .find(|&node| node.1 == to) {
Some(en) => { Some(en) => {
match my_index < en.0 { match my_index < en.0 {
true => { Some(Tunnel::from_vec(Vec::<PublicKey>::from(self.nodes.clone()[en.0..].to_vec()))) } true => { Some(Tunnel::from_vec(Vec::<PublicKey>::from(self.nodes.clone()[my_index..=en.0].to_vec()))) }
false => { Some(Tunnel::from_vec(Vec::<PublicKey>::from(self.nodes.clone()[..=en.0].to_vec()))) } false => { Some(Tunnel::from_vec(Vec::<PublicKey>::from(self.nodes.clone()[en.0..=my_index].to_vec()))) }
} }
} }
None => { None => {
@ -42,7 +42,6 @@ impl Tunnel {
} }
} }
#[allow(dead_code)]
pub fn next(&self, me: &PublicKey, to: &PublicKey) -> Option<PublicKey> { pub fn next(&self, me: &PublicKey, to: &PublicKey) -> Option<PublicKey> {
let my_index = self.nodes let my_index = self.nodes
.iter() .iter()
@ -52,9 +51,13 @@ impl Tunnel {
.iter() .iter()
.enumerate() .enumerate()
.find(|&node| node.1 == to).unwrap().0; .find(|&node| node.1 == to).unwrap().0;
if to_index <= my_index { if to_index == my_index {
return None; 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())
};
} }
} }

3
src/way.rs

@ -1,6 +1,7 @@
use crate::message::Message; use crate::message::Message;
use crate::crypto::PublicKey;
pub trait Way { pub trait Way {
fn send(&self, msg: &Message); fn send(&self, msg: &Message);
fn receive(&self) -> Message; fn receive(&self) -> (Message, PublicKey);
} }

Loading…
Cancel
Save