X-Git-Url: http://git.euphorik.ch/?p=crypto_lab1.git;a=blobdiff_plain;f=src%2Fend_point.rs;h=2d0a66c9e09815cc76a7fffb2efa176ef43dbab8;hp=199fff7efad7096b5071f9e4876496762b6d4e84;hb=c9318a07ce0ec00f999ff17943b83048d536ecd1;hpb=786dd3ba778ac05917b00e42e8d82929db9fa529 diff --git a/src/end_point.rs b/src/end_point.rs index 199fff7..2d0a66c 100644 --- a/src/end_point.rs +++ b/src/end_point.rs @@ -1,8 +1,10 @@ +use std::io; use std::io::{ MemWriter, Acceptor, Listener, TcpStream, IoResult, IoError, EndOfFile }; use std::io::net::tcp::{ TcpAcceptor, TcpListener }; use packet; use packet::{ Packet, Command, Answer, Error, ReadingResult, PacketType }; +// Default timeout when waiting data on a stream (for instance: 'TcpStream::read'). static DEFAULT_TIMEOUT: Option = Some(500); // [ms]. pub struct Server { @@ -13,7 +15,7 @@ pub struct Client { end_point: EndPoint, } -struct EndPoint { +pub struct EndPoint { socket: TcpStream, current_timestamp: u64 } @@ -30,10 +32,10 @@ impl Server { loop { for stream in acceptor.incoming() { match stream { - Err(_) => return, Ok(stream) => spawn(proc() { - Server::handle_client(EndPoint { socket: stream, current_timestamp: 0 }); - }) + Server::handle_client(EndPoint::new(stream)); + }), + _ => return } } } @@ -104,6 +106,7 @@ impl Client { } /// Send some valid and not-valid packets to the server and check the result. + /// For each test a new client is created. pub fn start_tests(address: &str, port: u16) { let execute = |f: &mut |&mut Client| -> bool| -> bool { match Client::new(address, port) { @@ -118,52 +121,53 @@ impl Client { fn raw_packet(timestamp: u64) -> Vec { let mut m = MemWriter::new(); match (Packet { t: Command(Packet::random_packet_data([42])), timestamp: timestamp }).write(&mut m) { - Err(e) => vec!(), + Err(_) => vec!(), _ => m.unwrap() } } let mut tests = vec!( + // 1) |client: &mut Client| -> bool { - println!("Send a valid packet..."); + println!("Sending a valid packet..."); match client.end_point.send_with_result(Command(Packet::random_packet_data([42]))) { - Ok(Ok(packet@Packet { t: Answer(..), .. })) => true, + Ok(Ok(Packet { t: Answer(..), .. })) => true, other => { println!("Error: {}", other); false } } }, - + // 2) |client: &mut Client| -> bool { - println!("Send a packet with an unknown type..."); + println!("Sending a packet with an unknown type..."); client.end_point.current_timestamp += 1; let mut raw_packet = raw_packet(client.end_point.current_timestamp); raw_packet[2] = 0xEE; // Alter the type. match client.end_point.send_raw_with_result(raw_packet.as_slice()) { - Ok(Err(packet::IOReadError( IoError { kind: TimedOut, .. }))) => true, // OK: the server should not send any packet. + Ok(Err(packet::IOReadError( IoError { kind: io::TimedOut, .. }))) => true, // OK: the server should not send any packet. other => { println!("Error: {}", other); false } } }, - + // 3) |client: &mut Client| -> bool { - println!("Send a packet with an old timestamp..."); + println!("Sending a packet with an old timestamp..."); client.end_point.current_timestamp += 1; let raw_packet = raw_packet(0); match client.end_point.send_raw_with_result(raw_packet.as_slice()) { - Ok(Err(packet::IOReadError( IoError { kind: TimedOut, .. }))) => true, // OK: the server should not send any packet. + Ok(Err(packet::IOReadError( IoError { kind: io::TimedOut, .. }))) => true, // OK: the server should not send any packet. other => { println!("Error: {}", other); false } } }, - + // 4) |client: &mut Client| -> bool { - println!("Send a packet with altered crypted data..."); + println!("Sending a packet with altered crypted data (do not alter the padding)..."); client.end_point.current_timestamp += 1; let mut raw_packet = raw_packet(client.end_point.current_timestamp); raw_packet[11] = 0xDE; @@ -171,49 +175,49 @@ impl Client { raw_packet[13] = 0xBE; raw_packet[14] = 0xEF; match client.end_point.send_raw_with_result(raw_packet.as_slice()) { - Ok(Ok(packet@Packet { t: Error(packet::AuthError), .. })) => true, + Ok(Ok(Packet { t: Error(packet::AuthError), .. })) => true, other => { println!("Error: {}", other); false } } }, - + // 5) |client: &mut Client| -> bool { - println!("Send a packet with too small data..."); - let mut command = Command(Packet::new_packet_data(0, Vec::from_elem(6, 0x00))); + println!("Sending a packet with too small data..."); + let command = Command(Packet::new_packet_data(0, Vec::from_elem(6, 0x00))); match client.end_point.send_with_result(command) { - Ok(Err(packet::IOReadError( IoError { kind: TimedOut, .. }))) => true, // OK: the server should not send any packet. + Ok(Err(packet::IOReadError( IoError { kind: io::TimedOut, .. }))) => true, // OK: the server should not send any packet. other => { println!("Error: {}", other); false } } }, - + // 6) |client: &mut Client| -> bool { - println!("Send a packet with too large data..."); - let mut command = Command(Packet::new_packet_data(0, Vec::from_elem(40, 0x00))); + println!("Sending a packet with too large data..."); + let command = Command(Packet::new_packet_data(0, Vec::from_elem(40, 0x00))); match client.end_point.send_with_result(command) { - Ok(Err(packet::IOReadError( IoError { kind: TimedOut, .. }))) => true, // OK: the server should not send any packet. + Ok(Err(packet::IOReadError( IoError { kind: io::TimedOut, .. }))) => true, // OK: the server should not send any packet. other => { println!("Error: {}", other); false } } }, - + // 7) |client: &mut Client| -> bool { - println!("Send a packet with wrong padding..."); + println!("Sending a packet with wrong padding (all 0)..."); client.end_point.current_timestamp += 1; let mut m = MemWriter::new(); let raw_packet = match (Packet { t: Command(Packet::random_packet_data([42])), timestamp: client.end_point.current_timestamp }).write_with_padding_fun(&mut m, |_, _| -> u8 { 0 }) { - Err(e) => vec!(), + Err(_) => vec!(), _ => m.unwrap() }; match client.end_point.send_raw_with_result(raw_packet.as_slice()) { - Ok(Ok(packet@Packet { t: Error(packet::CryptError), .. })) => true, + Ok(Ok(Packet { t: Error(packet::CryptError), .. })) => true, other => { println!("Error: {}", other); false @@ -242,6 +246,10 @@ impl Client { } impl EndPoint { + pub fn new(socket: TcpStream) -> EndPoint { + EndPoint { socket: socket, current_timestamp: 0 } + } + fn close(&mut self) -> IoResult<()> { try!(self.socket.close_read()); try!(self.socket.close_write());