diff --git a/src/main.rs b/src/main.rs index b4023e3..17d33af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::io::{BufWriter, BufReader, Read}; +use std::{process, io::{BufWriter, BufReader, Read}}; use doby::{ cli, crypto::{EncryptionParams, DobyCipher}, @@ -7,7 +7,8 @@ use doby::{ encrypt, }; -fn main() { +fn run() -> bool { + let mut success = false; if let Some(cli_args) = cli::parse() { let mut reader = BufReader::with_capacity(cli_args.block_size, cli_args.reader); let mut writer = BufWriter::with_capacity(cli_args.block_size, cli_args.writer); @@ -27,7 +28,9 @@ fn main() { Ok(cipher) => { match decrypt(&mut reader, &mut writer, cipher, cli_args.block_size) { Ok(verified) => { - if !verified { + if verified { + success = true + } else { eprintln!("WARNING: HMAC verification failed !\nEither your password is incorrect or the ciphertext has been corrupted.\nBe careful, the data could have been altered by an attacker."); } } @@ -46,7 +49,7 @@ fn main() { let params = EncryptionParams::new(cli_args.argon2_params, cli_args.cipher); match DobyCipher::new(cli_args.password, ¶ms) { Ok(cipher) => { - if let Err(e) = encrypt( + match encrypt( &mut reader, &mut writer, ¶ms, @@ -54,7 +57,8 @@ fn main() { cli_args.block_size, Some(magic_bytes) ) { - eprintln!("I/O error while encrypting: {}", e); + Ok(_) => success = true, + Err(e) => eprintln!("I/O error while encrypting: {}", e) } } Err(e) => eprintln!("Invalid argon2 params: {}", e) @@ -64,4 +68,13 @@ fn main() { Err(e) => eprintln!("I/O error while reading magic bytes: {}", e), } } + success +} + +fn main() { + process::exit(if run() { + 0 + } else { + 1 + }); } diff --git a/tests/cli.rs b/tests/cli.rs index a4e3af6..9aa9746 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -34,8 +34,8 @@ fn files() -> io::Result<()> { #[test] fn argon2_params() -> io::Result<()> { - Command::cargo_bin("doby").unwrap().arg("-i").arg("0").assert().stderr("Invalid argon2 params: time cost is too small\n"); - Command::cargo_bin("doby").unwrap().arg("-m").arg("0").assert().stderr("Invalid argon2 params: memory cost is too small\n"); - Command::cargo_bin("doby").unwrap().arg("-t").arg("0").assert().stderr("Invalid argon2 params: too few lanes\n"); + Command::cargo_bin("doby").unwrap().arg("-i").arg("0").assert().failure().stderr("Invalid argon2 params: time cost is too small\n"); + Command::cargo_bin("doby").unwrap().arg("-m").arg("0").assert().failure().stderr("Invalid argon2 params: memory cost is too small\n"); + Command::cargo_bin("doby").unwrap().arg("-t").arg("0").assert().failure().stderr("Invalid argon2 params: too few lanes\n"); Ok(()) } \ No newline at end of file