diff --git a/src/inputs.rs b/src/inputs.rs index be196da..be0b5e5 100755 --- a/src/inputs.rs +++ b/src/inputs.rs @@ -1,4 +1,4 @@ -use std::io; +use std::io::BufRead; #[derive(Debug, PartialEq, Copy, Clone)] pub struct Config { @@ -22,22 +22,33 @@ pub fn validate_inputs(config: &Config) -> Result<(), frost_ed25519::Error> { Ok(()) } -pub fn request_inputs() -> Config { +pub fn request_inputs(input: &mut impl BufRead) -> Result { println!("The minimum number of signers: (2 or more)"); let mut min = String::new(); - io::stdin().read_line(&mut min).expect("Invalid input"); + input.read_line(&mut min).unwrap(); - let min_signers = min.trim().parse::().expect("Invalid input"); + let min_signers = min.trim().parse::(); + match min_signers { + Ok(_) => (), + Err(_) => { + return Err(frost_ed25519::Error::InvalidMinSigners); + } + } println!("The maximum number of signers: "); let mut max = String::new(); - io::stdin().read_line(&mut max).expect("Invalid input"); - let max_signers = max.trim().parse::().expect("Invalid input"); - - Config { - min_signers, - max_signers, + input.read_line(&mut max).unwrap(); + let max_signers = max.trim().parse::(); + match max_signers { + Ok(_) => (), + Err(_) => { + return Err(frost_ed25519::Error::InvalidMaxSigners); + } } + Ok(Config { + min_signers: min_signers.unwrap(), + max_signers: max_signers.unwrap(), + }) } diff --git a/src/main.rs b/src/main.rs index 30c05b1..d739a7e 100755 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,18 @@ use crate::inputs::{request_inputs, validate_inputs}; use crate::trusted_dealer_keygen::trusted_dealer_keygen; fn main() -> io::Result<()> { - let config = request_inputs(); + let mut reader = Box::new(io::stdin().lock()); + let config = request_inputs(&mut reader); + match config { + Ok(_) => (), + Err(e) => { + eprintln!("Error: {}", e); + std::process::exit(exitcode::DATAERR) + } + } + + let config = config.unwrap(); + let mut rng = thread_rng(); let valid = validate_inputs(&config); diff --git a/src/tests/inputs_tests.rs b/src/tests/inputs_tests.rs index 85b58b1..0f647fc 100755 --- a/src/tests/inputs_tests.rs +++ b/src/tests/inputs_tests.rs @@ -1,4 +1,4 @@ -use crate::inputs::{validate_inputs, Config}; +use crate::inputs::{request_inputs, validate_inputs, Config}; #[test] fn check_valid_input_for_signers() { @@ -47,3 +47,31 @@ fn return_error_if_max_participant_is_less_than_2() { assert_eq!(expected, Err(frost_ed25519::Error::InvalidMaxSigners)); } + +#[test] +fn return_config_if_valid_input() { + let mut valid_input = "3\n6\n".as_bytes(); + let config = request_inputs(&mut valid_input).unwrap(); + let expected = Config { + min_signers: 3, + max_signers: 6, + }; + + assert_eq!(expected, config) +} + +#[test] +fn return_error_if_invalid_min_signers_input() { + let mut invalid_input = "hello\n6\n".as_bytes(); + let expected = request_inputs(&mut invalid_input); + + assert_eq!(expected, Err(frost_ed25519::Error::InvalidMinSigners)) +} + +#[test] +fn return_error_if_invalid_max_signers_input() { + let mut invalid_input = "4\nworld\n".as_bytes(); + let expected = request_inputs(&mut invalid_input); + + assert_eq!(expected, Err(frost_ed25519::Error::InvalidMaxSigners)) +}