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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! This module contains code to dry-run test the state machine in against the existing Android/iOS implementations.
//! The idea is to create a `FxaStateChecker` instance, manually drive the state transitions and
//! check its state against the FirefoxAccount calls the existing implementation makes.
//!
//! Initially this will be tested by devs / QA.  Then we will ship it to real users and monitor which errors come back in Sentry.

use crate::{FxaEvent, FxaState};
use error_support::{breadcrumb, report_error};
use parking_lot::Mutex;

pub use super::internal_machines::Event as FxaStateCheckerEvent;
use super::internal_machines::State as InternalState;
use super::internal_machines::*;

/// State passed to the state checker, this is exactly the same as `internal_machines::State`
/// except the `Complete` variant uses a named field for UniFFI compatibility.
pub enum FxaStateCheckerState {
    GetAuthState,
    BeginOAuthFlow {
        scopes: Vec<String>,
        entrypoint: String,
    },
    BeginPairingFlow {
        pairing_url: String,
        scopes: Vec<String>,
        entrypoint: String,
    },
    CompleteOAuthFlow {
        code: String,
        state: String,
    },
    InitializeDevice,
    EnsureDeviceCapabilities,
    CheckAuthorizationStatus,
    GetProfile,
    Disconnect,
    Complete {
        new_state: FxaState,
    },
    Cancel,
}

pub struct FxaStateMachineChecker {
    inner: Mutex<FxaStateMachineCheckerInner>,
}

struct FxaStateMachineCheckerInner {
    public_state: FxaState,
    internal_state: InternalState,
    state_machine: Box<dyn InternalStateMachine + Send>,
    // Did we report an error?  If so, then we should give up checking things since the error is
    // likely to cascade
    reported_error: bool,
}

impl Default for FxaStateMachineChecker {
    fn default() -> Self {
        Self {
            inner: Mutex::new(FxaStateMachineCheckerInner {
                public_state: FxaState::Uninitialized,
                internal_state: InternalState::Cancel,
                state_machine: Box::new(UninitializedStateMachine),
                reported_error: false,
            }),
        }
    }
}

impl FxaStateMachineChecker {
    pub fn new() -> Self {
        Self::default()
    }

    /// Advance the internal state based on a public event
    pub fn handle_public_event(&self, event: FxaEvent) {
        let mut inner = self.inner.lock();
        if inner.reported_error {
            return;
        }
        match &inner.internal_state {
            InternalState::Complete(_) | InternalState::Cancel => (),
            internal_state => {
                report_error!(
                    "fxa-state-machine-checker",
                    "handle_public_event called with non-terminal internal state (event: {event}, internal state: {internal_state})",
                 );
                inner.reported_error = true;
                return;
            }
        }

        inner.state_machine = make_state_machine(&inner.public_state);
        match inner.state_machine.initial_state(event.clone()) {
            Ok(state) => {
                breadcrumb!(
                    "fxa-state-machine-checker: public transition start ({event} -> {state})"
                );
                inner.internal_state = state;
                if let InternalState::Complete(new_state) = &inner.internal_state {
                    inner.public_state = new_state.clone();
                    breadcrumb!("fxa-state-machine-checker: public transition end");
                }
            }
            Err(e) => {
                report_error!(
                    "fxa-state-machine-checker",
                    "Error in handle_public_event: {e}"
                );
                inner.reported_error = true;
            }
        }
    }

    /// Advance the internal state based on an internal event
    pub fn handle_internal_event(&self, event: FxaStateCheckerEvent) {
        let mut inner = self.inner.lock();
        if inner.reported_error {
            return;
        }
        match inner
            .state_machine
            .next_state(inner.internal_state.clone(), event.clone())
        {
            Ok(state) => {
                breadcrumb!("fxa-state-machine-checker: internal transition ({event} -> {state})");
                match &state {
                    InternalState::Complete(new_state) => {
                        inner.public_state = new_state.clone();
                        breadcrumb!("fxa-state-machine-checker: public transition end");
                    }
                    InternalState::Cancel => {
                        breadcrumb!("fxa-state-machine-checker: public transition end (cancelled)");
                    }
                    _ => (),
                };
                inner.internal_state = state;
            }
            Err(e) => {
                report_error!(
                    "fxa-state-machine-checker",
                    "Error in handle_internal_event: {e}"
                );
                inner.reported_error = true;
            }
        }
    }

    /// Check the internal state
    ///
    /// Call this when `processQueue`/`processEvent` has advanced the existing state machine to a public state.
    pub fn check_public_state(&self, state: FxaState) {
        let mut inner = self.inner.lock();
        if inner.reported_error {
            return;
        }
        match &inner.internal_state {
            InternalState::Complete(_) | InternalState::Cancel => (),
            internal_state => {
                report_error!(
                    "fxa-state-machine-checker",
                    "check_public_state called with non-terminal internal state (expected: {state} actual internal state: {internal_state})"
                 );
                inner.reported_error = true;
                return;
            }
        }
        if inner.public_state != state {
            report_error!(
                "fxa-state-machine-checker",
                "Public state mismatch: expected: {state}, actual: {} ({})",
                inner.public_state,
                inner.internal_state
            );
            inner.reported_error = true;
        } else {
            breadcrumb!("fxa-state-machine-checker: check_public_state successfull {state}");
        }
    }

    /// Check the internal state
    ///
    /// Call this when a FirefoxAccount call is about to be made
    pub fn check_internal_state(&self, state: FxaStateCheckerState) {
        let mut inner = self.inner.lock();
        if inner.reported_error {
            return;
        }
        let state: InternalState = state.into();
        if inner.internal_state != state {
            report_error!(
                "fxa-state-machine-checker",
                "Internal state mismatch (expected: {state}, actual: {})",
                inner.internal_state
            );
            inner.reported_error = true;
        } else {
            breadcrumb!("fxa-state-machine-checker: check_internal_state successfull {state}");
        }
    }
}

fn make_state_machine(public_state: &FxaState) -> Box<dyn InternalStateMachine + Send> {
    match public_state {
        FxaState::Uninitialized => Box::new(UninitializedStateMachine),
        FxaState::Disconnected => Box::new(DisconnectedStateMachine),
        FxaState::Authenticating { .. } => Box::new(AuthenticatingStateMachine),
        FxaState::Connected => Box::new(ConnectedStateMachine),
        FxaState::AuthIssues => Box::new(AuthIssuesStateMachine),
    }
}

impl From<InternalState> for FxaStateCheckerState {
    fn from(state: InternalState) -> Self {
        match state {
            InternalState::GetAuthState => Self::GetAuthState,
            InternalState::BeginOAuthFlow { scopes, entrypoint } => {
                Self::BeginOAuthFlow { scopes, entrypoint }
            }
            InternalState::BeginPairingFlow {
                pairing_url,
                scopes,
                entrypoint,
            } => Self::BeginPairingFlow {
                pairing_url,
                scopes,
                entrypoint,
            },
            InternalState::CompleteOAuthFlow { code, state } => {
                Self::CompleteOAuthFlow { code, state }
            }
            InternalState::InitializeDevice => Self::InitializeDevice,
            InternalState::EnsureDeviceCapabilities => Self::EnsureDeviceCapabilities,
            InternalState::CheckAuthorizationStatus => Self::CheckAuthorizationStatus,
            InternalState::Disconnect => Self::Disconnect,
            InternalState::GetProfile => Self::GetProfile,
            InternalState::Complete(new_state) => Self::Complete { new_state },
            InternalState::Cancel => Self::Cancel,
        }
    }
}

impl From<FxaStateCheckerState> for InternalState {
    fn from(state: FxaStateCheckerState) -> Self {
        match state {
            FxaStateCheckerState::GetAuthState => Self::GetAuthState,
            FxaStateCheckerState::BeginOAuthFlow { scopes, entrypoint } => {
                Self::BeginOAuthFlow { scopes, entrypoint }
            }
            FxaStateCheckerState::BeginPairingFlow {
                pairing_url,
                scopes,
                entrypoint,
            } => Self::BeginPairingFlow {
                pairing_url,
                scopes,
                entrypoint,
            },
            FxaStateCheckerState::CompleteOAuthFlow { code, state } => {
                Self::CompleteOAuthFlow { code, state }
            }
            FxaStateCheckerState::InitializeDevice => Self::InitializeDevice,
            FxaStateCheckerState::EnsureDeviceCapabilities => Self::EnsureDeviceCapabilities,
            FxaStateCheckerState::CheckAuthorizationStatus => Self::CheckAuthorizationStatus,
            FxaStateCheckerState::Disconnect => Self::Disconnect,
            FxaStateCheckerState::GetProfile => Self::GetProfile,
            FxaStateCheckerState::Complete { new_state } => Self::Complete(new_state),
            FxaStateCheckerState::Cancel => Self::Cancel,
        }
    }
}