summaryrefslogtreecommitdiffhomepage
path: root/test/test-manager/src/tests/ui.rs
blob: 6cac7d0d5edbdece78862727f3c8807df5f5fb25 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use super::config::TEST_CONFIG;
use super::helpers;
use super::{Error, TestContext};
use mullvad_management_interface::MullvadProxyClient;
use mullvad_types::relay_constraints::{RelayConstraints, RelaySettings};
use mullvad_types::relay_list::{Relay, RelayEndpointData};
use std::{
    collections::BTreeMap,
    fmt::Debug,
    path::{Path, PathBuf},
};
use test_macro::test_function;
use test_rpc::{meta::Os, ExecResult, ServiceClient};

pub async fn run_test<T: AsRef<str> + Debug>(
    rpc: &ServiceClient,
    params: &[T],
) -> Result<ExecResult, Error> {
    let env: [(&str, T); 0] = [];
    run_test_env(rpc, params, env).await
}

pub async fn run_test_env<
    I: IntoIterator<Item = (K, T)> + Debug,
    K: AsRef<str> + Debug,
    T: AsRef<str> + Debug,
>(
    rpc: &ServiceClient,
    params: &[T],
    env: I,
) -> Result<ExecResult, Error> {
    let new_params: Vec<String>;
    let bin_path;

    match TEST_CONFIG.os {
        Os::Linux => {
            bin_path = PathBuf::from("/usr/bin/xvfb-run");

            let ui_runner_path =
                Path::new(&TEST_CONFIG.artifacts_dir).join(&TEST_CONFIG.ui_e2e_tests_filename);
            new_params = std::iter::once(ui_runner_path.to_string_lossy().into_owned())
                .chain(params.iter().map(|param| param.as_ref().to_owned()))
                .collect();
        }
        _ => {
            bin_path =
                Path::new(&TEST_CONFIG.artifacts_dir).join(&TEST_CONFIG.ui_e2e_tests_filename);
            new_params = params
                .iter()
                .map(|param| param.as_ref().to_owned())
                .collect();
        }
    }

    let env: BTreeMap<String, String> = env
        .into_iter()
        .map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
        .collect();

    // env may contain sensitive info
    //log::info!("Running UI tests: {params:?}, env: {env:?}");
    log::info!("Running UI tests: {params:?}");

    let result = rpc
        .exec_env(
            bin_path.to_string_lossy().into_owned(),
            new_params.into_iter(),
            env,
        )
        .await?;

    if !result.success() {
        let stdout = std::str::from_utf8(&result.stdout).unwrap_or("invalid utf8");
        let stderr = std::str::from_utf8(&result.stderr).unwrap_or("invalid utf8");

        log::debug!("UI test failed:\n\nstdout:\n\n{stdout}\n\n{stderr}\n");
    }

    Ok(result)
}

/// Test how various tunnel settings are handled and displayed by the GUI
#[test_function]
pub async fn test_ui_tunnel_settings(
    _: TestContext,
    rpc: ServiceClient,
    mut mullvad_client: MullvadProxyClient,
) -> Result<(), Error> {
    // tunnel-state.spec precondition: a single WireGuard relay should be selected
    log::info!("Select WireGuard relay");
    let entry = helpers::filter_relays(&mut mullvad_client, |relay: &Relay| {
        relay.active && matches!(relay.endpoint_data, RelayEndpointData::Wireguard(_))
    })
    .await?
    .pop()
    .unwrap();

    // The test expects us to be disconnected and logged in but to have a specific relay selected
    let relay_settings = RelaySettings::Normal(RelayConstraints {
        location: helpers::into_constraint(&entry),
        ..Default::default()
    });

    helpers::set_relay_settings(&mut mullvad_client, relay_settings)
        .await
        .expect("failed to update relay settings");

    let ui_result = run_test_env(
        &rpc,
        &["tunnel-state.spec"],
        [
            ("HOSTNAME", entry.hostname.as_str()),
            ("IN_IP", &entry.ipv4_addr_in.to_string()),
            (
                "CONNECTION_CHECK_URL",
                &format!("https://am.i.{}", TEST_CONFIG.mullvad_host),
            ),
        ],
    )
    .await
    .unwrap();
    assert!(ui_result.success());

    Ok(())
}

/// Test whether logging in and logging out work in the GUI
#[test_function(priority = 500)]
pub async fn test_ui_login(_: TestContext, rpc: ServiceClient) -> Result<(), Error> {
    let ui_result = run_test_env(
        &rpc,
        &["login.spec"],
        [("ACCOUNT_NUMBER", &*TEST_CONFIG.account_number)],
    )
    .await
    .unwrap();
    assert!(ui_result.success());

    Ok(())
}

#[test_function(priority = 1000, must_succeed = true)]
async fn test_custom_access_methods_gui(
    _: TestContext,
    rpc: ServiceClient,
    mut mullvad_client: MullvadProxyClient,
) -> Result<(), Error> {
    use mullvad_api::env;
    use mullvad_relay_selector::{RelaySelector, SelectorConfig};
    use talpid_types::net::proxy::CustomProxy;
    // For this test to work, we need to supply the following env-variables:
    //
    // * SHADOWSOCKS_SERVER_IP
    // * SHADOWSOCKS_SERVER_PORT
    // * SHADOWSOCKS_SERVER_CIPHER
    // * SHADOWSOCKS_SERVER_PASSWORD
    //
    // See `gui/test/e2e/installed/state-dependent/api-access-methods.spec.ts`
    // for details. The setup should be the same as in
    // `test_manager::tests::access_methods::test_shadowsocks`.
    //
    // # Note
    //
    // API overrides have to be nullified before proceeding with this test. This
    // is accomplished by setting the env variable
    // `MULLVAD_API_FORCE_DIRECT=false` and restarting the daemon.

    let mut env = helpers::get_app_env();
    env.insert(env::API_FORCE_DIRECT_VAR.to_string(), "0".to_string());

    tokio::time::timeout(
        std::time::Duration::from_secs(60),
        rpc.set_daemon_environment(env),
    )
    .await
    .map_err(|_| Error::DaemonNotRunning)??;

    let gui_test = "api-access-methods.spec";
    let relay_list = mullvad_client.get_relay_locations().await.unwrap();
    let relay_selector = RelaySelector::from_list(SelectorConfig::default(), relay_list);
    let access_method = relay_selector
        .get_bridge_forced()
        .and_then(|proxy| match proxy {
            CustomProxy::Shadowsocks(s) => Some(s),
            _ => None
        })
        .expect("`test_shadowsocks` needs at least one shadowsocks relay to execute. Found none in relay list.");

    let ui_result = run_test_env(
        &rpc,
        &[gui_test],
        [
            (
                "SHADOWSOCKS_SERVER_IP",
                access_method.endpoint.ip().to_string().as_ref(),
            ),
            (
                "SHADOWSOCKS_SERVER_PORT",
                access_method.endpoint.port().to_string().as_ref(),
            ),
            ("SHADOWSOCKS_SERVER_CIPHER", access_method.cipher.as_ref()),
            (
                "SHADOWSOCKS_SERVER_PASSWORD",
                access_method.password.as_ref(),
            ),
        ],
    )
    .await
    .unwrap();

    assert!(ui_result.success());

    // Reset the `api-override` feature.
    tokio::time::timeout(
        std::time::Duration::from_secs(60),
        rpc.set_daemon_environment(helpers::get_app_env()),
    )
    .await
    .map_err(|_| Error::DaemonNotRunning)??;

    Ok(())
}

/// Test settings import / IP overrides in the GUI
///
/// # Note
/// This test expects the daemon to be logged in
#[test_function]
pub async fn test_import_settings_ui(_: TestContext, rpc: ServiceClient) -> Result<(), Error> {
    let ui_result = run_test(&rpc, &["settings-import.spec"]).await?;
    assert!(ui_result.success());
    Ok(())
}