agate

Simple gemini server for static files
git clone https://github.com/mbrubeck/agate.git
Log | Files | Refs | README

commit 7bf805b26d11367755839f1cfd8a89ad0f7cbcae
parent 48061e555b0eb002ddd3faa83a777acaab3f4c3a
Author: Johann150 <johann.galle@protonmail.com>
Date:   Sat, 18 Mar 2023 08:03:02 +0100

add test for unix socket

Diffstat:
Mtests/tests.rs | 54+++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 53 insertions(+), 1 deletion(-)

diff --git a/tests/tests.rs b/tests/tests.rs @@ -1,7 +1,7 @@ use anyhow::anyhow; use gemini_fetch::{Header, Page, Status}; use std::convert::TryInto; -use std::io::{BufRead, BufReader, Read}; +use std::io::{BufRead, BufReader, Read, Write}; use std::net::{SocketAddr, ToSocketAddrs}; use std::path::PathBuf; use std::process::{Command, Stdio}; @@ -163,6 +163,58 @@ fn index_page() { ); } +#[cfg(unix)] +#[test] +fn index_page_unix() { + use rustls::{Certificate, ClientConnection, RootCertStore}; + + let sock_path = std::env::temp_dir().join("agate-test-unix-socket"); + + // this uses multicert because those certificates are set up so rustls + // does not complain about them being CA certificates + let mut server = Server::new(&[ + "--certs", + "multicert", + "--socket", + sock_path + .to_str() + .expect("could not convert temp dir path to string"), + ]); + + // set up TLS connection via unix socket + let mut certs = RootCertStore::empty(); + certs + .add(&Certificate( + include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/data/multicert/example.com/cert.der" + )) + .to_vec(), + )) + .unwrap(); + let config = rustls::ClientConfig::builder() + .with_safe_defaults() + .with_root_certificates(certs) + .with_no_client_auth(); + let mut session = ClientConnection::new( + std::sync::Arc::new(config), + "example.com".try_into().unwrap(), + ) + .unwrap(); + let mut unix = + std::os::unix::net::UnixStream::connect(sock_path).expect("could not connect unix socket"); + let mut tls = rustls::Stream::new(&mut session, &mut unix); + + write!(tls, "gemini://example.com\r\n").unwrap(); + + let mut buf = [0; 16]; + tls.read(&mut buf).unwrap(); + + assert_eq!(&buf, b"20 text/gemini\r\n"); + + server.stop().expect("failed to stop server"); +} + #[test] /// - symlinked files are followed correctly fn symlink_page() {