51 lines
1.5 KiB
Rust
51 lines
1.5 KiB
Rust
//! Allocation test helpers.
|
|
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 std::time::Duration;
|
|
use tokio::net::UdpSocket;
|
|
|
|
pub fn sample_client() -> SocketAddr {
|
|
"127.0.0.1:41000".parse().expect("client addr")
|
|
}
|
|
|
|
pub fn sample_peer() -> SocketAddr {
|
|
"127.0.0.1:42000".parse().expect("peer addr")
|
|
}
|
|
|
|
pub fn lifetime_secs(secs: u64) -> Duration {
|
|
Duration::from_secs(secs)
|
|
}
|
|
|
|
pub fn build_auth_manager() -> AuthManager<InMemoryStore> {
|
|
let (user, password) = default_test_credentials();
|
|
test_auth_manager(user, password)
|
|
}
|
|
|
|
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 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<String> {
|
|
msg.attributes
|
|
.iter()
|
|
.find(|attr| attr.typ == ATTR_NONCE)
|
|
.and_then(|attr| String::from_utf8(attr.value.clone()).ok())
|
|
}
|