| 1 |
use std::fs::File; |
| 2 |
use std::io::Write; |
| 3 |
|
| 4 |
use predicates::prelude::*; |
| 5 |
|
| 6 |
use sequoia_openpgp as openpgp; |
| 7 |
use openpgp::Result; |
| 8 |
|
| 9 |
use super::common::{Sq, artifact}; |
| 10 |
|
| 11 |
// Integration tests should be done with subplot. |
| 12 |
// However, at this time, subplot does not support static binary files in tests. |
| 13 |
// Generating the test files would mean encrypting some static text symmetrically |
| 14 |
// and then extracting the session key, which means parsing of human readabe cli output. |
| 15 |
// So, for now, the tests go here. |
| 16 |
#[test] |
| 17 |
fn session_key() -> Result<()> { |
| 18 |
let sq = Sq::new(); |
| 19 |
sq.command() |
| 20 |
.arg("decrypt") |
| 21 |
.args(["--session-key", "1FE820EC21FB5D7E33D83367106D1D3747DCD48E6320C1AEC57EE7D18FC437D4"]) |
| 22 |
.arg(artifact("messages/rsa.msg.pgp")) |
| 23 |
.assert() |
| 24 |
.success() |
| 25 |
.stderr(predicate::str::contains("Decryption failed").not()); |
| 26 |
Ok(()) |
| 27 |
} |
| 28 |
|
| 29 |
#[test] |
| 30 |
fn session_key_with_prefix() -> Result<()> { |
| 31 |
let sq = Sq::new(); |
| 32 |
sq.command() |
| 33 |
.arg("decrypt") |
| 34 |
.args(["--session-key", "9:1FE820EC21FB5D7E33D83367106D1D3747DCD48E6320C1AEC57EE7D18FC437D4"]) |
| 35 |
.arg(artifact("messages/rsa.msg.pgp")) |
| 36 |
.assert() |
| 37 |
.success() |
| 38 |
.stderr(predicate::str::contains("Decryption failed").not()); |
| 39 |
Ok(()) |
| 40 |
} |
| 41 |
|
| 42 |
#[test] |
| 43 |
fn session_key_multiple() -> Result<()> { |
| 44 |
let sq = Sq::new(); |
| 45 |
sq.command() |
| 46 |
.arg("decrypt") |
| 47 |
.args(["--session-key", "2FE820EC21FB5D7E33D83367106D1D3747DCD48E6320C1AEC57EE7D18FC437D4"]) |
| 48 |
.args(["--session-key", "9:1FE820EC21FB5D7E33D83367106D1D3747DCD48E6320C1AEC57EE7D18FC437D4"]) |
| 49 |
.args(["--session-key", "3FE820EC21FB5D7E33D83367106D1D3747DCD48E6320C1AEC57EE7D18FC437D4"]) |
| 50 |
.arg(artifact("messages/rsa.msg.pgp")) |
| 51 |
.assert() |
| 52 |
.success() |
| 53 |
.stderr(predicate::str::contains("Decryption failed").not()); |
| 54 |
Ok(()) |
| 55 |
} |
| 56 |
|
| 57 |
#[test] |
| 58 |
fn session_key_wrong_key() -> Result<()> { |
| 59 |
let sq = Sq::new(); |
| 60 |
sq.command() |
| 61 |
.arg("decrypt") |
| 62 |
.args(["--session-key", "BB9CCB8EDE22DC222C83BD1C63AEB97335DDC7B696DB171BD16EAA5784CC0478"]) |
| 63 |
.arg(artifact("messages/rsa.msg.pgp")) |
| 64 |
.assert() |
| 65 |
.failure() |
| 66 |
.stderr(predicate::str::contains("Decryption failed")); |
| 67 |
Ok(()) |
| 68 |
} |
| 69 |
|
| 70 |
// Make sure verifying bad data fails, and removes the intermediate |
| 71 |
// file. |
| 72 |
#[test] |
| 73 |
fn sq_decrypt_bad() -> Result<()> { |
| 74 |
let sq = Sq::new(); |
| 75 |
|
| 76 |
// The signer. |
| 77 |
let (_cert, signer_file, _rev_file) = sq.key_generate(&[], &["alice"]); |
| 78 |
let (_cert, recip_file, _rev_file) = sq.key_generate(&[], &["bob"]); |
| 79 |
|
| 80 |
// Create a big file. When verifying, we buffer the first ~25 MB |
| 81 |
// of verified data so that we can at least withhold some data if |
| 82 |
// we can't verify the message. |
| 83 |
let data_file = sq.scratch_file("data"); |
| 84 |
let size = { |
| 85 |
let mut file = File::create(&data_file)?; |
| 86 |
let data = vec![42; 1024 * 1024]; |
| 87 |
let mut size = 0; |
| 88 |
for _ in 0..30 { |
| 89 |
file.write_all(&data).expect("can write"); |
| 90 |
size += data.len(); |
| 91 |
} |
| 92 |
|
| 93 |
size as u64 |
| 94 |
}; |
| 95 |
assert_eq!(std::fs::metadata(&data_file).expect("can stat").len(), |
| 96 |
size); |
| 97 |
|
| 98 |
// Sign and encrypt the message. |
| 99 |
let file = sq.scratch_file("file"); |
| 100 |
sq.encrypt( |
| 101 |
&[ |
| 102 |
"--for-file", &recip_file.display().to_string(), |
| 103 |
"--signer-file", &signer_file.display().to_string(), |
| 104 |
"--output", &file.display().to_string(), |
| 105 |
], |
| 106 |
&data_file); |
| 107 |
|
| 108 |
// Verify the signed message. |
| 109 |
let output = sq.scratch_file("output"); |
| 110 |
std::fs::write(&output, "xxx").expect("can write to scratch file"); |
| 111 |
sq.decrypt( |
| 112 |
&["--overwrite", |
| 113 |
"--recipient-file", &recip_file.display().to_string(), |
| 114 |
"--signer-file", &signer_file.display().to_string(), |
| 115 |
"--output", &output.display().to_string(), |
| 116 |
], |
| 117 |
&file); |
| 118 |
assert!(output.exists()); |
| 119 |
assert_eq!(std::fs::metadata(&output).expect("can stat").len(), |
| 120 |
size); |
| 121 |
|
| 122 |
// Making signing fail and ensure the output is deleted. |
| 123 |
let output2 = sq.scratch_file("output2"); |
| 124 |
std::fs::write(&output2, "xxx").expect("can write to scratch file"); |
| 125 |
assert!( |
| 126 |
sq.decrypt_maybe( |
| 127 |
&["--overwrite", |
| 128 |
"--recipient-file", &recip_file.display().to_string(), |
| 129 |
"--signer-file", &recip_file.display().to_string(), |
| 130 |
"--output", &output2.display().to_string(), |
| 131 |
], |
| 132 |
&file) |
| 133 |
.is_err()); |
| 134 |
// Since we failed to verify the message, we should have deleted |
| 135 |
// the output file. |
| 136 |
assert!(! output2.exists()); |
| 137 |
|
| 138 |
Ok(()) |
| 139 |
} |