blob: 653592ac8e7c3376ec1d496fe332c521f608d601 [file] [log] [blame]
Aaron Wood56d8ab32017-09-22 15:56:18 -07001//
2// Copyright (C) 2017 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include "update_engine/update_manager/policy_test_utils.h"
18
Aaron Woodc73fdc12017-12-06 11:09:15 -080019#include <memory>
Aaron Wood56d8ab32017-09-22 15:56:18 -070020#include <tuple>
21#include <vector>
22
23#include "update_engine/update_manager/next_update_check_policy_impl.h"
24
25using base::Time;
26using base::TimeDelta;
27using chromeos_update_engine::ErrorCode;
28using std::string;
29using std::tuple;
30using std::vector;
31
32namespace chromeos_update_manager {
33
34void UmPolicyTestBase::SetUp() {
35 loop_.SetAsCurrent();
36 SetUpDefaultClock();
Amin Hassania2c8b922019-08-14 19:41:03 -070037 eval_ctx_.reset(
38 new EvaluationContext(&fake_clock_, TimeDelta::FromSeconds(5)));
Aaron Wood56d8ab32017-09-22 15:56:18 -070039 SetUpDefaultState();
40}
41
42void UmPolicyTestBase::TearDown() {
43 EXPECT_FALSE(loop_.PendingTasks());
44}
45
46// Sets the clock to fixed values.
47void UmPolicyTestBase::SetUpDefaultClock() {
48 fake_clock_.SetMonotonicTime(Time::FromInternalValue(12345678L));
49 fake_clock_.SetWallclockTime(Time::FromInternalValue(12345678901234L));
50}
51
Adolfo Victoria94ffe132018-06-28 16:14:56 -070052void UmPolicyTestBase::SetUpDefaultTimeProvider() {
53 Time current_time = fake_clock_.GetWallclockTime();
54 base::Time::Exploded exploded;
55 current_time.LocalExplode(&exploded);
56 fake_state_.time_provider()->var_curr_hour()->reset(new int(exploded.hour));
57 fake_state_.time_provider()->var_curr_minute()->reset(
58 new int(exploded.minute));
59 fake_state_.time_provider()->var_curr_date()->reset(
60 new Time(current_time.LocalMidnight()));
61}
62
Aaron Wood56d8ab32017-09-22 15:56:18 -070063void UmPolicyTestBase::SetUpDefaultState() {
64 fake_state_.updater_provider()->var_updater_started_time()->reset(
65 new Time(fake_clock_.GetWallclockTime()));
66 fake_state_.updater_provider()->var_last_checked_time()->reset(
67 new Time(fake_clock_.GetWallclockTime()));
68 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->reset(
Aaron Woodc73fdc12017-12-06 11:09:15 -080069 new unsigned int(0)); // NOLINT(readability/casting)
Aaron Wood56d8ab32017-09-22 15:56:18 -070070 fake_state_.updater_provider()->var_server_dictated_poll_interval()->reset(
Aaron Woodc73fdc12017-12-06 11:09:15 -080071 new unsigned int(0)); // NOLINT(readability/casting)
Aaron Wood56d8ab32017-09-22 15:56:18 -070072 fake_state_.updater_provider()->var_forced_update_requested()->reset(
73 new UpdateRequestStatus{UpdateRequestStatus::kNone});
74
75 // Chosen by fair dice roll. Guaranteed to be random.
76 fake_state_.random_provider()->var_seed()->reset(new uint64_t(4));
77}
78
79// Returns a default UpdateState structure:
80UpdateState UmPolicyTestBase::GetDefaultUpdateState(
81 TimeDelta first_seen_period) {
82 Time first_seen_time = fake_clock_.GetWallclockTime() - first_seen_period;
83 UpdateState update_state = UpdateState();
84
85 // This is a non-interactive check returning a delta payload, seen for the
86 // first time (|first_seen_period| ago). Clearly, there were no failed
87 // attempts so far.
Amin Hassanied37d682018-04-06 13:22:00 -070088 update_state.interactive = false;
Aaron Wood56d8ab32017-09-22 15:56:18 -070089 update_state.is_delta_payload = false;
90 update_state.first_seen = first_seen_time;
91 update_state.num_checks = 1;
92 update_state.num_failures = 0;
93 update_state.failures_last_updated = Time(); // Needs to be zero.
94 // There's a single HTTP download URL with a maximum of 10 retries.
95 update_state.download_urls = vector<string>{"http://fake/url/"};
96 update_state.download_errors_max = 10;
97 // Download was never attempted.
98 update_state.last_download_url_idx = -1;
99 update_state.last_download_url_num_errors = 0;
100 // There were no download errors.
101 update_state.download_errors = vector<tuple<int, ErrorCode, Time>>();
102 // P2P is not disabled by Omaha.
103 update_state.p2p_downloading_disabled = false;
104 update_state.p2p_sharing_disabled = false;
105 // P2P was not attempted.
106 update_state.p2p_num_attempts = 0;
107 update_state.p2p_first_attempted = Time();
108 // No active backoff period, backoff is not disabled by Omaha.
109 update_state.backoff_expiry = Time();
110 update_state.is_backoff_disabled = false;
111 // There is no active scattering wait period (max 7 days allowed) nor check
112 // threshold (none allowed).
113 update_state.scatter_wait_period = TimeDelta();
114 update_state.scatter_check_threshold = 0;
115 update_state.scatter_wait_period_max = TimeDelta::FromDays(7);
116 update_state.scatter_check_threshold_min = 0;
117 update_state.scatter_check_threshold_max = 0;
118
119 return update_state;
120}
121
Aaron Woodc73fdc12017-12-06 11:09:15 -0800122} // namespace chromeos_update_manager