Debian Codegraph @

tests/gpg/trust_models.rs

raw
1 use anyhow::Result;
2
3 use sequoia_openpgp as openpgp;
4 use openpgp::{
5 cert::prelude::*,
6 parse::Parse,
7 serialize::{
8 Serialize,
9 SerializeInto,
10 },
11 };
12
13 use super::super::*;
14
15 const PLAINTEXT: &[u8] = b"plaintext";
16
17 /// Tests --trust-model=always.
18 #[test]
19 #[ntest::timeout(600000)]
20 fn always() -> Result<()> {
21 let mut experiment = make_experiment!()?;
22
23 let cert = experiment.artifact(
24 "cert",
25 || CertBuilder::general_purpose(
26 Some("Alice Lovelace <[email protected]>"))
27 .set_creation_time(Experiment::now())
28 .generate()
29 .map(|(cert, _rev)| cert),
30 |a, f| a.as_tsk().serialize(f),
31 |b| Cert::from_bytes(&b))?;
32
33 experiment.section("Importing cert...");
34 let diff = experiment.invoke(&[
35 "--import",
36 &experiment.store("cert", &cert.to_vec()?)?,
37 ])?;
38 diff.assert_success();
39 diff.assert_equal_up_to(0, 0);
40
41 run_test(cert, experiment, "always", true)
42 }
43
44 /// Tests --trust-model=pgp.
45 #[test]
46 #[ntest::timeout(600000)]
47 fn pgp() -> Result<()> {
48 for (owner_trust, expectation) in [
49 (None, false),
50 (Some(2), false),
51 (Some(3), false),
52 (Some(4), false),
53 (Some(5), false),
54 (Some(6), true),
55 ] {
56 let mut experiment = make_experiment!(owner_trust.unwrap_or(0))?;
57
58 let cert = experiment.artifact(
59 "cert",
60 || CertBuilder::general_purpose(
61 Some("Alice Lovelace <[email protected]>"))
62 .set_creation_time(Experiment::now())
63 .generate()
64 .map(|(cert, _rev)| cert),
65 |a, f| a.as_tsk().serialize(f),
66 |b| Cert::from_bytes(&b))?;
67
68 experiment.section("Importing cert...");
69 let diff = experiment.invoke(&[
70 "--import",
71 &experiment.store("cert", &cert.to_vec()?)?,
72 ])?;
73 diff.assert_success();
74 diff.assert_equal_up_to(0, 0);
75
76 if let Some(owner_trust) = owner_trust {
77 experiment.section(
78 format!("Setting ownertrust to {}...", owner_trust));
79 let diff = experiment.invoke(&[
80 "--import-ownertrust",
81 &experiment.store("ownertrust",
82 &format!("{:X}:{}:\n",
83 cert.fingerprint(), owner_trust))?,
84 ])?;
85 diff.assert_success();
86 diff.assert_equal_up_to(0, 9);
87 }
88
89 run_test(cert, experiment, "pgp", expectation)?;
90 }
91
92 Ok(())
93 }
94
95 /// Tests --trust-model=auto.
96 #[test]
97 #[ntest::timeout(600000)]
98 fn auto() -> Result<()> {
99 for (owner_trust, expectation) in [
100 (None, false),
101 (Some(2), false),
102 (Some(3), false),
103 (Some(4), false),
104 (Some(5), false),
105 (Some(6), true),
106 ] {
107 let mut experiment = make_experiment!(owner_trust.unwrap_or(0))?;
108
109 let cert = experiment.artifact(
110 "cert",
111 || CertBuilder::general_purpose(
112 Some("Alice Lovelace <[email protected]>"))
113 .set_creation_time(Experiment::now())
114 .generate()
115 .map(|(cert, _rev)| cert),
116 |a, f| a.as_tsk().serialize(f),
117 |b| Cert::from_bytes(&b))?;
118
119 experiment.section("Importing cert...");
120 let diff = experiment.invoke(&[
121 "--import",
122 &experiment.store("cert", &cert.to_vec()?)?,
123 ])?;
124 diff.assert_success();
125 diff.assert_equal_up_to(0, 0);
126
127 if let Some(owner_trust) = owner_trust {
128 experiment.section(
129 format!("Setting ownertrust to {}...", owner_trust));
130 let diff = experiment.invoke(&[
131 "--import-ownertrust",
132 &experiment.store("ownertrust",
133 &format!("{:X}:{}:\n",
134 cert.fingerprint(), owner_trust))?,
135 ])?;
136 diff.assert_success();
137 diff.assert_equal_up_to(0, 9);
138 }
139
140 run_test(cert, experiment, "auto", expectation)?;
141 }
142
143 Ok(())
144 }
145
146 /// Runs an experiment with both implementations.
147 ///
148 /// First, we import a cert, let the caller modify the environment
149 /// with `frobber`, check the trust database, lists the cert comparing
150 /// the machine readable output (which includes trust information
151 /// derived from the trust model), and tries to authenticate the cert.
152 fn run_test(cert: Cert, mut experiment: Experiment, model: &'static str,
153 expect_success: bool)
154 -> Result<()>
155 {
156 experiment.section("Checking the trust database...");
157 let diff = experiment.invoke(&["--check-trustdb"])?;
158 diff.assert_success();
159
160 // Now list the certs.
161 let diff = experiment.invoke(&[
162 "--list-keys",
163 "--with-colons",
164 ])?;
165 diff.assert_success();
166 diff.assert_equal_up_to(11, // Next trust db check time.
167 0);
168
169 // Try to authenticate the certificate as a recipient.
170 let diff = experiment.invoke(&[
171 "--batch",
172 "--no-auto-key-locate",
173 "--trust-model", model,
174 "--encrypt",
175 "--recipient", "<[email protected]>",
176 "--output", "ciphertext",
177 &experiment.store("plaintext", PLAINTEXT)?,
178 ])?;
179 if expect_success {
180 diff.assert_success();
181 diff.assert_limits(0, 0, 0);
182 let ciphertexts =
183 diff.with_working_dir(|p| p.get("ciphertext").cloned().ok_or_else(
184 || anyhow::anyhow!("no ciphertext produced")))?;
185
186 experiment.section("Importing key...");
187 let diff = experiment.invoke(&[
188 "--import",
189 &experiment.store("key", &cert.as_tsk().to_vec()?)?,
190 ])?;
191 diff.assert_success();
192 diff.assert_limits(0, 0, 78);
193
194 for ciphertext in ciphertexts {
195 let diff = experiment.invoke(&[
196 "--decrypt",
197 "--output", "plaintext",
198 &experiment.store("ciphertext", &ciphertext)?,
199 ])?;
200 diff.assert_success();
201 diff.assert_limits(0, 1, 0);
202 diff.with_working_dir(|p| {
203 assert_eq!(p.get("plaintext").expect("no output"), PLAINTEXT);
204 Ok(())
205 })?;
206 }
207 } else {
208 diff.assert_failure();
209 diff.assert_limits(0, 0, 0);
210 assert!(diff.with_working_dir(
211 |p| Ok(p.get("ciphertext").is_some()))?
212 .iter().all(|&exists| exists == false));
213 }
214
215 Ok(())
216 }