use std::fs::{self, File};
use std::io;
extern crate assert_cli;
use assert_cli::Assert;
extern crate tempfile;
use tempfile::TempDir;
extern crate sequoia_openpgp as openpgp;
use crate::openpgp::{Packet, PacketPile, TPK};
use crate::openpgp::crypto::KeyPair;
use crate::openpgp::packet::key::SecretKey;
use crate::openpgp::constants::{CompressionAlgorithm, DataFormat, SignatureType};
use crate::openpgp::parse::Parse;
use crate::openpgp::serialize::stream::{Message, Signer, Compressor, LiteralWriter};
fn p(filename: &str) -> String {
format!("../openpgp/tests/data/{}", filename)
}
#[test]
fn sq_sign() {
let tmp_dir = TempDir::new().unwrap();
let sig = tmp_dir.path().join("sig0");
// Sign message.
Assert::cargo_binary("sq")
.with_args(
&["--home",
&tmp_dir.path().to_string_lossy(),
"sign",
"--secret-key-file",
&p("keys/dennis-simon-anton-private.pgp"),
"--output",
&sig.to_string_lossy(),
&p("messages/a-cypherpunks-manifesto.txt")])
.unwrap();
// Check that the content is sane.
let packets: Vec<Packet> =
PacketPile::from_file(&sig).unwrap().into_children().collect();
assert_eq!(packets.len(), 3);
if let Packet::OnePassSig(ref ops) = packets[0] {
assert!(ops.last());
assert_eq!(ops.typ(), SignatureType::Binary);
} else {
panic!("expected one pass signature");
}
if let Packet::Literal(_) = packets[1] {
// Do nothing.
} else {
panic!("expected literal");
}
if let Packet::Signature(ref sig) = packets[2] {
assert_eq!(sig.typ(), SignatureType::Binary);
} else {
panic!("expected signature");
}
let content = fs::read(&sig).unwrap();
assert!(&content[..].starts_with(b"-----BEGIN PGP MESSAGE-----\n\n"));
// Verify signed message.
Assert::cargo_binary("sq")
.with_args(
&["--home",
&tmp_dir.path().to_string_lossy(),
"verify",
"--public-key-file",
&p("keys/dennis-simon-anton.pgp"),
&sig.to_string_lossy()])
.unwrap();
}
#[test]
fn sq_sign_append() {
let tmp_dir = TempDir::new().unwrap();
let sig0 = tmp_dir.path().join("sig0");
// Sign message.
Assert::cargo_binary("sq")
.with_args(
&["--home",
&tmp_dir.path().to_string_lossy(),
"sign",
"--secret-key-file",
&p("keys/dennis-simon-anton-private.pgp"),
"--output",
&sig0.to_string_lossy(),
&p("messages/a-cypherpunks-manifesto.txt")])
.unwrap();
// Check that the content is sane.
let packets: Vec<Packet> =
PacketPile::from_file(&sig0).unwrap().into_children().collect();
assert_eq!(packets.len(), 3);
if let Packet::OnePassSig(ref ops) = packets[0]