2024-07-11 20:04:15 -06:00
|
|
|
mod consts;
|
|
|
|
|
2024-07-08 13:40:07 -06:00
|
|
|
use anyhow::Context;
|
|
|
|
use assert_cmd::prelude::*;
|
2024-07-11 20:04:15 -06:00
|
|
|
use consts::*;
|
2024-07-08 13:40:07 -06:00
|
|
|
use predicates::prelude::*;
|
|
|
|
use std::process::Command;
|
2024-07-11 20:04:15 -06:00
|
|
|
use tempfile::TempDir;
|
2024-07-08 13:40:07 -06:00
|
|
|
|
2024-07-08 15:45:26 -06:00
|
|
|
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> {
|
2024-07-11 20:38:57 -06:00
|
|
|
Command::cargo_bin("naja")?
|
|
|
|
.env("NAJA_DATA_DIR", datadir)
|
2024-07-11 20:04:15 -06:00
|
|
|
.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(())
|
2024-07-08 15:45:26 -06:00
|
|
|
}
|
|
|
|
|
2024-07-08 13:40:07 -06:00
|
|
|
#[test]
|
2024-07-08 15:45:26 -06:00
|
|
|
fn help_works() -> Result<(), anyhow::Error> {
|
2024-07-11 20:04:15 -06:00
|
|
|
let tempdir = TempDir::new()?;
|
|
|
|
let datadir = tempdir
|
|
|
|
.path()
|
|
|
|
.to_str()
|
|
|
|
.context("Tempdir path was not valid utf8")?;
|
|
|
|
|
2024-07-11 20:38:57 -06:00
|
|
|
Command::cargo_bin("naja")?
|
|
|
|
.env("NAJA_DATA_DIR", datadir)
|
2024-07-11 20:04:15 -06:00
|
|
|
.arg("--help")
|
|
|
|
.assert()
|
|
|
|
.success()
|
|
|
|
.stdout(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION")));
|
|
|
|
|
2024-07-11 20:38:57 -06:00
|
|
|
Command::cargo_bin("naja")?
|
|
|
|
.env("NAJA_DATA_DIR", datadir)
|
2024-07-11 20:04:15 -06:00
|
|
|
.arg("-h")
|
|
|
|
.assert()
|
|
|
|
.success()
|
|
|
|
.stdout(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION")));
|
|
|
|
|
2024-07-11 20:38:57 -06:00
|
|
|
Command::cargo_bin("naja")?
|
|
|
|
.env("NAJA_DATA_DIR", datadir)
|
2024-07-11 20:04:15 -06:00
|
|
|
.arg("help")
|
|
|
|
.assert()
|
|
|
|
.success()
|
|
|
|
.stdout(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION")));
|
|
|
|
|
2024-07-11 20:38:57 -06:00
|
|
|
Command::cargo_bin("naja")?
|
|
|
|
.env("NAJA_DATA_DIR", datadir)
|
2024-07-11 20:04:15 -06:00
|
|
|
.assert()
|
|
|
|
.code(2)
|
|
|
|
.stderr(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION")));
|
|
|
|
|
|
|
|
Ok(())
|
2024-07-08 13:40:07 -06:00
|
|
|
}
|
2024-07-08 15:45:26 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn keys_generate_works() -> Result<(), anyhow::Error> {
|
2024-07-11 20:04:15 -06:00
|
|
|
let tempdir = TempDir::new()?;
|
|
|
|
let datadir = tempdir
|
|
|
|
.path()
|
|
|
|
.to_str()
|
|
|
|
.context("Tempdir path was not valid utf8")?;
|
2024-07-08 15:45:26 -06:00
|
|
|
|
2024-07-11 20:04:15 -06:00
|
|
|
assert_key_generated(datadir)
|
2024-07-08 15:45:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn keys_list_works() -> Result<(), anyhow::Error> {
|
2024-07-11 20:04:15 -06:00
|
|
|
let tempdir = TempDir::new()?;
|
|
|
|
let datadir = tempdir
|
|
|
|
.path()
|
|
|
|
.to_str()
|
|
|
|
.context("Tempdir path was not valid utf8")?;
|
|
|
|
|
|
|
|
assert_key_generated(datadir)?;
|
|
|
|
|
2024-07-11 20:38:57 -06:00
|
|
|
Command::cargo_bin("naja")?
|
|
|
|
.env("NAJA_DATA_DIR", datadir)
|
2024-07-11 20:04:15 -06:00
|
|
|
.arg("keys")
|
|
|
|
.arg("list")
|
|
|
|
.assert()
|
|
|
|
.success()
|
|
|
|
.stdout(predicate::str::contains(KEY_ALIAS));
|
|
|
|
|
|
|
|
Ok(())
|
2024-07-08 15:45:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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> {
|
2024-07-11 20:04:15 -06:00
|
|
|
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"] {
|
2024-07-11 20:38:57 -06:00
|
|
|
Command::cargo_bin("naja")?
|
|
|
|
.env("NAJA_DATA_DIR", datadir)
|
2024-07-11 20:04:15 -06:00
|
|
|
.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),
|
|
|
|
] {
|
2024-07-11 20:38:57 -06:00
|
|
|
Command::cargo_bin("naja")?
|
|
|
|
.env("NAJA_DATA_DIR", datadir)
|
2024-07-11 20:04:15 -06:00
|
|
|
.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)),
|
|
|
|
);
|
|
|
|
|
2024-07-11 20:38:57 -06:00
|
|
|
Command::cargo_bin("naja")?
|
|
|
|
.env("NAJA_DATA_DIR", datadir)
|
2024-07-11 20:04:15 -06:00
|
|
|
.arg("keys")
|
|
|
|
.arg("list")
|
|
|
|
.assert()
|
|
|
|
.success()
|
|
|
|
.stdout(predicate::str::contains(TEST_FINGERPRINT));
|
|
|
|
|
2024-07-11 20:38:57 -06:00
|
|
|
Command::cargo_bin("naja")?
|
|
|
|
.env("NAJA_DATA_DIR", datadir)
|
2024-07-11 20:04:15 -06:00
|
|
|
.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(())
|
2024-07-08 15:45:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn keys_delete_works() -> Result<(), anyhow::Error> {
|
2024-07-11 20:04:15 -06:00
|
|
|
let tempdir = TempDir::new()?;
|
|
|
|
let datadir = tempdir
|
|
|
|
.path()
|
|
|
|
.to_str()
|
|
|
|
.context("Tempdir path was not valid utf8")?;
|
|
|
|
|
|
|
|
assert_key_generated(datadir)?;
|
|
|
|
|
2024-07-11 20:38:57 -06:00
|
|
|
Command::cargo_bin("naja")?
|
|
|
|
.env("NAJA_DATA_DIR", datadir)
|
2024-07-11 20:04:15 -06:00
|
|
|
.arg("keys")
|
|
|
|
.arg("delete")
|
|
|
|
.arg("--no-confirm")
|
|
|
|
.arg(KEY_ALIAS)
|
|
|
|
.assert()
|
|
|
|
.success()
|
|
|
|
.stdout(predicate::str::contains(
|
|
|
|
"Successfully deleted key with fingerprint ",
|
|
|
|
));
|
|
|
|
|
2024-07-11 20:38:57 -06:00
|
|
|
Command::cargo_bin("naja")?
|
|
|
|
.env("NAJA_DATA_DIR", datadir)
|
2024-07-11 20:04:15 -06:00
|
|
|
.arg("keys")
|
|
|
|
.arg("list")
|
|
|
|
.assert()
|
|
|
|
.success()
|
|
|
|
.stdout(predicate::str::contains("Saved keys (0 total):"));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|