From b535bf390c642ae570a4519433be2c0bb81356ca Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Fri, 19 Jan 2018 14:41:34 +0100 Subject: [PATCH] Moved TestSocket to ethcore-network (#7633) --- Cargo.lock | 1 - devtools/src/lib.rs | 3 -- devtools/src/test_socket.rs | 95 ---------------------------------- util/network/Cargo.toml | 1 - util/network/src/connection.rs | 85 +++++++++++++++++++++++++++--- util/network/src/lib.rs | 3 -- 6 files changed, 77 insertions(+), 111 deletions(-) delete mode 100644 devtools/src/test_socket.rs diff --git a/Cargo.lock b/Cargo.lock index fe59a2605..925107d21 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -629,7 +629,6 @@ dependencies = [ "bytes 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "ethcore-bytes 0.1.0", - "ethcore-devtools 1.9.0", "ethcore-io 1.9.0", "ethcore-logger 1.9.0", "ethcrypto 0.1.0", diff --git a/devtools/src/lib.rs b/devtools/src/lib.rs index ba60223da..77faeaa8f 100644 --- a/devtools/src/lib.rs +++ b/devtools/src/lib.rs @@ -16,12 +16,9 @@ //! dev-tools - extern crate rand; mod random_path; -mod test_socket; pub mod http_client; pub use random_path::*; -pub use test_socket::*; diff --git a/devtools/src/test_socket.rs b/devtools/src/test_socket.rs deleted file mode 100644 index b5b0cefd3..000000000 --- a/devtools/src/test_socket.rs +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright 2015-2017 Parity Technologies (UK) Ltd. -// This file is part of Parity. - -// Parity is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Parity is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Parity. If not, see . - -use std::io::*; -use std::cmp; - -pub struct TestSocket { - pub read_buffer: Vec, - pub write_buffer: Vec, - pub cursor: usize, - pub buf_size: usize, -} - -impl Default for TestSocket { - fn default() -> Self { - TestSocket::new() - } -} - -impl TestSocket { - pub fn new() -> Self { - TestSocket { - read_buffer: vec![], - write_buffer: vec![], - cursor: 0, - buf_size: 0, - } - } - - pub fn new_buf(buf_size: usize) -> TestSocket { - TestSocket { - read_buffer: vec![], - write_buffer: vec![], - cursor: 0, - buf_size: buf_size, - } - } - - pub fn new_ready(data: Vec) -> TestSocket { - TestSocket { - read_buffer: data, - write_buffer: vec![], - cursor: 0, - buf_size: 0, - } - } -} - -impl Read for TestSocket { - fn read(&mut self, buf: &mut [u8]) -> Result { - let end_position = cmp::min(self.read_buffer.len(), self.cursor+buf.len()); - if self.cursor > end_position { return Ok(0) } - let len = cmp::max(end_position - self.cursor, 0); - match len { - 0 => Ok(0), - _ => { - for i in self.cursor..end_position { - buf[i-self.cursor] = self.read_buffer[i]; - } - self.cursor = end_position; - Ok(len) - } - } - } -} - -impl Write for TestSocket { - fn write(&mut self, buf: &[u8]) -> Result { - if self.buf_size == 0 || buf.len() < self.buf_size { - self.write_buffer.extend(buf.iter().cloned()); - Ok(buf.len()) - } - else { - self.write_buffer.extend(buf.iter().take(self.buf_size).cloned()); - Ok(self.buf_size) - } - } - - fn flush(&mut self) -> Result<()> { - unimplemented!(); - } -} diff --git a/util/network/Cargo.toml b/util/network/Cargo.toml index e21cafbc7..bd213e3b1 100644 --- a/util/network/Cargo.toml +++ b/util/network/Cargo.toml @@ -35,7 +35,6 @@ serde_json = "1.0" error-chain = { version = "0.11", default-features = false } [dev-dependencies] -ethcore-devtools = { path = "../../devtools" } tempdir = "0.3" [features] diff --git a/util/network/src/connection.rs b/util/network/src/connection.rs index e874db280..974947210 100644 --- a/util/network/src/connection.rs +++ b/util/network/src/connection.rs @@ -504,16 +504,85 @@ pub fn test_encryption() { #[cfg(test)] mod tests { - use super::*; + use std::cmp; + use std::collections::VecDeque; + use std::io::{Read, Write, Cursor, ErrorKind, Result, Error}; use std::sync::Arc; use std::sync::atomic::AtomicBool; - use super::super::stats::*; - use std::io::{Read, Write, Error, Cursor, ErrorKind}; + use mio::{Ready}; - use std::collections::VecDeque; use ethcore_bytes::Bytes; - use devtools::TestSocket; use io::*; + use super::super::stats::*; + use super::*; + + pub struct TestSocket { + pub read_buffer: Vec, + pub write_buffer: Vec, + pub cursor: usize, + pub buf_size: usize, + } + + impl Default for TestSocket { + fn default() -> Self { + TestSocket::new() + } + } + + impl TestSocket { + pub fn new() -> Self { + TestSocket { + read_buffer: vec![], + write_buffer: vec![], + cursor: 0, + buf_size: 0, + } + } + + pub fn new_buf(buf_size: usize) -> TestSocket { + TestSocket { + read_buffer: vec![], + write_buffer: vec![], + cursor: 0, + buf_size: buf_size, + } + } + } + + impl Read for TestSocket { + fn read(&mut self, buf: &mut [u8]) -> Result { + let end_position = cmp::min(self.read_buffer.len(), self.cursor+buf.len()); + if self.cursor > end_position { return Ok(0) } + let len = cmp::max(end_position - self.cursor, 0); + match len { + 0 => Ok(0), + _ => { + for i in self.cursor..end_position { + buf[i-self.cursor] = self.read_buffer[i]; + } + self.cursor = end_position; + Ok(len) + } + } + } + } + + impl Write for TestSocket { + fn write(&mut self, buf: &[u8]) -> Result { + if self.buf_size == 0 || buf.len() < self.buf_size { + self.write_buffer.extend(buf.iter().cloned()); + Ok(buf.len()) + } + else { + self.write_buffer.extend(buf.iter().take(self.buf_size).cloned()); + Ok(self.buf_size) + } + } + + fn flush(&mut self) -> Result<()> { + unimplemented!(); + } + } impl GenericSocket for TestSocket {} @@ -522,17 +591,17 @@ mod tests { } impl Read for TestBrokenSocket { - fn read(&mut self, _: &mut [u8]) -> Result { + fn read(&mut self, _: &mut [u8]) -> Result { Err(Error::new(ErrorKind::Other, self.error.clone())) } } impl Write for TestBrokenSocket { - fn write(&mut self, _: &[u8]) -> Result { + fn write(&mut self, _: &[u8]) -> Result { Err(Error::new(ErrorKind::Other, self.error.clone())) } - fn flush(&mut self) -> Result<(), Error> { + fn flush(&mut self) -> Result<()> { unimplemented!(); } } diff --git a/util/network/src/lib.rs b/util/network/src/lib.rs index 2686b61fc..2c1b4e782 100644 --- a/util/network/src/lib.rs +++ b/util/network/src/lib.rs @@ -89,9 +89,6 @@ extern crate error_chain; #[macro_use] extern crate log; -#[cfg(test)] -extern crate ethcore_devtools as devtools; - #[cfg(test)] extern crate tempdir;