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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
#![cfg(any(target_os = "windows", target_os = "macos"))]
//! This module contains fake/mock implementations of different updater/installer traits
use installer_downloader::delegate::{AppDelegate, AppDelegateQueue, ErrorMessage};
use installer_downloader::environment::{Architecture, Environment};
use installer_downloader::temp::DirectoryProvider;
use installer_downloader::ui_downloader::UiAppDownloaderParameters;
use mullvad_update::app::{
AppCache, AppDownloader, DownloadError, DownloadedInstaller, VerifiedInstaller,
};
use mullvad_update::fetch::ProgressUpdater;
use mullvad_update::format::{Response, SignedResponse};
use mullvad_update::version::{Version, VersionInfo, VersionParameters};
use mullvad_update::version_provider::VersionInfoProvider;
use std::io;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, LazyLock, Mutex};
use std::vec::Vec;
/// Fake version info provider
#[derive(Default)]
pub struct FakeVersionInfoProvider {
pub fail_fetching: Arc<AtomicBool>,
pub dump_metadata_to_file: Option<PathBuf>,
}
pub static FAKE_VERSION: LazyLock<VersionInfo> = LazyLock::new(|| VersionInfo {
stable: Version {
version: "2025.1".parse().unwrap(),
urls: vec!["https://mullvad.net/fakeapp".to_owned()],
size: 1234,
changelog: "a changelog".to_owned(),
sha256: [0u8; 32],
},
beta: None,
});
pub const FAKE_ENVIRONMENT: Environment = Environment {
architecture: Architecture::X86,
};
impl VersionInfoProvider for FakeVersionInfoProvider {
async fn get_version_info(&self, _params: &VersionParameters) -> anyhow::Result<VersionInfo> {
if self.fail_fetching.load(std::sync::atomic::Ordering::SeqCst) {
anyhow::bail!("Failed to fetch version info");
}
Ok(FAKE_VERSION.clone())
}
fn set_metadata_dump_path(&mut self, path: PathBuf) {
self.dump_metadata_to_file = Some(path);
}
}
pub struct FakeDirectoryProvider<const SUCCEED: bool> {}
#[async_trait::async_trait]
impl<const SUCCEEDED: bool> DirectoryProvider for FakeDirectoryProvider<SUCCEEDED> {
async fn create_download_dir() -> anyhow::Result<PathBuf> {
if SUCCEEDED {
Ok(Path::new("/tmp/fake").to_owned())
} else {
anyhow::bail!("Failed to create directory");
}
}
}
/// Downloader for which all steps immediately succeed
pub type FakeAppDownloaderHappyPath = FakeAppDownloader<true, true, true>;
/// Cache for which all steps immediately succeed
pub type FakeAppCacheHappyPath = FakeAppCache<true, FakeInstaller<true, true, true>>;
/// Cache for which the verification step fails
pub type FakeAppCacheVerifyFail = FakeAppCache<true, FakeInstaller<true, false, false>>;
/// A cache that returns nothing.
pub type FakeAppCacheEmpty = FakeAppCache<false, FakeInstaller<true, true, true>>;
/// Downloader for which the verification step fails
pub type FakeAppDownloaderVerifyFail = FakeAppDownloader<true, false, false>;
impl<const A: bool, const B: bool, const C: bool> From<UiAppDownloaderParameters<FakeAppDelegate>>
for FakeAppDownloader<A, B, C>
{
fn from(params: UiAppDownloaderParameters<FakeAppDelegate>) -> Self {
FakeAppDownloader { params }
}
}
/// Fake app downloader
///
/// Parameters:
/// * EXE_SUCCEED - whether fetching the binary succeeds
/// * VERIFY_SUCCEED - whether verifying the binary succeeds
/// * LAUNCH_SUCCEED - whether launching the binary succeeds
pub struct FakeAppDownloader<
const EXE_SUCCEED: bool,
const VERIFY_SUCCEED: bool,
const LAUNCH_SUCCEED: bool,
> {
params: UiAppDownloaderParameters<FakeAppDelegate>,
}
#[derive(Default, PartialEq, PartialOrd)]
pub struct FakeAppCache<const HAS_APP: bool, Installer: DownloadedInstaller + Clone + Default> {
_phantom: PhantomData<Installer>,
}
#[derive(Default, Clone, PartialEq, PartialOrd)]
pub struct FakeInstaller<
const EXE_SUCCEED: bool,
const VERIFY_SUCCEED: bool,
const LAUNCH_SUCCEED: bool,
>;
impl<const EXE_SUCCEED: bool, const VERIFY_SUCCEED: bool, const LAUNCH_SUCCEED: bool> AppDownloader
for FakeAppDownloader<EXE_SUCCEED, VERIFY_SUCCEED, LAUNCH_SUCCEED>
{
async fn download_executable(mut self) -> Result<impl DownloadedInstaller, DownloadError> {
self.params.app_progress.set_url(&self.params.app_url);
self.params.app_progress.clear_progress();
if EXE_SUCCEED {
self.params.app_progress.set_progress(1.);
Ok(FakeInstaller::<EXE_SUCCEED, VERIFY_SUCCEED, LAUNCH_SUCCEED>)
} else {
Err(DownloadError::FetchApp(anyhow::anyhow!(
"fetching app failed"
)))
}
}
}
impl<const HAS_APP: bool, Installer> AppCache for FakeAppCache<HAS_APP, Installer>
where
Installer: DownloadedInstaller + Clone + Default + PartialEq + PartialOrd,
{
type Installer = Installer;
fn new(_directory: PathBuf, _version_params: VersionParameters) -> Self {
Self::default()
}
fn get_cached_installers(self, _metadata: SignedResponse) -> Vec<Self::Installer> {
if HAS_APP {
vec![Installer::default()]
} else {
vec![]
}
}
#[allow(clippy::manual_async_fn)]
fn get_metadata(
&self,
) -> impl std::future::Future<Output = anyhow::Result<SignedResponse>> + Send {
async {
Ok(SignedResponse {
signatures: vec![],
signed: Response::default(),
})
}
}
}
impl<const EXE_SUCCEED: bool, const VERIFY_SUCCEED: bool, const LAUNCH_SUCCEED: bool>
DownloadedInstaller for FakeInstaller<EXE_SUCCEED, VERIFY_SUCCEED, LAUNCH_SUCCEED>
{
async fn verify(self) -> Result<impl VerifiedInstaller, DownloadError> {
if VERIFY_SUCCEED {
Ok(self)
} else {
Err(DownloadError::Verification(anyhow::anyhow!(
"verification failed"
)))
}
}
fn version(&self) -> &mullvad_version::Version {
&mullvad_version::Version {
year: 2042,
incremental: 1337,
pre_stable: None,
dev: None,
}
}
}
impl<const EXE_SUCCEED: bool, const VERIFY_SUCCEED: bool, const LAUNCH_SUCCEED: bool>
VerifiedInstaller for FakeInstaller<EXE_SUCCEED, VERIFY_SUCCEED, LAUNCH_SUCCEED>
{
async fn install(self) -> Result<(), DownloadError> {
if LAUNCH_SUCCEED {
Ok(())
} else {
Err(DownloadError::InstallFailed(io::Error::other(
"install failed",
)))
}
}
}
/// A fake queue that stores callbacks so that tests can run them later.
#[derive(Clone, Default)]
pub struct FakeQueue {
callbacks: Arc<Mutex<Vec<MainThreadCallback>>>,
}
pub type MainThreadCallback = Box<dyn FnOnce(&mut FakeAppDelegate) + Send>;
impl FakeQueue {
/// Run all queued callbacks on the given delegate.
pub fn run_callbacks(&self, delegate: &mut FakeAppDelegate) {
let mut callbacks = self.callbacks.lock().unwrap();
for cb in callbacks.drain(..) {
cb(delegate);
}
}
}
impl AppDelegateQueue<FakeAppDelegate> for FakeQueue {
fn queue_main<F: FnOnce(&mut FakeAppDelegate) + 'static + Send>(&self, callback: F) {
self.callbacks.lock().unwrap().push(Box::new(callback));
}
}
/// A fake [AppDelegate]
#[derive(Default)]
pub struct FakeAppDelegate {
/// Callback registered by `on_download`
pub download_callback: Option<Box<dyn Fn() + Send>>,
/// Callback registered by `on_cancel`
pub cancel_callback: Option<Box<dyn Fn() + Send>>,
/// Callback registered by `on_beta_link`
pub beta_callback: Option<Box<dyn Fn() + Send>>,
/// Callback registered by `on_stable_link`
pub stable_callback: Option<Box<dyn Fn() + Send>>,
/// Callback registered by `on_error_cancel`
pub error_cancel_callback: Option<Box<dyn Fn() + Send>>,
/// Callback registered by `on_error_retry`
pub error_retry_callback: Option<Box<dyn Fn() + Send>>,
/// State of delegate
pub state: DelegateState,
/// Queue used to simulate the main thread
pub queue: FakeQueue,
}
/// A complete state of the UI, including its call history
#[derive(Default, serde::Serialize)]
pub struct DelegateState {
pub status_text: String,
pub download_text: String,
pub download_button_visible: bool,
pub cancel_button_visible: bool,
pub cancel_button_enabled: bool,
pub download_button_enabled: bool,
pub download_progress: u32,
pub download_progress_visible: bool,
pub beta_text_visible: bool,
pub stable_text_visible: bool,
pub error_message_visible: bool,
pub error_message: ErrorMessage,
pub quit: bool,
/// Record of method calls.
pub call_log: Vec<String>,
}
impl AppDelegate for FakeAppDelegate {
type Queue = FakeQueue;
fn on_download<F>(&mut self, callback: F)
where
F: Fn() + Send + 'static,
{
self.state.call_log.push("on_download".into());
self.download_callback = Some(Box::new(callback));
}
fn on_cancel<F>(&mut self, callback: F)
where
F: Fn() + Send + 'static,
{
self.state.call_log.push("on_cancel".into());
self.cancel_callback = Some(Box::new(callback));
}
fn on_beta_link<F>(&mut self, callback: F)
where
F: Fn() + Send + 'static,
{
self.state.call_log.push("on_beta_link".into());
self.beta_callback = Some(Box::new(callback));
}
fn on_stable_link<F>(&mut self, callback: F)
where
F: Fn() + Send + 'static,
{
self.state.call_log.push("on_stable_link".into());
self.stable_callback = Some(Box::new(callback));
}
fn set_status_text(&mut self, text: &str) {
self.state.call_log.push(format!("set_status_text: {text}"));
self.state.status_text = text.to_owned();
}
fn clear_status_text(&mut self) {
self.state.call_log.push("clear_status_text".into());
self.state.status_text = "".to_owned();
}
fn set_download_text(&mut self, text: &str) {
self.state
.call_log
.push(format!("set_download_text: {text}"));
self.state.download_text = text.to_owned();
}
fn clear_download_text(&mut self) {
self.state.call_log.push("clear_download_text".into());
self.state.download_text = "".to_owned();
}
fn show_download_progress(&mut self) {
self.state.call_log.push("show_download_progress".into());
self.state.download_progress_visible = true;
}
fn hide_download_progress(&mut self) {
self.state.call_log.push("hide_download_progress".into());
self.state.download_progress_visible = false;
}
fn set_download_progress(&mut self, complete: u32) {
self.state
.call_log
.push(format!("set_download_progress: {complete}"));
self.state.download_progress = complete;
}
fn clear_download_progress(&mut self) {
self.state.call_log.push("clear_download_progress".into());
self.state.download_progress = 0;
}
fn show_download_button(&mut self) {
self.state.call_log.push("show_download_button".into());
self.state.download_button_visible = true;
}
fn hide_download_button(&mut self) {
self.state.call_log.push("hide_download_button".into());
self.state.download_button_visible = false;
}
fn enable_download_button(&mut self) {
self.state.call_log.push("enable_download_button".into());
self.state.download_button_enabled = true;
}
fn disable_download_button(&mut self) {
self.state.call_log.push("disable_download_button".into());
self.state.download_button_enabled = false;
}
fn show_cancel_button(&mut self) {
self.state.call_log.push("show_cancel_button".into());
self.state.cancel_button_visible = true;
}
fn hide_cancel_button(&mut self) {
self.state.call_log.push("hide_cancel_button".into());
self.state.cancel_button_visible = false;
}
fn enable_cancel_button(&mut self) {
self.state.call_log.push("enable_cancel_button".into());
self.state.cancel_button_enabled = true;
}
fn disable_cancel_button(&mut self) {
self.state.call_log.push("disable_cancel_button".into());
self.state.cancel_button_enabled = false;
}
fn show_beta_text(&mut self) {
self.state.call_log.push("show_beta_text".into());
self.state.beta_text_visible = true;
}
fn hide_beta_text(&mut self) {
self.state.call_log.push("hide_beta_text".into());
self.state.beta_text_visible = false;
}
fn show_stable_text(&mut self) {
self.state.call_log.push("show_stable_text".into());
self.state.stable_text_visible = true;
}
fn hide_stable_text(&mut self) {
self.state.call_log.push("hide_stable_text".into());
self.state.stable_text_visible = false;
}
fn show_error_message(&mut self, message: ErrorMessage) {
self.state.call_log.push(format!(
"show_error_message: {}. retry: {}. cancel: {}",
message.status_text, message.retry_button_text, message.cancel_button_text
));
self.state.error_message = message;
self.state.error_message_visible = true;
}
fn hide_error_message(&mut self) {
self.state.call_log.push("hide_error_message".into());
self.state.error_message_visible = false;
}
fn on_error_message_cancel<F>(&mut self, callback: F)
where
F: Fn() + Send + 'static,
{
self.state.call_log.push("on_error_message_cancel".into());
self.error_cancel_callback = Some(Box::new(callback));
}
fn on_error_message_retry<F>(&mut self, callback: F)
where
F: Fn() + Send + 'static,
{
self.state.call_log.push("on_error_message_retry".into());
self.error_retry_callback = Some(Box::new(callback));
}
fn quit(&mut self) {
self.state.call_log.push("quit".into());
self.state.quit = true;
}
fn queue(&self) -> Self::Queue {
self.queue.clone()
}
}
|