//! Helpers for channel-oriented tests. use crate::support::{default_test_credentials, test_auth_manager}; use niom_turn::alloc::AllocationManager; use niom_turn::auth::{AuthManager, InMemoryStore}; use niom_turn::constants::ATTR_NONCE; use niom_turn::models::stun::StunMessage; use std::net::SocketAddr; use std::sync::Arc; use tokio::net::UdpSocket; pub fn sample_channel_number() -> u16 { 0x4001 } pub fn sample_peer() -> SocketAddr { "127.0.0.1:43000".parse().expect("peer addr") } pub fn sample_payload() -> Vec { b"some-channel-payload".to_vec() } pub fn build_auth_manager() -> AuthManager { let (user, password) = default_test_credentials(); test_auth_manager(user, password) } pub async fn spawn_udp_server( auth: AuthManager, allocs: AllocationManager, ) -> SocketAddr { let server = UdpSocket::bind("127.0.0.1:0").await.expect("udp bind"); let addr = server.local_addr().expect("udp addr"); let arc = Arc::new(server); let reader = arc.clone(); let auth_clone = auth.clone(); let alloc_clone = allocs.clone(); tokio::spawn(async move { let _ = niom_turn::server::udp_reader_loop(reader, auth_clone, alloc_clone).await; }); addr } pub fn extract_nonce(msg: &StunMessage) -> Option { msg.attributes .iter() .find(|attr| attr.typ == ATTR_NONCE) .and_then(|attr| String::from_utf8(attr.value.clone()).ok()) }