pub mod common;
use fs_err as fs;
use std::env;
use std::time::Duration;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use assert_cmd::cargo::cargo_bin_cmd;
use tokio::net::TcpListener;
use axum::{routing::get, Router};
use axum::extract::State;
use axum::response::{Response, IntoResponse};
use axum::http;
use axum::body::Body;
use axum_auth::AuthBasic;
use dash_mpd::{MPD, Period, AdaptationSet, Representation, SegmentTemplate};
use anyhow::{Context, Result};
use test_log::test;
use tracing::info;
use common::generate_minimal_mp4;
#[derive(Debug, Default)]
struct AppState {
counter: AtomicUsize,
}
impl AppState {
fn new() -> AppState {
AppState { counter: AtomicUsize::new(0) }
}
}
#[test(tokio::test(flavor = "multi_thread", worker_threads = 2))]
async fn test_basic_auth() -> Result<()> {
let shared_state = Arc::new(AppState::new());
async fn send_mpd(AuthBasic((id, password)): AuthBasic) -> impl IntoResponse {
info!("mpd request: auth {id:?}:{password:?}");
let segment_template = SegmentTemplate {
initialization: Some("/media/f1.mp4".to_string()),
..Default::default()
};
let rep = Representation {
id: Some("1".to_string()),
mimeType: Some("video/mp4".to_string()),
codecs: Some("avc1.640028".to_string()),
width: Some(1920),
height: Some(800),
bandwidth: Some(1980081),
SegmentTemplate: Some(segment_template),
..Default::default()
};
let adapt = AdaptationSet {
id: Some("1".to_string()),
contentType: Some("video".to_string()),
representations: vec!(rep),
..Default::default()
};
let period = Period {
id: Some("1".to_string()),
duration: Some(Duration::new(5, 0)),
adaptations: vec!(adapt.clone()),
..Default::default()
};
let mpd = MPD {
mpdtype: Some("static".to_string()),
periods: vec!(period),
..Default::default()
};
let xml = quick_xml::se::to_string(&mpd).unwrap();
([(http::header::CONTENT_TYPE, "application/dash+xml")], xml)
}
async fn send_mp4(AuthBasic((id, password)): AuthBasic, State(state): State<Arc<AppState>>) -> Response {
info!("segment request: auth {id:?}:{password:?}");
state.counter.fetch_add(1, Ordering::SeqCst);
let data = generate_minimal_mp4();
Response::builder()
.status(http::StatusCode::OK)
.header(http::header::CONTENT_TYPE, "video/mp4")
.body(Body::from(data))
.unwrap()
}
async fn send_status(State(state): State<Arc<AppState>>) -> impl IntoResponse {
([(http::header::CONTENT_TYPE, "text/plain")], format!("{}", state.counter.load(Ordering::Relaxed)))
}
let app = Router::new()
.route("/mpd", get(send_mpd))
.route("/media/{seg}", get(send_mp4))
.route("/status", get(send_status))
.with_state(shared_state);
let listener = TcpListener::bind("127.0.0.1:6666").await.unwrap();
let backend = async {
axum::serve(listener, app.into_make_service())
.await
.unwrap()
};
tokio::spawn(backend);
tokio::time::sleep(Duration::from_millis(1000)).await;
let client = reqwest::Client::builder()
.timeout(Duration::new(10, 0))
.build()
.context("creating HTTP client")?;
let txt = client.get("http://localhost:6666/status")
.send().await?
.error_for_status()?
.text().await
.context("fetching status")?;
assert!(txt.eq("0"), "Expecting 0 fragment requests, saw {txt}");
let mpd_fail = client.get("http://localhost:6666/mpd")
.send().await
.expect("unauthenticated manifest request");
assert_eq!(mpd_fail.status(), http::StatusCode::BAD_REQUEST);
let segment_fail = client.get("http://localhost:6666/media/foo.mp4")
.send().await
.expect("unauthenticated segment request");
assert_eq!(segment_fail.status(), http::StatusCode::BAD_REQUEST);
let outpath = env::temp_dir().join("basic_auth.mp4");
cargo_bin_cmd!()
.args(["-v", "-v", "-v",
"--auth-username", "myuser", "--auth-password", "mypassword",
"-o", outpath.to_str().unwrap(),
"http://localhost:6666/mpd"])
.assert()
.success();
assert!(fs::metadata(outpath).is_ok());
let txt = client.get("http://localhost:6666/status")
.send().await?
.error_for_status()?
.text().await
.context("fetching status")?;
assert!(txt.eq("2"), "Expecting 2 fragment requests, saw {txt}");
let outpath = env::temp_dir().join("basic_auth2.mp4");
cargo_bin_cmd!()
.args(["--quiet",
"--auth-username", "myuser", "--auth-password", "mypassword",
"-o", outpath.to_str().unwrap(),
"http://localhost:6666/mpd"])
.assert()
.success();
assert!(fs::metadata(outpath).is_ok());
let txt = client.get("http://localhost:6666/status")
.send().await?
.error_for_status()?
.text().await
.context("fetching status")?;
assert!(txt.eq("3"), "Expecting 3 fragment requests, saw {txt}");
Ok(())
}