commit 1896c49423cb6ff00f0eb46806801765e1200f03
parent 9438aa7c9464a40279daad9e20d66c4cbf3854ac
Author: Stefan Koch <programming@stefan-koch.name>
Date: Fri, 13 Dec 2019 22:37:21 +0100
write an integration test for encryption / decryption
Diffstat:
4 files changed, 126 insertions(+), 6 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
@@ -11,6 +11,17 @@ version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "assert_cmd"
+version = "0.11.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "escargot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "predicates 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "predicates-core 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "predicates-tree 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "autocfg"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -81,6 +92,7 @@ dependencies = [
name = "cinderella"
version = "0.1.0"
dependencies = [
+ "assert_cmd 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)",
"duct 0.13.3 (registry+https://github.com/rust-lang/crates.io-index)",
"evalexpr 5.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -127,6 +139,11 @@ dependencies = [
]
[[package]]
+name = "difference"
+version = "2.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "duct"
version = "0.13.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -209,6 +226,17 @@ version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "escargot"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.100 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "evalexpr"
version = "5.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -547,6 +575,29 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "predicates"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "predicates-core 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "predicates-core"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "predicates-tree"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "predicates-core 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "treeline 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "proc-macro2"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -910,6 +961,11 @@ dependencies = [
]
[[package]]
+name = "treeline"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "unicode-bidi"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1006,6 +1062,7 @@ dependencies = [
[metadata]
"checksum adler32 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2"
"checksum ascii_utils 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "71938f30533e4d95a6d17aa530939da3842c2ab6f4f84b9dae68447e4129f74a"
+"checksum assert_cmd 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2dc477793bd82ec39799b6f6b3df64938532fdf2ab0d49ef817eac65856a5a1e"
"checksum autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "b671c8fb71b457dd4ae18c4ba1e59aa81793daacc361d82fcd410cef0d491875"
"checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e"
"checksum base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643"
@@ -1020,6 +1077,7 @@ dependencies = [
"checksum core-foundation 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "25b9e03f145fd4f2bf705e07b900cd41fc636598fe5dc452fd0db1441c3f496d"
"checksum core-foundation-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e7ca8a5221364ef15ce201e8ed2f609fc312682a8f4e0e3d4aa5879764e0fa3b"
"checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
+"checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
"checksum duct 0.13.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1607fa68d55be208e83bcfbcfffbc1ec65c9fbcf9eb1a5d548dc3ac0100743b0"
"checksum email 0.0.20 (registry+https://github.com/rust-lang/crates.io-index)" = "91549a51bb0241165f13d57fc4c72cef063b4088fb078b019ecbf464a45f22e4"
"checksum encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec"
@@ -1029,6 +1087,7 @@ dependencies = [
"checksum encoding-index-singlebyte 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3351d5acffb224af9ca265f435b859c7c01537c0849754d3db3fdf2bfe2ae84a"
"checksum encoding-index-tradchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18"
"checksum encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569"
+"checksum escargot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ceb9adbf9874d5d028b5e4c5739d22b71988252b25c9c98fe7cf9738bee84597"
"checksum evalexpr 5.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "5a8619223d40c23279f9120e2646712ec3eeeb68b546ed993c619d77682bdef8"
"checksum fast_chemail 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)" = "495a39d30d624c2caabe6312bfead73e7717692b44e0b32df168c275a2e8e9e4"
"checksum filetime 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1ff6d4dab0aa0c8e6346d46052e93b13a16cf847b54ed357087c35011048cc7d"
@@ -1068,6 +1127,9 @@ dependencies = [
"checksum percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
"checksum pkg-config 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c1d2cfa5a714db3b5f24f0915e74fcdf91d09d496ba61329705dda7774d2af"
"checksum ppv-lite86 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e3cbf9f658cdb5000fcf6f362b8ea2ba154b9f146a61c7a20d647034c6b6561b"
+"checksum predicates 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a9bfe52247e5cc9b2f943682a85a5549fb9662245caf094504e69a2f03fe64d4"
+"checksum predicates-core 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "06075c3a3e92559ff8929e7a280684489ea27fe44805174c3ebd9328dcb37178"
+"checksum predicates-tree 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8e63c4859013b38a76eca2414c64911fba30def9e3202ac461a2d22831220124"
"checksum proc-macro2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "afdc77cc74ec70ed262262942ebb7dac3d479e9e5cfa2da1841c0806f6cdabcc"
"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
"checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
@@ -1109,6 +1171,7 @@ dependencies = [
"checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"
"checksum toml 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c7aabe75941d914b72bf3e5d3932ed92ce0664d49d8432305a8b547c37227724"
+"checksum treeline 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a7f741b240f1a48843f9b8e0444fb55fb2a4ff67293b50a9179dfd5ea67f8d41"
"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5"
"checksum unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "141339a08b982d942be2ca06ff8b076563cbe223d1befd5450716790d44e2426"
"checksum unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7007dbd421b92cc6e28410fe7362e2e0a2503394908f417b68ec8d1c364c4e20"
diff --git a/Cargo.toml b/Cargo.toml
@@ -20,3 +20,4 @@ duct = "0.13"
[dev-dependencies]
tempfile = "3"
+assert_cmd = "0.11"
diff --git a/src/main.rs b/src/main.rs
@@ -17,8 +17,8 @@ fn main() {
Some(command) => {
match command.as_ref() {
"run" => run(args),
- "encrypt" => encrypt(),
- "decrypt" => decrypt(),
+ "encrypt" => encrypt(args),
+ "decrypt" => decrypt(args),
_ => println!("Unknown command!"),
}
},
@@ -26,15 +26,31 @@ fn main() {
}
}
-fn encrypt() {
- let pass = rpassword::read_password_from_tty(Some("Password: ")).unwrap();
+fn parse_password_arg(args: Vec<String>) -> Option<String> {
+ let mut opts = Options::new();
+ opts.optopt("p", "password", "set the password for encryption/decryption", "PASSWORD");
+
+ match opts.parse(&args[2..]) {
+ Ok(m) => m.opt_str("p"),
+ Err(f) => panic!(f.to_string()),
+ }
+}
+
+fn encrypt(args: Vec<String>) {
+ let pass = match parse_password_arg(args) {
+ Some(pass) => pass,
+ None => rpassword::read_password_from_tty(Some("Password: ")).unwrap(),
+ };
cinderella::encrypt(Path::new(".cinderella/secrets.toml"),
Path::new(".cinderella/secrets"), &pass);
}
-fn decrypt() {
- let pass = rpassword::read_password_from_tty(Some("Password: ")).unwrap();
+fn decrypt(args: Vec<String>) {
+ let pass = match parse_password_arg(args) {
+ Some(pass) => pass,
+ None => rpassword::read_password_from_tty(Some("Password: ")).unwrap(),
+ };
cinderella::decrypt(Path::new(".cinderella/secrets"),
Path::new(".cinderella/secrets.toml"), &pass);
diff --git a/tests/cli.rs b/tests/cli.rs
@@ -0,0 +1,40 @@
+use std::fs::{self, File};
+use std::io::Write;
+use std::process::Command;
+
+use assert_cmd::prelude::*;
+use tempfile;
+use assert_cmd;
+
+#[test]
+fn test_encrypt_decrypt() {
+ let dir = tempfile::tempdir().unwrap();
+ let cinderella_dir = dir.path().join(".cinderella");
+ let secrets_file = cinderella_dir.join("secrets.toml");
+
+ fs::create_dir(cinderella_dir).unwrap();
+ let mut file = File::create(&secrets_file).unwrap();
+ writeln!(file, "MY_SECRET = \"secret\"").unwrap();
+
+ Command::cargo_bin("cinderella").unwrap()
+ .current_dir(&dir)
+ .arg("encrypt")
+ .arg("-p")
+ .arg("my-pass")
+ .output()
+ .expect("Encryption failed");
+
+ fs::remove_file(&secrets_file).expect("Could not delete plaintext");
+
+ Command::cargo_bin("cinderella").unwrap()
+ .current_dir(&dir)
+ .arg("decrypt")
+ .arg("-p")
+ .arg("my-pass")
+ .output()
+ .expect("Decryption failed");
+
+ let res = fs::read_to_string(&secrets_file).unwrap();
+
+ assert_eq!("MY_SECRET = \"secret\"", res.trim());
+}