blob: dfeb2d4c941d2d83e38e33340f5d7c81c2d44f15 [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
19#include <tuple>
20#include <vector>
21
22#include "update_engine/update_manager/next_update_check_policy_impl.h"
23
24using base::Time;
25using base::TimeDelta;
26using chromeos_update_engine::ErrorCode;
27using std::string;
28using std::tuple;
29using std::vector;
30
31namespace chromeos_update_manager {
32
33void UmPolicyTestBase::SetUp() {
34 loop_.SetAsCurrent();
35 SetUpDefaultClock();
36 eval_ctx_ = new EvaluationContext(&fake_clock_, TimeDelta::FromSeconds(5));
37 SetUpDefaultState();
38}
39
40void UmPolicyTestBase::TearDown() {
41 EXPECT_FALSE(loop_.PendingTasks());
42}
43
44// Sets the clock to fixed values.
45void UmPolicyTestBase::SetUpDefaultClock() {
46 fake_clock_.SetMonotonicTime(Time::FromInternalValue(12345678L));
47 fake_clock_.SetWallclockTime(Time::FromInternalValue(12345678901234L));
48}
49
50void UmPolicyTestBase::SetUpDefaultState() {
51 fake_state_.updater_provider()->var_updater_started_time()->reset(
52 new Time(fake_clock_.GetWallclockTime()));
53 fake_state_.updater_provider()->var_last_checked_time()->reset(
54 new Time(fake_clock_.GetWallclockTime()));
55 fake_state_.updater_provider()->var_consecutive_failed_update_checks()->reset(
56 new unsigned int(0));
57 fake_state_.updater_provider()->var_server_dictated_poll_interval()->reset(
58 new unsigned int(0));
59 fake_state_.updater_provider()->var_forced_update_requested()->reset(
60 new UpdateRequestStatus{UpdateRequestStatus::kNone});
61
62 // Chosen by fair dice roll. Guaranteed to be random.
63 fake_state_.random_provider()->var_seed()->reset(new uint64_t(4));
64}
65
66// Returns a default UpdateState structure:
67UpdateState UmPolicyTestBase::GetDefaultUpdateState(
68 TimeDelta first_seen_period) {
69 Time first_seen_time = fake_clock_.GetWallclockTime() - first_seen_period;
70 UpdateState update_state = UpdateState();
71
72 // This is a non-interactive check returning a delta payload, seen for the
73 // first time (|first_seen_period| ago). Clearly, there were no failed
74 // attempts so far.
75 update_state.is_interactive = false;
76 update_state.is_delta_payload = false;
77 update_state.first_seen = first_seen_time;
78 update_state.num_checks = 1;
79 update_state.num_failures = 0;
80 update_state.failures_last_updated = Time(); // Needs to be zero.
81 // There's a single HTTP download URL with a maximum of 10 retries.
82 update_state.download_urls = vector<string>{"http://fake/url/"};
83 update_state.download_errors_max = 10;
84 // Download was never attempted.
85 update_state.last_download_url_idx = -1;
86 update_state.last_download_url_num_errors = 0;
87 // There were no download errors.
88 update_state.download_errors = vector<tuple<int, ErrorCode, Time>>();
89 // P2P is not disabled by Omaha.
90 update_state.p2p_downloading_disabled = false;
91 update_state.p2p_sharing_disabled = false;
92 // P2P was not attempted.
93 update_state.p2p_num_attempts = 0;
94 update_state.p2p_first_attempted = Time();
95 // No active backoff period, backoff is not disabled by Omaha.
96 update_state.backoff_expiry = Time();
97 update_state.is_backoff_disabled = false;
98 // There is no active scattering wait period (max 7 days allowed) nor check
99 // threshold (none allowed).
100 update_state.scatter_wait_period = TimeDelta();
101 update_state.scatter_check_threshold = 0;
102 update_state.scatter_wait_period_max = TimeDelta::FromDays(7);
103 update_state.scatter_check_threshold_min = 0;
104 update_state.scatter_check_threshold_max = 0;
105
106 return update_state;
107}
108
109} // namespace chromeos_update_manager