1 //
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/android_things_policy.h"
18 
19 #include <memory>
20 
21 #include "update_engine/update_manager/next_update_check_policy_impl.h"
22 #include "update_engine/update_manager/policy_test_utils.h"
23 
24 using base::Time;
25 using base::TimeDelta;
26 using chromeos_update_engine::ErrorCode;
27 using chromeos_update_engine::InstallPlan;
28 
29 namespace chromeos_update_manager {
30 
31 class UmAndroidThingsPolicyTest : public UmPolicyTestBase {
32  protected:
33   UmAndroidThingsPolicyTest() {
34     policy_ = std::make_unique<AndroidThingsPolicy>();
35   }
36 
37   void SetUpDefaultState() override {
38     UmPolicyTestBase::SetUpDefaultState();
39 
40     // For the purpose of the tests, this is an official build
41     fake_state_.system_provider()->var_is_official_build()->reset(
42         new bool(true));
43     // NOLINTNEXTLINE(readability/casting)
44     fake_state_.system_provider()->var_num_slots()->reset(new unsigned int(2));
45   }
46 
47   // Configures the policy to return a desired value from UpdateCheckAllowed by
48   // faking the current wall clock time as needed. Restores the default state.
49   // This is used when testing policies that depend on this one.
50   virtual void SetUpdateCheckAllowed(bool allow_check) {
51     Time next_update_check;
52     CallMethodWithContext(&NextUpdateCheckTimePolicyImpl::NextUpdateCheckTime,
53                           &next_update_check,
54                           AndroidThingsPolicy::kNextUpdateCheckPolicyConstants);
55     SetUpDefaultState();
56     Time curr_time = next_update_check;
57     if (allow_check)
58       curr_time += TimeDelta::FromSeconds(1);
59     else
60       curr_time -= TimeDelta::FromSeconds(1);
61     fake_clock_.SetWallclockTime(curr_time);
62   }
63 };
64 
65 TEST_F(UmAndroidThingsPolicyTest, UpdateCheckAllowedWaitsForTheTimeout) {
66   // We get the next update_check timestamp from the policy's private method
67   // and then we check the public method respects that value on the normal
68   // case.
69   Time next_update_check;
70   Time last_checked_time =
71       fake_clock_.GetWallclockTime() + TimeDelta::FromMinutes(1234);
72 
73   LOG(INFO) << "last_checked_time: " << last_checked_time;
74   fake_state_.updater_provider()->var_last_checked_time()->reset(
75       new Time(last_checked_time));
76   CallMethodWithContext(&NextUpdateCheckTimePolicyImpl::NextUpdateCheckTime,
77                         &next_update_check,
78                         AndroidThingsPolicy::kNextUpdateCheckPolicyConstants);
79   LOG(INFO) << "Next check allowed at: " << next_update_check;
80 
81   // Check that the policy blocks until the next_update_check is reached.
82   SetUpDefaultClock();
83   SetUpDefaultState();
84   fake_state_.updater_provider()->var_last_checked_time()->reset(
85       new Time(last_checked_time));
86   fake_clock_.SetWallclockTime(next_update_check - TimeDelta::FromSeconds(1));
87 
88   UpdateCheckParams result;
89   ExpectPolicyStatus(
90       EvalStatus::kAskMeAgainLater, &Policy::UpdateCheckAllowed, &result);
91 
92   SetUpDefaultClock();
93   SetUpDefaultState();
94   fake_state_.updater_provider()->var_last_checked_time()->reset(
95       new Time(last_checked_time));
96   fake_clock_.SetWallclockTime(next_update_check + TimeDelta::FromSeconds(1));
97   ExpectPolicyStatus(
98       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
99   EXPECT_TRUE(result.updates_enabled);
100   EXPECT_FALSE(result.interactive);
101 }
102 
103 TEST_F(UmAndroidThingsPolicyTest,
104        UpdateCheckAllowedUpdatesDisabledForUnofficialBuilds) {
105   // UpdateCheckAllowed should return kAskMeAgainLater if this is an unofficial
106   // build; we don't want periodic update checks on developer images.
107 
108   fake_state_.system_provider()->var_is_official_build()->reset(
109       new bool(false));
110 
111   UpdateCheckParams result;
112   ExpectPolicyStatus(
113       EvalStatus::kAskMeAgainLater, &Policy::UpdateCheckAllowed, &result);
114 }
115 
116 TEST_F(UmAndroidThingsPolicyTest,
117        UpdateCheckAllowedUpdatesDisabledWhenNotEnoughSlotsAbUpdates) {
118   // UpdateCheckAllowed should return false (kSucceeded) if the image booted
119   // without enough slots to do A/B updates.
120 
121   // NOLINTNEXTLINE(readability/casting)
122   fake_state_.system_provider()->var_num_slots()->reset(new unsigned int(1));
123 
124   UpdateCheckParams result;
125   ExpectPolicyStatus(
126       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
127   EXPECT_FALSE(result.updates_enabled);
128 }
129 
130 TEST_F(UmAndroidThingsPolicyTest,
131        UpdateCheckAllowedForcedUpdateRequestedInteractive) {
132   // UpdateCheckAllowed should return true because a forced update request was
133   // signaled for an interactive update.
134 
135   SetUpdateCheckAllowed(true);
136   fake_state_.updater_provider()->var_forced_update_requested()->reset(
137       new UpdateRequestStatus(UpdateRequestStatus::kInteractive));
138 
139   UpdateCheckParams result;
140   ExpectPolicyStatus(
141       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
142   EXPECT_TRUE(result.updates_enabled);
143   EXPECT_TRUE(result.interactive);
144 }
145 
146 TEST_F(UmAndroidThingsPolicyTest,
147        UpdateCheckAllowedForcedUpdateRequestedPeriodic) {
148   // UpdateCheckAllowed should return true because a forced update request was
149   // signaled for a periodic check.
150 
151   SetUpdateCheckAllowed(true);
152   fake_state_.updater_provider()->var_forced_update_requested()->reset(
153       new UpdateRequestStatus(UpdateRequestStatus::kPeriodic));
154 
155   UpdateCheckParams result;
156   ExpectPolicyStatus(
157       EvalStatus::kSucceeded, &Policy::UpdateCheckAllowed, &result);
158   EXPECT_TRUE(result.updates_enabled);
159   EXPECT_FALSE(result.interactive);
160 }
161 
162 TEST_F(UmAndroidThingsPolicyTest, UpdateCanBeAppliedOk) {
163   // UpdateCanBeApplied should return kSucceeded in the base case
164 
165   InstallPlan plan;
166   ErrorCode result;
167   ExpectPolicyStatus(
168       EvalStatus::kSucceeded, &Policy::UpdateCanBeApplied, &result, &plan);
169 
170   EXPECT_EQ(ErrorCode::kSuccess, result);
171 }
172 
173 TEST_F(UmAndroidThingsPolicyTest, UpdateCanBeAppliedRestricted) {
174   // UpdateCanBeApplied should return kOmahaUpdateDeferredPerPolicy in
175   // when the restricted flag is set in the Updater.
176 
177   fake_state_.updater_provider()->var_update_restrictions()->reset(
178       new UpdateRestrictions(UpdateRestrictions::kRestrictDownloading));
179 
180   InstallPlan plan;
181   ErrorCode result;
182   ExpectPolicyStatus(
183       EvalStatus::kSucceeded, &Policy::UpdateCanBeApplied, &result, &plan);
184 
185   EXPECT_EQ(ErrorCode::kOmahaUpdateDeferredPerPolicy, result);
186 }
187 
188 }  // namespace chromeos_update_manager
189