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
|
use super::helpers::{get_package_desc, AbortOnDrop};
use super::{Error, TestContext};
use super::config::TEST_CONFIG;
use crate::network_monitor::{start_packet_monitor, MonitorOptions};
use mullvad_management_interface::types;
use std::{
collections::HashMap,
net::{SocketAddr, ToSocketAddrs},
time::Duration,
};
use test_macro::test_function;
use test_rpc::meta::Os;
use test_rpc::{mullvad_daemon::ServiceStatus, Interface, ServiceClient};
/// 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> {
let inet_destination: SocketAddr = "1.1.1.1:1337".parse().unwrap();
// 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)
.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", ["relay", "set", "location", "xx"])
.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::DaemonError(String::from("Failed to enter blocking error state")))?;
//
// Begin monitoring outgoing traffic and pinging
//
let guest_ip = rpc
.get_interface_ip(Interface::NonTunnel)
.await
.expect("failed to obtain tunnel IP");
log::debug!("Guest IP: {guest_ip}");
log::debug!("Monitoring outgoing traffic");
let monitor = start_packet_monitor(
move |packet| {
// NOTE: Many packets will likely be observed for API traffic. Rather than filtering all
// of those specifically, simply fail if our probes are observed.
packet.source.ip() == guest_ip && packet.destination.ip() == inet_destination.ip()
},
MonitorOptions::default(),
)
.await;
let ping_rpc = rpc.clone();
let probe_sender = AbortOnDrop::new(tokio::spawn(async move {
loop {
super::helpers::send_guest_probes_without_monitor(
ping_rpc.clone(),
None,
inet_destination,
)
.await;
tokio::time::sleep(Duration::from_secs(1)).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 probe_sender = probe_sender.into_inner();
probe_sender.abort();
let _ = probe_sender.await;
let monitor_result = monitor.into_result().await.unwrap();
assert_eq!(
monitor_result.packets.len(),
0,
"observed unexpected packets from {guest_ip}"
);
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")
.into_inner();
const EXPECTED_COUNTRY: &str = "xx";
let relay_location_was_preserved = match &settings.relay_settings {
Some(types::RelaySettings {
endpoint:
Some(types::relay_settings::Endpoint::Normal(types::NormalRelaySettings {
location:
Some(types::LocationConstraint {
r#type:
Some(types::location_constraint::Type::Location(
types::GeographicLocationConstraint { 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: mullvad_management_interface::ManagementServiceClient,
) -> 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_inner();
let uninstalled_device = uninstalled_device
.device
.expect("missing account/device")
.device
.expect("missing device id")
.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)
.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(())
}
fn get_app_env() -> HashMap<String, String> {
let mut map = HashMap::new();
let api_host = format!("api.{}", TEST_CONFIG.mullvad_host);
let api_addr = format!("{api_host}:443")
.to_socket_addrs()
.expect("failed to resolve API host")
.next()
.unwrap();
map.insert("MULLVAD_API_HOST".to_string(), api_host);
map.insert("MULLVAD_API_ADDR".to_string(), api_addr.to_string());
map
}
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 rpc.get_os().await.expect("failed to get 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)
}
|