Files
Linux-Hello/tests/integration/ipc_test.rs
2026-01-30 09:44:12 +01:00

213 lines
6.2 KiB
Rust

//! IPC Integration Tests
//!
//! Tests for the IPC communication between daemon and clients.
use linux_hello_daemon::ipc::{IpcRequest, IpcResponse, IpcServer};
/// Test IPC request serialization
#[test]
fn test_authenticate_request_serialization() {
let request = IpcRequest::Authenticate {
user: "testuser".to_string(),
};
let json = serde_json::to_string(&request).unwrap();
assert!(json.contains("\"action\":\"authenticate\""));
assert!(json.contains("\"user\":\"testuser\""));
// Deserialize back
let parsed: IpcRequest = serde_json::from_str(&json).unwrap();
match parsed {
IpcRequest::Authenticate { user } => {
assert_eq!(user, "testuser");
}
_ => panic!("Expected Authenticate request"),
}
}
/// Test enroll request serialization
#[test]
fn test_enroll_request_serialization() {
let request = IpcRequest::Enroll {
user: "testuser".to_string(),
label: "default".to_string(),
frame_count: 5,
};
let json = serde_json::to_string(&request).unwrap();
assert!(json.contains("\"action\":\"enroll\""));
assert!(json.contains("\"user\":\"testuser\""));
assert!(json.contains("\"label\":\"default\""));
// Deserialize back
let parsed: IpcRequest = serde_json::from_str(&json).unwrap();
match parsed {
IpcRequest::Enroll {
user,
label,
frame_count,
} => {
assert_eq!(user, "testuser");
assert_eq!(label, "default");
assert_eq!(frame_count, 5);
}
_ => panic!("Expected Enroll request"),
}
}
/// Test list request serialization
#[test]
fn test_list_request_serialization() {
let request = IpcRequest::List {
user: "testuser".to_string(),
};
let json = serde_json::to_string(&request).unwrap();
assert!(json.contains("\"action\":\"list\""));
let parsed: IpcRequest = serde_json::from_str(&json).unwrap();
match parsed {
IpcRequest::List { user } => {
assert_eq!(user, "testuser");
}
_ => panic!("Expected List request"),
}
}
/// Test remove request serialization
#[test]
fn test_remove_request_serialization() {
// Remove specific label
let request = IpcRequest::Remove {
user: "testuser".to_string(),
label: Some("default".to_string()),
all: false,
};
let json = serde_json::to_string(&request).unwrap();
let parsed: IpcRequest = serde_json::from_str(&json).unwrap();
match parsed {
IpcRequest::Remove { user, label, all } => {
assert_eq!(user, "testuser");
assert_eq!(label, Some("default".to_string()));
assert!(!all);
}
_ => panic!("Expected Remove request"),
}
// Remove all
let request = IpcRequest::Remove {
user: "testuser".to_string(),
label: None,
all: true,
};
let json = serde_json::to_string(&request).unwrap();
let parsed: IpcRequest = serde_json::from_str(&json).unwrap();
match parsed {
IpcRequest::Remove { user, label, all } => {
assert_eq!(user, "testuser");
assert!(label.is_none());
assert!(all);
}
_ => panic!("Expected Remove request"),
}
}
/// Test ping request serialization
#[test]
fn test_ping_request_serialization() {
let request = IpcRequest::Ping;
let json = serde_json::to_string(&request).unwrap();
assert!(json.contains("\"action\":\"ping\""));
let parsed: IpcRequest = serde_json::from_str(&json).unwrap();
match parsed {
IpcRequest::Ping => {}
_ => panic!("Expected Ping request"),
}
}
/// Test response serialization
#[test]
fn test_response_serialization() {
// Success response with confidence
let response = IpcResponse {
success: true,
message: Some("Authentication successful".to_string()),
confidence: Some(0.95),
templates: None,
};
let json = serde_json::to_string(&response).unwrap();
assert!(json.contains("\"success\":true"));
assert!(json.contains("\"confidence\":0.95"));
// Deserialize
let parsed: IpcResponse = serde_json::from_str(&json).unwrap();
assert!(parsed.success);
assert_eq!(parsed.confidence, Some(0.95));
// Response with templates
let response = IpcResponse {
success: true,
message: None,
confidence: None,
templates: Some(vec!["default".to_string(), "glasses".to_string()]),
};
let json = serde_json::to_string(&response).unwrap();
let parsed: IpcResponse = serde_json::from_str(&json).unwrap();
assert_eq!(
parsed.templates,
Some(vec!["default".to_string(), "glasses".to_string()])
);
}
/// Test error response serialization
#[test]
fn test_error_response_serialization() {
let response = IpcResponse {
success: false,
message: Some("User not enrolled".to_string()),
confidence: None,
templates: None,
};
let json = serde_json::to_string(&response).unwrap();
assert!(json.contains("\"success\":false"));
assert!(json.contains("User not enrolled"));
let parsed: IpcResponse = serde_json::from_str(&json).unwrap();
assert!(!parsed.success);
assert!(parsed.message.unwrap().contains("not enrolled"));
}
/// Test default socket path
#[test]
fn test_default_socket_path() {
let path = IpcServer::default_socket_path();
assert_eq!(path.to_str().unwrap(), "/run/linux-hello/auth.sock");
}
/// Test JSON protocol compatibility with PAM module
#[test]
fn test_pam_protocol_compatibility() {
// Test that the JSON format matches what the PAM module expects
let auth_request = r#"{"action":"authenticate","user":"testuser"}"#;
let parsed: IpcRequest = serde_json::from_str(auth_request).unwrap();
match parsed {
IpcRequest::Authenticate { user } => {
assert_eq!(user, "testuser");
}
_ => panic!("Failed to parse PAM-format request"),
}
// Test response format
let success_response =
r#"{"success":true,"message":"Authentication successful","confidence":1.0}"#;
let parsed: IpcResponse = serde_json::from_str(success_response).unwrap();
assert!(parsed.success);
}