diff options
Diffstat (limited to 'tests/util/server/src/servers/mod.rs')
-rw-r--r-- | tests/util/server/src/servers/mod.rs | 333 |
1 files changed, 53 insertions, 280 deletions
diff --git a/tests/util/server/src/servers/mod.rs b/tests/util/server/src/servers/mod.rs index abc451715..ca1932e9d 100644 --- a/tests/util/server/src/servers/mod.rs +++ b/tests/util/server/src/servers/mod.rs @@ -1,4 +1,5 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + // Usage: provide a port as argument to run hyper_hello benchmark server // otherwise this starts multiple servers on many ports for test endpoints. use base64::prelude::BASE64_STANDARD; @@ -29,10 +30,7 @@ use prost::Message; use std::collections::HashMap; use std::convert::Infallible; use std::env; -use std::net::Ipv6Addr; use std::net::SocketAddr; -use std::net::SocketAddrV6; -use std::path::PathBuf; use std::result::Result; use std::time::Duration; use tokio::io::AsyncWriteExt; @@ -40,7 +38,8 @@ use tokio::net::TcpStream; mod grpc; mod hyper_utils; -mod registry; +mod jsr_registry; +mod npm_registry; mod ws; use hyper_utils::run_server; @@ -48,9 +47,10 @@ use hyper_utils::run_server_with_acceptor; use hyper_utils::ServerKind; use hyper_utils::ServerOptions; +use crate::TEST_SERVERS_COUNT; + use super::https::get_tls_listener_stream; use super::https::SupportedHttpVersions; -use super::npm; use super::std_path; use super::testdata_path; @@ -69,7 +69,10 @@ const REDIRECT_ABSOLUTE_PORT: u16 = 4550; const AUTH_REDIRECT_PORT: u16 = 4551; const TLS_CLIENT_AUTH_PORT: u16 = 4552; const BASIC_AUTH_REDIRECT_PORT: u16 = 4554; +// 4555 is used by the proxy server, and 4556 is used by net_listen_allow_localhost_4555_fail const TLS_PORT: u16 = 4557; +pub(crate) const PUBLIC_NPM_REGISTRY_PORT: u16 = 4558; +pub(crate) const PRIVATE_NPM_REGISTRY_1_PORT: u16 = 4559; const HTTPS_PORT: u16 = 5545; const H1_ONLY_TLS_PORT: u16 = 5546; const H2_ONLY_TLS_PORT: u16 = 5547; @@ -83,9 +86,8 @@ const WS_CLOSE_PORT: u16 = 4244; const WS_PING_PORT: u16 = 4245; const H2_GRPC_PORT: u16 = 4246; const H2S_GRPC_PORT: u16 = 4247; -const REGISTRY_SERVER_PORT: u16 = 4250; +const JSR_REGISTRY_SERVER_PORT: u16 = 4250; const PROVENANCE_MOCK_SERVER_PORT: u16 = 4251; -pub(crate) const PRIVATE_NPM_REGISTRY_1_PORT: u16 = 4252; // Use the single-threaded scheduler. The hyper server is used as a point of // comparison for the (single-threaded!) benchmarks in cli/bench. We're not @@ -120,7 +122,6 @@ pub async fn run_all_servers() { let client_auth_server_https_fut = wrap_client_auth_https_server(HTTPS_CLIENT_AUTH_PORT); let main_server_fut = wrap_main_server(PORT); - let main_server_ipv6_fut = wrap_main_ipv6_server(PORT); let main_server_https_fut = wrap_main_https_server(HTTPS_PORT); let h1_only_server_tls_fut = wrap_https_h1_only_tls_server(H1_ONLY_TLS_PORT); let h2_only_server_tls_fut = wrap_https_h2_only_tls_server(H2_ONLY_TLS_PORT); @@ -128,45 +129,48 @@ pub async fn run_all_servers() { let h2_only_server_fut = wrap_http_h2_only_server(H2_ONLY_PORT); let h2_grpc_server_fut = grpc::h2_grpc_server(H2_GRPC_PORT, H2S_GRPC_PORT); - let registry_server_fut = registry::registry_server(REGISTRY_SERVER_PORT); + let registry_server_fut = + jsr_registry::registry_server(JSR_REGISTRY_SERVER_PORT); let provenance_mock_server_fut = - registry::provenance_mock_server(PROVENANCE_MOCK_SERVER_PORT); - let private_npm_registry_1_server_fut = - wrap_private_npm_registry1(PRIVATE_NPM_REGISTRY_1_PORT); - - let server_fut = async { - futures::join!( - redirect_server_fut, - ws_server_fut, - ws_ping_server_fut, - wss_server_fut, - wss2_server_fut, - tls_server_fut, - tls_client_auth_server_fut, - ws_close_server_fut, - another_redirect_server_fut, - auth_redirect_server_fut, - basic_auth_redirect_server_fut, - inf_redirects_server_fut, - double_redirects_server_fut, - abs_redirect_server_fut, - main_server_fut, - main_server_ipv6_fut, - main_server_https_fut, - client_auth_server_https_fut, - h1_only_server_tls_fut, - h2_only_server_tls_fut, - h1_only_server_fut, - h2_only_server_fut, - h2_grpc_server_fut, - registry_server_fut, - provenance_mock_server_fut, - private_npm_registry_1_server_fut, - ) - } - .boxed_local(); - - server_fut.await; + jsr_registry::provenance_mock_server(PROVENANCE_MOCK_SERVER_PORT); + + let npm_registry_server_futs = + npm_registry::public_npm_registry(PUBLIC_NPM_REGISTRY_PORT); + let private_npm_registry_1_server_futs = + npm_registry::private_npm_registry1(PRIVATE_NPM_REGISTRY_1_PORT); + + let mut futures = vec![ + redirect_server_fut.boxed_local(), + ws_server_fut.boxed_local(), + ws_ping_server_fut.boxed_local(), + wss_server_fut.boxed_local(), + wss2_server_fut.boxed_local(), + tls_server_fut.boxed_local(), + tls_client_auth_server_fut.boxed_local(), + ws_close_server_fut.boxed_local(), + another_redirect_server_fut.boxed_local(), + auth_redirect_server_fut.boxed_local(), + basic_auth_redirect_server_fut.boxed_local(), + inf_redirects_server_fut.boxed_local(), + double_redirects_server_fut.boxed_local(), + abs_redirect_server_fut.boxed_local(), + main_server_fut.boxed_local(), + main_server_https_fut.boxed_local(), + client_auth_server_https_fut.boxed_local(), + h1_only_server_tls_fut.boxed_local(), + h2_only_server_tls_fut.boxed_local(), + h1_only_server_fut.boxed_local(), + h2_only_server_fut.boxed_local(), + h2_grpc_server_fut.boxed_local(), + registry_server_fut.boxed_local(), + provenance_mock_server_fut.boxed_local(), + ]; + futures.extend(npm_registry_server_futs); + futures.extend(private_npm_registry_1_server_futs); + + assert_eq!(futures.len(), TEST_SERVERS_COUNT); + + futures::future::join_all(futures).await; } fn empty_body() -> UnsyncBoxBody<Bytes, Infallible> { @@ -1131,16 +1135,7 @@ async fn main_server( return Ok(file_resp); } - // serve npm registry files - if let Some(resp) = try_serve_npm_registry( - uri_path, - file_path.clone(), - &npm::PUBLIC_TEST_NPM_REGISTRY, - ) - .await - { - return resp; - } else if let Some(suffix) = uri_path.strip_prefix("/deno_std/") { + if let Some(suffix) = uri_path.strip_prefix("/deno_std/") { let file_path = std_path().join(suffix); if let Ok(file) = tokio::fs::read(&file_path).await { let file_resp = custom_headers(uri_path, file); @@ -1164,216 +1159,6 @@ async fn main_server( }; } -const PRIVATE_NPM_REGISTRY_AUTH_TOKEN: &str = "private-reg-token"; - -async fn wrap_private_npm_registry1(port: u16) { - let npm_registry_addr = SocketAddr::from(([127, 0, 0, 1], port)); - run_server( - ServerOptions { - addr: npm_registry_addr, - kind: ServerKind::Auto, - error_msg: "HTTP server error", - }, - private_npm_registry1, - ) - .await; -} - -async fn private_npm_registry1( - req: Request<hyper::body::Incoming>, -) -> Result<Response<UnsyncBoxBody<Bytes, Infallible>>, anyhow::Error> { - let auth = req - .headers() - .get("authorization") - .and_then(|x| x.to_str().ok()) - .unwrap_or_default(); - if auth != format!("Bearer {}", PRIVATE_NPM_REGISTRY_AUTH_TOKEN) { - return Ok( - Response::builder() - .status(StatusCode::UNAUTHORIZED) - .body(empty_body()) - .unwrap(), - ); - } - - let uri_path = req.uri().path(); - let mut testdata_file_path = testdata_path().to_path_buf(); - testdata_file_path.push(&uri_path[1..].replace("%2f", "/")); - - if let Some(resp) = try_serve_npm_registry( - uri_path, - testdata_file_path, - &npm::PRIVATE_TEST_NPM_REGISTRY_1, - ) - .await - { - return resp; - } - - Response::builder() - .status(StatusCode::NOT_FOUND) - .body(empty_body()) - .map_err(|e| e.into()) -} - -fn handle_custom_npm_registry_path( - scope_name: &str, - path: &str, - test_npm_registry: &npm::TestNpmRegistry, -) -> Result<Option<Response<UnsyncBoxBody<Bytes, Infallible>>>, anyhow::Error> { - let mut parts = path - .split('/') - .filter(|p| !p.is_empty()) - .collect::<Vec<_>>(); - let remainder = parts.split_off(1); - let name = parts[0]; - let package_name = format!("{}/{}", scope_name, name); - - if remainder.len() == 1 { - if let Some(file_bytes) = test_npm_registry - .tarball_bytes(&package_name, remainder[0].trim_end_matches(".tgz"))? - { - let file_resp = custom_headers("file.tgz", file_bytes); - return Ok(Some(file_resp)); - } - } else if remainder.is_empty() { - if let Some(registry_file) = - test_npm_registry.registry_file(&package_name)? - { - let file_resp = custom_headers("registry.json", registry_file); - return Ok(Some(file_resp)); - } - } - - Ok(None) -} - -fn should_download_npm_packages() -> bool { - // when this env var is set, it will download and save npm packages - // to the testdata/npm/registry directory - std::env::var("DENO_TEST_UTIL_UPDATE_NPM") == Ok("1".to_string()) -} - -async fn try_serve_npm_registry( - uri_path: &str, - mut testdata_file_path: PathBuf, - test_npm_registry: &npm::TestNpmRegistry, -) -> Option<Result<Response<UnsyncBoxBody<Bytes, Infallible>>, anyhow::Error>> { - if let Some((scope_name, package_name_with_path)) = test_npm_registry - .get_test_scope_and_package_name_with_path_from_uri_path(uri_path) - { - // serve all requests to the `DENOTEST_SCOPE_NAME` or `DENOTEST2_SCOPE_NAME` - // using the file system at that path - match handle_custom_npm_registry_path( - scope_name, - package_name_with_path, - test_npm_registry, - ) { - Ok(Some(response)) => return Some(Ok(response)), - Ok(None) => {} // ignore, not found - Err(err) => { - return Some( - Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) - .body(string_body(&format!("{err:#}"))) - .map_err(|e| e.into()), - ); - } - } - } else if test_npm_registry.uri_path_starts_with_registry_path(uri_path) { - // otherwise, serve based on registry.json and tgz files - let is_tarball = uri_path.ends_with(".tgz"); - if !is_tarball { - testdata_file_path.push("registry.json"); - } - if let Ok(file) = tokio::fs::read(&testdata_file_path).await { - let file_resp = custom_headers(uri_path, file); - return Some(Ok(file_resp)); - } else if should_download_npm_packages() { - if let Err(err) = download_npm_registry_file( - test_npm_registry, - uri_path, - &testdata_file_path, - is_tarball, - ) - .await - { - return Some( - Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) - .body(string_body(&format!("{err:#}"))) - .map_err(|e| e.into()), - ); - }; - - // serve the file - if let Ok(file) = tokio::fs::read(&testdata_file_path).await { - let file_resp = custom_headers(uri_path, file); - return Some(Ok(file_resp)); - } - } - } - - None -} - -// Replaces URL of public npm registry (`https://registry.npmjs.org/`) with -// the test registry (`http://localhost:4545/npm/registry/`). -// -// These strings end up in `registry.json` files for each downloaded package -// that are stored in `tests/testdata/` directory. -// -// If another npm test registry wants to use them, it should replace -// these values with appropriate URL when serving. -fn replace_default_npm_registry_url_with_test_npm_registry_url( - str_: String, - package_name: &str, -) -> String { - str_.replace( - &format!("https://registry.npmjs.org/{package_name}/-/"), - &format!("http://localhost:4545/npm/registry/{package_name}/"), - ) -} - -async fn download_npm_registry_file( - test_npm_registry: &npm::TestNpmRegistry, - uri_path: &str, - testdata_file_path: &PathBuf, - is_tarball: bool, -) -> Result<(), anyhow::Error> { - let url_parts = test_npm_registry - .strip_registry_path_prefix_from_uri_path(uri_path) - .unwrap() - .split('/') - .collect::<Vec<_>>(); - let package_name = if url_parts[0].starts_with('@') { - url_parts.into_iter().take(2).collect::<Vec<_>>().join("/") - } else { - url_parts.into_iter().take(1).collect::<Vec<_>>().join("/") - }; - let url = if is_tarball { - let file_name = testdata_file_path.file_name().unwrap().to_string_lossy(); - format!("https://registry.npmjs.org/{package_name}/-/{file_name}") - } else { - format!("https://registry.npmjs.org/{package_name}") - }; - let client = reqwest::Client::new(); - let response = client.get(url).send().await?; - let bytes = response.bytes().await?; - let bytes = if is_tarball { - bytes.to_vec() - } else { - replace_default_npm_registry_url_with_test_npm_registry_url( - String::from_utf8(bytes.to_vec()).unwrap(), - &package_name, - ) - .into_bytes() - }; - std::fs::create_dir_all(testdata_file_path.parent().unwrap())?; - std::fs::write(testdata_file_path, bytes)?; - Ok(()) -} - async fn wrap_redirect_server(port: u16) { let redirect_addr = SocketAddr::from(([127, 0, 0, 1], port)); run_server( @@ -1467,21 +1252,9 @@ async fn wrap_abs_redirect_server(port: u16) { async fn wrap_main_server(port: u16) { let main_server_addr = SocketAddr::from(([127, 0, 0, 1], port)); - wrap_main_server_for_addr(&main_server_addr).await -} - -// necessary because on Windows the npm binary will resolve localhost to ::1 -async fn wrap_main_ipv6_server(port: u16) { - let ipv6_loopback = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1); - let main_server_addr = - SocketAddr::V6(SocketAddrV6::new(ipv6_loopback, port, 0, 0)); - wrap_main_server_for_addr(&main_server_addr).await -} - -async fn wrap_main_server_for_addr(main_server_addr: &SocketAddr) { run_server( ServerOptions { - addr: *main_server_addr, + addr: main_server_addr, kind: ServerKind::Auto, error_msg: "HTTP server error", }, @@ -1588,7 +1361,7 @@ async fn wrap_client_auth_https_server(port: u16) { .await } -fn custom_headers( +pub fn custom_headers( p: &str, body: Vec<u8>, ) -> Response<UnsyncBoxBody<Bytes, Infallible>> { |