summaryrefslogtreecommitdiffhomepage
path: root/test/test-manager/src/tests/mod.rs
blob: ada613ca3465b3625c7ade1a3102a0f0b30bcbe9 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
mod account;
pub mod config;
mod dns;
mod helpers;
mod install;
mod settings;
mod test_metadata;
mod tunnel;
mod tunnel_state;
mod ui;

use crate::mullvad_daemon::RpcClientProvider;
use anyhow::Context;
use helpers::reset_relay_settings;
pub use test_metadata::TestMetadata;
use test_rpc::ServiceClient;

use futures::future::BoxFuture;

use mullvad_management_interface::{
    types::{self, Settings},
    ManagementServiceClient,
};
use once_cell::sync::OnceCell;
use std::time::Duration;

const PING_TIMEOUT: Duration = Duration::from_secs(3);
const WAIT_FOR_TUNNEL_STATE_TIMEOUT: Duration = Duration::from_secs(40);

#[derive(Clone)]
pub struct TestContext {
    pub rpc_provider: RpcClientProvider,
}

pub type TestWrapperFunction = Box<
    dyn Fn(
        TestContext,
        ServiceClient,
        Box<dyn std::any::Any + Send>,
    ) -> BoxFuture<'static, Result<(), Error>>,
>;

#[derive(err_derive::Error, Debug)]
pub enum Error {
    #[error(display = "RPC call failed")]
    Rpc(#[source] test_rpc::Error),

    #[error(display = "Timeout waiting for ping")]
    PingTimeout,

    #[error(display = "geoip lookup failed")]
    GeoipError(test_rpc::Error),

    #[error(display = "Found running daemon unexpectedly")]
    DaemonRunning,

    #[error(display = "Daemon unexpectedly not running")]
    DaemonNotRunning,

    #[error(display = "The daemon returned an error: {}", _0)]
    DaemonError(String),

    #[error(display = "Failed to parse gRPC response")]
    InvalidGrpcResponse(#[error(source)] types::FromProtobufTypeError),

    #[cfg(target_os = "macos")]
    #[error(display = "An error occurred: {}", _0)]
    Other(String),
}

static DEFAULT_SETTINGS: OnceCell<Settings> = OnceCell::new();

/// Initializes `DEFAULT_SETTINGS`. This has only has an effect the first time it's called.
pub async fn init_default_settings(mullvad_client: &mut ManagementServiceClient) {
    if DEFAULT_SETTINGS.get().is_none() {
        let settings: Settings = mullvad_client
            .get_settings(())
            .await
            .expect("Failed to obtain settings")
            .into_inner();
        DEFAULT_SETTINGS.set(settings).unwrap();
    }
}

/// Restore settings to `DEFAULT_SETTINGS`.
///
/// # Panics
///
/// `DEFAULT_SETTINGS` must be initialized using `init_default_settings` before any settings are
/// modified, or this function panics.
pub async fn cleanup_after_test(
    mullvad_client: &mut ManagementServiceClient,
) -> anyhow::Result<()> {
    log::debug!("Cleaning up daemon in test cleanup");

    let default_settings = DEFAULT_SETTINGS
        .get()
        .expect("default settings were not initialized");

    reset_relay_settings(mullvad_client).await?;

    mullvad_client
        .set_auto_connect(default_settings.auto_connect)
        .await
        .context("Could not set auto connect in cleanup")?;
    mullvad_client
        .set_allow_lan(default_settings.allow_lan)
        .await
        .context("Could not set allow lan in cleanup")?;
    mullvad_client
        .set_show_beta_releases(default_settings.show_beta_releases)
        .await
        .context("Could not set show beta releases in cleanup")?;
    mullvad_client
        .set_bridge_state(default_settings.bridge_state.clone().unwrap())
        .await
        .context("Could not set bridge state in cleanup")?;
    mullvad_client
        .set_bridge_settings(default_settings.bridge_settings.clone().unwrap())
        .await
        .context("Could not set bridge settings in cleanup")?;
    mullvad_client
        .set_obfuscation_settings(default_settings.obfuscation_settings.clone().unwrap())
        .await
        .context("Could set obfuscation settings in cleanup")?;
    mullvad_client
        .set_block_when_disconnected(default_settings.block_when_disconnected)
        .await
        .context("Could not set block when disconnected setting in cleanup")?;
    mullvad_client
        .clear_split_tunnel_apps(())
        .await
        .context("Could not clear split tunnel apps in cleanup")?;
    mullvad_client
        .clear_split_tunnel_processes(())
        .await
        .context("Could not clear split tunnel processes in cleanup")?;
    mullvad_client
        .set_dns_options(
            default_settings
                .tunnel_options
                .as_ref()
                .unwrap()
                .dns_options
                .as_ref()
                .unwrap()
                .clone(),
        )
        .await
        .context("Could not clear dns options in cleanup")?;
    mullvad_client
        .set_quantum_resistant_tunnel(
            default_settings
                .tunnel_options
                .as_ref()
                .unwrap()
                .wireguard
                .as_ref()
                .unwrap()
                .quantum_resistant
                .as_ref()
                .unwrap()
                .clone(),
        )
        .await
        .context("Could not clear PQ options in cleanup")?;

    Ok(())
}