Cotton code
100 lines · 3.6 KB · RustRaw
//! A relay's address is a value: what it builds, what it reads back, and what//! it refuses to be made from.use super::*;#[test]fn cottons_own_relay_is_the_default_and_builds_the_addresses_everything_used_to_spell() {    let ours = RelayAddress::cotton();    assert_eq!(ours.domain(), DEFAULT_RELAY_DOMAIN);    assert_eq!(ours.tunnel_host(), DEFAULT_TUNNEL_HOST);    assert_eq!(ours.host("harbour-lamp"), "harbour-lamp.relay.cottondb.com");    assert_eq!(        ours.origin("harbour-lamp"),        "https://harbour-lamp.relay.cottondb.com"    );    assert_eq!(        ours.directory_origin(),        "https://directory.relay.cottondb.com"    );}/// RULE: every address a household is reached at is made from the relay/// address it was handed — a household on its own relay gets its own hosts/// and none of Cotton's.#[test]fn a_households_own_relay_builds_its_own_addresses_and_none_of_ours() {    let theirs =        RelayAddress::new("relay.example.org", "tunnel.example.org").expect("two hostnames");    assert_eq!(theirs.tunnel_host(), "tunnel.example.org");    assert_eq!(        theirs.origin("harbour-lamp"),        "https://harbour-lamp.relay.example.org"    );    assert_eq!(        theirs.directory_origin(),        "https://directory.relay.example.org"    );    for built in [        theirs.host("harbour-lamp"),        theirs.origin("harbour-lamp"),        theirs.directory_origin(),    ] {        assert!(!built.contains("cottondb.com"), "{built}");    }    // And it does not answer for a name under ours, nor ours for one under it.    assert!(!theirs.holds("harbour-lamp.relay.cottondb.com"));    assert!(!RelayAddress::cotton().holds("harbour-lamp.relay.example.org"));}#[test]fn reads_the_label_back_only_from_a_host_under_its_own_domain() {    let ours = RelayAddress::cotton();    assert_eq!(        ours.label_of_host("harbour-lamp.relay.cottondb.com"),        Some("harbour-lamp")    );    // Deeper, empty and reserved labels are the caller's to refuse.    assert_eq!(ours.label_of_host("a.b.relay.cottondb.com"), Some("a.b"));    assert_eq!(ours.label_of_host(".relay.cottondb.com"), Some(""));    // The domain itself is the relay's and has no label.    assert_eq!(ours.label_of_host("relay.cottondb.com"), None);    assert!(ours.holds("relay.cottondb.com"));    // A look-alike is not under the domain: the dot is part of the match.    assert_eq!(ours.label_of_host("evilrelay.cottondb.com"), None);    assert!(!ours.holds("evilrelay.cottondb.com"));    assert_eq!(ours.label_of_host("x.relay.cottondb.com.evil.test"), None);}/// RULE: a relay address is two bare hostnames and is refused, never repaired/// — it is what every device of the household dials and signs.#[test]fn only_two_bare_lower_case_hostnames_make_an_address() {    assert!(RelayAddress::new("relay.example.org", "tunnel.example.org").is_ok());    for domain in [        "",        "localhost",        "Relay.example.org",        "https://relay.example.org",        "relay.example.org/",        "relay.example.org:8443",        "relay..example.org",        "-relay.example.org",        "relay-.example.org",        "relay.example.org.",        "relay example.org",    ] {        assert_eq!(            RelayAddress::new(domain, "tunnel.example.org"),            Err(RelayAddressRefusal::Domain),            "{domain:?}"        );    }    for tunnel in ["", "tunnel", "wss://tunnel.example.org", "tunnel.example.org/tunnel/v1"] {        assert_eq!(            RelayAddress::new("relay.example.org", tunnel),            Err(RelayAddressRefusal::Tunnel),            "{tunnel:?}"        );    }}