Skip to content

Commit

Permalink
fix: test fixes wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Tjemmmic committed Dec 10, 2024
1 parent 4753fc7 commit b00b591
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 204 deletions.
53 changes: 36 additions & 17 deletions citadel-internal-service/tests/error_handling_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InternalServiceRequest>, UnboundedReceiver<InternalServiceResponse>, u64), Box<dyn Error>> {
async fn setup_test_environment() -> Result<
(
UnboundedSender<InternalServiceRequest>,
UnboundedReceiver<InternalServiceResponse>,
u64,
),
Box<dyn Error>,
> {
let (server, server_bind_address) = server_info_skip_cert_verification();
tokio::task::spawn(server);

Expand All @@ -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::<PreSharedKey>,
};

Expand All @@ -52,14 +56,17 @@ mod tests {
#[tokio::test]
async fn test_invalid_request_handling() -> Result<(), Box<dyn Error>> {
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();

Expand Down Expand Up @@ -111,17 +118,23 @@ 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();

// Test very large message
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();

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();

Expand Down
60 changes: 32 additions & 28 deletions citadel-internal-service/tests/file_transfer_edge_cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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;
}
Expand All @@ -370,15 +372,17 @@ mod tests {
async fn send_file_and_wait_for_completion(
to_service: &UnboundedSender<InternalServiceRequest>,
from_service: &mut UnboundedReceiver<InternalServiceResponse>,
cid: u64,
cid: &mut u64,
file_path: PathBuf,
) -> Result<(), Box<dyn Error>> {
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() {
Expand Down
5 changes: 1 addition & 4 deletions citadel-internal-service/tests/group_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Loading

0 comments on commit b00b591

Please sign in to comment.