test sockets, connections setup

This commit is contained in:
Nikolay Volf 2016-02-03 14:04:24 +03:00
parent 206cb6b227
commit e86a680b23
2 changed files with 64 additions and 4 deletions

View File

@ -22,7 +22,7 @@ use tiny_keccak::Keccak;
const ENCRYPTED_HEADER_LEN: usize = 32; const ENCRYPTED_HEADER_LEN: usize = 32;
const RECIEVE_PAYLOAD_TIMEOUT: u64 = 30000; const RECIEVE_PAYLOAD_TIMEOUT: u64 = 30000;
pub trait GenericSocket : Sized + Read + Write { pub trait GenericSocket : Read + Write {
} }
impl GenericSocket for TcpStream { impl GenericSocket for TcpStream {

View File

@ -20,6 +20,61 @@ impl Default for TestProtocol {
} }
} }
struct TestSocket {
read_buffer: Vec<u8>,
write_buffer: Vec<u8>,
cursor: usize,
}
impl TestSocket {
fn new() -> TestSocket {
TestSocket {
read_buffer: vec![],
write_buffer: vec![]
}
}
}
impl Read for TestSocket {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let end_position = cmp::min(self.read_buffer.len(), self.cursor+buf.len());
let len = cmp::max(self.end_position - self.cursor, 0);
match len {
0 => Ok(0),
_ => {
bytes::copy_memory(buf, &mut self.read_buffer[cursor..len]);
self.cursor = self.cursor + buf.len();
Ok(len);
}
}
}
}
impl Write for TestSocket {
fn write(&mut self, buf: &mut [u8]) -> Result<u8> {
self.write_buffer.extend(buf.iter().cloned());
Ok(buf.len());
}
}
impl GenericSocket for TestSocket {}
type TestConnection = GenericConnection<TestSocket>;
impl TestConnection {
pub fn new() -> Connection {
Connection {
token: 999998888usize,
socket: TestSocket::new(),
send_queue: VecDeque::new(),
rec_buf: Bytes::new(),
rec_size: 0,
interest: EventSet::hup() | EventSet::readable(),
stats: Arc::<NetworkStats>::new(),
}
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct TestProtocolMessage { pub struct TestProtocolMessage {
payload: u32, payload: u32,
@ -68,13 +123,13 @@ impl NetworkProtocolHandler<TestProtocolMessage> for TestProtocol {
#[test] #[test]
fn test_net_service() { fn net_service() {
let mut service = NetworkService::<TestProtocolMessage>::start(NetworkConfiguration::new()).expect("Error creating network service"); let mut service = NetworkService::<TestProtocolMessage>::start(NetworkConfiguration::new()).expect("Error creating network service");
service.register_protocol(Arc::new(TestProtocol::default()), "myproto", &[1u8]).unwrap(); service.register_protocol(Arc::new(TestProtocol::default()), "myproto", &[1u8]).unwrap();
} }
#[test] #[test]
fn test_net_connect() { fn net_connect() {
let key1 = KeyPair::create().unwrap(); let key1 = KeyPair::create().unwrap();
let mut config1 = NetworkConfiguration::new_with_port(30344); let mut config1 = NetworkConfiguration::new_with_port(30344);
config1.use_secret = Some(key1.secret().clone()); config1.use_secret = Some(key1.secret().clone());
@ -93,7 +148,7 @@ fn test_net_connect() {
} }
#[test] #[test]
fn test_net_timeout() { fn net_timeout() {
let config = NetworkConfiguration::new_with_port(30346); let config = NetworkConfiguration::new_with_port(30346);
let mut service = NetworkService::<TestProtocolMessage>::start(config).unwrap(); let mut service = NetworkService::<TestProtocolMessage>::start(config).unwrap();
let handler = TestProtocol::register(&mut service); let handler = TestProtocol::register(&mut service);
@ -101,3 +156,8 @@ fn test_net_timeout() {
thread::sleep(Duration::from_millis(50)); thread::sleep(Duration::from_millis(50));
} }
} }
#[test]
fn connection_expect() {
let connection = TestConnection::new();
}