automatic-timezoned 2.0.173

Automatically update system timezone based on location
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
use std::os::unix::net::UnixStream;
use std::sync::{Arc, Mutex, MutexGuard};

use futures_util::future::try_join;
use zbus::{
    connection::Builder, interface, message::Header, names::BusName, object_server::SignalEmitter,
    Connection, Guid, ObjectPath,
};

const CLIENT_PATH: &str = "/org/freedesktop/GeoClue2/Client/1";
const OLD_LOCATION_PATH: &str = "/org/freedesktop/GeoClue2/Location/0";
fn check_destination(header: &Header<'_>, expected: &str) -> zbus::fdo::Result<()> {
    let destination = header.destination().map(BusName::as_str);
    if destination == Some(expected) {
        Ok(())
    } else {
        Err(zbus::fdo::Error::Failed(format!(
            "unexpected destination: {destination:?}"
        )))
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Update {
    Squamish,
    Oyonnax,
    OutOfRange,
    Kampot,
    Missing,
}

#[derive(Clone, Copy)]
struct Location {
    path: &'static str,
    latitude: f64,
    longitude: f64,
    read_error: Option<&'static str>,
}

const SQUAMISH: Location = Location {
    path: "/org/freedesktop/GeoClue2/Location/1",
    latitude: 49.701_633,
    longitude: -123.155_815,
    read_error: None,
};

const OYONNAX: Location = Location {
    path: "/org/freedesktop/GeoClue2/Location/2",
    latitude: 46.257_580,
    longitude: 5.656_080,
    read_error: None,
};

const OUT_OF_RANGE: Location = Location {
    path: "/org/freedesktop/GeoClue2/Location/empty",
    latitude: 91.0,
    longitude: 0.0,
    read_error: None,
};

const KAMPOT: Location = Location {
    path: "/org/freedesktop/GeoClue2/Location/3",
    latitude: 10.591_015_4,
    longitude: 104.126_958_1,
    read_error: None,
};

const MISSING: Location = Location {
    path: "/org/freedesktop/GeoClue2/Location/missing",
    latitude: 0.0,
    longitude: 0.0,
    read_error: Some("location not found"),
};

impl Update {
    pub(super) const ALL: [Self; 5] = [
        Self::Squamish,
        Self::Oyonnax,
        Self::OutOfRange,
        Self::Kampot,
        Self::Missing,
    ];

    const fn location(self) -> &'static Location {
        match self {
            Self::Squamish => &SQUAMISH,
            Self::Oyonnax => &OYONNAX,
            Self::OutOfRange => &OUT_OF_RANGE,
            Self::Kampot => &KAMPOT,
            Self::Missing => &MISSING,
        }
    }

    pub(super) fn path(self) -> &'static str {
        self.location().path
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Failure {
    GetClient,
    DesktopId,
    DistanceThreshold,
    AccuracyLevel,
    Start,
    SetTimezone,
    Latitude,
    Longitude,
}

#[derive(Default)]
pub(super) struct Config {
    pub(super) fail: Option<Failure>,
    pub(super) updates: &'static [Update],
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum Event {
    GetClient,
    DesktopId(String),
    DistanceThreshold(u32),
    AccuracyLevel(u32),
    Start,
    LatitudeRead(Update),
    LongitudeRead(Update),
    SetTimezone(SetTimezoneCall),
}

impl Event {
    pub(super) fn failure(&self) -> Failure {
        match self {
            Self::GetClient => Failure::GetClient,
            Self::DesktopId(_) => Failure::DesktopId,
            Self::DistanceThreshold(_) => Failure::DistanceThreshold,
            Self::AccuracyLevel(_) => Failure::AccuracyLevel,
            Self::Start => Failure::Start,
            Self::LatitudeRead(_) => Failure::Latitude,
            Self::LongitudeRead(_) => Failure::Longitude,
            Self::SetTimezone(_) => Failure::SetTimezone,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct SetTimezoneCall {
    pub(super) timezone: String,
    pub(super) interactive: bool,
}

#[derive(Clone)]
struct MockState {
    events: Arc<Mutex<Vec<Event>>>,
    fail: Option<Failure>,
}

impl MockState {
    fn new(fail: Option<Failure>) -> Self {
        Self {
            events: Arc::new(Mutex::new(Vec::new())),
            fail,
        }
    }

    fn lock_events(&self) -> MutexGuard<'_, Vec<Event>> {
        self.events.lock().expect("mock event lock poisoned")
    }

    fn record(&self, event: Event) -> zbus::fdo::Result<()> {
        let failure = event.failure();
        self.lock_events().push(event);
        if self.fail == Some(failure) {
            Err(zbus::fdo::Error::Failed(format!("{failure:?} failed")))
        } else {
            Ok(())
        }
    }

    fn events(&self) -> Vec<Event> {
        self.lock_events().clone()
    }
}

pub(super) struct MockBus {
    server: Connection,
    client: Connection,
    state: MockState,
}

impl MockBus {
    pub(super) async fn start(config: Config) -> Self {
        let (server_socket, client_socket) =
            UnixStream::pair().expect("create the p2p socket pair");
        // A fixed GUID keeps the p2p handshake deterministic.
        let guid = Guid::from_static_str("0123456789abcdef0123456789abcdef")
            .expect("parse the fixed p2p GUID");
        let state = MockState::new(config.fail);

        let mut server_builder = Builder::unix_stream(server_socket)
            .server(guid)
            .p2p()
            .serve_at(
                "/org/freedesktop/GeoClue2/Manager",
                MockManager {
                    state: state.clone(),
                },
            )
            .serve_at(
                CLIENT_PATH,
                MockClient {
                    state: state.clone(),
                    updates: config.updates,
                },
            );
        for update in Update::ALL {
            server_builder = server_builder.serve_at(
                update.path(),
                MockLocation {
                    update,
                    state: state.clone(),
                },
            );
        }
        let server_builder = server_builder.serve_at(
            "/org/freedesktop/timedate1",
            MockTimedate {
                state: state.clone(),
            },
        );
        let (server, client) = try_join(
            server_builder.build(),
            Builder::unix_stream(client_socket).p2p().build(),
        )
        .await
        .expect("build the p2p server and client");

        Self {
            server,
            client,
            state,
        }
    }

    pub(super) fn client(&self) -> Connection {
        self.client.clone()
    }

    pub(super) fn events(&self) -> Vec<Event> {
        self.state.events()
    }

    pub(super) async fn emit_update(&self, update: Update) -> zbus::Result<()> {
        let emitter =
            SignalEmitter::new(&self.server, CLIENT_PATH).expect("create the signal emitter");
        let old = ObjectPath::from_static_str(OLD_LOCATION_PATH).expect("valid old location path");
        emitter
            .location_updated(
                old,
                ObjectPath::from_static_str(update.path()).expect("valid location path"),
            )
            .await
    }

    pub(super) async fn shutdown(self) {
        let _ = self.client.close().await;
        let _ = self.server.close().await;
    }
}

struct MockManager {
    state: MockState,
}

#[interface(name = "org.freedesktop.GeoClue2.Manager")]
impl MockManager {
    #[expect(
        clippy::needless_pass_by_value,
        reason = "zbus injects the message header by value"
    )]
    fn get_client(
        &self,
        #[zbus(header)] header: Header<'_>,
    ) -> zbus::fdo::Result<ObjectPath<'static>> {
        check_destination(&header, "org.freedesktop.GeoClue2")?;
        self.state.record(Event::GetClient)?;
        Ok(ObjectPath::from_static_str(CLIENT_PATH).expect("valid client path"))
    }
}

struct MockLocation {
    update: Update,
    state: MockState,
}

impl MockLocation {
    fn read(&self, event: Event, coordinate: impl Fn(&Location) -> f64) -> zbus::fdo::Result<f64> {
        let location = self.update.location();
        self.state.record(event)?;
        if let Some(reason) = location.read_error {
            return Err(zbus::fdo::Error::Failed(reason.to_string()));
        }
        Ok(coordinate(location))
    }
}

#[interface(name = "org.freedesktop.GeoClue2.Location")]
impl MockLocation {
    #[expect(
        clippy::needless_pass_by_value,
        reason = "zbus injects the message header by value"
    )]
    #[zbus(property)]
    fn latitude(&self, #[zbus(header)] header: Option<Header<'_>>) -> zbus::fdo::Result<f64> {
        if let Some(header) = &header {
            check_destination(header, "org.freedesktop.GeoClue2")?;
        }
        self.read(Event::LatitudeRead(self.update), |location| {
            location.latitude
        })
    }

    #[expect(
        clippy::needless_pass_by_value,
        reason = "zbus injects the message header by value"
    )]
    #[zbus(property)]
    fn longitude(&self, #[zbus(header)] header: Option<Header<'_>>) -> zbus::fdo::Result<f64> {
        if let Some(header) = &header {
            check_destination(header, "org.freedesktop.GeoClue2")?;
        }
        self.read(Event::LongitudeRead(self.update), |location| {
            location.longitude
        })
    }
}

struct MockClient {
    state: MockState,
    updates: &'static [Update],
}

#[interface(name = "org.freedesktop.GeoClue2.Client")]
impl MockClient {
    async fn start(
        &self,
        #[zbus(signal_emitter)] emitter: SignalEmitter<'_>,
        #[zbus(header)] header: Header<'_>,
    ) -> zbus::fdo::Result<()> {
        check_destination(&header, "org.freedesktop.GeoClue2")?;
        self.state.record(Event::Start)?;

        let old = ObjectPath::from_static_str(OLD_LOCATION_PATH).expect("valid old location path");
        for update in self.updates {
            emitter
                .location_updated(
                    old.clone(),
                    ObjectPath::from_static_str(update.path()).expect("valid location path"),
                )
                .await?;
        }

        Ok(())
    }

    #[expect(
        clippy::needless_pass_by_value,
        reason = "zbus injects the message header by value"
    )]
    #[zbus(property)]
    fn set_desktop_id(
        &mut self,
        id: String,
        #[zbus(header)] header: Option<Header<'_>>,
    ) -> zbus::fdo::Result<()> {
        if let Some(header) = &header {
            check_destination(header, "org.freedesktop.GeoClue2")?;
        }
        self.state.record(Event::DesktopId(id))
    }

    #[expect(
        clippy::needless_pass_by_value,
        reason = "zbus injects the message header by value"
    )]
    #[zbus(property)]
    fn set_distance_threshold(
        &mut self,
        meters: u32,
        #[zbus(header)] header: Option<Header<'_>>,
    ) -> zbus::fdo::Result<()> {
        if let Some(header) = &header {
            check_destination(header, "org.freedesktop.GeoClue2")?;
        }
        self.state.record(Event::DistanceThreshold(meters))
    }

    #[expect(
        clippy::needless_pass_by_value,
        reason = "zbus injects the message header by value"
    )]
    #[zbus(property)]
    fn set_requested_accuracy_level(
        &mut self,
        level: u32,
        #[zbus(header)] header: Option<Header<'_>>,
    ) -> zbus::fdo::Result<()> {
        if let Some(header) = &header {
            check_destination(header, "org.freedesktop.GeoClue2")?;
        }
        self.state.record(Event::AccuracyLevel(level))
    }

    #[zbus(signal)]
    async fn location_updated(
        emitter: &SignalEmitter<'_>,
        old: ObjectPath<'_>,
        new: ObjectPath<'_>,
    ) -> zbus::Result<()>;
}

struct MockTimedate {
    state: MockState,
}

#[interface(name = "org.freedesktop.timedate1")]
impl MockTimedate {
    #[expect(
        clippy::fn_params_excessive_bools,
        reason = "the D-Bus SetTimezone signature has an interactive bool"
    )]
    #[expect(
        clippy::needless_pass_by_value,
        reason = "zbus injects the message header by value"
    )]
    fn set_timezone(
        &self,
        timezone: String,
        interactive: bool,
        #[zbus(header)] header: Header<'_>,
    ) -> zbus::fdo::Result<()> {
        check_destination(&header, "org.freedesktop.timedate1")?;
        self.state.record(Event::SetTimezone(SetTimezoneCall {
            timezone,
            interactive,
        }))
    }
}