niom-turn/tests/auth/helpers.rs

51 lines
1.5 KiB
Rust

//! Helpers dedicated to auth-focused tests.
use crate::support::{default_test_credentials, test_auth_manager};
use niom_turn::alloc::AllocationManager;
use niom_turn::auth::{AuthManager, InMemoryStore};
use niom_turn::config::AuthOptions;
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 default_options() -> AuthOptions {
AuthOptions::default()
}
pub fn loopback_peer() -> SocketAddr {
"127.0.0.1:55000".parse().expect("loopback socket")
}
pub fn build_auth_manager() -> AuthManager<InMemoryStore> {
let (user, password) = default_test_credentials();
test_auth_manager(user, password)
}
pub fn default_credentials() -> (&'static str, &'static str) {
default_test_credentials()
}
pub async fn spawn_udp_server(
auth: AuthManager<InMemoryStore>,
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 server_arc = Arc::new(server);
let reader = server_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<String> {
msg.attributes
.iter()
.find(|attr| attr.typ == ATTR_NONCE)
.and_then(|attr| String::from_utf8(attr.value.clone()).ok())
}