rings-node 1.0.0-pre.5

Rings is a browser-native, structured peer-to-peer network: browser tabs and native daemons find each other by DID and talk over WebRTC, routed by a Chord DHT.
use rings_core::dht::RelayCustody;
use rings_core::dht::DEFAULT_STORAGE_VIRTUAL_POSITIONS_PER_OWNER;
use rings_core::dht::MAX_STORAGE_VIRTUAL_POSITIONS_PER_OWNER;
use rings_core::message::OriginQuotaConfig;
use rings_core::message::OriginQuotaLaneConfig;

use super::common::*;
use super::*;
use crate::descriptor::registry_lease;
use crate::processor::config::parse_webrtc_udp_port_range;
use crate::registration::registry_refresh_bound;
use crate::tests::test_processor_builder;

#[test]
fn test_webrtc_udp_port_range_absent_by_default() {
    let range = parse_webrtc_udp_port_range(None, None);

    assert!(matches!(range, core::result::Result::Ok(None)));
}

#[test]
fn test_webrtc_udp_port_range_accepts_valid_bounds() {
    let range = parse_webrtc_udp_port_range(Some(49160), Some(49200));

    assert!(matches!(
        range,
        Ok(Some(range)) if range.min() == 49160 && range.max() == 49200
    ));
}

#[test]
fn test_webrtc_udp_port_range_rejects_partial_bounds() {
    let range = parse_webrtc_udp_port_range(Some(49160), None);

    assert!(matches!(
        range,
        Err(Error::IncompleteWebrtcUdpPortRange {
            min: Some(49160),
            max: None
        })
    ));
}

#[test]
fn test_webrtc_udp_port_range_rejects_zero_bound() {
    let range = parse_webrtc_udp_port_range(Some(0), Some(49200));

    assert!(matches!(
        range,
        Err(Error::InvalidWebrtcUdpPortRange(
            rings_transport::webrtc_config::WebrtcUdpPortRangeError::ZeroBound {
                min: 0,
                max: 49200
            }
        ))
    ));
}

#[test]
fn test_webrtc_udp_port_range_rejects_inverted_bounds() {
    let range = parse_webrtc_udp_port_range(Some(49200), Some(49160));

    assert!(matches!(
        range,
        Err(Error::InvalidWebrtcUdpPortRange(
            rings_transport::webrtc_config::WebrtcUdpPortRangeError::Inverted {
                min: 49200,
                max: 49160
            }
        ))
    ));
}

#[test]
fn test_online_node_timing_requires_heartbeat_interval_less_than_ttl_when_enabled() {
    let key = SecretKey::random();
    let delegatee_key = DelegateeKey::new_with_seckey(&key).unwrap();
    let mut config = ProcessorConfig::new(
        0,
        "stun://stun.l.google.com:19302".to_string(),
        delegatee_key,
        3,
    );
    config.online_node_heartbeat_interval = Duration::from_secs(90);
    config.online_node_ttl = Duration::from_secs(30);

    assert!(matches!(
        ProcessorBuilder::from_config(&config).and_then(ProcessorBuilder::build),
        Err(Error::InvalidConfig(message))
            if message.contains("online_node_heartbeat_interval")
                && message.contains("online_node_ttl")
    ));
}

/// Both registries accept a heartbeat interval just below half the registry lease, `Λ / 2`, and
/// refuse one at it, at which a lease would no longer span two heartbeats (#918). The bound is
/// pinned to the 300 s the configuration documentation states.
#[test]
fn test_registry_heartbeat_must_refresh_before_a_descriptor_expires() -> Result<()> {
    let key = SecretKey::random();
    let delegatee_key = DelegateeKey::new_with_seckey(&key).unwrap();
    let bound = registry_refresh_bound();
    assert_eq!(bound, Duration::from_secs(300));
    let below = bound - Duration::from_millis(1);

    let presence = |interval: Duration| {
        let mut config = ProcessorConfig::new(
            0,
            "stun://stun.l.google.com:19302".to_string(),
            delegatee_key.clone(),
            3,
        );
        config.online_node_heartbeat_interval = interval;
        config.online_node_ttl = bound * 2;
        ProcessorBuilder::from_config(&config).and_then(ProcessorBuilder::build)
    };
    assert!(presence(below).is_ok());
    assert!(matches!(
        presence(bound),
        Err(Error::InvalidConfig(message))
            if message.contains("online_node_heartbeat_interval")
                && message.contains("half the registry lease")
    ));

    let exit = |interval: Duration| -> Result<_> {
        let mut config = ProcessorConfig::new(
            0,
            "stun://stun.l.google.com:19302".to_string(),
            delegatee_key.clone(),
            3,
        )
        .onion_role(OnionRole::Exit(OnionExitOffer::new(
            [OnionServiceName::tcp()],
            onion_policy(&["example.com:443"], &[])?,
        )?));
        config.onion_exit_heartbeat_interval = interval;
        config.onion_exit_ttl = bound * 2;
        Ok(ProcessorBuilder::from_config(&config).and_then(ProcessorBuilder::build))
    };
    assert!(exit(below)?.is_ok());
    assert!(matches!(
        exit(bound)?,
        Err(Error::InvalidConfig(message))
            if message.contains("onion_exit_heartbeat_interval")
                && message.contains("half the registry lease")
    ));
    Ok(())
}

/// A registry TTL longer than the lease is refused at start: readers refuse a freshness window
/// past the lease (the lease law), so the node's own descriptor would never be listed.
#[test]
fn test_registry_ttl_must_not_exceed_the_lease() {
    let key = SecretKey::random();
    let delegatee_key = DelegateeKey::new_with_seckey(&key).unwrap();
    let presence = |ttl: Duration| {
        let mut config = ProcessorConfig::new(
            0,
            "stun://stun.l.google.com:19302".to_string(),
            delegatee_key.clone(),
            3,
        );
        config.online_node_ttl = ttl;
        ProcessorBuilder::from_config(&config).and_then(ProcessorBuilder::build)
    };
    let lease = registry_lease();
    assert!(presence(lease).is_ok());
    assert!(matches!(
        presence(lease + Duration::from_millis(1)),
        Err(Error::InvalidConfig(message))
            if message.contains("online_node_ttl") && message.contains("registry lease")
    ));
}

#[test]
fn test_presence_advertisement_can_be_disabled() {
    let key = SecretKey::random();
    let delegatee_key = DelegateeKey::new_with_seckey(&key).unwrap();
    let mut config = ProcessorConfig::new(
        0,
        "stun://stun.l.google.com:19302".to_string(),
        delegatee_key,
        3,
    );
    config.online_node_heartbeat_interval = Duration::from_secs(90);
    config.online_node_ttl = Duration::from_secs(30);
    config.advertise_presence = false;

    let processor = ProcessorBuilder::from_config(&config)
        .unwrap()
        .build()
        .unwrap();

    assert!(processor.registration_tasks.is_empty());
}

#[test]
fn test_presence_advertisement_is_enabled_by_default() {
    let key = SecretKey::random();
    let delegatee_key = DelegateeKey::new_with_seckey(&key).unwrap();
    let config = ProcessorConfig::new(
        0,
        "stun://stun.l.google.com:19302".to_string(),
        delegatee_key,
        3,
    );

    let builder = ProcessorBuilder::from_config(&config).unwrap();

    assert!(builder.advertise_presence);
    assert_eq!(
        builder.dht_virtual_nodes,
        DEFAULT_STORAGE_VIRTUAL_POSITIONS_PER_OWNER
    );
    assert_eq!(builder.origin_quota, OriginQuotaConfig::default());
}

#[test]
fn test_dht_virtual_nodes_rejects_values_above_cost_bound() {
    let key = SecretKey::random();
    let delegatee_key = DelegateeKey::new_with_seckey(&key).unwrap();
    let config = ProcessorConfig::new(
        0,
        "stun://stun.l.google.com:19302".to_string(),
        delegatee_key,
        3,
    )
    .dht_virtual_nodes(MAX_STORAGE_VIRTUAL_POSITIONS_PER_OWNER.saturating_add(1));

    assert!(matches!(
        ProcessorBuilder::from_config(&config).and_then(ProcessorBuilder::build),
        Err(Error::InvalidConfig(message))
            if message.contains("dht_virtual_nodes")
                && message.contains(&MAX_STORAGE_VIRTUAL_POSITIONS_PER_OWNER.to_string())
    ));
}

#[test]
fn test_serialized_processor_config_defaults_dht_virtual_nodes() {
    let key = SecretKey::random();
    let delegatee_key = DelegateeKey::new_with_seckey(&key).unwrap();
    let yaml = format!(
        r#"
network_id: 0
ice_servers: stun://stun.l.google.com:19302
external_address: null
webrtc_udp_port_min: null
webrtc_udp_port_max: null
delegatee_key: "{}"
stabilize_interval: 15
online_node_heartbeat_interval_secs: 30
online_node_ttl_secs: 60
online_node_type: Native
advertise_presence: true
"#,
        delegatee_key.dump().unwrap()
    );

    let serialized = serde_saphyr::from_str::<ProcessorConfigSerialized>(&yaml).unwrap();
    let config = ProcessorConfig::try_from(serialized).unwrap();
    let builder = ProcessorBuilder::from_config(&config).unwrap();

    assert_eq!(
        builder.dht_virtual_nodes,
        DEFAULT_STORAGE_VIRTUAL_POSITIONS_PER_OWNER
    );
    assert_eq!(builder.origin_quota, OriginQuotaConfig::default());
}

#[test]
fn test_processor_construction_preserves_explicit_origin_quota() {
    let key = SecretKey::random();
    let delegatee_key = DelegateeKey::new_with_seckey(&key).unwrap();
    let lane = OriginQuotaLaneConfig::new(2, 3, 5, 7, 11).unwrap();
    let quota = OriginQuotaConfig::new(lane, lane, lane, lane);
    let config = ProcessorConfig::new(
        0,
        "stun://stun.l.google.com:19302".to_string(),
        delegatee_key,
        3,
    )
    .origin_quota(quota);

    let builder = ProcessorBuilder::from_config(&config).unwrap();

    assert_eq!(builder.origin_quota, quota);
}

/// A config for network `0` with a fresh key.
fn onion_test_config() -> ProcessorConfig {
    let key = SecretKey::random();
    let delegatee_key = DelegateeKey::new_with_seckey(&key).unwrap();
    ProcessorConfig::new(
        0,
        "stun://stun.l.google.com:19302".to_string(),
        delegatee_key,
        3,
    )
}

/// An exit offering `https` to `example.com:443`.
fn https_exit_role() -> Result<OnionRole<OnionExitOffer>> {
    Ok(OnionRole::Exit(OnionExitOffer::new(
        vec![OnionServiceName::https()],
        onion_policy(&["example.com:443"], &[])?,
    )?))
}

/// `relay ∈ Σ_n ⇒ advertise_presence`: the relay registration lives in the online-node
/// descriptor, for a relay and for an exit alike.
#[test]
fn test_onion_relay_requires_presence_advertisement() -> Result<()> {
    for role in [OnionRole::Relay, https_exit_role()?] {
        let mut config = onion_test_config().onion_role(role);
        config.advertise_presence = false;

        assert!(matches!(
            ProcessorBuilder::from_config(&config).and_then(ProcessorBuilder::build),
            Err(Error::InvalidConfig(message))
                if message.contains("advertise_onion_relay")
                    && message.contains("advertise_presence")
        ));
    }
    Ok(())
}

/// A typed role survives the round trip through the serialized flags, which are parsed onto
/// the role once (`OnionRole::from_flags`, whose rejections `onion::role` tests).
#[test]
fn test_serialized_onion_flags_parse_onto_the_role() -> Result<()> {
    let config = onion_test_config().onion_role(https_exit_role()?);
    let serialized = ProcessorConfigSerialized::try_from(config.clone())?;

    assert!(serialized.advertise_onion_relay && serialized.advertise_onion_exit);
    assert_eq!(
        ProcessorConfig::try_from(serialized)?.onion_role,
        config.onion_role
    );
    Ok(())
}

/// The reserved HTTPS name is valid without a parallel transport discriminator; an exit
/// registers `relay` with it, whose capability carries the process epoch `e_n`, and every build
/// (process start) draws a fresh one (#834 D2).
#[tokio::test]
async fn test_https_onion_exit_registers_relay_with_a_fresh_process_epoch() -> Result<()> {
    let config = onion_test_config().onion_role(https_exit_role()?);
    let start = || test_processor_builder(&config).build();
    let processor = start()?;
    let restarted = start()?;
    let descriptor = processor.online_node_descriptor_at(get_epoch_ms())?;

    assert_eq!(processor.onion_role(), &https_exit_role()?);
    // The test processor keeps no relay store, so it declares that it holds no relay mail.
    assert_eq!(descriptor.capabilities, OnlineNodeCapabilities {
        relay_custody: RelayCustody::HoldsNone,
        ..OnlineNodeCapabilities::onion_relay(processor.onion_process_epoch.get())
    });
    assert_ne!(
        processor.onion_process_epoch.get(),
        restarted.onion_process_epoch.get()
    );
    Ok(())
}

/// An exit registers world-facing symbols of the closed signature only: neither names outside
/// `Σ` nor the identity symbol `relay` parse as an exit service, in code or in a config file.
#[test]
fn test_onion_exit_service_must_be_a_world_facing_symbol() {
    assert_eq!(
        serde_saphyr::from_str::<Vec<OnionServiceName>>("[tcp, https]").ok(),
        Some(vec![OnionServiceName::tcp(), OnionServiceName::https()])
    );
    for outside in ["web", "relay"] {
        assert!(OnionServiceName::parse(outside).is_err());
        assert!(serde_saphyr::from_str::<Vec<OnionServiceName>>(&format!("[{outside}]")).is_err());
    }
}

#[test]
fn test_serialized_remote_rpc_proxy_url_round_trips_and_validates() -> Result<()> {
    use crate::remote_endpoint::RemoteRpcProxy;

    let proxy = RemoteRpcProxy::parse("http://127.0.0.1:18080")?;
    let config = onion_test_config().remote_rpc_proxy(proxy.clone());
    let serialized = ProcessorConfigSerialized::try_from(config)?;
    assert_eq!(
        serialized.remote_rpc_proxy_url.as_deref(),
        Some("http://127.0.0.1:18080/")
    );
    let restored = ProcessorConfig::try_from(serialized.clone())?;
    assert_eq!(restored.remote_rpc_proxy, Some(proxy));

    let mut invalid = serialized;
    invalid.remote_rpc_proxy_url = Some("socks5://127.0.0.1:1080".to_string());
    assert!(matches!(
        ProcessorConfig::try_from(invalid),
        Err(Error::InvalidConfig(_))
    ));
    Ok(())
}