Skip to content

Commit 0f1f3f8

Browse files
Merge branch 'add-unit-test-for-resetting-daemon-settings-des-2584'
2 parents 0111ceb + 81ce6da commit 0f1f3f8

File tree

2 files changed

+125
-8
lines changed

2 files changed

+125
-8
lines changed

mullvad-types/src/settings/dns.rs

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct DnsOptions {
1919
}
2020

2121
/// Default DNS config
22-
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
22+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
2323
#[serde(default)]
2424
pub struct DefaultDnsOptions {
2525
pub block_ads: bool,
@@ -30,15 +30,57 @@ pub struct DefaultDnsOptions {
3030
pub block_social_media: bool,
3131
}
3232

33-
/// Custom DNS config
34-
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
35-
pub struct CustomDnsOptions {
36-
pub addresses: Vec<IpAddr>,
37-
}
38-
3933
impl DefaultDnsOptions {
34+
/// Create [`DefaultDnsOptions`] which does not configure DNS.
35+
pub const fn new() -> Self {
36+
Self {
37+
block_ads: false,
38+
block_trackers: false,
39+
block_malware: false,
40+
block_adult_content: false,
41+
block_gambling: false,
42+
block_social_media: false,
43+
}
44+
}
45+
46+
/// Enable ads DNS content blocker.
47+
pub const fn block_ads(mut self) -> Self {
48+
self.block_ads = true;
49+
self
50+
}
51+
52+
/// Enable trackers DNS content blocker.
53+
pub const fn block_trackers(mut self) -> Self {
54+
self.block_trackers = true;
55+
self
56+
}
57+
58+
/// Enable malware DNS content blocker.
59+
pub const fn block_malware(mut self) -> Self {
60+
self.block_malware = true;
61+
self
62+
}
63+
64+
/// Enable adult content DNS content blocker.
65+
pub const fn block_adult_content(mut self) -> Self {
66+
self.block_adult_content = true;
67+
self
68+
}
69+
70+
/// Enable gambling DNS content blocker.
71+
pub const fn block_gambling(mut self) -> Self {
72+
self.block_gambling = true;
73+
self
74+
}
75+
76+
/// Enable social media DNS content blocker.
77+
pub const fn block_social_media(mut self) -> Self {
78+
self.block_social_media = true;
79+
self
80+
}
81+
4082
/// Return whether any content blockers are enabled.
41-
pub fn any_blockers_enabled(&self) -> bool {
83+
pub const fn any_blockers_enabled(&self) -> bool {
4284
let DefaultDnsOptions {
4385
block_ads,
4486
block_trackers,
@@ -56,3 +98,15 @@ impl DefaultDnsOptions {
5698
|| block_social_media
5799
}
58100
}
101+
102+
impl Default for DefaultDnsOptions {
103+
fn default() -> DefaultDnsOptions {
104+
DefaultDnsOptions::new()
105+
}
106+
}
107+
108+
/// Custom DNS config
109+
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
110+
pub struct CustomDnsOptions {
111+
pub addresses: Vec<IpAddr>,
112+
}

test/test-manager/src/tests/settings.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use super::{
33
helpers::{connect_and_wait, send_guest_probes},
44
};
55

6+
use anyhow::{Context, ensure};
67
use mullvad_management_interface::MullvadProxyClient;
8+
use mullvad_types::settings::{DefaultDnsOptions, DnsOptions, Settings};
79
use std::net::SocketAddr;
810
use test_macro::test_function;
911
use test_rpc::ServiceClient;
@@ -185,3 +187,64 @@ pub async fn test_lockdown(
185187

186188
Ok(())
187189
}
190+
191+
/// Reset settings to their default values.
192+
///
193+
/// The account should still be logged in after resetting settings.
194+
#[test_function]
195+
pub async fn test_reset_settings(
196+
_: TestContext,
197+
_: ServiceClient,
198+
mut mullvad_client: MullvadProxyClient,
199+
) -> anyhow::Result<()> {
200+
// Change some settings to non-default values.
201+
{
202+
let err = "Failed to generate random settings";
203+
mullvad_client.set_allow_lan(true).await.context(err)?;
204+
mullvad_client
205+
.set_dns_options(DnsOptions {
206+
default_options: DefaultDnsOptions::new().block_ads().block_malware(),
207+
..Default::default()
208+
})
209+
.await
210+
.context(err)?;
211+
mullvad_client.set_lockdown_mode(true).await.context(err)?;
212+
}
213+
// Note: We do not double-check that the daemon reports these settings changes. We trust other
214+
// E2E tests to cover this.
215+
// Soft-reset settings by invoking the `reset-settings` rpc.
216+
mullvad_client
217+
.reset_settings()
218+
.await
219+
.context("Failed to reset settings")?;
220+
// Check that all changed settings have indeed been reset.
221+
let daemon = mullvad_client
222+
.get_settings()
223+
.await
224+
.context("Failed to get settings")?;
225+
let default = Settings::default();
226+
// Santiy-check that data that shouldn't have been touched have indeed been kept intact.
227+
// Are we still logged in?
228+
// Are the custom lists intact?
229+
ensure!(
230+
mullvad_client.get_account_history().await?.is_some(),
231+
"Logged out after reset settings RPC"
232+
);
233+
// Note: We can not compare `daemon` with `default` directly because Settings::default is
234+
// non-deterministic ..
235+
ensure!(
236+
daemon.lockdown_mode == default.lockdown_mode,
237+
"Lockdown mode was not reset"
238+
);
239+
ensure!(
240+
daemon.allow_lan == default.allow_lan,
241+
"Lockdown mode was not reset"
242+
);
243+
ensure!(
244+
daemon.tunnel_options == default.tunnel_options,
245+
"Lockdown mode was not reset"
246+
);
247+
// TODO: check other data as well, such as log files. Do this after having written such a test
248+
// first.
249+
Ok(())
250+
}

0 commit comments

Comments
 (0)