use super::{tests::limits, websocket::*, *}; use crate::{net, task, tls}; use futures_util::{SinkExt, StreamExt}; use std::{ sync::{Arc, Mutex}, time::Duration, }; use tokio::net::TcpListener; use tokio_tungstenite::tungstenite::Message as Frame; fn ws_limits() -> websocket::Limits { websocket::Limits { message_bytes: 16, idle_ms: 1000, write_ms: 1000, } } fn live_limits() -> LiveLimits { LiveLimits { chunk_bytes: 1024, idle_ms: 1000, heartbeat_ms: 100, } } #[test] fn websocket_client_uses_library_frames_pong_and_close() { task::run(true, 16, async |scope| { let listener = TcpListener::bind(("127.0.0.1", 0)).await.unwrap(); let port = listener.local_addr().unwrap().port(); let peer = tokio::spawn(async move { let (stream, _) = listener.accept().await.unwrap(); let mut socket = tokio_tungstenite::accept_async(stream).await.unwrap(); assert_eq!( socket.next().await.unwrap().unwrap(), Frame::Text("hello".into()) ); for frame in [ Frame::Text("world".into()), Frame::Binary(Bytes::from_static(b"bytes")), Frame::Ping(Bytes::from_static(b"ping")), ] { socket.send(frame).await.unwrap(); } assert_eq!( socket.next().await.unwrap().unwrap(), Frame::Pong(Bytes::from_static(b"ping")) ); assert!(matches!( socket.next().await.unwrap().unwrap(), Frame::Close(_) )); socket.flush().await.unwrap(); }); let socket = connect("127.0.0.1", port.into(), "/chat", &ws_limits(), scope) .await .unwrap(); assert!(socket.send(Message::Text("x".repeat(17))).await.is_err()); socket.send(Message::Text("hello".into())).await.unwrap(); assert_eq!( socket.receive().await.unwrap(), Some(Message::Text("world".into())) ); assert_eq!( socket.receive().await.unwrap(), Some(Message::Binary(Bytes::from_static(b"bytes"))) ); assert_eq!( socket.receive().await.unwrap(), Some(Message::Ping(Bytes::from_static(b"ping"))) ); socket.close(1000, "done").await.unwrap(); assert!(socket.receive().await.unwrap().is_none()); peer.await.unwrap(); Ok::<_, ()>(()) }) .unwrap() .unwrap(); } #[test] fn upgraded_socket_cannot_outlive_cancelled_handler_even_through_an_alias() { task::run(true, 16, async |scope| { let listener = net::listen("127.0.0.1", 0, scope).await.unwrap(); let port = listener.port().unwrap(); let active = listener.clone(); let (send, receive) = tokio::sync::oneshot::channel(); let send = Arc::new(Mutex::new(Some(send))); let server = scope .spawn(move |child| async move { serve_live( &child, move |_, reply, _| { let send = send.clone(); async move { let socket = accept(&reply, &ws_limits()) .await .map_err(|e| e.to_string())?; send.lock().unwrap().take().unwrap().send(socket).unwrap(); std::future::pending().await } }, &active, &limits(false), &live_limits(), None, ) .await }) .unwrap(); let client = connect("127.0.0.1", port, "/chat", &ws_limits(), scope) .await .unwrap(); let alias = receive.await.unwrap(); assert!( tokio::time::timeout(Duration::from_secs(2), server.stop()) .await .unwrap() .unwrap() .is_none() ); assert!(alias.receive().await.is_err()); assert!(client.receive().await.is_err()); listener.close().unwrap(); Ok::<_, ()>(()) }) .unwrap() .unwrap(); } #[test] fn websocket_verifies_tls_and_preserves_upgrade_after_http_finishes() { task::run(true, 16, async |scope| { let listener = net::listen("127.0.0.1", 0, scope).await.unwrap(); let port = listener.port().unwrap(); let active = listener.clone(); let server = scope .spawn(move |child| async move { let tls = tls::server( include_bytes!("../../../fixtures/tls/server.pem"), include_bytes!("../../../fixtures/tls/server-key.pem"), ) .unwrap(); serve_live( &child, |_, reply, _| async move { let socket = accept(&reply, &ws_limits()) .await .map_err(|e| e.to_string())?; tokio::time::sleep(Duration::from_millis(30)).await; socket .send(Message::Text("upgraded".into())) .await .map_err(|e| e.to_string())?; assert!(socket.receive().await.map_err(|e| e.to_string())?.is_none()); Ok(()) }, &active, &limits(false), &live_limits(), Some(&tls), ) .await }) .unwrap(); let trust = tls::client(include_bytes!("../../../fixtures/tls/root.pem")).unwrap(); let url = format!("wss://localhost:{port}/chat"); assert!( open(&url, &tls::system(), &ws_limits(), scope) .await .is_err() ); let client = open(&url, &trust, &ws_limits(), scope).await.unwrap(); assert_eq!( client.receive().await.unwrap(), Some(Message::Text("upgraded".into())) ); client.close(1000, "done").await.unwrap(); listener.close().unwrap(); server.await.unwrap().unwrap(); Ok::<_, ()>(()) }) .unwrap() .unwrap(); }