use std::fs::{self, File};
use std::io;
use assert_cli;
use assert_cli::Assert;
use tempfile;
use tempfile::TempDir;
use sequoia_openpgp as openpgp;
use crate::openpgp::{Packet, PacketPile, Cert};
use crate::openpgp::crypto::KeyPair;
use crate::openpgp::packet::key::SecretKeyMaterial;
use crate::openpgp::types::{CompressionAlgorithm, SignatureType};
use crate::openpgp::parse::Parse;
use crate::openpgp::serialize::stream::{Message, Signer, Compressor, LiteralWriter};
fn artifact(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(
&["sign",
"--secret-key-file",
&artifact("keys/dennis-simon-anton-private.pgp"),
"--output",
&sig.to_string_lossy(),
&artifact("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(
&["verify",
"--sender-cert-file",
&artifact("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(
&["sign",
"--secret-key-file",
&artifact("keys/dennis-simon-anton-private.pgp"),
"--output",
&sig0.to_string_lossy(),
&artifact("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] {
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 <