systemd-resolved-rs 0.1.1

A compatibility-oriented reimplementation of systemd-resolved
#[cfg(test)]
mod test_26_request_flags {
    use super::*;
    use crate::dbus_resolve1_abi::flags::{
        SD_RESOLVED_DNS, SD_RESOLVED_MDNS_IPV4, SD_RESOLVED_MDNS_IPV6,
        SD_RESOLVED_NO_NETWORK, SD_RESOLVED_NO_SYNTHESIZE,
    };

    #[test]
    fn no_synthesize_and_no_network_exclude_localhost() {
        let mut config = Config::default();
        config.upstreams.clear();
        config.fallback_upstreams.clear();
        let resolver = Resolver::new(config);
        let query = make_query("localhost", TYPE_A, 0x7600).expect("localhost query");
        resolver
            .query_on_link_with_flags(
                &query,
                QueryMode::Full,
                None,
                SD_RESOLVED_NO_SYNTHESIZE | SD_RESOLVED_NO_NETWORK,
            )
            .expect_err("synthesis and network are both disabled");
    }

    #[test]
    fn no_network_still_allows_synthetic_answers() {
        let resolver = Resolver::new(Config::default());
        let query = make_query("localhost", TYPE_A, 0x7601).expect("localhost query");
        let (_, flags) = resolver
            .query_on_link_with_flags(
                &query,
                QueryMode::Full,
                None,
                SD_RESOLVED_NO_NETWORK,
            )
            .expect("synthetic response");
        assert_eq!(flags, synthetic_response_flags(0, &query));
    }

    #[test]
    fn protocol_masks_exclude_incompatible_scopes() {
        let resolver = Resolver::new(Config::default());
        let local = make_query("printer.local", TYPE_A, 0x7602).expect("mDNS query");
        assert!(matches!(
            resolver.query_on_link_with_flags(&local, QueryMode::Full, None, SD_RESOLVED_DNS),
            Err(ResolveError::NoSuchResourceRecord)
        ));

        let dns = make_query("example.test", TYPE_A, 0x7603).expect("DNS query");
        assert!(matches!(
            resolver.query_on_link_with_flags(
                &dns,
                QueryMode::Full,
                None,
                SD_RESOLVED_MDNS_IPV4 | SD_RESOLVED_MDNS_IPV6,
            ),
            Err(ResolveError::NoNameServers)
        ));
    }

    #[test]
    fn synthetic_protocol_follows_the_request_mask() {
        let resolver = Resolver::new(Config::default());
        let query = make_query("localhost", TYPE_A, 0x7604).expect("localhost query");
        let (_, flags) = resolver
            .query_on_link_with_flags(
                &query,
                QueryMode::Full,
                None,
                crate::dbus_resolve1_abi::flags::SD_RESOLVED_LLMNR_IPV4,
            )
            .expect("LLMNR-labelled synthetic response");
        assert_ne!(flags & crate::dbus_resolve1_abi::flags::SD_RESOLVED_LLMNR_IPV4, 0);
        assert_eq!(flags & SD_RESOLVED_DNS, 0);
    }

    #[test]
    fn output_only_flags_are_rejected() {
        assert!(query_flags_are_valid(0, 0));
        assert!(!query_flags_are_valid(
            crate::dbus_resolve1_abi::flags::SD_RESOLVED_FROM_NETWORK,
            0
        ));
        assert!(!query_flags_are_valid(
            crate::dbus_resolve1_abi::flags::SD_RESOLVED_REQUIRE_PRIMARY
                | crate::dbus_resolve1_abi::flags::SD_RESOLVED_CLAMP_TTL,
            0
        ));
    }
}