use std::fs::File;
use std::io::Write;
use anyhow::Context;
use assert_cmd::Command;
use chrono::prelude::*;
use chrono::Duration;
use tempfile::TempDir;
use sequoia_openpgp as openpgp;
use openpgp::Result;
use openpgp::cert::prelude::*;
use openpgp::parse::Parse;
use openpgp::Packet;
use openpgp::packet::Key;
use openpgp::packet::UserID;
use openpgp::PacketPile;
use openpgp::policy::StandardPolicy;
use openpgp::serialize::Serialize;
use openpgp::types::SignatureType;
use openpgp::types::ReasonForRevocation;
use openpgp::types::RevocationStatus;
const TRACE: bool = false;
mod integration {
use super::*;
const P: &StandardPolicy = &StandardPolicy::new();
const ALICE: &str = "<alice@example.org>";
#[derive(PartialEq, Eq)]
enum Subcommand {
Certificate,
UserID(Vec<String>),
Subkey,
}
impl Subcommand {
fn userids(&self) -> &[String] {
if let Subcommand::UserID(ref userids) = self {
assert!(userids.len() > 0);
userids
} else {
&[]
}
}
}
// If subkey is not None, then a subkey will be revoked.
//
// Otherwise, USERIDS is a vector of User IDs. If it is empty,
// then a default User ID will be used and the *certificate* will
// be revoked. If it contains at least one entry, then each entry
// will be added as a User ID, and the last User ID will be
// revoked.
fn t(subcommand: &Subcommand,
reason: ReasonForRevocation,
reason_message: &str,
stdin: bool,
third_party: bool,
notations: &[(&str, &str)],
time: Option<DateTime<Utc>>) -> Result<()>
{
// Round it down to a whole second to match the resolution of
// OpenPGP's timestamp.
let time = time.map(|t| {
t - Duration::nanoseconds(t.timestamp_subsec_nanos() as i64)
});
let gen = |userids: &[&str]| {
let mut builder = CertBuilder::new()
.add_signing_subkey()
.set_creation_time(
time.map(|t| (t - Duration::hours(1)).into()));
for &u in userids {
builder = builder.add_userid(u);
}
builder.generate().map(|(key, _rev)| key)
};
let mut userid: Option<&str> = None;
let mut userids: Vec<&str> = vec![ ALICE ];
if let Subcommand::UserID(_) = subcommand {
userids = subcommand.userids().iter()
.map(|u| u.as_str()).collect();
userid = userids.last().map(|u| *u)
}
// We're going to revoke alice's certificate or a User ID. If
// we're doing it via a third-party revocation, then bob is
// the revoker. Otherwise, it's alice.
let alice = gen(&userids)?;
let bob = gen(&[ "<revoker@some.org>" ])?;
let mut cert = Vec::new();
alice.serialize(&mut cert)?;
let mut revoker = Vec::new();
if third_party {
bob.as_tsk().serialize(&mut revoker)?;
} else {
alice.as_tsk().serialize(&mut revoker)?