Skip to content

Commit

Permalink
CryptoProvider process default for tests
Browse files Browse the repository at this point in the history
CryptoProvider process default
  • Loading branch information
algesten committed Dec 10, 2024
1 parent 3838a8a commit 6880bf5
Show file tree
Hide file tree
Showing 26 changed files with 103 additions and 23 deletions.
37 changes: 37 additions & 0 deletions src/crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(unreachable_patterns)]

use once_cell::sync::OnceCell;
use std::fmt;
use std::io;
use thiserror::Error;
Expand All @@ -23,13 +24,49 @@ pub enum CryptoProvider {
WinCrypto,
}

static PROCESS_DEFAULT: OnceCell<CryptoProvider> = OnceCell::new();

impl CryptoProvider {
pub(crate) fn srtp_crypto(&self) -> SrtpCrypto {
match self {
CryptoProvider::OpenSsl => SrtpCrypto::new_openssl(),
CryptoProvider::WinCrypto => SrtpCrypto::new_wincrypto(),
}
}

/// Install the selected crypto provider as default for the process.
///
/// This makes any new instance of [`Rtc`][crate::Rtc] pick up this default automatically.
///
/// The process default can only be installed once, the second time will panic. Libraries
/// should never install a process default.
pub fn install_process_default(&self) {
PROCESS_DEFAULT
.set(*self)
.expect("CryptoProvider::install_process_default() called once");
}

/// Can be repeated in the same process.
#[doc(hidden)]
pub fn __test_install_process_default(&self) {
let _ = PROCESS_DEFAULT.set(*self);
}

/// Get a possible crypto backend using feature flags.
///
/// Favors **openssl** if enabled. Panics if no crypto backend is available.
pub fn from_feature_flags() -> CryptoProvider {
if cfg!(feature = "openssl") {
return CryptoProvider::OpenSsl;
} else if cfg!(all(feature = "wincrypto", target_os = "windows")) {
return CryptoProvider::WinCrypto;
}
panic!("No crypto backend enabled");
}

pub(crate) fn process_default() -> Option<CryptoProvider> {
PROCESS_DEFAULT.get().cloned()
}
}

#[cfg(feature = "openssl")]
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,8 @@ impl RtcConfig {
/// This happens implicitly if you use [`RtcConfig::set_dtls_cert()`].
///
/// Panics: If you `set_dtls_cert()` followed by a different [`CryptoProvider`].
///
/// This overrides what is set in [`CryptoProvider::install_process_default()`].
pub fn set_crypto_provider(mut self, p: CryptoProvider) -> Self {
if let Some(c) = &self.dtls_cert {
if p != c.crypto_provider() {
Expand All @@ -1889,6 +1891,9 @@ impl RtcConfig {
}

/// The configured crypto provider.
///
/// Defaults to what's set in [`CryptoProvider::install_process_default()`] followed
/// by a fallback to [`CryptoProvider::OpenSsl`].
pub fn crypto_provider(&self) -> CryptoProvider {
self.crypto_provider
}
Expand Down Expand Up @@ -2335,7 +2340,7 @@ impl Default for RtcConfig {
fn default() -> Self {
Self {
local_ice_credentials: None,
crypto_provider: CryptoProvider::OpenSsl,
crypto_provider: CryptoProvider::process_default().unwrap_or(CryptoProvider::OpenSsl),
dtls_cert: None,
fingerprint_verification: true,
ice_lite: false,
Expand Down
3 changes: 2 additions & 1 deletion tests/bidirectional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use str0m::{Candidate, Event, RtcError};
use tracing::info_span;

mod common;
use common::{init_log, progress, TestRtc};
use common::{init_crypto_default, init_log, progress, TestRtc};

#[test]
pub fn bidirectional_same_m_line() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let mut l = TestRtc::new(info_span!("L"));
let mut r = TestRtc::new(info_span!("R"));
Expand Down
4 changes: 4 additions & 0 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ pub fn init_log() {
});
}

pub fn init_crypto_default() {
str0m::config::CryptoProvider::from_feature_flags().__test_install_process_default();
}

pub fn connect_l_r() -> (TestRtc, TestRtc) {
let rtc1 = Rtc::builder()
.set_rtp_mode(true)
Expand Down
6 changes: 5 additions & 1 deletion tests/contiguous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use str0m::{Candidate, Event, RtcError};
use tracing::info_span;

mod common;
use common::{init_log, progress, TestRtc};
use common::{init_crypto_default, init_log, progress, TestRtc};

#[test]
pub fn contiguous_all_the_way() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let output = Server::with_vp8_input()
.timeout(Duration::from_secs(5))
Expand All @@ -35,6 +36,7 @@ pub fn contiguous_all_the_way() -> Result<(), RtcError> {
#[test]
pub fn not_contiguous() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let output = Server::with_vp8_input()
.skip_packet(14337)
Expand Down Expand Up @@ -62,6 +64,7 @@ pub fn not_contiguous() -> Result<(), RtcError> {
#[test]
pub fn vp9_contiguous_all_the_way() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let output = Server::with_vp9_input().get_output()?;
let mut count = 0;
Expand All @@ -83,6 +86,7 @@ pub fn vp9_contiguous_all_the_way() -> Result<(), RtcError> {
#[test]
pub fn vp9_not_contiguous() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let output = Server::with_vp9_input().skip_packet(30952).get_output()?;
let mut count = 0;
Expand Down
3 changes: 2 additions & 1 deletion tests/data-channel-direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use str0m::{Candidate, Event, RtcConfig, RtcError};
use tracing::info_span;

mod common;
use common::{init_log, progress, TestRtc};
use common::{init_crypto_default, init_log, progress, TestRtc};

#[test]
pub fn data_channel_direct() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let mut l = TestRtc::new(info_span!("L"));

Expand Down
3 changes: 2 additions & 1 deletion tests/data-channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use str0m::{Candidate, RtcError};
use tracing::info_span;

mod common;
use common::{init_log, progress, TestRtc};
use common::{init_crypto_default, init_log, progress, TestRtc};

#[test]
pub fn data_channel() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let mut l = TestRtc::new(info_span!("L"));
let mut r = TestRtc::new(info_span!("R"));
Expand Down
3 changes: 2 additions & 1 deletion tests/flappy-ice-state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use str0m::{Candidate, Event, RtcError};
use tracing::info_span;

mod common;
use common::{init_log, progress, TestRtc};
use common::{init_crypto_default, init_log, progress, TestRtc};

#[test]
pub fn flappy_ice_lite_state() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let mut l = TestRtc::new(info_span!("L"));

Expand Down
3 changes: 2 additions & 1 deletion tests/ice-restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use str0m::{Candidate, RtcError};
use tracing::info_span;

mod common;
use common::{init_log, progress, TestRtc};
use common::{init_crypto_default, init_log, progress, TestRtc};

#[test]
pub fn ice_restart() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let mut l = TestRtc::new(info_span!("L"));

Expand Down
5 changes: 4 additions & 1 deletion tests/keyframes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ use str0m::{Candidate, Event, RtcError};
use tracing::info_span;

mod common;
use common::{h264_data, vp8_data, vp9_data};
use common::{h264_data, init_crypto_default, vp8_data, vp9_data};
use common::{init_log, progress, TestRtc};

#[test]
pub fn test_vp8_keyframes_detection() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let mut l = TestRtc::new(info_span!("L"));
let mut r = TestRtc::new(info_span!("R"));
Expand Down Expand Up @@ -104,6 +105,7 @@ pub fn test_vp8_keyframes_detection() -> Result<(), RtcError> {
#[test]
pub fn test_vp9_keyframes_detection() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let mut l = TestRtc::new(info_span!("L"));
let mut r = TestRtc::new(info_span!("R"));
Expand Down Expand Up @@ -196,6 +198,7 @@ pub fn test_vp9_keyframes_detection() -> Result<(), RtcError> {
#[test]
pub fn test_h264_keyframes_detection() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let mut l = TestRtc::new(info_span!("L"));
let mut r = TestRtc::new(info_span!("R"));
Expand Down
4 changes: 3 additions & 1 deletion tests/nack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ use str0m::rtp::{ExtensionValues, RawPacket, SeqNo, Ssrc};
use str0m::RtcError;

mod common;
use common::{connect_l_r, init_log, progress};
use common::{connect_l_r, init_crypto_default, init_log, progress};

use crate::common::progress_with_loss;

#[test]
pub fn loss_recovery() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let (mut l, mut r) = connect_l_r();

Expand Down Expand Up @@ -171,6 +172,7 @@ pub fn loss_recovery() -> Result<(), RtcError> {
#[test]
pub fn nack_delay() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let (mut l, mut r) = connect_l_r();

Expand Down
4 changes: 3 additions & 1 deletion tests/remb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use str0m::{Candidate, Event, Rtc, RtcError};
use tracing::info_span;

mod common;
use common::{init_log, negotiate, progress, TestRtc};
use common::{init_crypto_default, init_log, negotiate, progress, TestRtc};

#[test]
pub fn remb() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let l_rtc = Rtc::builder().build();
let r_rtc = Rtc::builder().build();

Expand Down
3 changes: 2 additions & 1 deletion tests/repeated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use str0m::rtp::{ExtensionValues, Ssrc};
use str0m::{Event, RtcError};

mod common;
use common::{connect_l_r, init_log, progress};
use common::{connect_l_r, init_crypto_default, init_log, progress};

#[test]
pub fn repeated() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let (mut l, mut r) = connect_l_r();

Expand Down
3 changes: 2 additions & 1 deletion tests/rtp-direct-mid-rid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use str0m::rtp::{ExtensionValues, Ssrc};
use str0m::{Event, RtcError};

mod common;
use common::{connect_l_r, init_log, progress};
use common::{connect_l_r, init_crypto_default, init_log, progress};

#[test]
pub fn rtp_direct_mid_rid() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let (mut l, mut r) = connect_l_r();

Expand Down
3 changes: 2 additions & 1 deletion tests/rtp-direct-mid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use str0m::rtp::{ExtensionValues, Ssrc};
use str0m::{Event, RtcError};

mod common;
use common::{connect_l_r, init_log, progress};
use common::{connect_l_r, init_crypto_default, init_log, progress};

#[test]
pub fn rtp_direct_mid() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let (mut l, mut r) = connect_l_r();

Expand Down
3 changes: 2 additions & 1 deletion tests/rtp-direct-ssrc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use str0m::rtp::{ExtensionValues, Ssrc};
use str0m::{Event, RtcError};

mod common;
use common::{connect_l_r, init_log, progress};
use common::{connect_l_r, init_crypto_default, init_log, progress};

#[test]
pub fn rtp_direct_ssrc() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let (mut l, mut r) = connect_l_r();

Expand Down
3 changes: 2 additions & 1 deletion tests/rtp-direct-with-roc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ use str0m::rtp::{ExtensionValues, Ssrc};
use str0m::{Event, RtcError};

mod common;
use common::{connect_l_r, init_log, progress};
use common::{connect_l_r, init_crypto_default, init_log, progress};

#[test]
pub fn rtp_direct_with_roc() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let (mut l, mut r) = connect_l_r();

Expand Down
3 changes: 2 additions & 1 deletion tests/rtp_to_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use str0m::rtp::{ExtensionValues, Ssrc};
use str0m::{Event, Rtc, RtcError};

mod common;
use common::{connect_l_r_with_rtc, init_log, progress};
use common::{connect_l_r_with_rtc, init_crypto_default, init_log, progress};

#[test]
pub fn audio_start_of_talk_spurt() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let rtc1 = Rtc::builder().set_rtp_mode(true).build();
let rtc2 = Rtc::builder().set_reordering_size_audio(0).build();
Expand Down
3 changes: 2 additions & 1 deletion tests/rtx-cache-0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use str0m::rtp::{ExtensionValues, Ssrc};
use str0m::{Event, RtcError};

mod common;
use common::{connect_l_r, init_log, progress};
use common::{connect_l_r, init_crypto_default, init_log, progress};

#[test]
pub fn rtx_cache_0() -> Result<(), RtcError> {
init_log();
init_crypto_default();

let (mut l, mut r) = connect_l_r();

Expand Down
2 changes: 2 additions & 0 deletions tests/sdp-negotiation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod common;
use common::init_crypto_default;
use common::init_log;
use common::negotiate;
use common::TestRtc;
Expand All @@ -18,6 +19,7 @@ use tracing::Span;
#[test]
pub fn change_default_pt() {
init_log();
init_crypto_default();

// First proposed PT is 100, R side adjusts its default from 102 -> 100
let (l, r) = with_params(
Expand Down
Loading

0 comments on commit 6880bf5

Please sign in to comment.