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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
use super::config::TEST_CONFIG;
use super::helpers::{
connect_and_wait, get_app_env, get_package_desc, wait_for_tunnel_state, Pinger,
};
use super::{Error, TestContext};
use mullvad_management_interface::MullvadProxyClient;
use mullvad_types::{constraints::Constraint, relay_constraints};
use test_macro::test_function;
use test_rpc::meta::Os;
use test_rpc::{mullvad_daemon::ServiceStatus, ServiceClient};
use std::time::Duration;
/// Install the last stable version of the app and verify that it is running.
#[test_function(priority = -200)]
pub async fn test_install_previous_app(_: TestContext, rpc: ServiceClient) -> Result<(), Error> {
// verify that daemon is not already running
if rpc.mullvad_daemon_get_status().await? != ServiceStatus::NotRunning {
return Err(Error::DaemonRunning);
}
// install package
log::debug!("Installing old app");
rpc.install_app(get_package_desc(&TEST_CONFIG.previous_app_filename)?)
.await?;
// verify that daemon is running
if rpc.mullvad_daemon_get_status().await? != ServiceStatus::Running {
return Err(Error::DaemonNotRunning);
}
replace_openvpn_cert(&rpc).await?;
// Override env vars
rpc.set_daemon_environment(get_app_env()).await?;
Ok(())
}
/// Upgrade to the "version under test". This test fails if:
///
/// * Leaks (TCP/UDP/ICMP) to a single public IP address are successfully produced during the
/// upgrade.
/// * The installer does not successfully complete.
/// * The VPN service is not running after the upgrade.
#[test_function(priority = -190)]
pub async fn test_upgrade_app(ctx: TestContext, rpc: ServiceClient) -> Result<(), Error> {
// Verify that daemon is running
if rpc.mullvad_daemon_get_status().await? != ServiceStatus::Running {
return Err(Error::DaemonNotRunning);
}
super::account::clear_devices(&super::account::new_device_client())
.await
.expect("failed to clear devices");
// Login to test preservation of device/account
// TODO: Cannot do this now because overriding the API is impossible for releases
//mullvad_client
// .login_account(TEST_CONFIG.account_number.clone())
// .await
// .expect("login failed");
//
// Start blocking
//
log::debug!("Entering blocking error state");
rpc.exec("mullvad", ["debug", "block-connection"])
.await
.expect("Failed to set relay location");
rpc.exec("mullvad", ["connect"])
.await
.expect("Failed to begin connecting");
tokio::time::timeout(super::WAIT_FOR_TUNNEL_STATE_TIMEOUT, async {
// use polling for sake of simplicity
loop {
const FIND_SLICE: &[u8] = b"Blocked:";
let result = rpc
.exec("mullvad", ["status"])
.await
.expect("Failed to poll tunnel status");
if result
.stdout
.windows(FIND_SLICE.len())
.any(|subslice| subslice == FIND_SLICE)
{
break;
}
tokio::time::sleep(Duration::from_secs(1)).await;
}
})
.await
.map_err(|_error| Error::Daemon(String::from("Failed to enter blocking error state")))?;
//
// Begin monitoring outgoing traffic and pinging
//
let pinger = Pinger::start(&rpc).await;
// install new package
log::debug!("Installing new app");
rpc.install_app(get_package_desc(&TEST_CONFIG.current_app_filename)?)
.await?;
// Give it some time to start
tokio::time::sleep(Duration::from_secs(3)).await;
// verify that daemon is running
if rpc.mullvad_daemon_get_status().await? != ServiceStatus::Running {
return Err(Error::DaemonNotRunning);
}
//
// Check if any traffic was observed
//
let guest_ip = pinger.guest_ip;
let monitor_result = pinger.stop().await.unwrap();
assert_eq!(
monitor_result.packets.len(),
0,
"observed unexpected packets from {guest_ip}"
);
// NOTE: Need to create a new `mullvad_client` here after the restart
// otherwise we *probably* can't communicate with the daemon.
let mut mullvad_client = ctx.rpc_provider.new_client().await;
// check if settings were (partially) preserved
log::info!("Sanity checking settings");
let settings = mullvad_client
.get_settings()
.await
.expect("failed to obtain settings");
const EXPECTED_COUNTRY: &str = "xx";
let relay_location_was_preserved = match &settings.relay_settings {
relay_constraints::RelaySettings::Normal(relay_constraints::RelayConstraints {
location:
Constraint::Only(relay_constraints::LocationConstraint::Location(
relay_constraints::GeographicLocationConstraint::Country(country),
)),
..
}) => country == EXPECTED_COUNTRY,
_ => false,
};
assert!(
relay_location_was_preserved,
"relay location was not preserved after upgrade. new settings: {:?}",
settings,
);
// check if account history was preserved
// TODO: Cannot check account history because overriding the API is impossible for releases
/*
let history = mullvad_client
.get_account_history(())
.await
.expect("failed to obtain account history");
assert_eq!(
history.into_inner().token,
Some(TEST_CONFIG.account_number.clone()),
"lost account history"
);
*/
Ok(())
}
/// Uninstall the app version being tested. This verifies
/// that that the uninstaller works, and also that logs,
/// application files, system services are removed.
/// It also tests whether the device is removed from
/// the account.
///
/// # Limitations
///
/// Files due to Electron, temporary files, registry
/// values/keys, and device drivers are not guaranteed
/// to be deleted.
#[test_function(priority = -170, cleanup = false)]
pub async fn test_uninstall_app(
_: TestContext,
rpc: ServiceClient,
mut mullvad_client: MullvadProxyClient,
) -> Result<(), Error> {
if rpc.mullvad_daemon_get_status().await? != ServiceStatus::Running {
return Err(Error::DaemonNotRunning);
}
// Login to test preservation of device/account
// TODO: Remove once we can login before upgrade above
mullvad_client
.login_account(TEST_CONFIG.account_number.clone())
.await
.expect("login failed");
// save device to verify that uninstalling removes the device
// we should still be logged in after upgrading
let uninstalled_device = mullvad_client
.get_device()
.await
.expect("failed to get device data")
.into_device()
.expect("failed to get device")
.device
.id;
log::debug!("Uninstalling app");
rpc.uninstall_app(get_app_env()).await?;
let app_traces = rpc
.find_mullvad_app_traces()
.await
.expect("failed to obtain remaining Mullvad files");
assert!(
app_traces.is_empty(),
"found files after uninstall: {app_traces:?}"
);
if rpc.mullvad_daemon_get_status().await? != ServiceStatus::NotRunning {
return Err(Error::DaemonRunning);
}
// verify that device was removed
let devices = super::account::list_devices_with_retries(&super::account::new_device_client())
.await
.expect("failed to list devices");
assert!(
!devices.iter().any(|device| device.id == uninstalled_device),
"device id {} still exists after uninstall",
uninstalled_device,
);
Ok(())
}
/// Install the app cleanly, failing if the installer doesn't succeed
/// or if the VPN service is not running afterwards.
#[test_function(always_run = true, must_succeed = true, priority = -160)]
pub async fn test_install_new_app(_: TestContext, rpc: ServiceClient) -> Result<(), Error> {
// verify that daemon is not already running
if rpc.mullvad_daemon_get_status().await? != ServiceStatus::NotRunning {
return Err(Error::DaemonRunning);
}
// install package
log::debug!("Installing new app");
rpc.install_app(get_package_desc(&TEST_CONFIG.current_app_filename)?)
.await?;
// verify that daemon is running
if rpc.mullvad_daemon_get_status().await? != ServiceStatus::Running {
return Err(Error::DaemonNotRunning);
}
// Set the log level to trace
rpc.set_daemon_log_level(test_rpc::mullvad_daemon::Verbosity::Trace)
.await?;
replace_openvpn_cert(&rpc).await?;
// Override env vars
rpc.set_daemon_environment(get_app_env()).await?;
Ok(())
}
/// Install the multiple times starting from a connected state with auto-connect
/// disabled, failing if the app starts in a disconnected state.
///
/// This test is supposed to guard against regressions to this fix included in
/// the 2021.3-beta1 release:
/// https://github.com/mullvad/mullvadvpn-app/blob/2021.3-beta1/CHANGELOG.md#security
#[test_function(priority = -150)]
pub async fn test_installation_idempotency(
_: TestContext,
rpc: ServiceClient,
mut mullvad_client: MullvadProxyClient,
) -> Result<(), Error> {
// Connect to any relay. This forces the daemon to enter a secured target state
connect_and_wait(&mut mullvad_client)
.await
.or_else(|error| match error {
Error::UnexpectedErrorState(_) => Ok(()),
err => Err(err),
})?;
// Disable auto-connect
mullvad_client
.set_auto_connect(false)
.await
.expect("failed to enable auto-connect");
// Check for traffic leaks during the installation processes.
//
// Start continously pinging while monitoring the network traffic. No
// traffic should be observed going outside of the tunnel during either
// installation process.
let pinger = Pinger::start(&rpc).await;
for _ in 1..=2 {
// install package
log::debug!("Installing new app");
rpc.install_app(get_package_desc(&TEST_CONFIG.current_app_filename)?)
.await?;
// verify that the daemon starts in a non-disconnected state
wait_for_tunnel_state(mullvad_client.clone(), |state| !state.is_disconnected())
.await
.map_err(|err| {
log::error!(
"App did not start in the expected `Connected` state after the installation process."
);
err
})?;
// Wait for an arbitrary amount of time. The point is that the pinger
// should be able to ping while the newly installed app is running.
if let Some(delay) = pinger.period().checked_mul(3) {
tokio::time::sleep(delay).await;
}
}
// Make sure that no network leak occured during any installation process.
let guest_ip = pinger.guest_ip;
let monitor_result = pinger.stop().await.unwrap();
assert_eq!(
monitor_result.packets.len(),
0,
"observed unexpected packets from {guest_ip}"
);
Ok(())
}
async fn replace_openvpn_cert(rpc: &ServiceClient) -> Result<(), Error> {
use std::path::Path;
const SOURCE_CERT_FILENAME: &str = "openvpn.ca.crt";
const DEST_CERT_FILENAME: &str = "ca.crt";
let dest_dir = match TEST_CONFIG.os {
Os::Windows => "C:\\Program Files\\Mullvad VPN\\resources",
Os::Linux => "/opt/Mullvad VPN/resources",
Os::Macos => "/Applications/Mullvad VPN.app/Contents/Resources",
};
rpc.copy_file(
Path::new(&TEST_CONFIG.artifacts_dir)
.join(SOURCE_CERT_FILENAME)
.as_os_str()
.to_string_lossy()
.into_owned(),
Path::new(dest_dir)
.join(DEST_CERT_FILENAME)
.as_os_str()
.to_string_lossy()
.into_owned(),
)
.await
.map_err(Error::Rpc)
}
|