Skip to content

Commit

Permalink
add DKG tests
Browse files Browse the repository at this point in the history
  • Loading branch information
conradoplg committed Jul 17, 2023
1 parent d0cb462 commit 0c327c9
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 269 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dkg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ rand = "0.8"
serde_json = "1.0"
itertools = "0.11.0"
exitcode = "1.1.2"
pipe = "0.4.0"
4 changes: 2 additions & 2 deletions dkg/src/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn request_inputs(
.parse::<u16>()
.map_err(|_| Error::InvalidMinSigners)?;

writeln!(logger, "The maximum number of signers: ")?;
writeln!(logger, "The maximum number of signers:")?;

let mut max = String::new();
input.read_line(&mut max)?;
Expand All @@ -71,7 +71,7 @@ pub fn request_inputs(
let u16_identifier = identifier_input
.trim()
.parse::<u16>()
.map_err(|_| eyre!("Invalid identifier"))?;
.map_err(|_| Error::MalformedIdentifier)?;
let identifier = u16_identifier.try_into()?;

let config = Config {
Expand Down
179 changes: 93 additions & 86 deletions dkg/src/tests/inputs_tests.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,93 @@
// use crate::inputs::{request_inputs, Config};
// use frost::Error;
// use frost_ed25519 as frost;

// #[test]
// fn check_valid_input_for_signers() {
// let config = Config {
// min_signers: 2,
// max_signers: 3,
// secret: Vec::new(),
// };

// let mut valid_input = "2\n3\n\n".as_bytes();
// let expected = request_inputs(&mut valid_input);

// assert_eq!(expected, Ok(config));
// }

// #[test]
// fn return_error_if_min_participant_greater_than_max_participant() {
// let mut invalid_input = "4\n3\n\n".as_bytes();
// let expected = request_inputs(&mut invalid_input);

// assert_eq!(expected, Err(Error::InvalidMinSigners));
// }

// #[test]
// fn return_error_if_min_participant_is_less_than_2() {
// let mut invalid_input = "1\n3\n\n".as_bytes();
// let expected = request_inputs(&mut invalid_input);

// assert_eq!(expected, Err(Error::InvalidMinSigners));
// }

// #[test]
// fn return_error_if_max_participant_is_less_than_2() {
// let mut invalid_input = "2\n1\n\n".as_bytes();
// let expected = request_inputs(&mut invalid_input);

// assert_eq!(expected, Err(Error::InvalidMaxSigners));
// }

// // Testing inclusion of secret input

// #[test]
// fn check_valid_input_with_secret() {
// let mut valid_input =
// "3\n6\n7b1c33d3f5291d85de664833beb1ad469f7fb6025a0ec78b3a790c6e13a98304\n".as_bytes();
// let config = request_inputs(&mut valid_input).unwrap();

// let secret: Vec<u8> = vec![
// 123, 28, 51, 211, 245, 41, 29, 133, 222, 102, 72, 51, 190, 177, 173, 70, 159, 127, 182, 2,
// 90, 14, 199, 139, 58, 121, 12, 110, 19, 169, 131, 4,
// ];
// let expected = Config {
// min_signers: 3,
// max_signers: 6,
// secret,
// };

// assert_eq!(expected, config)
// }

// #[test]
// fn return_error_if_invalid_min_signers_input() {
// let mut invalid_input = "hello\n6\n\n".as_bytes();
// let expected = request_inputs(&mut invalid_input);

// assert_eq!(expected, Err(Error::InvalidMinSigners))
// }

// #[test]
// fn return_error_if_invalid_max_signers_input() {
// let mut invalid_input = "4\nworld\n\n".as_bytes();
// let expected = request_inputs(&mut invalid_input);

// assert_eq!(expected, Err(Error::InvalidMaxSigners))
// }

// #[test]
// fn return_malformed_signing_key_error_if_secret_is_invalid() {
// let mut secret_input = "4\n6\nasecret\n".as_bytes();
// let expected = request_inputs(&mut secret_input);

// assert_eq!(expected, Err(Error::MalformedSigningKey))
// }
use std::io::BufWriter;

use crate::inputs::{request_inputs, Config};
use frost::Error;
use frost_ed25519 as frost;

#[test]
fn check_valid_input_for_signers() {
let config = Config {
min_signers: 2,
max_signers: 3,
identifier: 1u16.try_into().unwrap(),
};

let mut buf = BufWriter::new(Vec::new());
let mut valid_input = "2\n3\n1\n".as_bytes();
let expected = request_inputs(&mut valid_input, &mut buf).unwrap();

assert_eq!(expected, config);
}

#[test]
fn return_error_if_min_participant_greater_than_max_participant() {
let mut invalid_input = "4\n3\n1\n".as_bytes();
let mut buf = BufWriter::new(Vec::new());
let expected = request_inputs(&mut invalid_input, &mut buf).unwrap_err();

assert_eq!(
*expected.downcast::<Error>().unwrap(),
Error::InvalidMinSigners
);
}

#[test]
fn return_error_if_min_participant_is_less_than_2() {
let mut invalid_input = "1\n3\n1\n".as_bytes();
let mut buf = BufWriter::new(Vec::new());
let expected = request_inputs(&mut invalid_input, &mut buf).unwrap_err();

assert_eq!(
*expected.downcast::<Error>().unwrap(),
Error::InvalidMinSigners
);
}

#[test]
fn return_error_if_max_participant_is_less_than_2() {
let mut invalid_input = "2\n1\n1\n".as_bytes();
let mut buf = BufWriter::new(Vec::new());
let expected = request_inputs(&mut invalid_input, &mut buf).unwrap_err();

assert_eq!(
*expected.downcast::<Error>().unwrap(),
Error::InvalidMaxSigners
);
}

#[test]
fn return_error_if_invalid_min_signers_input() {
let mut invalid_input = "hello\n6\n1\n".as_bytes();
let mut buf = BufWriter::new(Vec::new());
let expected = request_inputs(&mut invalid_input, &mut buf).unwrap_err();

assert_eq!(
*expected.downcast::<Error>().unwrap(),
Error::InvalidMinSigners
);
}

#[test]
fn return_error_if_invalid_max_signers_input() {
let mut invalid_input = "4\nworld\n1\n".as_bytes();
let mut buf = BufWriter::new(Vec::new());
let expected = request_inputs(&mut invalid_input, &mut buf).unwrap_err();

assert_eq!(
*expected.downcast::<Error>().unwrap(),
Error::InvalidMaxSigners
);
}

#[test]
fn return_malformed_identifier_error_if_identifier_invalid() {
let mut invalid_input = "4\n6\nasecret\n".as_bytes();
let mut buf = BufWriter::new(Vec::new());
let expected = request_inputs(&mut invalid_input, &mut buf).unwrap_err();

println!("{:?}", expected);
assert_eq!(
*expected.downcast::<Error>().unwrap(),
Error::MalformedIdentifier
);
}
Loading

0 comments on commit 0c327c9

Please sign in to comment.