Skip to content

Commit

Permalink
test encrypt subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
glehmann committed Feb 9, 2024
1 parent 77e4a6a commit c70a9e4
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 26 deletions.
13 changes: 0 additions & 13 deletions tests/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,6 @@ use predicates::prelude::predicate::str::*;

use crate::common::*;

const YAML_CONTENT: &str = "foo: bar
titi:
toto: 42
array:
- 1
- 2
- 3
empty_map: {}
empty_array: []
empty_string: ''
empty: null
";

#[test]
fn check_clear() {
let tmp = temp_dir();
Expand Down
38 changes: 38 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,41 @@ pub fn create_key(tmp: &TempDir) -> (PathBuf, PathBuf) {
.stderr(is_pub_key_info());
(key_path.path().into(), public_path.path().into())
}

pub const YAML_CONTENT: &str = "foo: bar
titi:
toto: 42
array:
- 1
- 2
- 3
empty_map: {}
empty_array: []
empty_string: ''
empty: null
";

pub fn generate_encrypted_file() -> (TempDir, PathBuf, PathBuf, PathBuf, PathBuf) {
let tmp = temp_dir();
let (key_path, pub_path) = create_key(&tmp);
let yaml_path = tmp.child("file.yaml");
write(&yaml_path, YAML_CONTENT);
let encrypted_path = tmp.child("file.enc.yaml");
yage!(
"encrypt",
"-R",
&pub_path,
&yaml_path,
"-o",
&encrypted_path
)
.stdout(is_empty())
.stderr(is_empty());
(
tmp,
key_path,
pub_path,
yaml_path.path().to_owned(),
encrypted_path.path().to_owned(),
)
}
134 changes: 134 additions & 0 deletions tests/decrypt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
mod common;

use assert_fs::prelude::*;
use predicates::str::{contains, is_empty};

use crate::common::*;

#[test]
fn decrypt_to_stdout() {
let (_tmp, key_path, _, yaml_path, encrypted_path) = generate_encrypted_file();
let output = yage!("decrypt", "--key-file", &key_path, &encrypted_path)
.stderr(is_empty())
.get_output()
.clone();
assert_eq!(String::from_utf8(output.stdout).unwrap(), read(&yaml_path));
}

#[test]
fn decrypt_to_file() {
let (tmp, key_path1, _, yaml_path, encrypted_path) = generate_encrypted_file();
let (key_path2, _) = create_key(&tmp);
let decrypted_path = tmp.child("file.dec.yaml");
yage!(
"decrypt",
"--key-file",
&key_path1,
"-K",
&key_path2,
&encrypted_path,
"--output",
&decrypted_path
)
.stdout(is_empty())
.stderr(is_empty());
assert_eq!(read(&decrypted_path), read(&yaml_path));
}

#[test]
fn decrypt_from_stdin() {
let (tmp, key_path, _, yaml_path, encrypted_path) = generate_encrypted_file();
let decrypted_path = tmp.child("file.dec.yaml");
yage_cmd!(
"decrypt",
"--key",
read(&key_path).trim(),
"-",
"--output",
&decrypted_path
)
.write_stdin(read(&encrypted_path))
.assert()
.success()
.stdout(is_empty())
.stderr(is_empty());
assert_eq!(read(&decrypted_path), read(&yaml_path));
}

#[test]
fn decrypt_key_stdin() {
let (tmp, key_path, _, yaml_path, encrypted_path) = generate_encrypted_file();
let decrypted_path = tmp.child("file.dec.yaml");
yage_cmd!(
"decrypt",
"--key-file",
"-",
&encrypted_path,
"-o",
&decrypted_path
)
.write_stdin(read(&key_path))
.assert()
.success()
.stdout(is_empty())
.stderr(is_empty());
assert_eq!(read(&decrypted_path), read(&yaml_path));
}

#[test]
fn decrypt_in_place() {
let (_tmp, key_path, _, yaml_path, encrypted_path) = generate_encrypted_file();
yage!(
"decrypt",
"-k",
read(&key_path).trim(),
&encrypted_path,
"-i"
)
.stdout(is_empty())
.stderr(is_empty());
assert_eq!(read(&encrypted_path), read(&yaml_path));
}

#[test]
fn decrypt_stdin_in_place() {
yage_cmd!("decrypt", "--in-place", "-")
.assert()
.failure()
.stdout(is_empty())
.stderr(contains("error: stdin can't be modified in place"));
}

#[test]
fn decrypt_key_from_env() {
let (tmp, key_path1, _, yaml_path, encrypted_path) = generate_encrypted_file();
let (key_path2, _) = create_key(&tmp);
let decrypted_path = tmp.child("file.dec.yaml");
yage_cmd!("decrypt", &encrypted_path, "--output", &decrypted_path)
.env(
"YAGE_KEY",
format!("{},{}", read(&key_path1).trim(), read(&key_path2).trim()),
)
.assert()
.success()
.stdout(is_empty())
.stderr(is_empty());
assert_eq!(read(&decrypted_path), read(&yaml_path));
}

#[test]
fn decrypt_key_file_from_env() {
let (tmp, key_path1, _, yaml_path, encrypted_path) = generate_encrypted_file();
let (key_path2, _) = create_key(&tmp);
let decrypted_path = tmp.child("file.dec.yaml");
yage_cmd!("decrypt", &encrypted_path, "--output", &decrypted_path)
.env(
"YAGE_KEY_FILE",
std::env::join_paths(vec![&key_path1, &key_path2]).unwrap(),
)
.assert()
.success()
.stdout(is_empty())
.stderr(is_empty());
assert_eq!(read(&decrypted_path), read(&yaml_path));
}
13 changes: 0 additions & 13 deletions tests/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@ use yage::EncryptionStatus;

use crate::common::*;

const YAML_CONTENT: &str = "foo: bar
titi:
toto: 42
array:
- 1
- 2
- 3
empty_map: {}
empty_array: []
empty_string: ''
empty: null
";

const YAML_CONTENT_ENCRYPTED_PATTERN: &str = r"foo: yage\[[0-9a-zA-Z/=\-+]+\]
titi:
toto: yage\[[0-9a-zA-Z/=\-+]+\]
Expand Down

0 comments on commit c70a9e4

Please sign in to comment.