From 19fbb51d128d307a7ea005cf820e9db337a9d0d3 Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 16 Jan 2025 04:20:12 -0500 Subject: [PATCH] keyfork-tests: initial commit. also, fixup test_util's Panicable to not be generic. it's always unit type --- Cargo.lock | 90 ++++++++++++++++++++++ Cargo.toml | 1 + crates/daemon/keyforkd/src/test_util.rs | 6 +- crates/tests/Cargo.toml | 11 +++ crates/tests/src/keyfork/derive/mod.rs | 1 + crates/tests/src/keyfork/derive/openpgp.rs | 58 ++++++++++++++ crates/tests/src/keyfork/mod.rs | 1 + crates/tests/src/lib.rs | 2 + 8 files changed, 167 insertions(+), 3 deletions(-) create mode 100644 crates/tests/Cargo.toml create mode 100644 crates/tests/src/keyfork/derive/mod.rs create mode 100644 crates/tests/src/keyfork/derive/openpgp.rs create mode 100644 crates/tests/src/keyfork/mod.rs create mode 100644 crates/tests/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index a035c46..e8abfb4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -147,6 +147,22 @@ dependencies = [ "term", ] +[[package]] +name = "assert_cmd" +version = "2.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1835b7f27878de8525dc71410b5a31cdcc5f230aed5ba5df968e09c201b23d" +dependencies = [ + "anstyle", + "bstr", + "doc-comment", + "libc", + "predicates", + "predicates-core", + "predicates-tree", + "wait-timeout", +] + [[package]] name = "async-channel" version = "1.9.0" @@ -466,6 +482,17 @@ dependencies = [ "cipher", ] +[[package]] +name = "bstr" +version = "1.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "531a9155a481e2ee699d4f98f43c0ca4ff8ee1bfd55c31e9e98fb29d2b176fe0" +dependencies = [ + "memchr", + "regex-automata 0.4.9", + "serde", +] + [[package]] name = "buffered-reader" version = "1.3.1" @@ -889,6 +916,12 @@ dependencies = [ "cipher", ] +[[package]] +name = "difflib" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" + [[package]] name = "digest" version = "0.10.7" @@ -933,6 +966,12 @@ dependencies = [ "syn", ] +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + [[package]] name = "dsa" version = "0.6.3" @@ -1936,6 +1975,15 @@ dependencies = [ "smex", ] +[[package]] +name = "keyfork-tests" +version = "0.1.0" +dependencies = [ + "assert_cmd", + "keyforkd", + "sequoia-openpgp", +] + [[package]] name = "keyfork-zbar" version = "0.1.1" @@ -2661,6 +2709,33 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" +[[package]] +name = "predicates" +version = "3.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5d19ee57562043d37e82899fade9a22ebab7be9cef5026b07fda9cdd4293573" +dependencies = [ + "anstyle", + "difflib", + "predicates-core", +] + +[[package]] +name = "predicates-core" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "727e462b119fe9c93fd0eb1429a5f7647394014cf3c04ab2c0350eeb09095ffa" + +[[package]] +name = "predicates-tree" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72dd2d6d381dfb73a193c7fca536518d7caee39fc8503f74e7dc0be0531b425c" +dependencies = [ + "predicates-core", + "termtree", +] + [[package]] name = "prettyplease" version = "0.2.25" @@ -3292,6 +3367,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "termtree" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f50febec83f5ee1df3015341d8bd429f2d1cc62bcba7ea2076759d315084683" + [[package]] name = "thiserror" version = "1.0.69" @@ -3600,6 +3681,15 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "wait-timeout" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" +dependencies = [ + "libc", +] + [[package]] name = "walkdir" version = "2.5.0" diff --git a/Cargo.toml b/Cargo.toml index c2dfd82..c8fd9b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ members = [ "crates/util/keyfork-prompt", "crates/util/keyfork-slip10-test-data", "crates/util/smex", + "crates/tests", ] [workspace.dependencies] diff --git a/crates/daemon/keyforkd/src/test_util.rs b/crates/daemon/keyforkd/src/test_util.rs index a926ff0..c2af309 100644 --- a/crates/daemon/keyforkd/src/test_util.rs +++ b/crates/daemon/keyforkd/src/test_util.rs @@ -26,7 +26,7 @@ pub enum UninstantiableError {} /// }; /// assert!(closure().is_ok()); /// ``` -pub type Panicable = std::result::Result; +pub type Panicable = std::result::Result<(), UninstantiableError>; /// Run a test making use of a Keyforkd server. The test may use a seed (the first argument) from a /// test suite, or (as shown in the example below) a simple seed may be used solely to ensure @@ -62,9 +62,9 @@ pub type Panicable = std::result::Result; /// }).unwrap(); /// ``` #[allow(clippy::missing_errors_doc)] -pub fn run_test(seed: &[u8], closure: F) -> Result<(), E> +pub fn run_test(seed: &[u8], closure: F) -> std::result::Result<(), E> where - F: FnOnce(&std::path::Path) -> Result<(), E> + Send + 'static, + F: FnOnce(&std::path::Path) -> std::result::Result<(), E> + Send + 'static, E: Send + 'static, { let rt = Builder::new_multi_thread() diff --git a/crates/tests/Cargo.toml b/crates/tests/Cargo.toml new file mode 100644 index 0000000..92d9fc6 --- /dev/null +++ b/crates/tests/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "keyfork-tests" +version = "0.1.0" +edition = "2021" +publish = false +license = "MIT" + +[dependencies] +assert_cmd = "2.0.16" +keyforkd = { workspace = true, features = ["default"] } +sequoia-openpgp = { workspace = true, features = ["crypto-nettle"] } diff --git a/crates/tests/src/keyfork/derive/mod.rs b/crates/tests/src/keyfork/derive/mod.rs new file mode 100644 index 0000000..fc28427 --- /dev/null +++ b/crates/tests/src/keyfork/derive/mod.rs @@ -0,0 +1 @@ +mod openpgp; diff --git a/crates/tests/src/keyfork/derive/openpgp.rs b/crates/tests/src/keyfork/derive/openpgp.rs new file mode 100644 index 0000000..a677b9a --- /dev/null +++ b/crates/tests/src/keyfork/derive/openpgp.rs @@ -0,0 +1,58 @@ +use sequoia_openpgp as openpgp; + +use assert_cmd::Command; +use openpgp::{ + parse::{PacketParser, Parse}, + policy::StandardPolicy, + types::KeyFlags, + Cert, +}; + +const KEYFORK_BIN: &str = "keyfork"; + +#[test] +fn test() { + let policy = StandardPolicy::new(); + + let command_output = Command::cargo_bin(KEYFORK_BIN) + .unwrap() + .args([ + "derive", + "openpgp", + "Ryan Heywood (RyanSquared) ", + ]) + .assert() + .success(); + + let packets = PacketParser::from_bytes(&command_output.get_output().stdout).unwrap(); + let cert = Cert::try_from(packets).unwrap(); + + // assert the cert contains _any_ secret key data + assert!( + cert.is_tsk(), + "exported key should contain secret key data, indicated by the key being a TSK" + ); + + // assert the correct keys were added in the correct order + let mut key_formats = std::collections::HashSet::from([ + KeyFlags::empty().set_certification(), + KeyFlags::empty().set_signing(), + KeyFlags::empty() + .set_transport_encryption() + .set_storage_encryption(), + KeyFlags::empty().set_authentication(), + ]); + let valid_cert = cert.with_policy(&policy, None).unwrap(); + for key in valid_cert.keys() { + let flags = key.key_flags().unwrap(); + assert!( + key_formats.remove(&flags), + "could not find key flag set: {flags:?}" + ); + key.alive().expect("is live after being generated"); + key.parts_into_secret().expect("has secret keys"); + } + if !key_formats.is_empty() { + panic!("remaining key formats: {key_formats:?}"); + } +} diff --git a/crates/tests/src/keyfork/mod.rs b/crates/tests/src/keyfork/mod.rs new file mode 100644 index 0000000..bc7c83c --- /dev/null +++ b/crates/tests/src/keyfork/mod.rs @@ -0,0 +1 @@ +mod derive; diff --git a/crates/tests/src/lib.rs b/crates/tests/src/lib.rs new file mode 100644 index 0000000..dbd00d1 --- /dev/null +++ b/crates/tests/src/lib.rs @@ -0,0 +1,2 @@ +#[cfg(test)] +mod keyfork;