End with exit code

This commit is contained in:
Matéo Duparc 2021-07-04 18:33:59 +02:00
parent 51f0a8b2fe
commit c3f24b2f4a
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
2 changed files with 21 additions and 8 deletions

View File

@ -1,4 +1,4 @@
use std::io::{BufWriter, BufReader, Read}; use std::{process, io::{BufWriter, BufReader, Read}};
use doby::{ use doby::{
cli, cli,
crypto::{EncryptionParams, DobyCipher}, crypto::{EncryptionParams, DobyCipher},
@ -7,7 +7,8 @@ use doby::{
encrypt, encrypt,
}; };
fn main() { fn run() -> bool {
let mut success = false;
if let Some(cli_args) = cli::parse() { if let Some(cli_args) = cli::parse() {
let mut reader = BufReader::with_capacity(cli_args.block_size, cli_args.reader); 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); let mut writer = BufWriter::with_capacity(cli_args.block_size, cli_args.writer);
@ -27,7 +28,9 @@ fn main() {
Ok(cipher) => { Ok(cipher) => {
match decrypt(&mut reader, &mut writer, cipher, cli_args.block_size) { match decrypt(&mut reader, &mut writer, cipher, cli_args.block_size) {
Ok(verified) => { 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."); 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); let params = EncryptionParams::new(cli_args.argon2_params, cli_args.cipher);
match DobyCipher::new(cli_args.password, &params) { match DobyCipher::new(cli_args.password, &params) {
Ok(cipher) => { Ok(cipher) => {
if let Err(e) = encrypt( match encrypt(
&mut reader, &mut reader,
&mut writer, &mut writer,
&params, &params,
@ -54,7 +57,8 @@ fn main() {
cli_args.block_size, cli_args.block_size,
Some(magic_bytes) 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) Err(e) => eprintln!("Invalid argon2 params: {}", e)
@ -64,4 +68,13 @@ fn main() {
Err(e) => eprintln!("I/O error while reading magic bytes: {}", e), Err(e) => eprintln!("I/O error while reading magic bytes: {}", e),
} }
} }
success
}
fn main() {
process::exit(if run() {
0
} else {
1
});
} }

View File

@ -34,8 +34,8 @@ fn files() -> io::Result<()> {
#[test] #[test]
fn argon2_params() -> io::Result<()> { 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("-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().stderr("Invalid argon2 params: memory 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().stderr("Invalid argon2 params: too few lanes\n"); Command::cargo_bin("doby").unwrap().arg("-t").arg("0").assert().failure().stderr("Invalid argon2 params: too few lanes\n");
Ok(()) Ok(())
} }