// Fetches over loopback sockets (native only: the interpreter has no // sockets). They run when LOUISE_NET_TESTS is set, and are skipped // otherwise: // // LOUISE_NET_TESTS=1 louise test --native // // The servers are std/http, a raw TCP server that answers what it is // given, and a TLS server with the certificates of test/fixtures/tls // (`localhost` and `127.0.0.1`, from the "Louise test CA"). import fetch. { type Options , type Response , AnyAddress , BadHeader , BadUrl , Connect , Malformed , NoAddress , Options , PublicOnly , Refused , Response , TimedOut , Tls } import std/bytes import std/ffi.{type Resource} import std/http.{type Request, Settings, Streamed, Whole} import std/list import std/net.{type TcpListener, type TcpStream} import std/option import std/os import std/process.{type proc} import std/result import std/string import std/test.{assert, assert_equal} import std/time.{type clock} import std/tls.{type TlsConfig, type TlsServerConfig} import url.{type Url, Url} fn enabled :: -> Bool / { option.is_some (os.env "LOUISE_NET_TESTS") } fn fixture :: name: String -> String { "test/fixtures/tls/" <> name } fn client_tls :: -> Resource / { result.expect (tls.config (tls.with_ca_file (tls.defaults ()) (fixture "ca.pem"))) "the client's TLS configuration" } fn local :: -> Options / { Options( ..fetch.options (client_tls ()) , policy: AnyAddress , deadline: fetch.deadline_in 5000 ) } fn at :: text: String -> Url { result.expect (url.parse text) text } fn now_ms :: -> Int / { time.in_milliseconds (time.monotonic ()) } // ---- servers ------------------------------------------------------------------- /// A std/http server for `connections` connections; its port. fn http_server :: connections: Int -> Int / { let server = result.expect (http.listen "127.0.0.1" 0) "listen" let port = net.port server let _ = process.spawn fn { http.serve_with server Settings(..http.defaults (), connections: connections) fn req { handler req } } port } fn handler :: req: Request -> http.Response { match http.path req { "/hello" -> http.text 200 "hello" , "/no-content" -> no_body_response 204 , "/not-modified" -> no_body_response 304 , "/chunked" -> http.Response( 200 , [("Content-Type", "text/plain")] , Streamed(fn w { pieces w ["hel", "lo"] }) ) , "/big" -> http.response 200 "text/plain" (filled 102400) , "/big-chunked" -> http.Response( 200 , [("Content-Type", "text/plain")] , Streamed( fn w { pieces w (list.map (list.range 1 10) fn _ { string.repeat "x" 10240 }) } ) ) , "/redirect" -> http.Response(302, [("Location", "/hello")], Whole(bytes.empty ())) // what came: the method, the length it said, the body , "/echo" -> http.text 200 "{req.method} {option.unwrap_or (http.header req "Content-Length") "-"} {bytes.to_string_lossy req.body}" , _ -> http.not_found () } } fn no_body_response :: status: Int -> http.Response { http.Response( status , [("ETag", "\"v1\""), ("Cache-Control", "max-age=60")] , Whole(bytes.from_string "not sent") ) } fn pieces :: w: http.Writer texts: List -> () / { match texts { [] -> () , [t, ..rest] -> match http.write_string w t { Ok(_) -> pieces w rest, Error(_) -> () } } } fn filled :: n: Int -> Bytes { bytes.from_string (string.repeat "x" n) } /// A server for one connection: it reads the request head, writes what /// `answer` makes of it, and closes. fn raw_server :: answer: send fn(Bytes) -> Bytes -> Int / { let listener = result.expect (net.listen "127.0.0.1" 0) "listen" let port = net.port listener let _ = process.spawn fn { answer_once listener answer } port } fn answer_once :: listener: Resource answer: fn(Bytes) -> Bytes -> () / { match net.accept listener { Ok(conn) -> { let request = head_from conn (bytes.empty ()) let _ = net.write conn (answer request) net.close conn } , Error(_) -> () } } fn head_from :: conn: Resource have: Bytes -> Bytes / { if string.contains (bytes.to_string_lossy have) "\r\n\r\n" { have } else { match net.read conn 4096 { Ok(more) if bytes.length more > 0 -> head_from conn (bytes.concat have more) , _ -> have } } } fn answering :: text: String -> Int / { raw_server fn _ { bytes.from_string text } } /// A server that accepts and never answers. fn stalling_server :: -> Int / { let listener = result.expect (net.listen "127.0.0.1" 0) "listen" let port = net.port listener let _ = process.spawn fn { match net.accept listener { Ok(conn) -> { process.sleep 10000 net.close conn } , Error(_) -> () } } port } /// A TLS server for one connection with `cert`; it answers `text`, then /// ends the TLS stream properly or (`clean` false) just closes the socket. fn tls_server :: cert: String text: String clean: Bool -> Int / { let config = result.expect ( tls.server_config ( tls.server_options (fixture (cert <> ".pem")) (fixture (cert <> ".key")) ) ) "the server's TLS configuration" let listener = result.expect (net.listen "127.0.0.1" 0) "listen" let port = net.port listener let _ = process.spawn fn { tls_once listener config text clean } port } fn tls_once :: listener: Resource config: Resource text: String clean: Bool -> () / { match net.accept listener { Ok(conn) -> match tls.accept conn config { Ok(stream) -> { let _ = tls.read stream 65536 let _ = tls.write_string stream text if clean { tls.close stream } else { net.close conn } } , Error(_) -> net.close conn } , Error(_) -> () } } fn body_text :: answer: Result -> Result<(Int, String, Bool), fetch.Error> { answer |> result.map fn r { (r.status, bytes.to_string_lossy r.body, r.truncated) } } // ---- bodies -------------------------------------------------------------------- @test fn a_body_by_content_length :: -> () / { if enabled () { let port = http_server 1 let answer = fetch.get (at "http://127.0.0.1:{port}/hello") [] (local ()) assert_equal (body_text answer) Ok((200, "hello", False)) match answer { Ok(resp) -> assert_equal (fetch.header resp "CONTENT-LENGTH") Some("5") , Error(_) -> () } } } @test fn final_statuses_without_bodies_reach_the_client :: -> () / { if enabled () { let port = http_server 2 let no_content = fetch.get (at "http://127.0.0.1:{port}/no-content") [] (local ()) assert_equal (body_text no_content) Ok((204, "", False)) match no_content { Ok(resp) -> { assert_equal (fetch.header resp "Content-Length") None assert_equal (fetch.header resp "Transfer-Encoding") None assert_equal (fetch.header resp "ETag") Some("\"v1\"") assert_equal (fetch.header resp "Cache-Control") Some("max-age=60") } , Error(e) -> panic (show e) } let not_modified = fetch.get (at "http://127.0.0.1:{port}/not-modified") [] (local ()) assert_equal (body_text not_modified) Ok((304, "", False)) match not_modified { Ok(resp) -> { assert_equal (fetch.header resp "Content-Length") None assert_equal (fetch.header resp "Transfer-Encoding") None assert_equal (fetch.header resp "ETag") Some("\"v1\"") assert_equal (fetch.header resp "Cache-Control") Some("max-age=60") } , Error(e) -> panic (show e) } } } @test fn a_chunked_body :: -> () / { if enabled () { let port = http_server 1 assert_equal ( body_text (fetch.get (at "http://127.0.0.1:{port}/chunked") [] (local ())) ) Ok((200, "hello", False)) } } @test fn a_body_until_the_server_closes :: -> () / { if enabled () { let port = answering "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nall of it" assert_equal (body_text (fetch.get (at "http://127.0.0.1:{port}/") [] (local ()))) Ok((200, "all of it", False)) let port = answering "HTTP/1.0 200 OK\r\n\r\nall of it" assert_equal ( body_text ( fetch.get (at "http://127.0.0.1:{port}/") [] Options(..local (), max_body: 3) ) ) Ok((200, "all", True)) } } @test fn bodies_are_cut_at_max_body :: -> () / { if enabled () { let port = http_server 2 let opts = Options(..local (), max_body: 1000) assert_equal (body_text (fetch.get (at "http://127.0.0.1:{port}/big") [] opts)) Ok((200, string.repeat "x" 1000, True)) assert_equal (body_text (fetch.get (at "http://127.0.0.1:{port}/big-chunked") [] opts)) Ok((200, string.repeat "x" 1000, True)) } } @test fn informational_responses_are_skipped :: -> () / { if enabled () { let port = answering "HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 103 Early Hints\r\nLink: \r\n\r\nHTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok" assert_equal (body_text (fetch.get (at "http://127.0.0.1:{port}/") [] (local ()))) Ok((200, "ok", False)) } } @test fn a_redirect_is_returned_not_followed :: -> () / { if enabled () { let port = http_server 1 match fetch.get (at "http://127.0.0.1:{port}/redirect") [] (local ()) { Ok(resp) -> { assert_equal resp.status 302 assert_equal (fetch.header resp "location") Some("/hello") } , Error(e) -> panic (show e) } } } @test fn what_is_sent :: -> () / { if enabled () { let port = raw_server fn request { bytes.concat ( bytes.from_string "HTTP/1.1 200 OK\r\nContent-Length: {bytes.length request}\r\n\r\n" ) request } let headers = [ ("User-Agent", "test/1") , ("Host", "evil.example") , ("accept-encoding", "gzip") , ("Connection", "keep-alive") , ("Accept", "text/html") ] let answer = fetch.get (at "http://127.0.0.1:{port}/a b/é?q=1#frag") headers (local ()) assert_equal (body_text answer) Ok( ( 200 , "GET /a%20b/%C3%A9?q=1 HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nConnection: close\r\nAccept-Encoding: identity\r\nUser-Agent: test/1\r\nAccept: text/html\r\n\r\n" , False ) ) } } // ---- requests with a body -------------------------------------------------------- @test fn a_post_sends_its_body_and_its_length :: -> () / { if enabled () { let port = http_server 3 let post = fn headers body { fetch.request "POST" (at "http://127.0.0.1:{port}/echo") headers (bytes.from_string body) (local ()) |> body_text } assert_equal (post [("Content-Type", "application/json")] "\{\"a\":\"é\"\}") Ok((200, "POST 10 \{\"a\":\"é\"\}", False)) // the caller's own length is dropped for the body's assert_equal (post [("Content-Length", "999")] "abc") Ok((200, "POST 3 abc", False)) // an empty POST still says its length assert_equal (post [] "") Ok((200, "POST 0 ", False)) } } @test fn a_method_that_is_not_a_token_is_refused :: -> () / { if enabled () { match fetch.request "GE T" (at "http://127.0.0.1:1/") [] (bytes.empty ()) (local ()) { Error(fetch.BadHeader(_)) -> () , other -> panic "expected BadHeader, got {other}" } } } // ---- failures ------------------------------------------------------------------ @test fn malformed_responses :: -> () / { if enabled () { let malformed = fn text { let port = answering text match fetch.get (at "http://127.0.0.1:{port}/") [] (local ()) { Error(Malformed(_)) -> True, _ -> False } } assert (malformed "HTTP/1.1 200 OK\r\nTransfer-Encoding: gzip\r\n\r\nxx") "malformed HTTP/1.1 200 OKrnTransfer-Encoding: gziprnrnxx" assert (malformed "HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nabc") "malformed HTTP/1.1 200 OKrnContent-Length: 10rnrnabc" assert (malformed "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nab") "malformed HTTP/1.1 200 OKrnTransfer-Encoding: chunkedrnrn5rnab" assert (malformed "SSH-2.0-OpenSSH_9.6\r\n\r\n") "malformed SSH-2.0-OpenSSH_9.6rnrn" assert (malformed "") "malformed " assert ( malformed ("HTTP/1.1 200 OK\r\nX: " <> string.repeat "a" 70000 <> "\r\n\r\n") ) "malformed (HTTP/1.1 200 OKrnX: <> string.repeat a 70000 <> rnrn)" } } @test fn public_only_refuses_loopback :: -> () / { if enabled () { let opts = Options(..local (), policy: PublicOnly) assert_equal (fetch.get (at "http://127.0.0.1:9/") [] opts) Error(Refused("127.0.0.1", "127.0.0.1")) assert_equal (fetch.get (at "http://[::1]:9/") [] opts) Error(Refused("::1", "::1")) match fetch.get (at "http://localhost:9/") [] opts { Error(Refused("localhost", _)) -> () , other -> panic "localhost: {show other}" } } } @test fn a_stalling_server_times_out :: -> () / { if enabled () { let port = stalling_server () let start = now_ms () let opts = Options(..local (), deadline: fetch.deadline_in 300) assert_equal (fetch.get (at "http://127.0.0.1:{port}/") [] opts) Error(TimedOut) let took = now_ms () - start assert (took >= 250 && took < 2000) "took >= 250 && took < 2000" } } @test fn refused_before_any_connection :: -> () / { if enabled () { let opts = local () assert (is_bad_url (fetch.get (at "ftp://127.0.0.1/") [] opts)) "is_bad_url (fetch.get (at ftp://127.0.0.1/) [] opts)" assert ( is_bad_url (fetch.get (Url("http", "a\r\nb", None, "/", None, None)) [] opts) ) "is_bad_url (fetch.get (Url(http, arnb, None, /, None, None)) [] opts)" assert ( is_bad_url (fetch.get (Url("http", "a", None, "/", None, None)) [] opts) == False ) "is_bad_url (fetch.get (Url(http, a, None, /, None, None)) [] opts) ..." assert_equal (fetch.get (at "http://127.0.0.1:9/") [("X\r\nY", "1")] opts) Error(BadHeader("X\r\nY")) assert_equal (fetch.get (at "http://127.0.0.1:9/") [("X", "1\r\nY: 2")] opts) Error(BadHeader("X")) assert_equal (fetch.get (at "http://127.0.0.1:9/") [("Bad Name", "1")] opts) Error(BadHeader("Bad Name")) assert_equal ( fetch.get (at "http://127.0.0.1:9/") [] Options(..opts, deadline: fetch.deadline_in (-1)) ) Error(TimedOut) } } fn is_bad_url :: answer: Result -> Bool { match answer { Error(BadUrl(_)) -> True, _ -> False } } @test fn nothing_listening :: -> () / { if enabled () { let listener = result.expect (net.listen "127.0.0.1" 0) "listen" let port = net.port listener net.close_listener listener match fetch.get (at "http://127.0.0.1:{port}/") [] (local ()) { Error(Connect(_)) -> (), other -> panic (show other) } } } @test fn a_name_that_does_not_resolve :: -> () / { if enabled () { match fetch.get (at "http://louise-fetch-test.invalid/") [] (local ()) { Error(NoAddress("louise-fetch-test.invalid")) -> () , Error(TimedOut) -> () , other -> panic (show other) } } } /// A kill can only land between two steps of the worker: one waiting in /// `net.resolve` (on the `io` blocking pool) is killed when the lookup /// returns (`runtime/louise_rt/src/blocking.rs`). `get` does not wait /// for that: it answers `TimedOut` at the deadline, and the worker's late /// reply, if any, is dropped. Whether a 1 ms deadline lands inside the /// lookup depends on the resolver, so this checks the outcome, not where /// the kill landed. @test fn a_deadline_during_the_lookup :: -> () / { if enabled () { list.each (list.range 1 10) fn ms { let start = now_ms () let opts = Options(..local (), deadline: fetch.deadline_in ms) match fetch.get (at "http://louise-fetch-test-{ms}.invalid/") [] opts { Error(TimedOut) | Error(NoAddress(_)) -> () , other -> panic (show other) } assert (now_ms () - start < ms + 1000) "now_ms () - start < ms + 1000" } let port = http_server 1 assert_equal (body_text (fetch.get (at "http://127.0.0.1:{port}/hello") [] (local ()))) Ok((200, "hello", False)) } } // ---- TLS ----------------------------------------------------------------------- @test fn https_with_a_private_authority :: -> () / { if enabled () { let port = tls_server "localhost" "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok" True assert_equal (body_text (fetch.get (at "https://localhost:{port}/") [] (local ()))) Ok((200, "ok", False)) } } @test fn a_tls_close_without_close_notify_ends_the_body :: -> () / { if enabled () { let port = tls_server "localhost" "HTTP/1.1 200 OK\r\n\r\nuntil the end" False assert_equal (body_text (fetch.get (at "https://127.0.0.1:{port}/") [] (local ()))) Ok((200, "until the end", False)) } } @test fn a_certificate_for_another_name_is_refused :: -> () / { if enabled () { let port = tls_server "other-host" "HTTP/1.1 200 OK\r\n\r\nno" True match fetch.get (at "https://localhost:{port}/") [] (local ()) { Error(Tls(_)) -> (), other -> panic (show other) } } } @test fn an_unknown_authority_is_refused :: -> () / { if enabled () { let port = tls_server "localhost" "HTTP/1.1 200 OK\r\n\r\nno" True let opts = Options( ..local () , tls: result.expect (tls.config (tls.defaults ())) "Mozilla's roots" ) match fetch.get (at "https://localhost:{port}/") [] opts { Error(Tls(_)) -> (), other -> panic (show other) } } }