From b00b591d158bf24679cb50777e00fb2814ecdbf9 Mon Sep 17 00:00:00 2001 From: Tjemmmic Date: Mon, 9 Dec 2024 21:05:04 -0600 Subject: [PATCH] fix: test fixes wip --- .../tests/error_handling_tests.rs | 53 ++++-- .../tests/file_transfer_edge_cases.rs | 60 ++++--- citadel-internal-service/tests/group_chat.rs | 5 +- .../tests/intra_kernel.rs | 79 ++++---- .../tests/kv_store_edge_cases.rs | 161 +++++++++++------ .../tests/peer_auth_edge_cases.rs | 169 +++++++++++------- 6 files changed, 323 insertions(+), 204 deletions(-) diff --git a/citadel-internal-service/tests/error_handling_tests.rs b/citadel-internal-service/tests/error_handling_tests.rs index 3d80433..00039ae 100644 --- a/citadel-internal-service/tests/error_handling_tests.rs +++ b/citadel-internal-service/tests/error_handling_tests.rs @@ -3,23 +3,27 @@ use citadel_internal_service_test_common as common; #[cfg(test)] mod tests { use crate::common::{ - register_and_connect_to_server, server_info_skip_cert_verification, - RegisterAndConnectItems, + register_and_connect_to_server, server_info_skip_cert_verification, RegisterAndConnectItems, }; use citadel_internal_service::kernel::CitadelWorkspaceService; use citadel_internal_service_types::{ InternalServiceRequest, InternalServiceResponse, MessageSendFailure, - PeerConnectFailure, GroupCreateFailure, }; use citadel_sdk::prelude::*; use std::error::Error; use std::net::SocketAddr; use std::time::Duration; use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; - use bytes::BytesMut; use uuid::Uuid; - async fn setup_test_environment() -> Result<(UnboundedSender, UnboundedReceiver, u64), Box> { + async fn setup_test_environment() -> Result< + ( + UnboundedSender, + UnboundedReceiver, + u64, + ), + Box, + > { let (server, server_bind_address) = server_info_skip_cert_verification(); tokio::task::spawn(server); @@ -40,7 +44,7 @@ mod tests { server_addr: server_bind_address, full_name: "Test Peer".to_string(), username: "test.peer".to_string(), - password: "secret".into_bytes().to_owned(), + password: "secret".as_bytes().to_vec(), pre_shared_key: None::, }; @@ -52,14 +56,17 @@ mod tests { #[tokio::test] async fn test_invalid_request_handling() -> Result<(), Box> { crate::common::setup_log(); - let (to_service, mut from_service, _) = setup_test_environment().await?; + let (to_service, mut from_service, cid) = setup_test_environment().await?; // Test invalid peer CID let invalid_cid = 999999; to_service .send(InternalServiceRequest::Message { - peer_cid: invalid_cid, - message: BytesMut::from("test message"), + request_id: Uuid::new_v4(), + cid, + peer_cid: Some(invalid_cid), + message: "test message".as_bytes().to_vec(), + security_level: SecurityLevel::Standard, }) .unwrap(); @@ -111,8 +118,11 @@ mod tests { // Test empty message to_service .send(InternalServiceRequest::Message { - peer_cid: cid, - message: BytesMut::new(), + request_id: Uuid::new_v4(), + cid, + peer_cid: Some(cid), + message: Vec::new(), + security_level: SecurityLevel::Standard, }) .unwrap(); @@ -120,8 +130,11 @@ mod tests { let large_message = vec![b'x'; 10 * 1024 * 1024]; // 10MB to_service .send(InternalServiceRequest::Message { - peer_cid: cid, - message: BytesMut::from(&large_message[..]), + request_id: Uuid::new_v4(), + cid, + peer_cid: Some(cid), + message: large_message.to_vec(), + security_level: SecurityLevel::Standard, }) .unwrap(); @@ -176,8 +189,11 @@ mod tests { for _ in 0..3 { to_service .send(InternalServiceRequest::Message { - peer_cid: 999999, // invalid CID - message: BytesMut::from("test message"), + request_id: Uuid::new_v4(), + cid, + peer_cid: Some(999999), // invalid CID + message: "test message".as_bytes().to_vec(), + security_level: SecurityLevel::Standard, }) .unwrap(); } @@ -225,8 +241,11 @@ mod tests { let invalid_cid = 999999 + i; to_service .send(InternalServiceRequest::Message { - peer_cid: invalid_cid, - message: BytesMut::from("test message"), + request_id: Uuid::new_v4(), + cid: 0, + peer_cid: Some(invalid_cid), + message: "test message".as_bytes().to_vec(), + security_level: SecurityLevel::Standard, }) .unwrap(); diff --git a/citadel-internal-service/tests/file_transfer_edge_cases.rs b/citadel-internal-service/tests/file_transfer_edge_cases.rs index 5c9bc4b..412c8c1 100644 --- a/citadel-internal-service/tests/file_transfer_edge_cases.rs +++ b/citadel-internal-service/tests/file_transfer_edge_cases.rs @@ -279,12 +279,9 @@ mod tests { fs::write(&file_path, "test content").unwrap(); // Test file transfer - let transfer_result = send_file_and_wait_for_completion( - to_service, - from_service, - cid, - file_path.clone(), - ).await; + let transfer_result = + send_file_and_wait_for_completion(to_service, from_service, cid, file_path.clone()) + .await; fs::remove_file(file_path).unwrap(); assert!(transfer_result.is_ok()); @@ -311,12 +308,9 @@ mod tests { fs::write(&file_path, &content).unwrap(); // Test file transfer - let transfer_result = send_file_and_wait_for_completion( - to_service, - from_service, - cid, - file_path.clone(), - ).await; + let transfer_result = + send_file_and_wait_for_completion(to_service, from_service, cid, file_path.clone()) + .await; fs::remove_file(file_path).unwrap(); assert!(transfer_result.is_ok()); @@ -337,25 +331,33 @@ mod tests { // Start file transfer let transfer_id = Uuid::new_v4(); - to_service.send(InternalServiceRequest::FileTransfer { - transfer_id, - file_path: file_path.clone(), - peer_cid: None, - }).unwrap(); + to_service + .send(InternalServiceRequest::SendFile { + request_id: transfer_id, + source: file_path.clone(), + cid: *cid, + transfer_type: TransferType::FileTransfer, + peer_cid: None, + chunk_size: None, + }) + .unwrap(); // Wait for transfer to start tokio::time::sleep(Duration::from_millis(100)).await; // Cancel transfer - to_service.send(InternalServiceRequest::CancelFileTransfer { - transfer_id, - }).unwrap(); + to_service + .send(InternalServiceRequest::CancelTransfer { + request_id: transfer_id, + cid: *cid, + }) + .unwrap(); // Verify cancellation let mut cancelled = false; while let Ok(response) = from_service.try_recv() { - if let InternalServiceResponse::FileTransferStatus(status) = response { - if status.status == ObjectTransferStatus::Cancelled { + if let InternalServiceResponse::TransferStatus(status) = response { + if status.status == TransferStatus::Cancelled { cancelled = true; break; } @@ -370,15 +372,17 @@ mod tests { async fn send_file_and_wait_for_completion( to_service: &UnboundedSender, from_service: &mut UnboundedReceiver, - cid: u64, + cid: &mut u64, file_path: PathBuf, ) -> Result<(), Box> { - let transfer_id = Uuid::new_v4(); - to_service.send(InternalServiceRequest::FileTransfer { - transfer_id, - file_path, + to_service.send(InternalServiceRequest::SendFile { + request_id: Uuid::new_v4(), + source: file_path, + cid: *cid, + transfer_type: TransferType::FileTransfer, peer_cid: None, - }).unwrap(); + chunk_size: None, + })?; let mut completed = false; while let Ok(response) = from_service.try_recv() { diff --git a/citadel-internal-service/tests/group_chat.rs b/citadel-internal-service/tests/group_chat.rs index 6414c9d..bd026fd 100644 --- a/citadel-internal-service/tests/group_chat.rs +++ b/citadel-internal-service/tests/group_chat.rs @@ -1359,10 +1359,7 @@ mod tests { to_service_a .send(InternalServiceRequest::GroupCreate { group_id: *group_id, - initial_users: vec![ - UserIdentifier::from(cid_b), - UserIdentifier::from(cid_c), - ], + initial_users: vec![UserIdentifier::from(cid_b), UserIdentifier::from(cid_c)], }) .unwrap(); } diff --git a/citadel-internal-service/tests/intra_kernel.rs b/citadel-internal-service/tests/intra_kernel.rs index 5c898e2..4bb40c0 100644 --- a/citadel-internal-service/tests/intra_kernel.rs +++ b/citadel-internal-service/tests/intra_kernel.rs @@ -1,20 +1,18 @@ +use citadel_internal_service::kernel::CitadelWorkspaceService; use citadel_internal_service_test_common as common; +use citadel_internal_service_types::{ + DeleteVirtualFileSuccess, DownloadFileSuccess, FileTransferRequestNotification, + FileTransferStatusNotification, InternalServiceRequest, InternalServiceResponse, + MessageNotification, MessageSendFailure, MessageSendSuccess, SendFileRequestSuccess, +}; +use citadel_sdk::prelude::*; +use std::path::PathBuf; +use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; +use uuid::Uuid; #[cfg(test)] mod tests { - use crate::common::{ - exhaust_stream_to_file_completion, register_and_connect_to_server, - server_info_skip_cert_verification, RegisterAndConnectItems, - }; - use citadel_internal_service::kernel::CitadelWorkspaceService; - use citadel_internal_service_types::{ - DeleteVirtualFileSuccess, DownloadFileSuccess, FileTransferRequestNotification, - FileTransferStatusNotification, InternalServiceRequest, InternalServiceResponse, - MessageNotification, MessageSendFailure, MessageSendSuccess, SendFileRequestSuccess, - }; - use citadel_sdk::prelude::*; - use std::path::PathBuf; - use uuid::Uuid; + use super::*; #[tokio::test] async fn test_intra_kernel_service_and_peers() -> Result<(), Box> { @@ -43,7 +41,7 @@ mod tests { server_addr: server_bind_address, full_name: "Peer 0".to_string(), username: "peer.0".to_string(), - password: "secret_0".to_string().into_bytes().to_owned(), + password: "secret_0".to_string().as_bytes().to_vec(), pre_shared_key: None::, }, RegisterAndConnectItems { @@ -51,7 +49,7 @@ mod tests { server_addr: server_bind_address, full_name: "Peer 1".to_string(), username: "peer.1".to_string(), - password: "secret_1".to_string().into_bytes().to_owned(), + password: "secret_1".to_string().as_bytes().to_vec(), pre_shared_key: None::, }, ]; @@ -113,7 +111,7 @@ mod tests { server_addr: server_bind_address, full_name: "Peer 0".to_string(), username: "peer.0".to_string(), - password: "secret_0".to_string().into_bytes().to_owned(), + password: "secret_0".to_string().as_bytes().to_vec(), pre_shared_key: None::, }, RegisterAndConnectItems { @@ -121,7 +119,7 @@ mod tests { server_addr: server_bind_address, full_name: "Peer 1".to_string(), username: "peer.1".to_string(), - password: "secret_1".to_string().into_bytes().to_owned(), + password: "secret_1".to_string().as_bytes().to_vec(), pre_shared_key: None::, }, ]; @@ -155,10 +153,10 @@ mod tests { .await?; let message_request = InternalServiceRequest::Message { request_id: Uuid::new_v4(), - message: "Test Message From Peer 0.".to_string().into_bytes(), - cid: peer_0_cid, - peer_cid: Some(peer_1_cid), - security_level: Default::default(), + cid, + peer_cid: Some(peer_cid), + message: message.into(), + security_level: SecurityLevel::Standard, }; peer_0_tx.send(message_request)?; match peer_0_rx.recv().await.unwrap() { @@ -219,7 +217,7 @@ mod tests { server_addr: server_bind_address, full_name: "Peer 0".to_string(), username: "peer.0".to_string(), - password: "secret_0".to_string().into_bytes().to_owned(), + password: "secret_0".to_string().as_bytes().to_vec(), pre_shared_key: None::, }, RegisterAndConnectItems { @@ -227,7 +225,7 @@ mod tests { server_addr: server_bind_address, full_name: "Peer 1".to_string(), username: "peer.1".to_string(), - password: "secret_1".to_string().into_bytes().to_owned(), + password: "secret_1".to_string().as_bytes().to_vec(), pre_shared_key: None::, }, ]; @@ -360,7 +358,7 @@ mod tests { server_addr: server_bind_address, full_name: "Peer 0".to_string(), username: "peer.0".to_string(), - password: "secret_0".to_string().into_bytes().to_owned(), + password: "secret_0".to_string().as_bytes().to_vec(), pre_shared_key: None::, }, RegisterAndConnectItems { @@ -368,7 +366,7 @@ mod tests { server_addr: server_bind_address, full_name: "Peer 1".to_string(), username: "peer.1".to_string(), - password: "secret_1".to_string().into_bytes().to_owned(), + password: "secret_1".to_string().as_bytes().to_vec(), pre_shared_key: None::, }, ]; @@ -531,7 +529,7 @@ mod tests { server_addr: server_bind_address, full_name: format!("Peer {}", i), username: format!("peer.{}", i), - password: format!("secret_{}", i).into_bytes().to_owned(), + password: format!("secret_{}", i).to_string().as_bytes().to_vec(), pre_shared_key: None::, }); } @@ -568,7 +566,7 @@ mod tests { server_addr: server_bind_address, full_name: "Test Peer".to_string(), username: "test.peer".to_string(), - password: "secret".into_bytes().to_owned(), + password: "secret".to_string().as_bytes().to_vec(), pre_shared_key: None::, }; @@ -576,7 +574,12 @@ mod tests { let (to_service, mut from_service, cid) = service_vec.get_mut(0).unwrap(); // Disconnect - to_service.send(InternalServiceRequest::Disconnect).unwrap(); + to_service + .send(InternalServiceRequest::Disconnect { + request_id: Uuid::new_v4(), + cid: cid, + }) + .unwrap(); // Wait for disconnect to complete let mut disconnected = false; @@ -597,7 +600,10 @@ mod tests { to_service2 .send(InternalServiceRequest::Message { peer_cid: cid2, + request_id: Uuid::new_v4(), + cid, message: message.clone().into(), + security_level: SecurityLevel::Standard, }) .unwrap(); @@ -614,7 +620,8 @@ mod tests { } #[tokio::test] - async fn test_intra_kernel_message_to_disconnected_peer() -> Result<(), Box> { + async fn test_intra_kernel_message_to_disconnected_peer( + ) -> Result<(), Box> { crate::common::setup_log(); let (server, server_bind_address) = server_info_skip_cert_verification(); @@ -639,7 +646,7 @@ mod tests { server_addr: server_bind_address, full_name: "Peer A".to_string(), username: "peer.a".to_string(), - password: "secret_a".into_bytes().to_owned(), + password: "secret_a".to_string().as_bytes().to_vec(), pre_shared_key: None::, }, RegisterAndConnectItems { @@ -647,7 +654,7 @@ mod tests { server_addr: server_bind_address, full_name: "Peer B".to_string(), username: "peer.b".to_string(), - password: "secret_b".into_bytes().to_owned(), + password: "secret_b".to_string().as_bytes().to_vec(), pre_shared_key: None::, }, ]; @@ -657,7 +664,12 @@ mod tests { let (to_service_b, mut from_service_b, cid_b) = service_vec.get_mut(1).unwrap(); // Disconnect peer B - to_service_b.send(InternalServiceRequest::Disconnect).unwrap(); + to_service_b + .send(InternalServiceRequest::Disconnect { + request_id: Uuid::new_v4(), + cid: cid_b, + }) + .unwrap(); // Wait for disconnect let mut disconnected = false; @@ -674,7 +686,10 @@ mod tests { to_service_a .send(InternalServiceRequest::Message { peer_cid: cid_b, + request_id: Uuid::new_v4(), + cid: cid_a, message: message.clone().into(), + security_level: SecurityLevel::Standard, }) .unwrap(); diff --git a/citadel-internal-service/tests/kv_store_edge_cases.rs b/citadel-internal-service/tests/kv_store_edge_cases.rs index a66c0b8..4e65656 100644 --- a/citadel-internal-service/tests/kv_store_edge_cases.rs +++ b/citadel-internal-service/tests/kv_store_edge_cases.rs @@ -1,20 +1,27 @@ use citadel_internal_service_test_common as common; +use uuid::Uuid; #[cfg(test)] mod tests { - use crate::common::{register_and_connect_to_server, server_info_skip_cert_verification, RegisterAndConnectItems}; - use citadel_internal_service::kernel::CitadelWorkspaceService; - use citadel_internal_service_types::{ - InternalServiceRequest, InternalServiceResponse, LocalDbGetKvSuccess, LocalDbSetKvSuccess, - LocalDbDeleteKvSuccess, LocalDbClearAllKvSuccess, + use crate::common::{ + register_and_connect_to_server, server_info_skip_cert_verification, RegisterAndConnectItems, }; + use citadel_internal_service::kernel::CitadelWorkspaceService; + use citadel_internal_service_types::{InternalServiceRequest, InternalServiceResponse}; use citadel_sdk::prelude::*; use std::error::Error; - use std::net::SocketAddr; use std::time::Duration; - use bytes::BytesMut; - - async fn setup_test_environment() -> Result<(UnboundedSender, UnboundedReceiver, u64), Box> { + use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; + use uuid::Uuid; + + async fn setup_test_environment() -> Result< + ( + UnboundedSender, + UnboundedReceiver, + u64, + ), + Box, + > { let (server, server_bind_address) = server_info_skip_cert_verification(); tokio::task::spawn(server); @@ -47,21 +54,24 @@ mod tests { #[tokio::test] async fn test_kv_store_large_values() -> Result<(), Box> { crate::common::setup_log(); - let (to_service, mut from_service, _) = setup_test_environment().await?; + let (to_service, mut from_service, cid) = setup_test_environment().await?; // Test with a large value (1MB) let large_value = vec![b'x'; 1024 * 1024]; to_service - .send(InternalServiceRequest::LocalDbSetKv { - key: "large_key".into(), - value: large_value.clone().into(), + .send(InternalServiceRequest::LocalDBSetKV { + cid: Some(cid), + request_id: Uuid::new_v4(), + peer_cid: None, + key: "large_key".to_string(), + value: large_value, }) .unwrap(); // Verify set operation succeeded let mut set_success = false; while let Ok(response) = from_service.try_recv() { - if let InternalServiceResponse::LocalDbSetKvSuccess(_) = response { + if let InternalServiceResponse::LocalDBSetKVSuccess(_) = response { set_success = true; break; } @@ -70,15 +80,18 @@ mod tests { // Retrieve and verify large value to_service - .send(InternalServiceRequest::LocalDbGetKv { - key: "large_key".into(), + .send(InternalServiceRequest::LocalDBGetKV { + cid: Some(cid), + request_id: Uuid::new_v4(), + peer_cid: None, + key: "large_key".to_string(), }) .unwrap(); let mut get_success = false; while let Ok(response) = from_service.try_recv() { - if let InternalServiceResponse::LocalDbGetKvSuccess(success) = response { - assert_eq!(success.value.to_vec(), large_value); + if let InternalServiceResponse::LocalDBGetKVSuccess(success) = response { + assert_eq!(success.value, large_value); get_success = true; break; } @@ -91,16 +104,19 @@ mod tests { #[tokio::test] async fn test_kv_store_concurrent_operations() -> Result<(), Box> { crate::common::setup_log(); - let (to_service, mut from_service, _) = setup_test_environment().await?; + let (to_service, mut from_service, cid) = setup_test_environment().await?; // Perform multiple set operations concurrently for i in 0..10 { let key = format!("key_{}", i); let value = format!("value_{}", i).into_bytes(); to_service - .send(InternalServiceRequest::LocalDbSetKv { - key: key.into(), - value: value.into(), + .send(InternalServiceRequest::LocalDBSetKV { + cid: Some(cid), + request_id: Uuid::new_v4(), + peer_cid: None, + key: key, + value: value.to_vec(), }) .unwrap(); } @@ -108,7 +124,7 @@ mod tests { // Count successful set operations let mut set_success_count = 0; while let Ok(response) = from_service.try_recv() { - if let InternalServiceResponse::LocalDbSetKvSuccess(_) = response { + if let InternalServiceResponse::LocalDBSetKVSuccess(_) = response { set_success_count += 1; if set_success_count == 10 { break; @@ -121,17 +137,20 @@ mod tests { for i in 0..10 { let key = format!("key_{}", i); to_service - .send(InternalServiceRequest::LocalDbGetKv { - key: key.into(), + .send(InternalServiceRequest::LocalDBGetKV { + cid: Some(cid), + request_id: Uuid::new_v4(), + peer_cid: None, + key: key, }) .unwrap(); } let mut get_success_count = 0; while let Ok(response) = from_service.try_recv() { - if let InternalServiceResponse::LocalDbGetKvSuccess(success) = response { + if let InternalServiceResponse::LocalDBGetKVSuccess(success) = response { let expected_value = format!("value_{}", get_success_count).into_bytes(); - assert_eq!(success.value.to_vec(), expected_value); + assert_eq!(success.value, expected_value); get_success_count += 1; if get_success_count == 10 { break; @@ -146,22 +165,25 @@ mod tests { #[tokio::test] async fn test_kv_store_special_keys() -> Result<(), Box> { crate::common::setup_log(); - let (to_service, mut from_service, _) = setup_test_environment().await?; + let (to_service, mut from_service, cid) = setup_test_environment().await?; // Test special characters in keys let special_keys = vec![ "key with spaces", "key_with_@#$%^&*", - "很長的鑰匙", // Unicode characters + "很長的鑰匙", // Unicode characters "", // Empty key ]; // Set values for special keys for key in special_keys.iter() { to_service - .send(InternalServiceRequest::LocalDbSetKv { - key: key.to_string().into(), - value: BytesMut::from(&b"test_value"[..]), + .send(InternalServiceRequest::LocalDBSetKV { + cid: Some(cid), + request_id: Uuid::new_v4(), + peer_cid: None, + key: key.to_string(), + value: b"test_value".to_vec(), }) .unwrap(); } @@ -169,7 +191,7 @@ mod tests { // Verify all sets succeeded let mut set_success_count = 0; while let Ok(response) = from_service.try_recv() { - if let InternalServiceResponse::LocalDbSetKvSuccess(_) = response { + if let InternalServiceResponse::LocalDBSetKVSuccess(_) = response { set_success_count += 1; if set_success_count == special_keys.len() { break; @@ -181,8 +203,11 @@ mod tests { // Try to retrieve values for special keys for key in special_keys.iter() { to_service - .send(InternalServiceRequest::LocalDbGetKv { - key: key.to_string().into(), + .send(InternalServiceRequest::LocalDBGetKV { + cid: Some(cid), + request_id: Uuid::new_v4(), + peer_cid: None, + key: key.to_string(), }) .unwrap(); } @@ -190,8 +215,8 @@ mod tests { // Verify all gets succeeded let mut get_success_count = 0; while let Ok(response) = from_service.try_recv() { - if let InternalServiceResponse::LocalDbGetKvSuccess(success) = response { - assert_eq!(success.value.to_vec(), b"test_value"); + if let InternalServiceResponse::LocalDBGetKVSuccess(success) = response { + assert_eq!(success.value, b"test_value"); get_success_count += 1; if get_success_count == special_keys.len() { break; @@ -206,16 +231,19 @@ mod tests { #[tokio::test] async fn test_kv_store_delete_and_clear() -> Result<(), Box> { crate::common::setup_log(); - let (to_service, mut from_service, _) = setup_test_environment().await?; + let (to_service, mut from_service, cid) = setup_test_environment().await?; // Set up some initial key-value pairs for i in 0..5 { let key = format!("key_{}", i); let value = format!("value_{}", i).into_bytes(); to_service - .send(InternalServiceRequest::LocalDbSetKv { - key: key.into(), - value: value.into(), + .send(InternalServiceRequest::LocalDBSetKV { + cid: Some(cid), + request_id: Uuid::new_v4(), + peer_cid: None, + key: key, + value: value.to_vec(), }) .unwrap(); } @@ -223,7 +251,7 @@ mod tests { // Wait for all sets to complete let mut set_success_count = 0; while let Ok(response) = from_service.try_recv() { - if let InternalServiceResponse::LocalDbSetKvSuccess(_) = response { + if let InternalServiceResponse::LocalDBSetKVSuccess(_) = response { set_success_count += 1; if set_success_count == 5 { break; @@ -233,21 +261,27 @@ mod tests { // Delete specific keys to_service - .send(InternalServiceRequest::LocalDbDeleteKv { - key: "key_0".into(), + .send(InternalServiceRequest::LocalDBDeleteKV { + cid: Some(cid), + request_id: Uuid::new_v4(), + peer_cid: None, + key: "key_0".to_string(), }) .unwrap(); to_service - .send(InternalServiceRequest::LocalDbDeleteKv { - key: "key_1".into(), + .send(InternalServiceRequest::LocalDBDeleteKV { + cid: Some(cid), + request_id: Uuid::new_v4(), + peer_cid: None, + key: "key_1".to_string(), }) .unwrap(); // Verify deletes succeeded let mut delete_success_count = 0; while let Ok(response) = from_service.try_recv() { - if let InternalServiceResponse::LocalDbDeleteKvSuccess(_) = response { + if let InternalServiceResponse::LocalDBDeleteKVSuccess(_) = response { delete_success_count += 1; if delete_success_count == 2 { break; @@ -258,21 +292,27 @@ mod tests { // Try to get deleted keys to_service - .send(InternalServiceRequest::LocalDbGetKv { - key: "key_0".into(), + .send(InternalServiceRequest::LocalDBGetKV { + cid: Some(cid), + request_id: Uuid::new_v4(), + peer_cid: None, + key: "key_0".to_string(), }) .unwrap(); to_service - .send(InternalServiceRequest::LocalDbGetKv { - key: "key_1".into(), + .send(InternalServiceRequest::LocalDBGetKV { + cid: Some(cid), + request_id: Uuid::new_v4(), + peer_cid: None, + key: "key_1".to_string(), }) .unwrap(); // Verify gets return empty results let mut get_empty_count = 0; while let Ok(response) = from_service.try_recv() { - if let InternalServiceResponse::LocalDbGetKvSuccess(success) = response { + if let InternalServiceResponse::LocalDBGetKVSuccess(success) = response { assert!(success.value.is_empty()); get_empty_count += 1; if get_empty_count == 2 { @@ -284,13 +324,17 @@ mod tests { // Clear all remaining key-value pairs to_service - .send(InternalServiceRequest::LocalDbClearAllKv) + .send(InternalServiceRequest::LocalDBClearAllKV { + cid: Some(cid), + request_id: Uuid::new_v4(), + peer_cid: None, + }) .unwrap(); // Verify clear succeeded let mut clear_success = false; while let Ok(response) = from_service.try_recv() { - if let InternalServiceResponse::LocalDbClearAllKvSuccess(_) = response { + if let InternalServiceResponse::LocalDBClearAllKVSuccess(_) = response { clear_success = true; break; } @@ -301,8 +345,11 @@ mod tests { for i in 2..5 { let key = format!("key_{}", i); to_service - .send(InternalServiceRequest::LocalDbGetKv { - key: key.into(), + .send(InternalServiceRequest::LocalDBGetKV { + cid: Some(cid), + request_id: Uuid::new_v4(), + peer_cid: None, + key: key, }) .unwrap(); } @@ -310,7 +357,7 @@ mod tests { // Verify all gets return empty results let mut get_empty_count = 0; while let Ok(response) = from_service.try_recv() { - if let InternalServiceResponse::LocalDbGetKvSuccess(success) = response { + if let InternalServiceResponse::LocalDBGetKVSuccess(success) = response { assert!(success.value.is_empty()); get_empty_count += 1; if get_empty_count == 3 { diff --git a/citadel-internal-service/tests/peer_auth_edge_cases.rs b/citadel-internal-service/tests/peer_auth_edge_cases.rs index 41801a2..9adf17a 100644 --- a/citadel-internal-service/tests/peer_auth_edge_cases.rs +++ b/citadel-internal-service/tests/peer_auth_edge_cases.rs @@ -1,23 +1,30 @@ use citadel_internal_service_test_common as common; +use uuid::Uuid; #[cfg(test)] mod tests { use crate::common::{ - register_and_connect_to_server, server_info_skip_cert_verification, - RegisterAndConnectItems, + register_and_connect_to_server, server_info_skip_cert_verification, RegisterAndConnectItems, }; use citadel_internal_service::kernel::CitadelWorkspaceService; use citadel_internal_service_types::{ - InternalServiceRequest, InternalServiceResponse, PeerConnectFailure, - PeerConnectSuccess, PeerDisconnectNotification, PeerEvent, + InternalServiceRequest, InternalServiceResponse, PeerConnectFailure, PeerConnectSuccess, + PeerDisconnectNotification as DisconnectNotification, }; use citadel_sdk::prelude::*; use std::error::Error; - use std::net::SocketAddr; use std::time::Duration; use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; - - async fn setup_test_environment() -> Result, UnboundedReceiver, u64)>, Box> { + use uuid::Uuid; + + async fn setup_test_environment() -> Result< + Vec<( + UnboundedSender, + UnboundedReceiver, + u64, + )>, + Box, + > { let (server, server_bind_address) = server_info_skip_cert_verification(); tokio::task::spawn(server); @@ -27,7 +34,7 @@ mod tests { server_addr: server_bind_address, full_name: "Peer A".to_string(), username: "peer.a".to_string(), - password: "secret_a".into_bytes().to_owned(), + password: "secret_a".bytes().collect::>(), pre_shared_key: None::, }, RegisterAndConnectItems { @@ -35,7 +42,7 @@ mod tests { server_addr: server_bind_address, full_name: "Peer B".to_string(), username: "peer.b".to_string(), - password: "secret_b".into_bytes().to_owned(), + password: "secret_b".bytes().collect::>(), pre_shared_key: None::, }, ]; @@ -51,12 +58,16 @@ mod tests { let (to_service_a, mut from_service_a, cid_a) = service_vec.remove(0); let (_, _, cid_b) = service_vec.remove(0); - // Try to connect with invalid PSK - let invalid_psk = PreSharedKey::generate(); + // Try to connect with invalid session password + let invalid_password = "invalid_password".bytes().collect::>(); to_service_a .send(InternalServiceRequest::PeerConnect { + cid: cid_a, peer_cid: cid_b, - pre_shared_key: Some(invalid_psk), + request_id: Uuid::new_v4(), + udp_mode: false, + session_security_settings: None, + peer_session_password: Some(invalid_password.into()), }) .unwrap(); @@ -84,8 +95,12 @@ mod tests { for _ in 0..3 { to_service_a .send(InternalServiceRequest::PeerConnect { + cid: cid_a, peer_cid: cid_b, - pre_shared_key: None, + request_id: Uuid::new_v4(), + udp_mode: false, + session_security_settings: None, + peer_session_password: None, }) .unwrap(); } @@ -118,8 +133,12 @@ mod tests { let nonexistent_cid = 999999; to_service_a .send(InternalServiceRequest::PeerConnect { + cid: nonexistent_cid, peer_cid: nonexistent_cid, - pre_shared_key: None, + request_id: Uuid::new_v4(), + udp_mode: false, + session_security_settings: None, + peer_session_password: None, }) .unwrap(); @@ -136,53 +155,6 @@ mod tests { Ok(()) } - #[tokio::test] - async fn test_peer_auth_reconnect_after_failure() -> Result<(), Box> { - crate::common::setup_log(); - let mut service_vec = setup_test_environment().await?; - let (to_service_a, mut from_service_a, cid_a) = service_vec.remove(0); - let (_, _, cid_b) = service_vec.remove(0); - - // First try with invalid PSK - let invalid_psk = PreSharedKey::generate(); - to_service_a - .send(InternalServiceRequest::PeerConnect { - peer_cid: cid_b, - pre_shared_key: Some(invalid_psk), - }) - .unwrap(); - - // Wait for failure - let mut first_connect_failed = false; - while let Ok(response) = from_service_a.try_recv() { - if let InternalServiceResponse::PeerConnectFailure(_) = response { - first_connect_failed = true; - break; - } - } - assert!(first_connect_failed); - - // Try again with no PSK - to_service_a - .send(InternalServiceRequest::PeerConnect { - peer_cid: cid_b, - pre_shared_key: None, - }) - .unwrap(); - - // Verify second attempt succeeds - let mut second_connect_succeeded = false; - while let Ok(response) = from_service_a.try_recv() { - if let InternalServiceResponse::PeerConnectSuccess(_) = response { - second_connect_succeeded = true; - break; - } - } - assert!(second_connect_succeeded); - - Ok(()) - } - #[tokio::test] async fn test_peer_auth_rapid_connect_disconnect() -> Result<(), Box> { crate::common::setup_log(); @@ -195,8 +167,12 @@ mod tests { // Connect to_service_a .send(InternalServiceRequest::PeerConnect { + cid: cid_a, peer_cid: cid_b, - pre_shared_key: None, + request_id: Uuid::new_v4(), + udp_mode: false, + session_security_settings: None, + peer_session_password: None, }) .unwrap(); @@ -213,7 +189,9 @@ mod tests { // Immediately disconnect to_service_a .send(InternalServiceRequest::PeerDisconnect { - peer_cid: cid_b, + cid: cid_a, + peer_cid: Some(cid_b), + request_id: Uuid::new_v4(), }) .unwrap(); @@ -231,6 +209,61 @@ mod tests { Ok(()) } + #[tokio::test] + async fn test_peer_auth_reconnect_after_failure() -> Result<(), Box> { + crate::common::setup_log(); + let mut service_vec = setup_test_environment().await?; + let (to_service_a, mut from_service_a, cid_a) = service_vec.remove(0); + let (_, _, cid_b) = service_vec.remove(0); + + // First try with invalid PSK + let invalid_psk = PreSharedKey::generate(); + to_service_a + .send(InternalServiceRequest::PeerConnect { + cid: cid_a, + peer_cid: cid_b, + request_id: Uuid::new_v4(), + udp_mode: false, + session_security_settings: None, + peer_session_password: Some(invalid_psk.into()), + }) + .unwrap(); + + // Wait for failure + let mut first_connect_failed = false; + while let Ok(response) = from_service_a.try_recv() { + if let InternalServiceResponse::PeerConnectFailure(_) = response { + first_connect_failed = true; + break; + } + } + assert!(first_connect_failed); + + // Try again with no PSK + to_service_a + .send(InternalServiceRequest::PeerConnect { + cid: cid_a, + peer_cid: cid_b, + request_id: Uuid::new_v4(), + udp_mode: false, + session_security_settings: None, + peer_session_password: None, + }) + .unwrap(); + + // Verify second attempt succeeds + let mut second_connect_succeeded = false; + while let Ok(response) = from_service_a.try_recv() { + if let InternalServiceResponse::PeerConnectSuccess(_) = response { + second_connect_succeeded = true; + break; + } + } + assert!(second_connect_succeeded); + + Ok(()) + } + #[tokio::test] async fn test_peer_auth_concurrent_connections() -> Result<(), Box> { crate::common::setup_log(); @@ -245,7 +278,7 @@ mod tests { server_addr: server_bind_address, full_name: format!("Peer {}", i), username: format!("peer.{}", i), - password: format!("secret_{}", i).into_bytes().to_owned(), + password: format!("secret_{}", i).bytes().collect::>(), pre_shared_key: None::, }); } @@ -258,8 +291,12 @@ mod tests { for &peer_cid in &peer_cids { to_service_main .send(InternalServiceRequest::PeerConnect { - peer_cid, - pre_shared_key: None, + cid: cid_main, + peer_cid: peer_cid, + request_id: Uuid::new_v4(), + udp_mode: false, + session_security_settings: None, + peer_session_password: None, }) .unwrap(); }