mod consts; use anyhow::Context; use assert_cmd::prelude::*; use consts::*; use predicates::prelude::*; use std::process::Command; use tempfile::TempDir; static KEY_ALIAS: &str = "TESTKEY"; static KEY_PASSWORD: &str = "TESTKEYPASSWORD"; // TODO: Also test ed25519 key generation fn assert_key_generated(datadir: &str) -> Result<(), anyhow::Error> { Command::cargo_bin("naja")? .env("NAJA_DATA_DIR", datadir) .arg("keys") .arg("generate") .env("KEY_PASSWORD", KEY_PASSWORD) .arg("es256") .arg("--key-alias") .arg(KEY_ALIAS) .assert() .success() .stdout(predicate::str::starts_with( "Successfully generated a new key!", )); Ok(()) } #[test] fn help_works() -> Result<(), anyhow::Error> { let tempdir = TempDir::new()?; let datadir = tempdir .path() .to_str() .context("Tempdir path was not valid utf8")?; Command::cargo_bin("naja")? .env("NAJA_DATA_DIR", datadir) .arg("--help") .assert() .success() .stdout(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION"))); Command::cargo_bin("naja")? .env("NAJA_DATA_DIR", datadir) .arg("-h") .assert() .success() .stdout(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION"))); Command::cargo_bin("naja")? .env("NAJA_DATA_DIR", datadir) .arg("help") .assert() .success() .stdout(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION"))); Command::cargo_bin("naja")? .env("NAJA_DATA_DIR", datadir) .assert() .code(2) .stderr(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION"))); Ok(()) } #[test] fn keys_generate_works() -> Result<(), anyhow::Error> { let tempdir = TempDir::new()?; let datadir = tempdir .path() .to_str() .context("Tempdir path was not valid utf8")?; assert_key_generated(datadir) } #[test] fn keys_list_works() -> Result<(), anyhow::Error> { let tempdir = TempDir::new()?; let datadir = tempdir .path() .to_str() .context("Tempdir path was not valid utf8")?; assert_key_generated(datadir)?; Command::cargo_bin("naja")? .env("NAJA_DATA_DIR", datadir) .arg("keys") .arg("list") .assert() .success() .stdout(predicate::str::contains(KEY_ALIAS)); Ok(()) } // This test takes a bit due to testing each export format individually, causing the password to be hashed multiple times #[test] fn keys_export_works() -> Result<(), anyhow::Error> { let tempdir = TempDir::new()?; let datadir = tempdir .path() .to_str() .context("Tempdir path was not valid utf8")?; assert_key_generated(datadir)?; for export_format in ["pkcs8", "asp-tool", "jwk"] { Command::cargo_bin("naja")? .env("NAJA_DATA_DIR", datadir) .arg("keys") .arg("export") .env("KEY_PASSWORD", KEY_PASSWORD) .arg(export_format) .arg(KEY_ALIAS) .assert() .success() .stderr(predicate::str::contains("Exported key \"")); } Ok(()) } #[test] fn keys_import_works() -> Result<(), anyhow::Error> { let tempdir = TempDir::new()?; let datadir = tempdir .path() .to_str() .context("Tempdir path was not valid utf8")?; for (export_format, key) in [ ("pkcs8", TEST_KEY_PKCS8), ("asp-tool", TEST_KEY_ASPTOOL), ("jwk", TEST_KEY_JWK), ] { Command::cargo_bin("naja")? .env("NAJA_DATA_DIR", datadir) .arg("keys") .arg("import") .env("KEY_PASSWORD", TEST_KEY_PASSWORD) .arg("--key-alias") .arg(KEY_ALIAS) .arg(export_format) .arg(key) .assert() .success() .stdout( predicate::str::contains("Successfully imported key!") .and(predicate::str::contains(TEST_FINGERPRINT)), ); Command::cargo_bin("naja")? .env("NAJA_DATA_DIR", datadir) .arg("keys") .arg("list") .assert() .success() .stdout(predicate::str::contains(TEST_FINGERPRINT)); Command::cargo_bin("naja")? .env("NAJA_DATA_DIR", datadir) .arg("keys") .arg("delete") .arg("--no-confirm") .arg(TEST_FINGERPRINT) .assert() .success() .stdout(predicate::str::contains(format!( "Successfully deleted key with fingerprint {TEST_FINGERPRINT}" ))); } Ok(()) } #[test] fn keys_delete_works() -> Result<(), anyhow::Error> { let tempdir = TempDir::new()?; let datadir = tempdir .path() .to_str() .context("Tempdir path was not valid utf8")?; assert_key_generated(datadir)?; Command::cargo_bin("naja")? .env("NAJA_DATA_DIR", datadir) .arg("keys") .arg("delete") .arg("--no-confirm") .arg(KEY_ALIAS) .assert() .success() .stdout(predicate::str::contains( "Successfully deleted key with fingerprint ", )); Command::cargo_bin("naja")? .env("NAJA_DATA_DIR", datadir) .arg("keys") .arg("list") .assert() .success() .stdout(predicate::str::contains("Saved keys (0 total):")); Ok(()) }