Debian Codegraph @

tests/integration/sq_cert_import.rs

raw
1 use std::fs::File;
2
3 use sequoia_openpgp as openpgp;
4 use openpgp::Packet;
5 use openpgp::PacketPile;
6 use openpgp::Result;
7 use openpgp::cert::prelude::*;
8 use openpgp::packet::Tag;
9 use openpgp::parse::Parse;
10 use openpgp::serialize::Serialize;
11 use openpgp::types::RevocationStatus;
12
13 use super::common::Sq;
14 use super::common::STANDARD_POLICY;
15
16 #[test]
17 fn sq_cert_import() -> Result<()>
18 {
19 let sq = Sq::new();
20
21 // Generate keys.
22 let (_cert, alice_pgp, _rev) =
23 sq.key_generate(&["--expiration", "never"], &["<[email protected]>"]);
24
25 let alice_bytes = std::fs::read(&alice_pgp)?;
26
27 let (_cert, bob_pgp, _rev) =
28 sq.key_generate(&["--expiration", "never"], &["<[email protected]>"]);
29
30 let (_cert, carol_pgp, _rev) =
31 sq.key_generate(&["--expiration", "never"], &["<[email protected]>"]);
32
33 let alice_pgp = alice_pgp.display().to_string();
34 let alice_pgp = &alice_pgp[..];
35 let bob_pgp = bob_pgp.display().to_string();
36 let bob_pgp = &bob_pgp[..];
37 let carol_pgp = carol_pgp.display().to_string();
38 let carol_pgp = &carol_pgp[..];
39
40 let files = &[ alice_pgp, bob_pgp, carol_pgp ];
41
42 let check = |files: &[&str], stdin: Option<&[u8]>, expected: usize|
43 {
44 // Use a fresh certd.
45 let sq = Sq::new();
46
47 // Import.
48 let mut cmd = sq.command();
49 cmd.args(["cert", "import"]);
50 cmd.args(files);
51 if let Some(stdin) = stdin {
52 cmd.write_stdin(stdin);
53 }
54 eprintln!("sq cert import {}{}",
55 files.join(" "),
56 if stdin.is_some() { "<BYTES" } else { "" });
57 cmd.assert().success();
58
59
60 // Export.
61 let mut cmd = sq.command();
62 cmd.args(["cert", "export", "--all"]);
63
64 eprintln!("sq cert export...");
65
66 let output = cmd.output().expect("success");
67 let stdout = String::from_utf8_lossy(&output.stdout);
68 let stderr = String::from_utf8_lossy(&output.stderr);
69
70 assert!(output.status.success(),
71 "sq cert export should succeed\n\
72 stdout:\n{}\nstderr:\n{}",
73 stdout, stderr);
74
75 let parser = CertParser::from_bytes(stdout.as_bytes())
76 .expect("valid");
77 let found = parser.collect::<Result<Vec<Cert>>>()
78 .expect("valid");
79
80 assert_eq!(expected, found.len(),
81 "expected: {}\nfound: {} ({})\n\
82 stdout:\n{}\nstderr:\n{}",
83 expected, found.len(),
84 found.iter().map(|c| c.fingerprint().to_string())
85 .collect::<Vec<_>>()
86 .join(", "),
87 stdout, stderr);
88 };
89
90 // Import from N files.
91 for i in 1..=files.len() {
92 check(&files[0..i], None, i);
93 }
94
95 // Import from stdin.
96 check(&[], Some(&alice_bytes[..]), 1);
97
98 // Specify "-".
99 check(&["-"], Some(&alice_bytes[..]), 1);
100
101 // Provide stdin and a file. Only the file should be read.
102 check(&[bob_pgp], Some(&alice_bytes[..]), 1);
103
104 // Provide stdin explicitly and a file. Both should be read.
105 check(&[bob_pgp, "-"], Some(&alice_bytes[..]), 2);
106
107 Ok(())
108 }
109
110 #[test]
111 fn sq_cert_import_rev() -> Result<()>
112 {
113 let sq = Sq::new();
114
115 // Generate a key. (We don't use sq on purpose: we want to make
116 // sure we have a bare revocation certificate.)
117 let (cert, rev) = CertBuilder::general_purpose(
118 Some("[email protected]"))
119 .set_creation_time(sq.now())
120 .generate()?;
121
122 let cert_file = sq.scratch_file("cert");
123 cert.as_tsk().serialize(&mut File::create(&cert_file)?)?;
124
125 sq.key_import(&cert_file);
126
127
128 // We shouldn't be able to import a signature over a data file.
129
130 // Create a detached signature.
131 let sig_file = sq.scratch_file("sig");
132 sq.sign_detached(&[], cert.fingerprint(),
133 cert_file.as_path(), sig_file.as_path());
134
135 // Be extra sure that it is a single packet.
136 let pp = PacketPile::from_file(&sig_file)?;
137 let packets = pp.into_children().collect::<Vec<_>>();
138 assert_eq!(packets.len(), 1);
139 assert_eq!(packets[0].tag(), Tag::Signature);
140
141 // Assert that it can't be imported.
142 assert!(sq.cert_import_maybe(&sig_file).is_err());
143
144
145 // We should be able to import a bare revocation certificate.
146
147 // Assert that the certificate is not revoked.
148 let cert = sq.cert_export(cert.key_handle());
149 assert!(! matches!(
150 cert.revocation_status(STANDARD_POLICY, sq.now()),
151 RevocationStatus::Revoked(_)));
152
153 // Import the revocation certificate.
154 let rev_file = sq.scratch_file("rev");
155 Packet::from(rev).serialize(&mut File::create(&rev_file)?)?;
156 sq.cert_import(rev_file);
157
158 // Assert that the certificate is now revoked.
159 let cert = sq.cert_export(cert.key_handle());
160 assert!(matches!(
161 cert.revocation_status(STANDARD_POLICY, sq.now()),
162 RevocationStatus::Revoked(_)));
163
164 Ok(())
165 }