|
| 1 | +//! Integration tests for the TLS server bootstrap. |
| 2 | +//! |
| 3 | +//! Uses a self-signed certificate checked into `resources/test_tls/`. The cert covers |
| 4 | +//! `CN=localhost` (+ SAN `DNS:localhost`) and is valid for 100 years; regenerate with: |
| 5 | +//! |
| 6 | +//! ```bash |
| 7 | +//! openssl req -x509 -newkey rsa:2048 \ |
| 8 | +//! -keyout crates/starknet_transaction_prover/resources/test_tls/key.pem \ |
| 9 | +//! -out crates/starknet_transaction_prover/resources/test_tls/cert.pem \ |
| 10 | +//! -sha256 -days 36500 -nodes \ |
| 11 | +//! -subj "/CN=localhost" \ |
| 12 | +//! -addext "subjectAltName=DNS:localhost,IP:127.0.0.1" |
| 13 | +//! ``` |
| 14 | +
|
| 15 | +use std::io::Write; |
| 16 | +use std::net::SocketAddr; |
| 17 | +use std::path::{Path, PathBuf}; |
| 18 | + |
| 19 | +use serde_json::Value; |
| 20 | +use tempfile::NamedTempFile; |
| 21 | + |
| 22 | +use crate::server::mock_rpc::MockProvingRpc; |
| 23 | +use crate::server::rpc_api::ProvingRpcServer; |
| 24 | +use crate::server::rpc_impl::SPEC_VERSION; |
| 25 | +use crate::server::tls::{load_tls_acceptor, start_tls_server}; |
| 26 | + |
| 27 | +/// Installs the default rustls crypto provider (aws-lc-rs) if not already installed. |
| 28 | +/// Required by reqwest when using rustls-based TLS. |
| 29 | +fn ensure_crypto_provider() { |
| 30 | + let _ = tokio_rustls::rustls::crypto::aws_lc_rs::default_provider().install_default(); |
| 31 | +} |
| 32 | + |
| 33 | +fn test_cert_path() -> PathBuf { |
| 34 | + Path::new(env!("CARGO_MANIFEST_DIR")).join("resources/test_tls/cert.pem") |
| 35 | +} |
| 36 | + |
| 37 | +fn test_key_path() -> PathBuf { |
| 38 | + Path::new(env!("CARGO_MANIFEST_DIR")).join("resources/test_tls/key.pem") |
| 39 | +} |
| 40 | + |
| 41 | +/// Reads the checked-in self-signed test certificate as PEM bytes. |
| 42 | +fn read_test_cert_pem() -> Vec<u8> { |
| 43 | + std::fs::read(test_cert_path()).expect("Failed to read test cert.pem") |
| 44 | +} |
| 45 | + |
| 46 | +/// Writes PEM bytes to a temporary file and returns the handle. |
| 47 | +fn write_pem_to_tempfile(pem_bytes: &[u8]) -> NamedTempFile { |
| 48 | + let mut file = NamedTempFile::new().expect("Failed to create temp file"); |
| 49 | + file.write_all(pem_bytes).expect("Failed to write PEM"); |
| 50 | + file.flush().expect("Failed to flush PEM file"); |
| 51 | + file |
| 52 | +} |
| 53 | + |
| 54 | +/// Starts a TLS server with mock RPC methods, returns (addr, server_handle, cert_pem). |
| 55 | +async fn start_test_tls_server() -> (SocketAddr, jsonrpsee::server::ServerHandle, Vec<u8>) { |
| 56 | + let methods = MockProvingRpc::from_expected_json().into_rpc(); |
| 57 | + let addr: SocketAddr = "127.0.0.1:0".parse().unwrap(); |
| 58 | + |
| 59 | + let (local_addr, handle) = start_tls_server( |
| 60 | + addr, |
| 61 | + &test_cert_path(), |
| 62 | + &test_key_path(), |
| 63 | + methods, |
| 64 | + 10, // max_connections |
| 65 | + 5 * 1024 * 1024, // max_request_body_size |
| 66 | + None, // cors_layer |
| 67 | + None, // ohttp_layer |
| 68 | + ) |
| 69 | + .await |
| 70 | + .expect("Failed to start TLS server"); |
| 71 | + |
| 72 | + (local_addr, handle, read_test_cert_pem()) |
| 73 | +} |
| 74 | + |
| 75 | +#[tokio::test] |
| 76 | +async fn test_https_spec_version_succeeds() { |
| 77 | + ensure_crypto_provider(); |
| 78 | + let (addr, handle, cert_pem) = start_test_tls_server().await; |
| 79 | + |
| 80 | + let cert = reqwest::tls::Certificate::from_pem(&cert_pem) |
| 81 | + .expect("Failed to parse certificate for reqwest"); |
| 82 | + let client = reqwest::Client::builder() |
| 83 | + .add_root_certificate(cert) |
| 84 | + .build() |
| 85 | + .expect("Failed to build HTTPS client"); |
| 86 | + |
| 87 | + let body = serde_json::json!({ |
| 88 | + "jsonrpc": "2.0", |
| 89 | + "id": "1", |
| 90 | + "method": "starknet_specVersion" |
| 91 | + }); |
| 92 | + |
| 93 | + let response = client |
| 94 | + .post(format!("https://localhost:{}", addr.port())) |
| 95 | + .json(&body) |
| 96 | + .send() |
| 97 | + .await |
| 98 | + .expect("HTTPS request failed"); |
| 99 | + |
| 100 | + assert_eq!(response.status(), 200); |
| 101 | + |
| 102 | + let json: Value = response.json().await.expect("Failed to parse response JSON"); |
| 103 | + assert_eq!(json["result"].as_str().unwrap(), SPEC_VERSION); |
| 104 | + |
| 105 | + handle.stop().expect("Failed to stop server"); |
| 106 | +} |
| 107 | + |
| 108 | +#[tokio::test] |
| 109 | +async fn test_http_to_tls_server_fails() { |
| 110 | + ensure_crypto_provider(); |
| 111 | + let (addr, handle, _cert_pem) = start_test_tls_server().await; |
| 112 | + |
| 113 | + let client = reqwest::Client::new(); |
| 114 | + let body = serde_json::json!({ |
| 115 | + "jsonrpc": "2.0", |
| 116 | + "id": "1", |
| 117 | + "method": "starknet_specVersion" |
| 118 | + }); |
| 119 | + |
| 120 | + // Plain HTTP to a TLS server should fail (connection or protocol error). |
| 121 | + let result = client.post(format!("http://localhost:{}", addr.port())).json(&body).send().await; |
| 122 | + |
| 123 | + assert!(result.is_err(), "Expected HTTP to TLS server to fail, but got: {result:?}"); |
| 124 | + |
| 125 | + handle.stop().expect("Failed to stop server"); |
| 126 | +} |
| 127 | + |
| 128 | +#[test] |
| 129 | +fn test_load_tls_acceptor_missing_cert_file() { |
| 130 | + let key_file = write_pem_to_tempfile(b"dummy key content"); |
| 131 | + let result = load_tls_acceptor("/nonexistent/cert.pem".as_ref(), key_file.path()); |
| 132 | + assert!(result.is_err(), "Expected error for missing cert file"); |
| 133 | +} |
| 134 | + |
| 135 | +#[test] |
| 136 | +fn test_load_tls_acceptor_missing_key_file() { |
| 137 | + let cert_file = write_pem_to_tempfile(b"dummy cert content"); |
| 138 | + let result = load_tls_acceptor(cert_file.path(), "/nonexistent/key.pem".as_ref()); |
| 139 | + assert!(result.is_err(), "Expected error for missing key file"); |
| 140 | +} |
| 141 | + |
| 142 | +#[test] |
| 143 | +fn test_load_tls_acceptor_invalid_pem() { |
| 144 | + let cert_file = write_pem_to_tempfile(b"not a valid PEM certificate"); |
| 145 | + let key_file = write_pem_to_tempfile(b"not a valid PEM key"); |
| 146 | + let result = load_tls_acceptor(cert_file.path(), key_file.path()); |
| 147 | + assert!(result.is_err(), "Expected error for invalid PEM content"); |
| 148 | +} |
| 149 | + |
| 150 | +#[test] |
| 151 | +fn test_load_tls_acceptor_succeeds_for_valid_files() { |
| 152 | + // Sanity check that the checked-in test cert/key actually load as a TLS acceptor. |
| 153 | + load_tls_acceptor(&test_cert_path(), &test_key_path()) |
| 154 | + .expect("Expected test cert/key to load successfully"); |
| 155 | +} |
0 commit comments