diff options
| author | Markus Pettersson <markus.pettersson@mullvad.net> | 2023-12-07 11:09:33 +0100 |
|---|---|---|
| committer | Markus Pettersson <markus.pettersson@mullvad.net> | 2023-12-12 18:47:51 +0100 |
| commit | 37bf99017d6e8c38d2625144f77ecfd2b0829907 (patch) | |
| tree | 20c78993174b05098ae027f15710332296d6d625 | |
| parent | c3e32eda55f28773890d37c497773405b6fc07f8 (diff) | |
| download | mullvadvpn-37bf99017d6e8c38d2625144f77ecfd2b0829907.tar.xz mullvadvpn-37bf99017d6e8c38d2625144f77ecfd2b0829907.zip | |
Add unit tests for deserializing missing or invalid settings
| -rw-r--r-- | mullvad-daemon/src/settings/mod.rs | 63 |
1 files changed, 62 insertions, 1 deletions
diff --git a/mullvad-daemon/src/settings/mod.rs b/mullvad-daemon/src/settings/mod.rs index e1232394e7..c5639b8b76 100644 --- a/mullvad-daemon/src/settings/mod.rs +++ b/mullvad-daemon/src/settings/mod.rs @@ -407,7 +407,7 @@ impl<'a> SettingsSummary<'a> { #[cfg(test)] mod test { - use super::SettingsPersister; + use super::*; use mullvad_types::settings::SettingsVersion; use serde_json; @@ -491,4 +491,65 @@ mod test { let _ = SettingsPersister::load_from_bytes(settings).unwrap(); } + + /// The [`SettingsPersister`] should always succeed when deserializing a + /// [`Settings`] object from disk. However, there is a distinction between + /// different error cases. + /// + /// If the settings file is missing, it could be because the user starts the + /// app for the first time. As such, we should simply save the default + /// [`Settings`] to disk. + #[tokio::test] + async fn test_deserialize_missing_settings() { + let LoadSettingsResult { + should_save, + settings, + } = SettingsPersister::load_inner(|| async { + Err(Error::ReadError( + "Settings are missing".to_string(), + io::ErrorKind::NotFound.into(), + )) + }) + .await; + + assert!( + should_save, + "Settings should be saved to disk if they didn't exist previously" + ); + + assert!( + settings.block_when_disconnected == false, + "The daemon should not block the internet if settings are missing" + ); + } + + /// The [`SettingsPersister`] should always succeed when deserializing a + /// [`Settings`] object from disk. However, there is a distinction between + /// different error cases. + /// + /// If the settings file is corrupt, we can assume that the user has started + /// the app previously, but we can't know what settings the user have + /// changed. In this case, we should safeguard against leaks by locking down + /// the network before the user initiates a connection attempt or change + /// these settings. + #[tokio::test] + async fn test_deserialize_invalid_settings() { + let LoadSettingsResult { + should_save, + settings, + } = SettingsPersister::load_inner(|| async { + SettingsPersister::load_from_bytes(b"Not a valid settings file") + }) + .await; + + assert!( + should_save, + "Settings should be saved to disk if they have become corrupt" + ); + + assert!( + settings.block_when_disconnected, + "The daemon should block the internet if settings are corrupt" + ); + } } |
