Add --cipher integration test

This commit is contained in:
Matéo Duparc 2021-07-05 12:43:04 +02:00
parent 7716cc5a79
commit 5ada55a93d
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
2 changed files with 29 additions and 1 deletions

View File

@ -34,7 +34,7 @@ pub enum CipherAlgorithm {
}
impl CipherAlgorithm {
fn get_nonce_size(&self) -> usize {
pub fn get_nonce_size(&self) -> usize {
match self {
CipherAlgorithm::AesCtr => AES_NONCE_LEN,
CipherAlgorithm::XChaCha20 => XCHACHA20_NONCE_LEN,

View File

@ -1,6 +1,7 @@
use std::{io::{self, Read, Write}, fs::{self, File, create_dir}, path::PathBuf};
use assert_cmd::{Command, cargo::{CargoError, cargo_bin}};
use tempfile::TempDir;
use doby::crypto::CipherAlgorithm;
const PLAINTEXT: &[u8] = b"the plaintext";
const PASSWORD: &str = "the password";
@ -100,6 +101,33 @@ fn force_encrypt() -> io::Result<()> {
Ok(())
}
fn test_cipher(cipher_str: &str, cipher_algorithm: CipherAlgorithm) -> io::Result<()> {
let (_, tmp_plaintext, tmp_ciphertext) = setup_files()?;
doby_cmd().unwrap().arg("-c").arg(cipher_str).arg(tmp_plaintext).arg(&tmp_ciphertext).assert().success().stdout("").stderr("");
let ciphertext = fs::read(&tmp_ciphertext)?;
assert_eq!(ciphertext[4+64*2+4*2+1], cipher_algorithm as u8);
assert_eq!(ciphertext.len(), PLAINTEXT.len()+174+cipher_algorithm.get_nonce_size());
doby_cmd().unwrap().arg(tmp_ciphertext).assert().success().stdout(PLAINTEXT).stderr("");
Ok(())
}
#[test]
fn xchacha20_cipher() -> io::Result<()> {
test_cipher("xchacha20", CipherAlgorithm::XChaCha20)?;
Ok(())
}
#[test]
fn aes_cipher() -> io::Result<()> {
test_cipher("aes", CipherAlgorithm::AesCtr)?;
Ok(())
}
#[test]
fn argon2_params() -> io::Result<()> {
Command::cargo_bin("doby").unwrap().arg("-i").arg("0").assert().failure().stderr("Invalid argon2 params: time cost is too small\n");