From f75e1c59aacf5ce668fd327e4e3d827511661c2a Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 8 Jan 2026 00:50:54 +0000 Subject: chore: cargo fmt --- tests/common/git_server.rs | 98 ++++++++++++++++++++++++--------------- tests/common/mock_relay.rs | 23 +++++---- tests/common/purgatory_helpers.rs | 29 ++++++++---- tests/common/relay.rs | 5 +- 4 files changed, 99 insertions(+), 56 deletions(-) (limited to 'tests') diff --git a/tests/common/git_server.rs b/tests/common/git_server.rs index adf66b5..d0d727e 100644 --- a/tests/common/git_server.rs +++ b/tests/common/git_server.rs @@ -301,7 +301,10 @@ fn find_free_port() -> u16 { use std::net::TcpListener; let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port"); - let port = listener.local_addr().expect("Failed to get local addr").port(); + let port = listener + .local_addr() + .expect("Failed to get local addr") + .port(); drop(listener); port } @@ -320,7 +323,10 @@ async fn wait_for_server_ready(port: u16) { } Err(_) => { if attempt == max_attempts - 1 { - panic!("SimpleGitServer failed to start after {} attempts", max_attempts); + panic!( + "SimpleGitServer failed to start after {} attempts", + max_attempts + ); } tokio::time::sleep(delay).await; } @@ -366,10 +372,13 @@ mod tests { .await .expect("Failed to fetch info/refs"); - assert!(response.status().is_success(), "info/refs should be accessible"); + assert!( + response.status().is_success(), + "info/refs should be accessible" + ); let body = response.text().await.expect("Failed to read response body"); - + // Should contain at least one ref (HEAD or refs/heads/main) assert!( body.contains("refs/heads/main") || body.contains("HEAD"), @@ -404,7 +413,7 @@ mod tests { ); let stdout = String::from_utf8_lossy(&output.stdout); - + // Should list the main branch with the correct commit assert!( stdout.contains(&commit_hash), @@ -433,7 +442,7 @@ mod tests { // Create a destination repo to fetch into let dest_dir = tempfile::tempdir().expect("Failed to create dest dir"); - + // Initialize empty repo (using tokio::process::Command) let output = tokio::process::Command::new("git") .args(["init"]) @@ -487,14 +496,23 @@ mod tests { #[test] fn test_is_safe_path_blocks_traversal() { let repo_path = Path::new("/tmp/repo"); - + // Safe paths assert!(is_safe_path(Path::new("/tmp/repo/info/refs"), repo_path)); - assert!(is_safe_path(Path::new("/tmp/repo/objects/pack/file.pack"), repo_path)); - + assert!(is_safe_path( + Path::new("/tmp/repo/objects/pack/file.pack"), + repo_path + )); + // Unsafe paths (path traversal) - assert!(!is_safe_path(Path::new("/tmp/repo/../etc/passwd"), repo_path)); - assert!(!is_safe_path(Path::new("/tmp/repo/../../etc/passwd"), repo_path)); + assert!(!is_safe_path( + Path::new("/tmp/repo/../etc/passwd"), + repo_path + )); + assert!(!is_safe_path( + Path::new("/tmp/repo/../../etc/passwd"), + repo_path + )); } } @@ -563,17 +581,19 @@ impl SmartGitServer { } // 3. Create and bind listener (eliminates port race condition) - let std_listener = std::net::TcpListener::bind("127.0.0.1:0") - .expect("Failed to bind to random port"); - let port = std_listener.local_addr() + let std_listener = + std::net::TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port"); + let port = std_listener + .local_addr() .expect("Failed to get local addr") .port(); - + // Convert to tokio listener (keeps port bound) - std_listener.set_nonblocking(true) + std_listener + .set_nonblocking(true) .expect("Failed to set non-blocking"); - let listener = TcpListener::from_std(std_listener) - .expect("Failed to convert to tokio listener"); + let listener = + TcpListener::from_std(std_listener).expect("Failed to convert to tokio listener"); // 4. Create shutdown channel let (shutdown_tx, mut shutdown_rx) = oneshot::channel::<()>(); @@ -690,15 +710,13 @@ async fn handle_smart_request( // Route: GET /info/refs?service=git-upload-pack if method == hyper::Method::GET && path.ends_with("/info/refs") { // Parse service from query string - let service = query - .split('&') - .find_map(|param| { - let mut parts = param.splitn(2, '='); - match (parts.next(), parts.next()) { - (Some("service"), Some(svc)) => Some(svc), - _ => None, - } - }); + let service = query.split('&').find_map(|param| { + let mut parts = param.splitn(2, '='); + match (parts.next(), parts.next()) { + (Some("service"), Some(svc)) => Some(svc), + _ => None, + } + }); match service { Some("git-upload-pack") => { @@ -714,7 +732,9 @@ async fn handle_smart_request( _ => { return Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Full::new(Bytes::from("Missing or invalid service parameter"))) + .body(Full::new(Bytes::from( + "Missing or invalid service parameter", + ))) .unwrap()); } } @@ -740,8 +760,8 @@ async fn handle_info_refs_upload_pack( git_protocol_version: Option<&str>, ) -> Result>, hyper::Error> { use std::process::Stdio; - use tokio::process::Command as TokioCommand; use tokio::io::AsyncReadExt; + use tokio::process::Command as TokioCommand; // Spawn git upload-pack --advertise-refs let mut cmd = TokioCommand::new("git"); @@ -763,8 +783,7 @@ async fn handle_info_refs_upload_pack( .stdout(Stdio::piped()) .stderr(Stdio::piped()); - let mut child = match cmd.spawn() - { + let mut child = match cmd.spawn() { Ok(child) => child, Err(e) => { eprintln!("Failed to spawn git upload-pack: {}", e); @@ -800,7 +819,7 @@ async fn handle_info_refs_upload_pack( let len = service_line.len() + 4; response_body.extend_from_slice(format!("{:04x}", len).as_bytes()); response_body.extend_from_slice(service_line.as_bytes()); - + // Flush packet response_body.extend_from_slice(b"0000"); @@ -809,7 +828,10 @@ async fn handle_info_refs_upload_pack( Ok(Response::builder() .status(StatusCode::OK) - .header("Content-Type", "application/x-git-upload-pack-advertisement") + .header( + "Content-Type", + "application/x-git-upload-pack-advertisement", + ) .header("Cache-Control", "no-cache") .body(Full::new(Bytes::from(response_body))) .unwrap()) @@ -850,8 +872,7 @@ async fn handle_upload_pack( .stdout(Stdio::piped()) .stderr(Stdio::piped()); - let mut child = match cmd.spawn() - { + let mut child = match cmd.spawn() { Ok(child) => child, Err(e) => { eprintln!("Failed to spawn git upload-pack: {}", e); @@ -957,7 +978,10 @@ mod smart_git_server_tests { content_type ); - let body = response.bytes().await.expect("Failed to read response body"); + let body = response + .bytes() + .await + .expect("Failed to read response body"); // Should start with service advertisement pkt-line let body_str = String::from_utf8_lossy(&body); @@ -1077,7 +1101,7 @@ mod smart_git_server_tests { #[tokio::test] async fn test_smart_git_server_shallow_fetch() { // This is the KEY test - shallow fetch requires smart HTTP protocol - + // Create a source repo with a commit let source_dir = tempfile::tempdir().expect("Failed to create source dir"); let commit_hash = create_test_repo_with_commit(source_dir.path(), CommitVariant::StateTest) diff --git a/tests/common/mock_relay.rs b/tests/common/mock_relay.rs index b6376a7..e81c453 100644 --- a/tests/common/mock_relay.rs +++ b/tests/common/mock_relay.rs @@ -73,8 +73,8 @@ impl MockRelay { /// in an in-memory database. pub async fn start() -> Self { // Create and bind listener (eliminates port race condition) - let std_listener = std::net::TcpListener::bind("127.0.0.1:0") - .expect("Failed to bind to random port"); + let std_listener = + std::net::TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port"); let port = std_listener .local_addr() .expect("Failed to get local addr") @@ -84,8 +84,8 @@ impl MockRelay { std_listener .set_nonblocking(true) .expect("Failed to set non-blocking"); - let listener = TcpListener::from_std(std_listener) - .expect("Failed to convert to tokio listener"); + let listener = + TcpListener::from_std(std_listener).expect("Failed to convert to tokio listener"); Self::start_with_listener(listener, port).await } @@ -258,7 +258,10 @@ fn derive_accept_key(request_key: &[u8]) -> String { engine.input(request_key); engine.input(WS_GUID); let hash = Sha1Hash::from_engine(engine); - base64::Engine::encode(&base64::engine::general_purpose::STANDARD, hash.as_byte_array()) + base64::Engine::encode( + &base64::engine::general_purpose::STANDARD, + hash.as_byte_array(), + ) } /// Wait for the server to be ready to accept connections. @@ -275,10 +278,7 @@ async fn wait_for_server_ready(port: u16) { } Err(_) => { if attempt == max_attempts - 1 { - panic!( - "MockRelay failed to start after {} attempts", - max_attempts - ); + panic!("MockRelay failed to start after {} attempts", max_attempts); } tokio::time::sleep(delay).await; } @@ -309,7 +309,10 @@ mod tests { // Create a client and connect let keys = Keys::generate(); let client = Client::new(keys.clone()); - client.add_relay(mock.url()).await.expect("Failed to add relay"); + client + .add_relay(mock.url()) + .await + .expect("Failed to add relay"); client.connect().await; // Wait for connection diff --git a/tests/common/purgatory_helpers.rs b/tests/common/purgatory_helpers.rs index 125f485..b39982e 100644 --- a/tests/common/purgatory_helpers.rs +++ b/tests/common/purgatory_helpers.rs @@ -51,7 +51,7 @@ pub fn create_test_repo_with_commit(path: &Path, variant: CommitVariant) -> Resu // Configure git user for commits run_git(path, &["config", "user.email", "test@example.com"])?; run_git(path, &["config", "user.name", "Test User"])?; - + // Disable GPG signing for tests (prevents yubikey prompts) run_git(path, &["config", "commit.gpgsign", "false"])?; run_git(path, &["config", "tag.gpgsign", "false"])?; @@ -710,7 +710,8 @@ mod tests { // Check d-tag let has_d_tag = event.tags.iter().any(|tag| { let slice = tag.as_slice(); - slice.first().is_some_and(|t| t == "d") && slice.get(1).is_some_and(|v| v == "test-repo") + slice.first().is_some_and(|t| t == "d") + && slice.get(1).is_some_and(|v| v == "test-repo") }); assert!(has_d_tag, "Event should have 'd' tag with identifier"); @@ -751,7 +752,8 @@ mod tests { // Check a-tag let has_a_tag = event.tags.iter().any(|tag| { let slice = tag.as_slice(); - slice.first().is_some_and(|t| t == "a") && slice.get(1).is_some_and(|v| v == &repo_coord) + slice.first().is_some_and(|t| t == "a") + && slice.get(1).is_some_and(|v| v == &repo_coord) }); assert!(has_a_tag, "Event should have 'a' tag"); @@ -806,7 +808,10 @@ mod tests { &repo_coord, "abc123def456", "Test PR with clone", - &["http://fork-server.com/repo.git", "http://another-server.com/repo.git"], + &[ + "http://fork-server.com/repo.git", + "http://another-server.com/repo.git", + ], ) .expect("Failed to create PR event with clone"); @@ -815,7 +820,8 @@ mod tests { // Check a-tag let has_a_tag = event.tags.iter().any(|tag| { let slice = tag.as_slice(); - slice.first().is_some_and(|t| t == "a") && slice.get(1).is_some_and(|v| v == &repo_coord) + slice.first().is_some_and(|t| t == "a") + && slice.get(1).is_some_and(|v| v == &repo_coord) }); assert!(has_a_tag, "Event should have 'a' tag"); @@ -831,8 +837,12 @@ mod tests { let has_clone_tag = event.tags.iter().any(|tag| { let slice = tag.as_slice(); slice.first().is_some_and(|t| t == "clone") - && slice.get(1).is_some_and(|v| v == "http://fork-server.com/repo.git") - && slice.get(2).is_some_and(|v| v == "http://another-server.com/repo.git") + && slice + .get(1) + .is_some_and(|v| v == "http://fork-server.com/repo.git") + && slice + .get(2) + .is_some_and(|v| v == "http://another-server.com/repo.git") }); assert!(has_clone_tag, "Event should have 'clone' tag with URLs"); } @@ -855,6 +865,9 @@ mod tests { let slice = tag.as_slice(); slice.first().is_some_and(|t| t == "clone") }); - assert!(!has_clone_tag, "Event should not have 'clone' tag when no URLs provided"); + assert!( + !has_clone_tag, + "Event should not have 'clone' tag when no URLs provided" + ); } } diff --git a/tests/common/relay.rs b/tests/common/relay.rs index 8d20da6..fb5d421 100644 --- a/tests/common/relay.rs +++ b/tests/common/relay.rs @@ -144,7 +144,10 @@ impl TestRelay { .env("NGIT_SYNC_STARTUP_JITTER_MS", "0") // No jitter for tests .env("NGIT_SYNC_DISCONNECT_CHECK_INTERVAL_SECS", "1") // Fast reconnect attempts for tests .env("NGIT_SYNC_BASE_BACKOFF_SECS", "1") // Fast backoff for tests (1s instead of 5s default) - .env("RUST_LOG", std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string())) // Use RUST_LOG from environment or default to info + .env( + "RUST_LOG", + std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string()), + ) // Use RUST_LOG from environment or default to info .stdout(Stdio::null()) // Suppress stdout for cleaner test output .stderr(Stdio::null()); // Suppress stderr for cleaner test output -- cgit v1.2.3