From 030127405405c63fee036226e05003baa8e525d1 Mon Sep 17 00:00:00 2001 From: Dorian Date: Tue, 10 Mar 2026 23:55:11 +0000 Subject: [PATCH] test: add identity module unit tests (9 test cases) Co-Authored-By: Claude Opus 4.6 --- core/archipelago/src/identity.rs | 91 ++++++++++++++++++++++++++++++++ loop/plan.md | 2 +- 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/core/archipelago/src/identity.rs b/core/archipelago/src/identity.rs index e3fbc884..13c092be 100644 --- a/core/archipelago/src/identity.rs +++ b/core/archipelago/src/identity.rs @@ -120,3 +120,94 @@ pub fn did_key_from_pubkey_hex(pubkey_hex: &str) -> Result { multicodec_pubkey[2..34].copy_from_slice(&bytes); Ok(format!("did:key:z{}", bs58::encode(multicodec_pubkey).into_string())) } + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn test_load_or_create_generates_new_identity() { + let dir = tempfile::tempdir().unwrap(); + let identity_dir = dir.path().join("identity"); + + let identity = NodeIdentity::load_or_create(&identity_dir).await.unwrap(); + + // pubkey_hex should be 64 hex chars (32 bytes) + assert_eq!(identity.pubkey_hex().len(), 64); + // node_id should be first 16 chars of pubkey_hex + assert_eq!(identity.node_id(), &identity.pubkey_hex()[..16]); + } + + #[tokio::test] + async fn test_load_or_create_persists_and_reloads() { + let dir = tempfile::tempdir().unwrap(); + let identity_dir = dir.path().join("identity"); + + let identity1 = NodeIdentity::load_or_create(&identity_dir).await.unwrap(); + let pubkey1 = identity1.pubkey_hex(); + + let identity2 = NodeIdentity::load_or_create(&identity_dir).await.unwrap(); + let pubkey2 = identity2.pubkey_hex(); + + assert_eq!(pubkey1, pubkey2); + } + + #[tokio::test] + async fn test_sign_and_verify() { + let dir = tempfile::tempdir().unwrap(); + let identity = NodeIdentity::load_or_create(&dir.path().join("id")).await.unwrap(); + + let data = b"hello world"; + let sig = identity.sign(data); + + let valid = NodeIdentity::verify(&identity.pubkey_hex(), data, &sig).unwrap(); + assert!(valid); + } + + #[tokio::test] + async fn test_verify_wrong_data() { + let dir = tempfile::tempdir().unwrap(); + let identity = NodeIdentity::load_or_create(&dir.path().join("id")).await.unwrap(); + + let sig = identity.sign(b"hello"); + let valid = NodeIdentity::verify(&identity.pubkey_hex(), b"wrong", &sig).unwrap(); + assert!(!valid); + } + + #[tokio::test] + async fn test_did_key_format() { + let dir = tempfile::tempdir().unwrap(); + let identity = NodeIdentity::load_or_create(&dir.path().join("id")).await.unwrap(); + + let did = identity.did_key(); + assert!(did.starts_with("did:key:z")); + } + + #[test] + fn test_did_key_from_pubkey_hex() { + // 32-byte all-zeros pubkey in hex + let hex = "0000000000000000000000000000000000000000000000000000000000000000"; + let did = did_key_from_pubkey_hex(hex).unwrap(); + assert!(did.starts_with("did:key:z")); + } + + #[test] + fn test_did_key_from_invalid_hex() { + assert!(did_key_from_pubkey_hex("not_hex").is_err()); + } + + #[test] + fn test_did_key_from_wrong_length() { + assert!(did_key_from_pubkey_hex("0011").is_err()); + } + + #[tokio::test] + async fn test_node_address_format() { + let dir = tempfile::tempdir().unwrap(); + let identity = NodeIdentity::load_or_create(&dir.path().join("id")).await.unwrap(); + + let addr = identity.node_address("abc123.onion"); + assert!(addr.starts_with("archipelago://abc123.onion#")); + assert!(addr.contains(&identity.pubkey_hex())); + } +} diff --git a/loop/plan.md b/loop/plan.md index 4c19fcf4..b0f21f41 100644 --- a/loop/plan.md +++ b/loop/plan.md @@ -30,7 +30,7 @@ - [x] **TEST-07** — Create backend unit tests: auth module. Add `#[cfg(test)] mod tests` to `core/archipelago/src/auth.rs` testing: password hash/verify, session creation/validation/expiry, rate limiting. Target: 6+ test cases. Run on dev server with `cargo test -p archipelago`. **Acceptance**: all pass. -- [ ] **TEST-08** — Create backend unit tests: identity module. Add tests to `core/archipelago/src/identity.rs` testing: DID key generation, challenge signing/verification, pubkey hex conversion. Target: 5+ test cases. **Acceptance**: all pass on dev server. +- [x] **TEST-08** — Create backend unit tests: identity module. Add tests to `core/archipelago/src/identity.rs` testing: DID key generation, challenge signing/verification, pubkey hex conversion. Target: 5+ test cases. **Acceptance**: all pass on dev server. - [ ] **TEST-09** — Add CI-compatible test runner script. Create `scripts/run-tests.sh` that runs frontend tests locally (`cd neode-ui && npm test`) and backend tests on dev server via SSH. Reports pass/fail for both. **Acceptance**: script runs end-to-end, exit 0 when all pass.