test: add identity module unit tests (9 test cases)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Dorian 2026-03-10 23:55:11 +00:00
parent 615ce4f939
commit 0301274054
2 changed files with 92 additions and 1 deletions

View File

@ -120,3 +120,94 @@ pub fn did_key_from_pubkey_hex(pubkey_hex: &str) -> Result<String> {
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()));
}
}

View File

@ -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.