95 lines
2.7 KiB
Rust

//! Centralised mock definitions for test crates.
//! Sections are grouped by domain to keep the mapping between mocks and
//! their intended test areas obvious.
#![allow(dead_code)]
use async_trait::async_trait;
use mockall::mock;
use std::net::SocketAddr;
use std::time::{Duration, Instant};
pub mod predicates {
#![allow(unused_imports)]
pub use mockall::predicate::*;
}
// --- Auth domain -----------------------------------------------------------
mock! {
pub CredentialStore {}
#[async_trait]
impl niom_turn::traits::CredentialStore for CredentialStore {
async fn get_password(&self, username: &str) -> Option<String>;
}
}
// --- Allocation domain -----------------------------------------------------
#[async_trait]
pub trait RelayIo: Send + Sync {
async fn recv_from(&self, buf: &mut [u8]) -> std::io::Result<(usize, SocketAddr)>;
async fn send_to(&self, buf: &[u8], target: SocketAddr) -> std::io::Result<usize>;
}
mock! {
pub RelayIo {}
#[async_trait]
impl RelayIo for RelayIo {
async fn recv_from(&self, buf: &mut [u8]) -> std::io::Result<(usize, SocketAddr)>;
async fn send_to(&self, buf: &[u8], target: SocketAddr) -> std::io::Result<usize>;
}
}
pub trait AllocationClock: Send + Sync {
fn now(&self) -> Instant;
fn advance(&self, delta: Duration);
}
mock! {
pub AllocationClock {}
impl AllocationClock for AllocationClock {
fn now(&self) -> Instant;
fn advance(&self, delta: Duration);
}
}
// --- Channel domain --------------------------------------------------------
#[async_trait]
pub trait ChannelSink: Send + Sync {
async fn send_channel_data(&self, channel: u16, payload: Vec<u8>) -> anyhow::Result<()>;
async fn send_data_indication(&self, peer: SocketAddr, payload: Vec<u8>) -> anyhow::Result<()>;
}
mock! {
pub ChannelSink {}
#[async_trait]
impl ChannelSink for ChannelSink {
async fn send_channel_data(&self, channel: u16, payload: Vec<u8>) -> anyhow::Result<()>;
async fn send_data_indication(&self, peer: SocketAddr, payload: Vec<u8>) -> anyhow::Result<()>;
}
}
// --- Config domain ---------------------------------------------------------
pub trait ConfigSource: Send + Sync {
fn load(&self) -> Result<String, std::io::Error>;
}
mock! {
pub ConfigSource {}
impl ConfigSource for ConfigSource {
fn load(&self) -> Result<String, std::io::Error>;
}
}
// --- Error/Parser domain ---------------------------------------------------
#[async_trait]
pub trait FrameStream: Send + Sync {
async fn next_frame(&self) -> Option<Vec<u8>>;
}
mock! {
pub FrameStream {}
#[async_trait]
impl FrameStream for FrameStream {
async fn next_frame(&self) -> Option<Vec<u8>>;
}
}