|
| 1 | +//! Integration tests for the TLS server bootstrap. |
| 2 | +//! |
| 3 | +//! Uses a self-signed certificate checked into `resources/test_tls/` (CN=localhost, valid for |
| 4 | +//! 100 years). See `resources/test_tls/README.md` for the openssl regeneration command. |
| 5 | +
|
| 6 | +use std::io::Write; |
| 7 | +use std::net::SocketAddr; |
| 8 | +use std::path::{Path, PathBuf}; |
| 9 | + |
| 10 | +use rstest::rstest; |
| 11 | +use serde_json::Value; |
| 12 | +use tempfile::NamedTempFile; |
| 13 | + |
| 14 | +use crate::server::mock_rpc::MockProvingRpc; |
| 15 | +use crate::server::rpc_api::ProvingRpcServer; |
| 16 | +use crate::server::rpc_impl::SPEC_VERSION; |
| 17 | +use crate::server::tls::{load_tls_acceptor, start_tls_server}; |
| 18 | + |
| 19 | +fn ensure_crypto_provider() { |
| 20 | + let _ = tokio_rustls::rustls::crypto::aws_lc_rs::default_provider().install_default(); |
| 21 | +} |
| 22 | + |
| 23 | +fn test_cert_path() -> PathBuf { |
| 24 | + Path::new(env!("CARGO_MANIFEST_DIR")).join("resources/test_tls/cert.pem") |
| 25 | +} |
| 26 | + |
| 27 | +fn test_key_path() -> PathBuf { |
| 28 | + Path::new(env!("CARGO_MANIFEST_DIR")).join("resources/test_tls/key.pem") |
| 29 | +} |
| 30 | + |
| 31 | +fn write_pem_to_tempfile(pem_bytes: &[u8]) -> NamedTempFile { |
| 32 | + let mut file = NamedTempFile::new().unwrap(); |
| 33 | + file.write_all(pem_bytes).unwrap(); |
| 34 | + file.flush().unwrap(); |
| 35 | + file |
| 36 | +} |
| 37 | + |
| 38 | +fn spec_version_request() -> Value { |
| 39 | + serde_json::json!({ |
| 40 | + "jsonrpc": "2.0", |
| 41 | + "id": "1", |
| 42 | + "method": "starknet_specVersion" |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +async fn start_test_tls_server() -> (SocketAddr, jsonrpsee::server::ServerHandle, Vec<u8>) { |
| 47 | + let methods = MockProvingRpc::from_expected_json().into_rpc(); |
| 48 | + let addr: SocketAddr = "127.0.0.1:0".parse().unwrap(); |
| 49 | + |
| 50 | + let (local_addr, handle) = start_tls_server( |
| 51 | + addr, |
| 52 | + &test_cert_path(), |
| 53 | + &test_key_path(), |
| 54 | + methods, |
| 55 | + 10, // max_connections |
| 56 | + 5 * 1024 * 1024, // max_request_body_size |
| 57 | + None, // cors_layer |
| 58 | + None, // ohttp_layer |
| 59 | + ) |
| 60 | + .await |
| 61 | + .expect("Failed to start TLS server"); |
| 62 | + |
| 63 | + let cert_pem = std::fs::read(test_cert_path()).unwrap(); |
| 64 | + (local_addr, handle, cert_pem) |
| 65 | +} |
| 66 | + |
| 67 | +#[tokio::test] |
| 68 | +async fn test_https_spec_version_succeeds() { |
| 69 | + ensure_crypto_provider(); |
| 70 | + let (addr, handle, cert_pem) = start_test_tls_server().await; |
| 71 | + |
| 72 | + let cert = reqwest::tls::Certificate::from_pem(&cert_pem).unwrap(); |
| 73 | + let client = reqwest::Client::builder().add_root_certificate(cert).build().unwrap(); |
| 74 | + |
| 75 | + let response = client |
| 76 | + .post(format!("https://localhost:{}", addr.port())) |
| 77 | + .json(&spec_version_request()) |
| 78 | + .send() |
| 79 | + .await |
| 80 | + .expect("HTTPS request failed"); |
| 81 | + |
| 82 | + assert_eq!(response.status(), 200); |
| 83 | + let json: Value = response.json().await.unwrap(); |
| 84 | + assert_eq!(json["result"].as_str().unwrap(), SPEC_VERSION); |
| 85 | + |
| 86 | + handle.stop().unwrap(); |
| 87 | +} |
| 88 | + |
| 89 | +#[tokio::test] |
| 90 | +async fn test_http_to_tls_server_fails() { |
| 91 | + ensure_crypto_provider(); |
| 92 | + let (addr, handle, _cert_pem) = start_test_tls_server().await; |
| 93 | + |
| 94 | + // Plain HTTP to a TLS server should fail (connection or protocol error). |
| 95 | + let result = reqwest::Client::new() |
| 96 | + .post(format!("http://localhost:{}", addr.port())) |
| 97 | + .json(&spec_version_request()) |
| 98 | + .send() |
| 99 | + .await; |
| 100 | + assert!(result.is_err(), "Expected HTTP to TLS server to fail, got: {result:?}"); |
| 101 | + |
| 102 | + handle.stop().unwrap(); |
| 103 | +} |
| 104 | + |
| 105 | +/// How a given path argument is materialised for `load_tls_acceptor`. |
| 106 | +enum PathMode { |
| 107 | + /// Use the checked-in valid test fixture. |
| 108 | + Valid, |
| 109 | + /// Path to a file that does not exist. |
| 110 | + Missing, |
| 111 | + /// Path to a tempfile containing these bytes (returned alongside the path so the |
| 112 | + /// `NamedTempFile` is kept alive for the call). |
| 113 | + Junk(&'static [u8]), |
| 114 | +} |
| 115 | + |
| 116 | +/// `PathMode::Junk` returns `Some(tempfile)` so the tempfile is dropped after the test, not before. |
| 117 | +fn materialise(mode: PathMode, missing: &str, valid: PathBuf) -> (PathBuf, Option<NamedTempFile>) { |
| 118 | + match mode { |
| 119 | + PathMode::Valid => (valid, None), |
| 120 | + PathMode::Missing => (missing.into(), None), |
| 121 | + PathMode::Junk(bytes) => { |
| 122 | + let file = write_pem_to_tempfile(bytes); |
| 123 | + (file.path().into(), Some(file)) |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/// Each case isolates one specific failure path by holding the other input valid, so a green test |
| 129 | +/// proves `load_tls_acceptor` actually rejected on the named reason and not on something earlier. |
| 130 | +#[rstest] |
| 131 | +#[case::missing_cert(PathMode::Missing, PathMode::Valid)] |
| 132 | +#[case::missing_key(PathMode::Valid, PathMode::Missing)] |
| 133 | +#[case::invalid_cert_pem(PathMode::Junk(b"not a valid PEM cert"), PathMode::Valid)] |
| 134 | +#[case::invalid_key_pem(PathMode::Valid, PathMode::Junk(b"not a valid PEM key"))] |
| 135 | +fn test_load_tls_acceptor_failure(#[case] cert: PathMode, #[case] key: PathMode) { |
| 136 | + let (cert_path, _cert_tmp) = materialise(cert, "/nonexistent/cert.pem", test_cert_path()); |
| 137 | + let (key_path, _key_tmp) = materialise(key, "/nonexistent/key.pem", test_key_path()); |
| 138 | + |
| 139 | + assert!(load_tls_acceptor(&cert_path, &key_path).is_err()); |
| 140 | +} |
| 141 | + |
| 142 | +#[test] |
| 143 | +fn test_load_tls_acceptor_succeeds_for_valid_files() { |
| 144 | + // `load_tls_acceptor` builds a rustls `ServerConfig`, which requires a process-level crypto |
| 145 | + // provider. nextest runs each test in a fresh process, so install the provider here. |
| 146 | + ensure_crypto_provider(); |
| 147 | + load_tls_acceptor(&test_cert_path(), &test_key_path()).unwrap(); |
| 148 | +} |
0 commit comments