test: add identity module unit tests (9 test cases)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user