Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable privileged port test #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ impl RpcFactory {

#[derive(Debug)]
enum Error {
Docopt(docopt::Error),
Io(io::Error),
JsonRpc(jsonrpc_core::Error),
Network(net::Error),
Expand All @@ -159,12 +158,6 @@ impl From<net::Error> for Error {
}
}

impl From<docopt::Error> for Error {
fn from(err: docopt::Error) -> Self {
Error::Docopt(err)
}
}

impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::Io(err)
Expand Down Expand Up @@ -193,7 +186,6 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
Error::SockAddr(ref e) => write!(f, "{}", e),
Error::Docopt(ref e) => write!(f, "{}", e),
Error::Io(ref e) => write!(f, "{}", e),
Error::JsonRpc(ref e) => write!(f, "{:?}", e),
Error::Network(ref e) => write!(f, "{}", e),
Expand All @@ -206,27 +198,34 @@ impl fmt::Display for Error {
fn main() {
panic_hook::set_abort();

match execute(env::args()) {
let args = match parse_args(env::args()) {
Ok(args) => args,
Err(e) => e.exit()
};

initialize_logger(&args.flag_log);

match execute(args) {
Ok(_) => {
println!("whisper-cli terminated");
process::exit(1);
},
Err(Error::Docopt(ref e)) => e.exit(),
Err(err) => {
println!("{}", err);
process::exit(1);
}
}
}

fn execute<S, I>(command: I) -> Result<(), Error> where I: IntoIterator<Item=S>, S: AsRef<str> {
fn parse_args<S, I>(command: I) -> Result<Args, docopt::Error> where I: IntoIterator<Item=S>, S: AsRef<str> {
Docopt::new(USAGE)
.and_then(|d| d.argv(command).deserialize())
}

// Parse arguments
let args: Args = Docopt::new(USAGE).and_then(|d| d.argv(command).deserialize())?;
fn execute(args: Args) -> Result<(), Error> {
let pool_size = args.flag_whisper_pool_size * POOL_UNIT;
let rpc_url = format!("{}:{}", args.flag_rpc_address, args.flag_rpc_port);

initialize_logger(args.flag_log);
info!(target: "whisper-cli", "start");

// Filter manager that will dispatch `decryption tasks`
Expand Down Expand Up @@ -290,15 +289,16 @@ fn execute<S, I>(command: I) -> Result<(), Error> where I: IntoIterator<Item=S>,
Ok(())
}

fn initialize_logger(log_level: String) {
fn initialize_logger(log_level: &String) {
env_logger::Builder::from_env(env_logger::Env::default())
.parse(&log_level)
.parse(log_level)
.init();
}

#[cfg(test)]
mod tests {
use super::execute;
use parse_args;

#[test]
fn invalid_argument() {
Expand All @@ -307,46 +307,46 @@ mod tests {
.map(Into::into)
.collect::<Vec<String>>();

assert!(execute(command).is_err());

assert!(parse_args(command).is_err());
}

#[test]
#[ignore]
fn privileged_port() {
let command = vec!["whisper", "--port=3"]
// The Whisper pool size is of type usize. Invalid Whisper pool sizes include
// values below 0 and either above 2 ** 64 - 1 on a 64-bit processor or
// above 2 ** 32 - 1 on a 32-bit processor.
fn invalid_whisper_pool_size() {
let command_pool_size_too_low = vec!["whisper", "--whisper-pool-size=-1"]
.into_iter()
.map(Into::into)
.collect::<Vec<String>>();

assert!(execute(command).is_err());
}

#[test]
fn invalid_ip_address() {
let command = vec!["whisper", "--address=x.x.x.x"]
let command_pool_size_too_high = vec!["whisper", "--whisper-pool-size=18446744073709552000"]
.into_iter()
.map(Into::into)
.collect::<Vec<String>>();

assert!(execute(command).is_err());
assert!(parse_args(command_pool_size_too_low).is_err());
assert!(parse_args(command_pool_size_too_high).is_err());
}

#[test]
// The Whisper pool size is of type usize. Invalid Whisper pool sizes include
// values below 0 and either above 2 ** 64 - 1 on a 64-bit processor or
// above 2 ** 32 - 1 on a 32-bit processor.
fn invalid_whisper_pool_size() {
let command_pool_size_too_low = vec!["whisper", "--whisper-pool-size=-1"]
fn privileged_port() {
let command = vec!["whisper", "--port=3"]
.into_iter()
.map(Into::into)
.collect::<Vec<String>>();

let command_pool_size_too_high = vec!["whisper", "--whisper-pool-size=18446744073709552000"]
assert!(execute(parse_args(command).unwrap()).is_err());
}

#[test]
fn invalid_ip_address() {
let command = vec!["whisper", "--address=x.x.x.x"]
.into_iter()
.map(Into::into)
.collect::<Vec<String>>();

assert!(execute(command_pool_size_too_low).is_err());
assert!(execute(command_pool_size_too_high).is_err());
assert!(execute(parse_args(command).unwrap()).is_err());
}
}