omaha_client/
common.rs

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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
// Copyright 2019 The Fuchsia Authors
//
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
// This file may not be copied, modified, or distributed except according to
// those terms.

//! The omaha_client::common module contains those types that are common to many parts of the
//! library.  Many of these don't belong to a specific sub-module.

use crate::{
    protocol::{self, request::InstallSource, Cohort},
    storage::Storage,
    time::PartialComplexTime,
    version::Version,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::time::Duration;
use tracing::error;
use typed_builder::TypedBuilder;

/// Omaha has historically supported multiple methods of counting devices.  Currently, the
/// only recommended method is the Client Regulated - Date method.
///
/// See https://github.com/google/omaha/blob/HEAD/doc/ServerProtocolV3.md#client-regulated-counting-date-based
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum UserCounting {
    ClientRegulatedByDate(
        /// Date (sent by the server) of the last contact with Omaha.
        Option<u32>,
    ),
}

/// Helper implementation to bridge from the protocol to the internal representation for tracking
/// the data for client-regulated user counting.
impl From<Option<protocol::response::DayStart>> for UserCounting {
    fn from(opt_day_start: Option<protocol::response::DayStart>) -> Self {
        match opt_day_start {
            Some(day_start) => UserCounting::ClientRegulatedByDate(day_start.elapsed_days),
            None => UserCounting::ClientRegulatedByDate(None),
        }
    }
}

/// The App struct holds information about an application to perform an update check for.
#[derive(Clone, Debug, Eq, PartialEq, TypedBuilder)]
pub struct App {
    /// This is the app_id that Omaha uses to identify a given application.
    #[builder(setter(into))]
    pub id: String,

    /// This is the current version of the application.
    #[builder(setter(into))]
    pub version: Version,

    /// This is the fingerprint for the application package.
    ///
    /// See https://github.com/google/omaha/blob/HEAD/doc/ServerProtocolV3.md#packages--fingerprints
    #[builder(default)]
    #[builder(setter(into, strip_option))]
    pub fingerprint: Option<String>,

    /// The app's current cohort information (cohort id, hint, etc).  This is both provided to Omaha
    /// as well as returned by Omaha.
    #[builder(default)]
    pub cohort: Cohort,

    /// The app's current user-counting information.  This is both provided to Omaha as well as
    /// returned by Omaha.
    #[builder(default=UserCounting::ClientRegulatedByDate(None))]
    pub user_counting: UserCounting,

    /// Extra fields to include in requests to Omaha.  The client library does not inspect or
    /// operate on these, it just sends them to the service as part of the "app" objects in each
    /// request.
    #[builder(default)]
    #[builder(setter(into))]
    pub extra_fields: HashMap<String, String>,
}

/// Structure used to serialize per app data to be persisted.
/// Be careful when making changes to this struct to keep backward compatibility.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct PersistedApp {
    pub cohort: Cohort,
    pub user_counting: UserCounting,
}

impl From<&App> for PersistedApp {
    fn from(app: &App) -> Self {
        PersistedApp {
            cohort: app.cohort.clone(),
            user_counting: app.user_counting.clone(),
        }
    }
}

impl App {
    /// Load data from |storage|, only overwrite existing fields if data exists.
    pub async fn load<'a>(&'a mut self, storage: &'a impl Storage) {
        if let Some(app_json) = storage.get_string(&self.id).await {
            match serde_json::from_str::<PersistedApp>(&app_json) {
                Ok(persisted_app) => {
                    // Do not overwrite existing fields in app.
                    if self.cohort.id.is_none() {
                        self.cohort.id = persisted_app.cohort.id;
                    }
                    if self.cohort.hint.is_none() {
                        self.cohort.hint = persisted_app.cohort.hint;
                    }
                    if self.cohort.name.is_none() {
                        self.cohort.name = persisted_app.cohort.name;
                    }
                    if self.user_counting == UserCounting::ClientRegulatedByDate(None) {
                        self.user_counting = persisted_app.user_counting;
                    }
                }
                Err(e) => {
                    error!(
                        "Unable to deserialize PersistedApp from json {}: {}",
                        app_json, e
                    );
                }
            }
        }
    }

    /// Persist cohort and user counting to |storage|, will try to set all of them to storage even
    /// if previous set fails.
    /// It will NOT call commit() on |storage|, caller is responsible to call commit().
    pub async fn persist<'a>(&'a self, storage: &'a mut impl Storage) {
        let persisted_app = PersistedApp::from(self);
        match serde_json::to_string(&persisted_app) {
            Ok(json) => {
                if let Err(e) = storage.set_string(&self.id, &json).await {
                    error!("Unable to persist cohort id: {}", e);
                }
            }
            Err(e) => {
                error!(
                    "Unable to serialize PersistedApp {:?}: {}",
                    persisted_app, e
                );
            }
        }
    }

    /// Get the current channel name from cohort name, returns empty string if no cohort name set
    /// for the app.
    pub fn get_current_channel(&self) -> &str {
        self.cohort.name.as_deref().unwrap_or("")
    }

    /// Get the target channel name from cohort hint, fallback to current channel if no hint.
    pub fn get_target_channel(&self) -> &str {
        self.cohort
            .hint
            .as_deref()
            .unwrap_or_else(|| self.get_current_channel())
    }

    /// Set the cohort hint to |channel|.
    pub fn set_target_channel(&mut self, channel: Option<String>, id: Option<String>) {
        self.cohort.hint = channel;
        if let Some(id) = id {
            self.id = id;
        }
    }

    pub fn valid(&self) -> bool {
        !self.id.is_empty() && self.version != Version::from([0])
    }
}

/// Options controlling a single update check
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CheckOptions {
    /// Was this check initiated by a person that's waiting for an answer?
    ///  This is used to ignore the background poll rate, and to be aggressive about
    ///  failing fast, so as not to hang on not receiving a response.
    pub source: InstallSource,
}

/// This describes the data around the scheduling of update checks
#[derive(Clone, Copy, Default, PartialEq, Eq, TypedBuilder)]
pub struct UpdateCheckSchedule {
    // TODO(https://fxbug.dev/42143450): Theoretically last_update_time and last_update_check_time
    // do not need to coexist and we can do all the reporting we want via
    // last_update_time. However, the last update check metric doesn't (as currently
    // worded) match up with what last_update_time actually records.
    /// When the last update check was attempted (start time of the check process).
    #[builder(default, setter(into))]
    pub last_update_time: Option<PartialComplexTime>,

    /// When the last update check was attempted.
    #[builder(default, setter(into))]
    pub last_update_check_time: Option<PartialComplexTime>,

    /// When the next update should happen.
    #[builder(default, setter(into))]
    pub next_update_time: Option<CheckTiming>,
}

/// The fields used to describe the timing of the next update check.
///
/// This exists as a separate type mostly so that it can be moved around atomically, in a little bit
/// neater fashion than it could be if it was a tuple of `(PartialComplexTime, Option<Duration>)`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, TypedBuilder)]
pub struct CheckTiming {
    /// The upper time bounds on when it should be performed (expressed as along those timelines
    /// that are valid based on currently known time quality).
    #[builder(setter(into))]
    pub time: PartialComplexTime,

    /// The minimum wait until the next check, regardless of the wall or monotonic time it should be
    /// performed at.  This is handled separately as it creates a lower bound vs. the upper bound(s)
    /// that the `time` field provides.
    #[builder(default, setter(strip_option))]
    pub minimum_wait: Option<Duration>,
}

/// Helper struct that provides a nicer format for Debug printing `Option` by dropping the
/// `Some(...)` that wraps its value, and instead uses the Display trait implementation of the
/// value.
///
/// Examples:
/// `"MyStruct { option_string_field: None }"`
/// `"MyStruct { option_string_field: "string field value" }"`
///
pub struct PrettyOptionDisplay<T>(pub Option<T>)
where
    T: fmt::Display;
impl<T> fmt::Display for PrettyOptionDisplay<T>
where
    T: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.0 {
            None => write!(f, "None"),
            Some(value) => fmt::Display::fmt(value, f),
        }
    }
}
impl<T> fmt::Debug for PrettyOptionDisplay<T>
where
    T: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

/// The default Debug implementation for SystemTime will only print seconds since unix epoch, which
/// is not terribly useful in logs, so this prints a more human-relatable format.
///
/// e.g.
/// `UpdateCheckSchedule { last_update_time: None, next_uptime_time: None }`
/// `UpdateCheckSchedule { last_update_time: "2001-07-08 16:34:56.026 UTC (994518299.026420000)", next_uptime_time: None }`
impl fmt::Debug for UpdateCheckSchedule {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("UpdateCheckSchedule")
            .field(
                "last_update_time",
                &PrettyOptionDisplay(self.last_update_time),
            )
            .field(
                "next_update_time",
                &PrettyOptionDisplay(self.next_update_time),
            )
            .finish()
    }
}

impl fmt::Display for CheckTiming {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.minimum_wait {
            None => fmt::Display::fmt(&self.time, f),
            Some(wait) => write!(f, "{} wait: {:?}", &self.time, &wait),
        }
    }
}

/// These hold the data maintained request-to-request so that the requirements for
/// backoffs, throttling, proxy use, etc. can all be properly maintained.  This is
/// NOT the state machine's internal state.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ProtocolState {
    /// If the server has dictated the next poll interval, this holds what that
    /// interval is.
    pub server_dictated_poll_interval: Option<std::time::Duration>,

    /// The number of consecutive failed update checks.  Used to perform backoffs.
    pub consecutive_failed_update_checks: u32,

    /// The number of consecutive proxied requests.  Used to periodically not use
    /// proxies, in the case of an invalid proxy configuration.
    pub consecutive_proxied_requests: u32,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        storage::MemStorage,
        time::{MockTimeSource, TimeSource},
    };
    use futures::executor::block_on;
    use pretty_assertions::assert_eq;
    use std::str::FromStr;
    use std::time::SystemTime;

    #[test]
    fn test_app_new_version() {
        let app = App::builder()
            .id("some_id")
            .version([1, 2])
            .cohort(Cohort::from_hint("some-channel"))
            .build();
        assert_eq!(app.id, "some_id");
        assert_eq!(app.version, [1, 2].into());
        assert_eq!(app.fingerprint, None);
        assert_eq!(app.cohort.hint, Some("some-channel".to_string()));
        assert_eq!(app.cohort.name, None);
        assert_eq!(app.cohort.id, None);
        assert_eq!(app.user_counting, UserCounting::ClientRegulatedByDate(None));
        assert!(app.extra_fields.is_empty(), "Extra fields are not empty");
    }

    #[test]
    fn test_app_with_fingerprint() {
        let app = App::builder()
            .id("some_id_2")
            .version([4, 6])
            .cohort(Cohort::from_hint("test-channel"))
            .fingerprint("some_fp")
            .build();
        assert_eq!(app.id, "some_id_2");
        assert_eq!(app.version, [4, 6].into());
        assert_eq!(app.fingerprint, Some("some_fp".to_string()));
        assert_eq!(app.cohort.hint, Some("test-channel".to_string()));
        assert_eq!(app.cohort.name, None);
        assert_eq!(app.cohort.id, None);
        assert_eq!(app.user_counting, UserCounting::ClientRegulatedByDate(None));
        assert!(app.extra_fields.is_empty(), "Extra fields are not empty");
    }

    #[test]
    fn test_app_with_user_counting() {
        let app = App::builder()
            .id("some_id_2")
            .version([4, 6])
            .cohort(Cohort::from_hint("test-channel"))
            .user_counting(UserCounting::ClientRegulatedByDate(Some(42)))
            .build();
        assert_eq!(app.id, "some_id_2");
        assert_eq!(app.version, [4, 6].into());
        assert_eq!(app.cohort.hint, Some("test-channel".to_string()));
        assert_eq!(app.cohort.name, None);
        assert_eq!(app.cohort.id, None);
        assert_eq!(
            app.user_counting,
            UserCounting::ClientRegulatedByDate(Some(42))
        );
        assert!(app.extra_fields.is_empty(), "Extra fields are not empty");
    }

    #[test]
    fn test_app_with_extras() {
        let app = App::builder()
            .id("some_id_2")
            .version([4, 6])
            .cohort(Cohort::from_hint("test-channel"))
            .extra_fields([
                ("key1".to_string(), "value1".to_string()),
                ("key2".to_string(), "value2".to_string()),
            ])
            .build();
        assert_eq!(app.id, "some_id_2");
        assert_eq!(app.version, [4, 6].into());
        assert_eq!(app.cohort.hint, Some("test-channel".to_string()));
        assert_eq!(app.cohort.name, None);
        assert_eq!(app.cohort.id, None);
        assert_eq!(app.user_counting, UserCounting::ClientRegulatedByDate(None));
        assert_eq!(app.extra_fields.len(), 2);
        assert_eq!(app.extra_fields["key1"], "value1");
        assert_eq!(app.extra_fields["key2"], "value2");
    }

    #[test]
    fn test_app_load() {
        block_on(async {
            let mut storage = MemStorage::new();
            let json = serde_json::json!({
            "cohort": {
                "cohort": "some_id",
                "cohorthint":"some_hint",
                "cohortname": "some_name"
            },
            "user_counting": {
                "ClientRegulatedByDate":123
            }});
            let json = serde_json::to_string(&json).unwrap();
            let mut app = App::builder().id("some_id").version([1, 2]).build();
            storage.set_string(&app.id, &json).await.unwrap();
            app.load(&storage).await;

            let cohort = Cohort {
                id: Some("some_id".to_string()),
                hint: Some("some_hint".to_string()),
                name: Some("some_name".to_string()),
            };
            assert_eq!(cohort, app.cohort);
            assert_eq!(
                UserCounting::ClientRegulatedByDate(Some(123)),
                app.user_counting
            );
        });
    }

    #[test]
    fn test_app_load_empty_storage() {
        block_on(async {
            let storage = MemStorage::new();
            let cohort = Cohort {
                id: Some("some_id".to_string()),
                hint: Some("some_hint".to_string()),
                name: Some("some_name".to_string()),
            };
            let mut app = App::builder()
                .id("some_id")
                .version([1, 2])
                .cohort(cohort)
                .user_counting(UserCounting::ClientRegulatedByDate(Some(123)))
                .build();
            app.load(&storage).await;

            // existing data not overwritten
            let cohort = Cohort {
                id: Some("some_id".to_string()),
                hint: Some("some_hint".to_string()),
                name: Some("some_name".to_string()),
            };
            assert_eq!(cohort, app.cohort);
            assert_eq!(
                UserCounting::ClientRegulatedByDate(Some(123)),
                app.user_counting
            );
        });
    }

    #[test]
    fn test_app_load_malformed() {
        block_on(async {
            let mut storage = MemStorage::new();
            let cohort = Cohort {
                id: Some("some_id".to_string()),
                hint: Some("some_hint".to_string()),
                name: Some("some_name".to_string()),
            };
            let mut app = App::builder()
                .id("some_id")
                .version([1, 2])
                .cohort(cohort)
                .user_counting(UserCounting::ClientRegulatedByDate(Some(123)))
                .build();
            storage.set_string(&app.id, "not a json").await.unwrap();
            app.load(&storage).await;

            // existing data not overwritten
            let cohort = Cohort {
                id: Some("some_id".to_string()),
                hint: Some("some_hint".to_string()),
                name: Some("some_name".to_string()),
            };
            assert_eq!(cohort, app.cohort);
            assert_eq!(
                UserCounting::ClientRegulatedByDate(Some(123)),
                app.user_counting
            );
        });
    }

    #[test]
    fn test_app_load_partial() {
        block_on(async {
            let mut storage = MemStorage::new();
            let json = serde_json::json!({
            "cohort": {
                "cohorthint":"some_hint_2",
                "cohortname": "some_name_2"
            },
            "user_counting": {
                "ClientRegulatedByDate":null
            }});
            let json = serde_json::to_string(&json).unwrap();
            let cohort = Cohort {
                id: Some("some_id".to_string()),
                hint: Some("some_hint".to_string()),
                name: Some("some_name".to_string()),
            };
            let mut app = App::builder()
                .id("some_id")
                .version([1, 2])
                .cohort(cohort)
                .user_counting(UserCounting::ClientRegulatedByDate(Some(123)))
                .build();
            storage.set_string(&app.id, &json).await.unwrap();
            app.load(&storage).await;

            // existing data not overwritten
            let cohort = Cohort {
                id: Some("some_id".to_string()),
                hint: Some("some_hint".to_string()),
                name: Some("some_name".to_string()),
            };
            assert_eq!(cohort, app.cohort);
            assert_eq!(
                UserCounting::ClientRegulatedByDate(Some(123)),
                app.user_counting
            );
        });
    }

    #[test]
    fn test_app_load_override() {
        block_on(async {
            let mut storage = MemStorage::new();
            let json = serde_json::json!({
            "cohort": {
                "cohort": "some_id_2",
                "cohorthint":"some_hint_2",
                "cohortname": "some_name_2"
            },
            "user_counting": {
                "ClientRegulatedByDate":123
            }});
            let json = serde_json::to_string(&json).unwrap();
            let cohort = Cohort {
                id: Some("some_id".to_string()),
                hint: Some("some_hint".to_string()),
                name: None,
            };
            let mut app = App::builder()
                .id("some_id")
                .version([1, 2])
                .cohort(cohort)
                .user_counting(UserCounting::ClientRegulatedByDate(Some(123)))
                .build();
            storage.set_string(&app.id, &json).await.unwrap();
            app.load(&storage).await;

            // existing data not overwritten
            let cohort = Cohort {
                id: Some("some_id".to_string()),
                hint: Some("some_hint".to_string()),
                name: Some("some_name_2".to_string()),
            };
            assert_eq!(cohort, app.cohort);
            assert_eq!(
                UserCounting::ClientRegulatedByDate(Some(123)),
                app.user_counting
            );
        });
    }

    #[test]
    fn test_app_persist() {
        block_on(async {
            let mut storage = MemStorage::new();
            let cohort = Cohort {
                id: Some("some_id".to_string()),
                hint: Some("some_hint".to_string()),
                name: Some("some_name".to_string()),
            };
            let app = App::builder()
                .id("some_id")
                .version([1, 2])
                .cohort(cohort)
                .user_counting(UserCounting::ClientRegulatedByDate(Some(123)))
                .build();
            app.persist(&mut storage).await;

            let expected = serde_json::json!({
            "cohort": {
                "cohort": "some_id",
                "cohorthint":"some_hint",
                "cohortname": "some_name"
            },
            "user_counting": {
                "ClientRegulatedByDate":123
            }});
            let json = storage.get_string(&app.id).await.unwrap();
            assert_eq!(expected, serde_json::Value::from_str(&json).unwrap());
            assert!(!storage.committed());
        });
    }

    #[test]
    fn test_app_persist_empty() {
        block_on(async {
            let mut storage = MemStorage::new();
            let cohort = Cohort {
                id: None,
                hint: None,
                name: None,
            };
            let app = App::builder()
                .id("some_id")
                .version([1, 2])
                .cohort(cohort)
                .build();
            app.persist(&mut storage).await;

            let expected = serde_json::json!({
            "cohort": {},
            "user_counting": {
                "ClientRegulatedByDate":null
            }});
            let json = storage.get_string(&app.id).await.unwrap();
            assert_eq!(expected, serde_json::Value::from_str(&json).unwrap());
            assert!(!storage.committed());
        });
    }

    #[test]
    fn test_app_get_current_channel() {
        let cohort = Cohort {
            name: Some("current-channel-123".to_string()),
            ..Cohort::default()
        };
        let app = App::builder()
            .id("some_id")
            .version([0, 1])
            .cohort(cohort)
            .build();
        assert_eq!("current-channel-123", app.get_current_channel());
    }

    #[test]
    fn test_app_get_current_channel_default() {
        let app = App::builder().id("some_id").version([0, 1]).build();
        assert_eq!("", app.get_current_channel());
    }

    #[test]
    fn test_app_get_target_channel() {
        let cohort = Cohort::from_hint("target-channel-456");
        let app = App::builder()
            .id("some_id")
            .version([0, 1])
            .cohort(cohort)
            .build();
        assert_eq!("target-channel-456", app.get_target_channel());
    }

    #[test]
    fn test_app_get_target_channel_fallback() {
        let cohort = Cohort {
            name: Some("current-channel-123".to_string()),
            ..Cohort::default()
        };
        let app = App::builder()
            .id("some_id")
            .version([0, 1])
            .cohort(cohort)
            .build();
        assert_eq!("current-channel-123", app.get_target_channel());
    }

    #[test]
    fn test_app_get_target_channel_default() {
        let app = App::builder().id("some_id").version([0, 1]).build();
        assert_eq!("", app.get_target_channel());
    }

    #[test]
    fn test_app_set_target_channel() {
        let mut app = App::builder().id("some_id").version([0, 1]).build();
        assert_eq!("", app.get_target_channel());
        app.set_target_channel(Some("new-target-channel".to_string()), None);
        assert_eq!("new-target-channel", app.get_target_channel());
        app.set_target_channel(None, None);
        assert_eq!("", app.get_target_channel());
    }

    #[test]
    fn test_app_set_target_channel_and_id() {
        let mut app = App::builder().id("some_id").version([0, 1]).build();
        assert_eq!("", app.get_target_channel());
        app.set_target_channel(
            Some("new-target-channel".to_string()),
            Some("new-id".to_string()),
        );
        assert_eq!("new-target-channel", app.get_target_channel());
        assert_eq!("new-id", app.id);
        app.set_target_channel(None, None);
        assert_eq!("", app.get_target_channel());
        assert_eq!("new-id", app.id);
    }

    #[test]
    fn test_app_valid() {
        let app = App::builder().id("some_id").version([0, 1]).build();
        assert!(app.valid());
    }

    #[test]
    fn test_app_not_valid() {
        let app = App::builder().id("").version([0, 1]).build();
        assert!(!app.valid());
        let app = App::builder().id("some_id").version([0]).build();
        assert!(!app.valid());
    }

    #[test]
    fn test_pretty_option_display_with_none() {
        assert_eq!(
            "None",
            format!("{:?}", PrettyOptionDisplay(Option::<String>::None))
        );
    }

    #[test]
    fn test_pretty_option_display_with_some() {
        assert_eq!(
            "this is a test",
            format!("{:?}", PrettyOptionDisplay(Some("this is a test")))
        );
    }

    #[test]
    fn test_update_check_schedule_debug_with_defaults() {
        assert_eq!(
            "UpdateCheckSchedule { \
                last_update_time: None, \
                next_update_time: None \
            }",
            format!("{:?}", UpdateCheckSchedule::default())
        );
    }

    #[test]
    fn test_update_check_schedule_debug_with_values() {
        let mock_time = MockTimeSource::new_from_now();
        let last = mock_time.now();
        let next = last + Duration::from_secs(1000);
        assert_eq!(
            format!(
                "UpdateCheckSchedule {{ last_update_time: {}, next_update_time: {} }}",
                PartialComplexTime::from(last),
                next
            ),
            format!(
                "{:?}",
                UpdateCheckSchedule::builder()
                    .last_update_time(last)
                    .next_update_time(CheckTiming::builder().time(next).build())
                    .build()
            )
        );
    }

    #[test]
    fn test_update_check_schedule_builder_all_fields() {
        let mock_time = MockTimeSource::new_from_now();
        let now = PartialComplexTime::from(mock_time.now());
        assert_eq!(
            UpdateCheckSchedule::builder()
                .last_update_time(PartialComplexTime::from(
                    SystemTime::UNIX_EPOCH + Duration::from_secs(100000)
                ))
                .next_update_time(
                    CheckTiming::builder()
                        .time(now)
                        .minimum_wait(Duration::from_secs(100))
                        .build()
                )
                .build(),
            UpdateCheckSchedule {
                last_update_time: Some(PartialComplexTime::from(
                    SystemTime::UNIX_EPOCH + Duration::from_secs(100000)
                )),
                next_update_time: Some(CheckTiming {
                    time: now,
                    minimum_wait: Some(Duration::from_secs(100))
                }),
                ..Default::default()
            }
        );
    }

    #[test]
    fn test_update_check_schedule_builder_all_fields_from_options() {
        let next_time = PartialComplexTime::from(MockTimeSource::new_from_now().now());
        assert_eq!(
            UpdateCheckSchedule::builder()
                .last_update_time(Some(PartialComplexTime::from(
                    SystemTime::UNIX_EPOCH + Duration::from_secs(100000)
                )))
                .next_update_time(Some(
                    CheckTiming::builder()
                        .time(next_time)
                        .minimum_wait(Duration::from_secs(100))
                        .build()
                ))
                .build(),
            UpdateCheckSchedule {
                last_update_time: Some(PartialComplexTime::from(
                    SystemTime::UNIX_EPOCH + Duration::from_secs(100000)
                )),
                next_update_time: Some(CheckTiming {
                    time: next_time,
                    minimum_wait: Some(Duration::from_secs(100))
                }),
                ..Default::default()
            }
        );
    }

    #[test]
    fn test_update_check_schedule_builder_subset_fields() {
        assert_eq!(
            UpdateCheckSchedule::builder()
                .last_update_time(PartialComplexTime::from(
                    SystemTime::UNIX_EPOCH + Duration::from_secs(100000)
                ))
                .build(),
            UpdateCheckSchedule {
                last_update_time: Some(PartialComplexTime::from(
                    SystemTime::UNIX_EPOCH + Duration::from_secs(100000)
                )),
                ..Default::default()
            }
        );

        let next_time = PartialComplexTime::from(MockTimeSource::new_from_now().now());
        assert_eq!(
            UpdateCheckSchedule::builder()
                .next_update_time(
                    CheckTiming::builder()
                        .time(next_time)
                        .minimum_wait(Duration::from_secs(5))
                        .build()
                )
                .build(),
            UpdateCheckSchedule {
                next_update_time: Some(CheckTiming {
                    time: next_time,
                    minimum_wait: Some(Duration::from_secs(5))
                }),
                ..Default::default()
            }
        );
    }

    #[test]
    fn test_update_check_schedule_builder_defaults_are_same_as_default_impl() {
        assert_eq!(
            UpdateCheckSchedule::builder().build(),
            UpdateCheckSchedule::default()
        );
    }
}