summaryrefslogtreecommitdiffhomepage
path: root/test/test-manager/src/tests/macos.rs
blob: 140f77bb10450edf049097b5005ae3a07d1b1ccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! macOS-specific tests.

use anyhow::{Context, bail, ensure};
use mullvad_management_interface::MullvadProxyClient;
use std::net::{Ipv4Addr, SocketAddr};
use test_macro::test_function;
use test_rpc::ServiceClient;

use super::TestContext;

/// Test that we can add and remove IP "aliases" to network interfaces.
///
/// This is effectively testing that macOS behaves as expected, and that future versions of it
/// don't break this functionality.
#[test_function(target_os = "macos")]
async fn test_ifconfig_add_alias(
    _: TestContext,
    rpc: ServiceClient,
    _: MullvadProxyClient,
) -> anyhow::Result<()> {
    let alias = Ipv4Addr::new(127, 123, 123, 123);
    let interface = "lo0";

    log::info!("Will try to assign alias {alias} to interface {interface}");

    // Sanity-check that alias does not exist before we add it.
    ensure!(
        !alias_exists(&rpc, interface, alias).await?,
        "Alias shouldn't exist before it's created. Was it left over from a previous test?"
    );

    // Add alias and assert that it exists.
    rpc.ifconfig_alias_add(interface, alias).await?;
    ensure!(
        alias_exists(&rpc, interface, alias).await?,
        "Alias should have been created!"
    );

    // Ensure that we clean up the alias after the test, even if it fails
    let rpc2 = rpc.clone();
    let _cleanup_guard = scopeguard::guard((), |()| {
        log::info!("Cleaning up after test_ifconfig_add_alias");

        let Ok(runtime_handle) = tokio::runtime::Handle::try_current() else {
            log::error!("Missing tokio runtime");
            return;
        };

        runtime_handle.spawn(async move {
            // Ensure that the alias is removed even if the test fails.
            if let Err(e) = rpc2.ifconfig_alias_remove(interface, alias).await {
                log::error!("Failed to remove alias {alias} from interface {interface}: {e}");
            }
        });
    });

    // Assert that we can bind to the alias.
    rpc.send_udp(
        None,
        SocketAddr::from((alias, 0)),
        SocketAddr::from((Ipv4Addr::LOCALHOST, 1234)),
    )
    .await
    .context("Failed to bind to alias")?;

    // Remove alias and assert that it doesn't exist.
    rpc.ifconfig_alias_remove(interface, alias).await?;
    ensure!(
        !alias_exists(&rpc, interface, alias).await?,
        "Alias should have been removed!"
    );

    Ok(())
}

/// Check if an IP alias exists for `interface`.
async fn alias_exists(
    rpc: &ServiceClient,
    interface: &str,
    alias: Ipv4Addr,
) -> anyhow::Result<bool> {
    let alias = alias.to_string();
    let result = rpc.exec("ifconfig", [interface]).await?;

    let stdout = String::from_utf8(result.stdout)?;
    let stderr = String::from_utf8(result.stderr)?;

    if result.code != Some(0) {
        log::error!("ifconfig stdout:\n{stdout}");
        log::error!("ifconfig stderr:\n{stderr}");
        bail!("`ifconfig` exited with code {:?}", result.code);
    }

    Ok(stdout.contains(&alias))
}