#![allow(dead_code)] // each test binary uses a subset //! Shared helpers for the integration tests: raw v2.1 client sessions //! (handshake completed, manual framing) for the attack regressions. use omnivox::{ FrameType, frame::{FrameType as Ft, seal}, handshake::{Session, perform}, }; use serde::{Deserialize, Serialize}; use tokio::net::TcpStream; use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf}; /// Test message type: serde-derives make it a `Message` by blanket impl. #[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct Msg(pub Vec); pub const TEST_PSK: [u8; 32] = [42u8; 32]; pub fn bincode_cfg() -> bincode::config::Configuration< bincode::config::LittleEndian, bincode::config::Fixint, bincode::config::Limit<16777216>, > { bincode::config::standard() .with_fixed_int_encoding() .with_limit::<16777216>() } /// Serialize like the library does. pub fn encode(msg: &Msg) -> Vec { bincode::serde::encode_to_vec(msg, bincode_cfg()).unwrap() } /// One complete framed message (with u32 length prefix) ready to write. pub fn msg_frame(key: &[u8; 32], counter: u64, payload: &[u8]) -> Vec { seal(key, counter, Ft::Message, &encode(&Msg(payload.to_vec()))) } /// One complete framed stream chunk. pub fn stream_frame(key: &[u8; 32], counter: u64, payload: &[u8]) -> Vec { seal(key, counter, Ft::Stream, payload) } /// Arbitrary-kind frame for protocol-violation tests. pub fn raw_frame(key: &[u8; 32], counter: u64, kind: FrameType, plaintext: &[u8]) -> Vec { seal(key, counter, kind, plaintext) } /// Connect and complete the v2 handshake as the client role, returning /// raw halves and session keys for manual framing. pub async fn client_session(port: u16, psk: &[u8; 32]) -> (OwnedReadHalf, OwnedWriteHalf, Session) { let stream = TcpStream::connect(("127.0.0.1", port)).await.unwrap(); let (mut r, mut w) = stream.into_split(); let session = perform(&mut r, &mut w, psk, false).await.unwrap(); (r, w, session) }