use bincode_next::{Decode, Encode};
#[derive(Clone, Debug, Decode, Encode)]
pub struct AgentIdentityInfo {
pub algorithm: String,
pub fingerprint: String,
pub comment: String,
}
#[derive(Clone, Debug, Decode, Encode)]
pub enum AgentRequest {
ListIdentities,
ListSupportedIdentities {
supported_algorithms: Vec<String>,
},
GetPublicKey(String),
Sign {
fingerprint: String,
data: Vec<u8>,
},
AddIdentity {
key_path: String,
passphrase: Option<String>,
},
RemoveIdentity(String),
RemoveAllIdentities,
Lock,
Unlock(String),
Shutdown,
Status,
}
#[derive(Clone, Debug, Decode, Encode)]
pub enum AgentResponse {
Identities(Vec<AgentIdentityInfo>),
PublicKey(Vec<u8>),
Signature(Vec<u8>),
Ok,
Error(String),
AgentStatus {
locked: bool,
identities: Vec<AgentIdentityInfo>,
},
}
#[cfg(test)]
mod tests {
use bincode_next::{config::standard, decode_from_slice, encode_to_vec};
use super::{AgentIdentityInfo, AgentRequest, AgentResponse};
#[test]
fn roundtrip_request_lock() -> anyhow::Result<()> {
let encoded = encode_to_vec(&AgentRequest::Lock, standard())?;
let (rt, _): (AgentRequest, _) = decode_from_slice(&encoded, standard())?;
assert!(matches!(rt, AgentRequest::Lock));
Ok(())
}
#[test]
fn roundtrip_request_unlock() -> anyhow::Result<()> {
let encoded = encode_to_vec(AgentRequest::Unlock("secret".to_string()), standard())?;
let (rt, _): (AgentRequest, _) = decode_from_slice(&encoded, standard())?;
assert!(matches!(rt, AgentRequest::Unlock(ref s) if s == "secret"));
Ok(())
}
#[test]
fn roundtrip_request_remove_all() -> anyhow::Result<()> {
let encoded = encode_to_vec(&AgentRequest::RemoveAllIdentities, standard())?;
let (rt, _): (AgentRequest, _) = decode_from_slice(&encoded, standard())?;
assert!(matches!(rt, AgentRequest::RemoveAllIdentities));
Ok(())
}
#[test]
fn roundtrip_request_shutdown() -> anyhow::Result<()> {
let encoded = encode_to_vec(&AgentRequest::Shutdown, standard())?;
let (rt, _): (AgentRequest, _) = decode_from_slice(&encoded, standard())?;
assert!(matches!(rt, AgentRequest::Shutdown));
Ok(())
}
#[test]
fn roundtrip_response_ok() -> anyhow::Result<()> {
let encoded = encode_to_vec(&AgentResponse::Ok, standard())?;
let (rt, _): (AgentResponse, _) = decode_from_slice(&encoded, standard())?;
assert!(matches!(rt, AgentResponse::Ok));
Ok(())
}
#[test]
fn agent_identity_info_clone_and_debug() {
let info = AgentIdentityInfo {
algorithm: "P384".to_string(),
fingerprint: "SHA256:abcd".to_string(),
comment: "user@host".to_string(),
};
let cloned = info.clone();
assert_eq!(cloned.algorithm, "P384");
let debug_str = format!("{info:?}");
assert!(debug_str.contains("P384"));
}
}