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
// 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.
//
use crate::{
    common::{App, CheckOptions, CheckTiming, ProtocolState, UpdateCheckSchedule},
    installer::Plan,
    policy::{CheckDecision, Policy, PolicyData, PolicyEngine, UpdateDecision},
    request_builder::RequestParams,
    time::TimeSource,
};
use futures::future::BoxFuture;
use futures::prelude::*;

/// A stub policy implementation that allows everything immediately.
pub struct StubPolicy<P: Plan> {
    _phantom_data: std::marker::PhantomData<P>,
}

impl<P: Plan> Policy for StubPolicy<P> {
    type ComputeNextUpdateTimePolicyData = PolicyData;
    type UpdateCheckAllowedPolicyData = ();
    type UpdateCanStartPolicyData = ();
    type RebootPolicyData = ();
    type InstallPlan = P;

    fn compute_next_update_time(
        policy_data: &Self::ComputeNextUpdateTimePolicyData,
        _apps: &[App],
        _scheduling: &UpdateCheckSchedule,
        _protocol_state: &ProtocolState,
    ) -> CheckTiming {
        CheckTiming::builder()
            .time(policy_data.current_time)
            .build()
    }

    fn update_check_allowed(
        _policy_data: &Self::UpdateCheckAllowedPolicyData,
        _apps: &[App],
        _scheduling: &UpdateCheckSchedule,
        _protocol_state: &ProtocolState,
        check_options: &CheckOptions,
    ) -> CheckDecision {
        CheckDecision::Ok(RequestParams {
            source: check_options.source,
            use_configured_proxies: true,
            ..RequestParams::default()
        })
    }

    fn update_can_start(
        _policy_data: &Self::UpdateCanStartPolicyData,
        _proposed_install_plan: &Self::InstallPlan,
    ) -> UpdateDecision {
        UpdateDecision::Ok
    }

    fn reboot_allowed(
        _policy_data: &Self::RebootPolicyData,
        _check_options: &CheckOptions,
    ) -> bool {
        true
    }

    fn reboot_needed(_install_plan: &Self::InstallPlan) -> bool {
        true
    }
}

/// A stub PolicyEngine that just gathers the current time and hands it off to the StubPolicy as the
/// PolicyData.
#[derive(Debug)]
pub struct StubPolicyEngine<P: Plan, T: TimeSource> {
    time_source: T,
    _phantom_data: std::marker::PhantomData<P>,
}

impl<P, T> StubPolicyEngine<P, T>
where
    T: TimeSource,
    P: Plan,
{
    pub fn new(time_source: T) -> Self {
        Self {
            time_source,
            _phantom_data: std::marker::PhantomData,
        }
    }
}

impl<P, T> PolicyEngine for StubPolicyEngine<P, T>
where
    T: TimeSource + Clone,
    P: Plan,
{
    type TimeSource = T;
    type InstallResult = ();
    type InstallPlan = P;

    fn time_source(&self) -> &Self::TimeSource {
        &self.time_source
    }

    fn compute_next_update_time(
        &mut self,
        apps: &[App],
        scheduling: &UpdateCheckSchedule,
        protocol_state: &ProtocolState,
    ) -> BoxFuture<'_, CheckTiming> {
        let check_timing = StubPolicy::<P>::compute_next_update_time(
            &PolicyData::builder()
                .current_time(self.time_source.now())
                .build(),
            apps,
            scheduling,
            protocol_state,
        );
        future::ready(check_timing).boxed()
    }

    fn update_check_allowed(
        &mut self,
        apps: &[App],
        scheduling: &UpdateCheckSchedule,
        protocol_state: &ProtocolState,
        check_options: &CheckOptions,
    ) -> BoxFuture<'_, CheckDecision> {
        let decision = StubPolicy::<P>::update_check_allowed(
            &(),
            apps,
            scheduling,
            protocol_state,
            check_options,
        );
        future::ready(decision).boxed()
    }

    fn update_can_start<'p>(
        &mut self,
        proposed_install_plan: &'p Self::InstallPlan,
    ) -> BoxFuture<'p, UpdateDecision> {
        let decision = StubPolicy::<P>::update_can_start(&(), proposed_install_plan);
        future::ready(decision).boxed()
    }

    fn reboot_allowed(
        &mut self,
        check_options: &CheckOptions,
        _install_result: &Self::InstallResult,
    ) -> BoxFuture<'_, bool> {
        let decision = StubPolicy::<P>::reboot_allowed(&(), check_options);
        future::ready(decision).boxed()
    }

    fn reboot_needed(&mut self, install_plan: &Self::InstallPlan) -> BoxFuture<'_, bool> {
        let decision = StubPolicy::<P>::reboot_needed(install_plan);
        future::ready(decision).boxed()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        installer::stub::StubPlan, protocol::request::InstallSource, time::MockTimeSource,
    };

    #[test]
    fn test_compute_next_update_time() {
        let policy_data = PolicyData::builder()
            .current_time(MockTimeSource::new_from_now().now())
            .build();
        let update_check_schedule = UpdateCheckSchedule::default();
        let result = StubPolicy::<StubPlan>::compute_next_update_time(
            &policy_data,
            &[],
            &update_check_schedule,
            &ProtocolState::default(),
        );
        let expected = CheckTiming::builder()
            .time(policy_data.current_time)
            .build();
        assert_eq!(result, expected);
    }

    #[test]
    fn test_update_check_allowed_on_demand() {
        let check_options = CheckOptions {
            source: InstallSource::OnDemand,
        };
        let result = StubPolicy::<StubPlan>::update_check_allowed(
            &(),
            &[],
            &UpdateCheckSchedule::default(),
            &ProtocolState::default(),
            &check_options,
        );
        let expected = CheckDecision::Ok(RequestParams {
            source: check_options.source,
            use_configured_proxies: true,
            ..RequestParams::default()
        });
        assert_eq!(result, expected);
    }

    #[test]
    fn test_update_check_allowed_scheduled_task() {
        let check_options = CheckOptions {
            source: InstallSource::ScheduledTask,
        };
        let result = StubPolicy::<StubPlan>::update_check_allowed(
            &(),
            &[],
            &UpdateCheckSchedule::default(),
            &ProtocolState::default(),
            &check_options,
        );
        let expected = CheckDecision::Ok(RequestParams {
            source: check_options.source,
            use_configured_proxies: true,
            ..RequestParams::default()
        });
        assert_eq!(result, expected);
    }

    #[test]
    fn test_update_can_start() {
        let result = StubPolicy::update_can_start(&(), &StubPlan);
        assert_eq!(result, UpdateDecision::Ok);
    }
}