input_pipeline/
factory_reset_handler.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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::consumer_controls_binding::ConsumerControlsEvent;
use crate::input_handler::{InputHandlerStatus, UnhandledInputHandler};
use crate::{input_device, metrics};
use anyhow::{anyhow, Context as _, Error};
use async_trait::async_trait;
use async_utils::hanging_get::server::HangingGet;
use fidl::endpoints::DiscoverableProtocolMarker as _;
use fidl_fuchsia_media::AudioRenderUsage;
use fidl_fuchsia_media_sounds::{PlaySoundError, PlayerMarker};
use fidl_fuchsia_recovery::FactoryResetMarker;
use fidl_fuchsia_recovery_policy::{DeviceRequest, DeviceRequestStream};
use fidl_fuchsia_recovery_ui::{
    FactoryResetCountdownRequestStream, FactoryResetCountdownState,
    FactoryResetCountdownWatchResponder,
};
use fuchsia_async::{MonotonicDuration, MonotonicInstant, Task, TimeoutExt, Timer};
use fuchsia_inspect::health::Reporter;
use futures::StreamExt;
use metrics_registry::*;
use std::cell::RefCell;
use std::fs::{self, File};
use std::path::Path;
use std::rc::Rc;
use {fidl_fuchsia_input_report as fidl_input_report, fidl_fuchsia_io as fio};

/// FactoryResetState tracks the state of the device through the factory reset
/// process.
///
/// # Values
/// ## Disallowed
/// Factory reset of the device is not allowed. This is used to
/// keep public devices from being reset, such as when being used in kiosk mode.
///
/// ### Transitions
/// Disallowed → Idle
///
/// ## Idle
/// This is the default state for a device when factory resets are allowed but
/// is not currently being reset.
///
/// ### Transitions
/// Idle → Disallowed
/// Idle → ButtonCountdown
///
/// ## ButtonCountdown
/// This state represents the fact that the reset button has been pressed and a
/// countdown has started to verify that the button was pressed intentionally.
///
/// ### Transitions
/// ButtonCountdown → Disallowed
/// ButtonCountdown → Idle
/// ButtonCountdown → ResetCountdown
///
/// ## ResetCountdown
/// The button countdown has completed indicating that this was a purposeful
/// action so a reset countdown is started to give the user a chance to cancel
/// the factory reset.
///
/// ### Transitions
/// ResetCountdown → Disallowed
/// ResetCountdown → Idle
/// ResetCountdown → Resetting
///
/// ## Resetting
/// Once the device is in this state a factory reset is imminent and can no
/// longer be cancelled.
#[derive(Clone, Copy, Debug, PartialEq)]
enum FactoryResetState {
    Disallowed,
    Idle,
    ButtonCountdown { deadline: MonotonicInstant },
    ResetCountdown { deadline: MonotonicInstant },
    Resetting,
}

const FACTORY_RESET_DISALLOWED_PATH: &'static str = "/data/factory_reset_disallowed";
const FACTORY_RESET_SOUND_PATH: &'static str = "/config/data/chirp-start-tone.wav";

const BUTTON_TIMEOUT: MonotonicDuration = MonotonicDuration::from_millis(500);
const RESET_TIMEOUT: MonotonicDuration = MonotonicDuration::from_seconds(10);
/// Maximum length of time to wait for the reset earcon to play (after `RESET_TIMEOUT` elapses).
const EARCON_TIMEOUT: MonotonicDuration = MonotonicDuration::from_millis(2000);

type NotifyFn = Box<
    dyn Fn(
            &(FactoryResetState, metrics::MetricsLogger),
            FactoryResetCountdownWatchResponder,
        ) -> bool
        + Send,
>;
type ResetCountdownHangingGet = HangingGet<
    (FactoryResetState, metrics::MetricsLogger),
    FactoryResetCountdownWatchResponder,
    NotifyFn,
>;

/// A [`FactoryResetHandler`] tracks the state of the consumer control buttons
/// and starts the factory reset process after appropriate timeouts.
pub struct FactoryResetHandler {
    factory_reset_state: RefCell<FactoryResetState>,
    countdown_hanging_get: RefCell<ResetCountdownHangingGet>,

    /// The inventory of this handler's Inspect status.
    pub inspect_status: InputHandlerStatus,

    metrics_logger: metrics::MetricsLogger,
}

/// Uses the `ConsumerControlsEvent` to determine whether the device should
/// start the Factory Reset process. The driver will turn special button
/// combinations into a `FactoryReset` signal so this code only needs to
/// listen for that.
fn is_reset_requested(event: &ConsumerControlsEvent) -> bool {
    event.pressed_buttons.iter().any(|button| match button {
        fidl_input_report::ConsumerControlButton::FactoryReset => true,
        _ => false,
    })
}

impl FactoryResetHandler {
    /// Creates a new [`FactoryResetHandler`] that listens for the reset button
    /// and handles timing down and, ultimately, factory resetting the device.
    pub fn new(
        input_handlers_node: &fuchsia_inspect::Node,
        metrics_logger: metrics::MetricsLogger,
    ) -> Rc<Self> {
        let initial_state = if Path::new(FACTORY_RESET_DISALLOWED_PATH).exists() {
            FactoryResetState::Disallowed
        } else {
            FactoryResetState::Idle
        };

        let countdown_hanging_get =
            FactoryResetHandler::init_hanging_get(initial_state, metrics_logger.clone());
        let inspect_status = InputHandlerStatus::new(
            input_handlers_node,
            "factory_reset_handler",
            /* generates_events */ false,
        );

        Rc::new(Self {
            factory_reset_state: RefCell::new(initial_state),
            countdown_hanging_get: RefCell::new(countdown_hanging_get),
            inspect_status,
            metrics_logger,
        })
    }

    /// Handles the request stream for FactoryResetCountdown
    ///
    /// # Parameters
    /// `stream`: The `FactoryResetCountdownRequestStream` to be handled.
    pub fn handle_factory_reset_countdown_request_stream(
        self: Rc<Self>,
        mut stream: FactoryResetCountdownRequestStream,
    ) -> impl futures::Future<Output = Result<(), Error>> {
        let subscriber = self.countdown_hanging_get.borrow_mut().new_subscriber();

        async move {
            while let Some(request_result) = stream.next().await {
                let watcher = request_result?
                    .into_watch()
                    .ok_or_else(|| anyhow!("Failed to get FactoryResetCoundown Watcher"))?;
                subscriber.register(watcher)?;
            }

            Ok(())
        }
    }

    /// Handles the request stream for fuchsia.recovery.policy.Device
    ///
    /// # Parameters
    /// `stream`: The `DeviceRequestStream` to be handled.
    pub fn handle_recovery_policy_device_request_stream(
        self: Rc<Self>,
        mut stream: DeviceRequestStream,
    ) -> impl futures::Future<Output = Result<(), Error>> {
        async move {
            while let Some(request_result) = stream.next().await {
                let DeviceRequest::SetIsLocalResetAllowed { allowed, .. } = request_result?;
                match self.factory_reset_state() {
                    FactoryResetState::Disallowed if allowed => {
                        // Update state and delete file
                        self.set_factory_reset_state(FactoryResetState::Idle);
                        fs::remove_file(FACTORY_RESET_DISALLOWED_PATH).with_context(|| {
                            format!("failed to remove {}", FACTORY_RESET_DISALLOWED_PATH)
                        })?
                    }
                    _ if !allowed => {
                        // Update state and create file
                        self.set_factory_reset_state(FactoryResetState::Disallowed);
                        let _: File =
                            File::create(FACTORY_RESET_DISALLOWED_PATH).with_context(|| {
                                format!("failed to create {}", FACTORY_RESET_DISALLOWED_PATH)
                            })?;
                    }
                    _ => (),
                }
            }

            Ok(())
        }
    }

    /// Handles `ConsumerControlEvent`s when the device is in the
    /// `FactoryResetState::Idle` state
    async fn handle_allowed_event(self: &Rc<Self>, event: &ConsumerControlsEvent) {
        if is_reset_requested(event) {
            if let Err(error) = self.start_button_countdown().await {
                self.metrics_logger.log_error(
                    InputPipelineErrorMetricDimensionEvent::FactoryResetFailedToReset,
                    std::format!("Failed to factory reset device: {:?}", error),
                );
            }
        }
    }

    /// Handles `ConsumerControlEvent`s when the device is in the
    /// `FactoryResetState::Disallowed` state
    fn handle_disallowed_event(self: &Rc<Self>, event: &ConsumerControlsEvent) {
        if is_reset_requested(event) {
            self.metrics_logger.log_error(
                InputPipelineErrorMetricDimensionEvent::FactoryResetNotAllowedReset,
                "Attempted to factory reset a device that is not allowed to reset",
            );
        }
    }

    /// Handles `ConsumerControlEvent`s when the device is in the
    /// `FactoryResetState::ButtonCountdown` state
    fn handle_button_countdown_event(self: &Rc<Self>, event: &ConsumerControlsEvent) {
        if !is_reset_requested(event) {
            // Cancel button timeout
            self.set_factory_reset_state(FactoryResetState::Idle);
        }
    }

    /// Handles `ConsumerControlEvent`s when the device is in the
    /// `FactoryResetState::ResetCountdown` state
    fn handle_reset_countdown_event(self: &Rc<Self>, event: &ConsumerControlsEvent) {
        if !is_reset_requested(event) {
            // Cancel reset timeout
            self.set_factory_reset_state(FactoryResetState::Idle);
        }
    }

    fn init_hanging_get(
        initial_state: FactoryResetState,
        metrics_logger: metrics::MetricsLogger,
    ) -> ResetCountdownHangingGet {
        let notify_fn: NotifyFn = Box::new(|(state, metrics_logger), responder| {
            let deadline = match state {
                FactoryResetState::ResetCountdown { deadline } => {
                    Some(deadline.into_nanos() as i64)
                }
                _ => None,
            };

            let countdown_state =
                FactoryResetCountdownState { scheduled_reset_time: deadline, ..Default::default() };

            if responder.send(&countdown_state).is_err() {
                metrics_logger.log_error(
                    InputPipelineErrorMetricDimensionEvent::FactoryResetFailedToSendCountdown,
                    "Failed to send factory reset countdown state",
                );
            }

            true
        });

        ResetCountdownHangingGet::new((initial_state, metrics_logger), notify_fn)
    }

    /// Sets the state of FactoryResetHandler and notifies watchers of the updated state.
    fn set_factory_reset_state(self: &Rc<Self>, state: FactoryResetState) {
        *self.factory_reset_state.borrow_mut() = state;
        self.countdown_hanging_get
            .borrow_mut()
            .new_publisher()
            .set((state, self.metrics_logger.clone()));
    }

    fn factory_reset_state(self: &Rc<Self>) -> FactoryResetState {
        *self.factory_reset_state.borrow()
    }

    /// Handles waiting for the reset button to be held down long enough to start
    /// the factory reset countdown.
    async fn start_button_countdown(self: &Rc<Self>) -> Result<(), Error> {
        let deadline = MonotonicInstant::after(BUTTON_TIMEOUT);
        self.set_factory_reset_state(FactoryResetState::ButtonCountdown { deadline });

        // Wait for button timeout
        Timer::new(MonotonicInstant::after(BUTTON_TIMEOUT)).await;

        // Make sure the buttons are still held
        match self.factory_reset_state() {
            FactoryResetState::ButtonCountdown { deadline: state_deadline }
                if state_deadline == deadline =>
            {
                // Proceed with reset.
                self.start_reset_countdown().await?;
            }
            _ => {
                tracing::info!("Factory reset request cancelled");
            }
        }

        Ok(())
    }

    /// Handles waiting for the reset countdown to complete before resetting the
    /// device.
    async fn start_reset_countdown(self: &Rc<Self>) -> Result<(), Error> {
        let deadline = MonotonicInstant::after(RESET_TIMEOUT);
        self.set_factory_reset_state(FactoryResetState::ResetCountdown { deadline });

        // Wait for reset timeout
        Timer::new(MonotonicInstant::after(RESET_TIMEOUT)).await;

        // Make sure the buttons are still held
        match self.factory_reset_state() {
            FactoryResetState::ResetCountdown { deadline: state_deadline }
                if state_deadline == deadline =>
            {
                // Proceed with reset.
                self.reset().await?;
            }
            _ => {
                tracing::info!("Factory reset request cancelled");
            }
        }

        Ok(())
    }

    /// Retrieves and plays the sound associated with factory resetting the device.
    async fn play_reset_sound(self: &Rc<Self>) -> Result<(), Error> {
        tracing::debug!("Getting sound");
        // Get sound
        let (sound_endpoint, server_end) = fidl::endpoints::create_endpoints::<fio::FileMarker>();
        let () = fuchsia_fs::file::open_channel_in_namespace(
            FACTORY_RESET_SOUND_PATH,
            fuchsia_fs::PERM_READABLE,
            server_end,
        )
        .context("Failed to open factory reset sound file")?;

        tracing::debug!("Playing sound");
        // Play sound
        let sound_player = fuchsia_component::client::connect_to_protocol::<PlayerMarker>()
            .with_context(|| format!("failed to connect to {}", PlayerMarker::PROTOCOL_NAME))?;

        tracing::debug!("Connected to player");
        let sound_id = 0;
        let _duration: i64 = sound_player
            .add_sound_from_file(sound_id, sound_endpoint)
            .await
            .context("AddSoundFromFile error")?
            .map_err(zx::Status::from_raw)
            .context("AddSoundFromFile failed")?;
        tracing::debug!("Added sound from file");

        sound_player
            .play_sound(sound_id, AudioRenderUsage::Media)
            .await
            .context("PlaySound error")?
            .map_err(|err: PlaySoundError| anyhow!("PlaySound failed: {:?}", err))?;

        tracing::debug!("Played sound");

        Ok(())
    }

    /// Performs the actual factory reset.
    async fn reset(self: &Rc<Self>) -> Result<(), Error> {
        tracing::info!("Beginning reset sequence");
        if let Err(error) = self
            .play_reset_sound()
            .on_timeout(EARCON_TIMEOUT, || Err(anyhow!("play_reset_sound took too long")))
            .await
        {
            tracing::warn!("Failed to play reset sound: {:?}", error);
        }

        // Trigger reset
        self.set_factory_reset_state(FactoryResetState::Resetting);
        tracing::info!("Calling {}.Reset", FactoryResetMarker::PROTOCOL_NAME);
        let factory_reset = fuchsia_component::client::connect_to_protocol::<FactoryResetMarker>()
            .with_context(|| {
                format!("failed to connect to {}", FactoryResetMarker::PROTOCOL_NAME)
            })?;
        factory_reset.reset().await.context("failed while calling Reset")?;
        Ok(())
    }
}

#[async_trait(?Send)]
impl UnhandledInputHandler for FactoryResetHandler {
    /// This InputHandler doesn't consume any input events. It just passes them on to the next handler in the pipeline.
    /// Since it doesn't need exclusive access to the events this seems like the best way to avoid handlers further
    /// down the pipeline missing events that they need.
    async fn handle_unhandled_input_event(
        self: Rc<Self>,
        unhandled_input_event: input_device::UnhandledInputEvent,
    ) -> Vec<input_device::InputEvent> {
        match unhandled_input_event {
            input_device::UnhandledInputEvent {
                device_event: input_device::InputDeviceEvent::ConsumerControls(ref event),
                device_descriptor: input_device::InputDeviceDescriptor::ConsumerControls(_),
                event_time: _,
                trace_id: _,
            } => {
                self.inspect_status.count_received_event(input_device::InputEvent::from(
                    unhandled_input_event.clone(),
                ));
                match self.factory_reset_state() {
                    FactoryResetState::Idle => {
                        let event_clone = event.clone();
                        Task::local(async move { self.handle_allowed_event(&event_clone).await })
                            .detach()
                    }
                    FactoryResetState::Disallowed => self.handle_disallowed_event(event),
                    FactoryResetState::ButtonCountdown { deadline: _ } => {
                        self.handle_button_countdown_event(event)
                    }
                    FactoryResetState::ResetCountdown { deadline: _ } => {
                        self.handle_reset_countdown_event(event)
                    }
                    FactoryResetState::Resetting => {
                        tracing::warn!("Recieved an input event while factory resetting the device")
                    }
                };
            }
            _ => (),
        };

        vec![input_device::InputEvent::from(unhandled_input_event)]
    }

    fn set_handler_healthy(self: std::rc::Rc<Self>) {
        self.inspect_status.health_node.borrow_mut().set_ok();
    }

    fn set_handler_unhealthy(self: std::rc::Rc<Self>, msg: &str) {
        self.inspect_status.health_node.borrow_mut().set_unhealthy(msg);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::consumer_controls_binding::ConsumerControlsDeviceDescriptor;
    use crate::input_handler::InputHandler;
    use assert_matches::assert_matches;
    use fidl::endpoints::create_proxy_and_stream;
    use fidl_fuchsia_recovery_policy::{DeviceMarker, DeviceProxy};
    use fidl_fuchsia_recovery_ui::{FactoryResetCountdownMarker, FactoryResetCountdownProxy};
    use fuchsia_async::TestExecutor;
    use pretty_assertions::assert_eq;
    use std::pin::pin;
    use std::task::Poll;

    fn create_factory_reset_countdown_proxy(
        reset_handler: Rc<FactoryResetHandler>,
    ) -> FactoryResetCountdownProxy {
        let (countdown_proxy, countdown_stream) =
            create_proxy_and_stream::<FactoryResetCountdownMarker>();

        let stream_fut =
            reset_handler.clone().handle_factory_reset_countdown_request_stream(countdown_stream);

        Task::local(async move {
            if stream_fut.await.is_err() {
                tracing::warn!("Failed to handle factory reset countdown request stream");
            }
        })
        .detach();

        countdown_proxy
    }

    fn create_recovery_policy_proxy(reset_handler: Rc<FactoryResetHandler>) -> DeviceProxy {
        let (device_proxy, device_stream) = create_proxy_and_stream::<DeviceMarker>();

        Task::local(async move {
            if reset_handler
                .handle_recovery_policy_device_request_stream(device_stream)
                .await
                .is_err()
            {
                tracing::warn!("Failed to handle recovery policy device request stream");
            }
        })
        .detach();

        device_proxy
    }

    fn create_input_device_descriptor() -> input_device::InputDeviceDescriptor {
        input_device::InputDeviceDescriptor::ConsumerControls(ConsumerControlsDeviceDescriptor {
            buttons: vec![
                fidl_input_report::ConsumerControlButton::CameraDisable,
                fidl_input_report::ConsumerControlButton::FactoryReset,
                fidl_input_report::ConsumerControlButton::MicMute,
                fidl_input_report::ConsumerControlButton::Pause,
                fidl_input_report::ConsumerControlButton::VolumeDown,
                fidl_input_report::ConsumerControlButton::VolumeUp,
            ],
            device_id: 0,
        })
    }

    fn create_reset_consumer_controls_event() -> ConsumerControlsEvent {
        ConsumerControlsEvent::new(vec![fidl_input_report::ConsumerControlButton::FactoryReset])
    }

    fn create_non_reset_consumer_controls_event() -> ConsumerControlsEvent {
        ConsumerControlsEvent::new(vec![
            fidl_input_report::ConsumerControlButton::CameraDisable,
            fidl_input_report::ConsumerControlButton::MicMute,
            fidl_input_report::ConsumerControlButton::Pause,
            fidl_input_report::ConsumerControlButton::VolumeDown,
            fidl_input_report::ConsumerControlButton::VolumeUp,
        ])
    }

    fn create_non_reset_input_event() -> input_device::UnhandledInputEvent {
        let device_event = input_device::InputDeviceEvent::ConsumerControls(
            create_non_reset_consumer_controls_event(),
        );

        input_device::UnhandledInputEvent {
            device_event,
            device_descriptor: create_input_device_descriptor(),
            event_time: zx::MonotonicInstant::get(),
            trace_id: None,
        }
    }

    fn create_reset_input_event() -> input_device::UnhandledInputEvent {
        let device_event = input_device::InputDeviceEvent::ConsumerControls(
            create_reset_consumer_controls_event(),
        );

        input_device::UnhandledInputEvent {
            device_event,
            device_descriptor: create_input_device_descriptor(),
            event_time: zx::MonotonicInstant::get(),
            trace_id: None,
        }
    }

    #[fuchsia::test]
    async fn is_reset_requested_looks_for_reset_signal() {
        let reset_event = create_reset_consumer_controls_event();
        let non_reset_event = create_non_reset_consumer_controls_event();

        assert!(
            is_reset_requested(&reset_event),
            "Should reset when the reset signal is received."
        );
        assert!(
            !is_reset_requested(&non_reset_event),
            "Should only reset when the reset signal is received."
        );
    }

    #[fuchsia::test]
    async fn factory_reset_countdown_listener_gets_initial_state() {
        let inspector = fuchsia_inspect::Inspector::default();
        let test_node = inspector.root().create_child("test_node");
        let reset_handler = FactoryResetHandler::new(&test_node, metrics::MetricsLogger::default());
        let countdown_proxy = create_factory_reset_countdown_proxy(reset_handler.clone());
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Idle);
    }

    #[fuchsia::test]
    fn factory_reset_countdown_listener_is_notified_on_state_change() -> Result<(), Error> {
        let mut executor = TestExecutor::new_with_fake_time();
        let inspector = fuchsia_inspect::Inspector::default();
        let test_node = inspector.root().create_child("test_node");
        let reset_handler = FactoryResetHandler::new(&test_node, metrics::MetricsLogger::default());
        let countdown_proxy = create_factory_reset_countdown_proxy(reset_handler.clone());

        let get_countdown_state = |executor: &mut TestExecutor| {
            let mut fut = countdown_proxy.watch();
            loop {
                // NB: cannot call run_singlethreaded on an executor with a fake clock.
                match executor.run_until_stalled(&mut fut) {
                    Poll::Pending => continue,
                    Poll::Ready(state) => {
                        return state.expect("Failed to get countdown state");
                    }
                }
            }
        };

        // The initial state should be no scheduled reset time and the
        // FactoryRestHandler state should be FactoryResetState::Idle
        let countdown_state = get_countdown_state(&mut executor);
        let handler_state = reset_handler.factory_reset_state();
        assert_eq!(countdown_state.scheduled_reset_time, None);
        assert_eq!(handler_state, FactoryResetState::Idle);

        // Send a reset event
        let reset_event = create_reset_input_event();
        let mut handle_input_event_fut =
            pin!(reset_handler.clone().handle_unhandled_input_event(reset_event));
        assert_matches!(executor.run_until_stalled(&mut handle_input_event_fut), Poll::Ready(events) => {
            assert_matches!(events.as_slice(), [input_device::InputEvent { .. }]);
        });

        // The next state will be FactoryResetState::ButtonCountdown with no scheduled reset
        let countdown_state = get_countdown_state(&mut executor);
        let handler_state = reset_handler.factory_reset_state();
        assert_eq!(countdown_state.scheduled_reset_time, None);
        assert_matches!(handler_state, FactoryResetState::ButtonCountdown { deadline: _ });

        // Skip ahead 500ms for the ButtonCountdown
        executor.set_fake_time(MonotonicInstant::after(MonotonicDuration::from_millis(500)));
        executor.wake_expired_timers();

        // After the ButtonCountdown the reset_handler enters the
        // FactoryResetState::ResetCountdown state WITH a scheduled reset time.
        let countdown_state = get_countdown_state(&mut executor);
        let handler_state = reset_handler.factory_reset_state();
        assert_matches!(countdown_state.scheduled_reset_time, Some(_));
        assert_matches!(handler_state, FactoryResetState::ResetCountdown { deadline: _ });

        // Skip ahead 10s for the ResetCountdown
        executor.set_fake_time(MonotonicInstant::after(MonotonicDuration::from_seconds(10)));
        executor.wake_expired_timers();

        // After the ResetCountdown the reset_handler enters the
        // FactoryResetState::Resetting state with no scheduled reset time.
        let countdown_state = get_countdown_state(&mut executor);
        let handler_state = reset_handler.factory_reset_state();
        assert_eq!(countdown_state.scheduled_reset_time, None);
        assert_eq!(handler_state, FactoryResetState::Resetting);

        Ok(())
    }

    #[fuchsia::test]
    async fn recovery_policy_requests_update_reset_handler_state() {
        let inspector = fuchsia_inspect::Inspector::default();
        let test_node = inspector.root().create_child("test_node");
        let reset_handler = FactoryResetHandler::new(&test_node, metrics::MetricsLogger::default());
        let countdown_proxy = create_factory_reset_countdown_proxy(reset_handler.clone());

        // Initial state should be FactoryResetState::Idle with no scheduled reset
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Idle);

        // Set FactoryResetState to Disallow
        let device_proxy = create_recovery_policy_proxy(reset_handler.clone());
        device_proxy.set_is_local_reset_allowed(false).expect("Failed to set recovery policy");

        // State should now be in Disallow and scheduled_reset_time should be None
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Disallowed);

        // Send reset event
        let reset_event = create_reset_input_event();
        reset_handler.clone().handle_unhandled_input_event(reset_event).await;

        // State should still be Disallow
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Disallowed);

        // Set the state back to Allow
        let device_proxy = create_recovery_policy_proxy(reset_handler.clone());
        device_proxy.set_is_local_reset_allowed(true).expect("Failed to set recovery policy");

        // State should be FactoryResetState::Idle with no scheduled reset
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Idle);
    }

    #[fuchsia::test]
    fn handle_allowed_event_changes_state_with_reset() {
        let mut executor = TestExecutor::new();

        let reset_event = create_reset_consumer_controls_event();
        let inspector = fuchsia_inspect::Inspector::default();
        let test_node = inspector.root().create_child("test_node");
        let reset_handler = FactoryResetHandler::new(&test_node, metrics::MetricsLogger::default());
        let countdown_proxy = create_factory_reset_countdown_proxy(reset_handler.clone());

        // Initial state should be FactoryResetState::Idle with no scheduled reset
        let reset_state = executor
            .run_singlethreaded(countdown_proxy.watch())
            .expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Idle);

        let handle_allowed_event_fut = reset_handler.handle_allowed_event(&reset_event);
        futures::pin_mut!(handle_allowed_event_fut);
        assert_eq!(executor.run_until_stalled(&mut handle_allowed_event_fut), Poll::Pending);

        // This should result in the reset handler entering the ButtonCountdown state
        assert_matches!(
            executor.run_singlethreaded(countdown_proxy.watch()),
            Ok(FactoryResetCountdownState { scheduled_reset_time: None, .. })
        );
        assert_matches!(
            reset_handler.factory_reset_state(),
            FactoryResetState::ButtonCountdown { deadline: _ }
        );
    }

    #[fuchsia::test]
    async fn handle_allowed_event_wont_change_state_without_reset() {
        let inspector = fuchsia_inspect::Inspector::default();
        let test_node = inspector.root().create_child("test_node");
        let reset_handler = FactoryResetHandler::new(&test_node, metrics::MetricsLogger::default());
        let countdown_proxy = create_factory_reset_countdown_proxy(reset_handler.clone());

        // Initial state should be FactoryResetState::Idle with no scheduled reset
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Idle);

        let non_reset_event = create_non_reset_consumer_controls_event();
        reset_handler.clone().handle_allowed_event(&non_reset_event).await;

        // This should result in the reset handler staying in the Allowed state
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Idle);
    }

    #[fuchsia::test]
    async fn handle_disallowed_event_wont_change_state() {
        let inspector = fuchsia_inspect::Inspector::default();
        let test_node = inspector.root().create_child("test_node");
        let reset_handler = FactoryResetHandler::new(&test_node, metrics::MetricsLogger::default());
        *reset_handler.factory_reset_state.borrow_mut() = FactoryResetState::Disallowed;

        // Calling handle_disallowed_event shouldn't change the state no matter
        // what the contents of the event are
        let reset_event = create_reset_consumer_controls_event();
        reset_handler.handle_disallowed_event(&reset_event);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Disallowed);

        let non_reset_event = create_non_reset_consumer_controls_event();
        reset_handler.handle_disallowed_event(&non_reset_event);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Disallowed);
    }

    #[fuchsia::test]
    async fn handle_button_countdown_event_changes_state_when_reset_no_longer_requested() {
        let inspector = fuchsia_inspect::Inspector::default();
        let test_node = inspector.root().create_child("test_node");
        let reset_handler = FactoryResetHandler::new(&test_node, metrics::MetricsLogger::default());

        let deadline = MonotonicInstant::after(BUTTON_TIMEOUT);
        *reset_handler.factory_reset_state.borrow_mut() =
            FactoryResetState::ButtonCountdown { deadline };

        // Calling handle_button_countdown_event should reset the handler
        // to the idle state
        let non_reset_event = create_non_reset_consumer_controls_event();
        reset_handler.handle_button_countdown_event(&non_reset_event);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Idle);
    }

    #[fuchsia::test]
    async fn handle_reset_countdown_event_changes_state_when_reset_no_longer_requested() {
        let inspector = fuchsia_inspect::Inspector::default();
        let test_node = inspector.root().create_child("test_node");
        let reset_handler = FactoryResetHandler::new(&test_node, metrics::MetricsLogger::default());

        *reset_handler.factory_reset_state.borrow_mut() =
            FactoryResetState::ResetCountdown { deadline: MonotonicInstant::now() };

        // Calling handle_reset_countdown_event should reset the handler
        // to the idle state
        let non_reset_event = create_non_reset_consumer_controls_event();
        reset_handler.handle_reset_countdown_event(&non_reset_event);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Idle);
    }

    #[fuchsia::test]
    async fn factory_reset_disallowed_during_button_countdown() {
        let inspector = fuchsia_inspect::Inspector::default();
        let test_node = inspector.root().create_child("test_node");
        let reset_handler = FactoryResetHandler::new(&test_node, metrics::MetricsLogger::default());
        let countdown_proxy = create_factory_reset_countdown_proxy(reset_handler.clone());

        // Initial state should be FactoryResetState::Idle with no scheduled reset
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Idle);

        // Send reset event
        let reset_event = create_reset_input_event();
        reset_handler.clone().handle_unhandled_input_event(reset_event).await;

        // State should now be ButtonCountdown and scheduled_reset_time should be None
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_matches!(
            reset_handler.factory_reset_state(),
            FactoryResetState::ButtonCountdown { deadline: _ }
        );

        // Set FactoryResetState to Disallow
        let device_proxy = create_recovery_policy_proxy(reset_handler.clone());
        device_proxy.set_is_local_reset_allowed(false).expect("Failed to set recovery policy");

        // State should now be in Disallow and scheduled_reset_time should be None
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Disallowed);
    }

    #[fuchsia::test]
    async fn factory_reset_disallowed_during_reset_countdown() {
        let inspector = fuchsia_inspect::Inspector::default();
        let test_node = inspector.root().create_child("test_node");
        let reset_handler = FactoryResetHandler::new(&test_node, metrics::MetricsLogger::default());
        let countdown_proxy = create_factory_reset_countdown_proxy(reset_handler.clone());

        // Initial state should be FactoryResetState::Idle with no scheduled reset
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Idle);

        // Send reset event
        let reset_event = create_reset_input_event();
        reset_handler.clone().handle_unhandled_input_event(reset_event).await;

        // State should now be ButtonCountdown and scheduled_reset_time should be None
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_matches!(
            reset_handler.factory_reset_state(),
            FactoryResetState::ButtonCountdown { deadline: _ }
        );

        // State should now be ResetCountdown and scheduled_reset_time should be Some
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_matches!(reset_state.scheduled_reset_time, Some(_));
        assert_matches!(
            reset_handler.factory_reset_state(),
            FactoryResetState::ResetCountdown { deadline: _ }
        );

        // Set FactoryResetState to Disallow
        let device_proxy = create_recovery_policy_proxy(reset_handler.clone());
        device_proxy.set_is_local_reset_allowed(false).expect("Failed to set recovery policy");

        // State should now be in Disallow and scheduled_reset_time should be None
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Disallowed);
    }

    #[fuchsia::test]
    async fn factory_reset_cancelled_during_button_countdown() {
        let inspector = fuchsia_inspect::Inspector::default();
        let test_node = inspector.root().create_child("test_node");
        let reset_handler = FactoryResetHandler::new(&test_node, metrics::MetricsLogger::default());
        let countdown_proxy = create_factory_reset_countdown_proxy(reset_handler.clone());

        // Initial state should be FactoryResetState::Idle with no scheduled reset
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Idle);

        // Send reset event
        let reset_event = create_reset_input_event();
        reset_handler.clone().handle_unhandled_input_event(reset_event).await;

        // State should now be ButtonCountdown and scheduled_reset_time should be None
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_matches!(
            reset_handler.factory_reset_state(),
            FactoryResetState::ButtonCountdown { deadline: _ }
        );

        // Pass in an event to simulate releasing the reset button
        let non_reset_event = create_non_reset_input_event();
        reset_handler.clone().handle_unhandled_input_event(non_reset_event).await;

        // State should now be in Idle and scheduled_reset_time should be None
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Idle);
    }

    #[fuchsia::test]
    async fn factory_reset_cancelled_during_reset_countdown() {
        let inspector = fuchsia_inspect::Inspector::default();
        let test_node = inspector.root().create_child("test_node");
        let reset_handler = FactoryResetHandler::new(&test_node, metrics::MetricsLogger::default());
        let countdown_proxy = create_factory_reset_countdown_proxy(reset_handler.clone());

        // Initial state should be FactoryResetState::Idle with no scheduled reset
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Idle);

        // Send reset event
        let reset_event = create_reset_input_event();
        reset_handler.clone().handle_unhandled_input_event(reset_event).await;

        // State should now be ButtonCountdown and scheduled_reset_time should be None
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_matches!(
            reset_handler.factory_reset_state(),
            FactoryResetState::ButtonCountdown { deadline: _ }
        );

        // State should now be ResetCountdown and scheduled_reset_time should be Some
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_matches!(reset_state.scheduled_reset_time, Some(_));
        assert_matches!(
            reset_handler.factory_reset_state(),
            FactoryResetState::ResetCountdown { deadline: _ }
        );

        // Pass in an event to simulate releasing the reset button
        let non_reset_event = create_non_reset_input_event();
        reset_handler.clone().handle_unhandled_input_event(non_reset_event).await;

        // State should now be in Idle and scheduled_reset_time should be None
        let reset_state = countdown_proxy.watch().await.expect("Failed to get countdown state");
        assert_eq!(reset_state.scheduled_reset_time, None);
        assert_eq!(reset_handler.factory_reset_state(), FactoryResetState::Idle);
    }

    #[fuchsia::test]
    fn factory_reset_handler_initialized_with_inspect_node() {
        let inspector = fuchsia_inspect::Inspector::default();
        let fake_handlers_node = inspector.root().create_child("input_handlers_node");
        let _handler =
            FactoryResetHandler::new(&fake_handlers_node, metrics::MetricsLogger::default());
        diagnostics_assertions::assert_data_tree!(inspector, root: {
            input_handlers_node: {
                factory_reset_handler: {
                    events_received_count: 0u64,
                    events_handled_count: 0u64,
                    last_received_timestamp_ns: 0u64,
                    "fuchsia.inspect.Health": {
                        status: "STARTING_UP",
                        // Timestamp value is unpredictable and not relevant in this context,
                        // so we only assert that the property is present.
                        start_timestamp_nanos: diagnostics_assertions::AnyProperty
                    },
                }
            }
        });
    }

    #[fuchsia::test]
    async fn factory_reset_handler_inspect_counts_events() {
        let inspector = fuchsia_inspect::Inspector::default();
        let fake_handlers_node = inspector.root().create_child("input_handlers_node");
        let reset_handler =
            FactoryResetHandler::new(&fake_handlers_node, metrics::MetricsLogger::default());

        // Send reset event
        let reset_event = create_reset_input_event();
        reset_handler.clone().handle_unhandled_input_event(reset_event).await;

        // Send handled event that should be ignored.
        let mut handled_event = input_device::InputEvent::from(create_reset_input_event());
        handled_event.handled = input_device::Handled::Yes;
        reset_handler.clone().handle_input_event(handled_event).await;

        // Send event to simulate releasing the reset button
        let non_reset_event = create_non_reset_input_event();
        let last_event_timestamp: u64 =
            non_reset_event.clone().event_time.into_nanos().try_into().unwrap();
        reset_handler.clone().handle_unhandled_input_event(non_reset_event).await;

        diagnostics_assertions::assert_data_tree!(inspector, root: {
            input_handlers_node: {
                factory_reset_handler: {
                    events_received_count: 2u64,
                    events_handled_count: 0u64,
                    last_received_timestamp_ns: last_event_timestamp,
                    "fuchsia.inspect.Health": {
                        status: "STARTING_UP",
                        // Timestamp value is unpredictable and not relevant in this context,
                        // so we only assert that the property is present.
                        start_timestamp_nanos: diagnostics_assertions::AnyProperty
                    },
                }
            }
        });
    }
}