1 //
2 // Copyright (C) 2018 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/update_time_restrictions_policy_impl.h"
18
19 #include <memory>
20
21 #include <base/time/time.h>
22
23 #include "update_engine/update_manager/policy_test_utils.h"
24 #include "update_engine/update_manager/weekly_time.h"
25
26 using base::Time;
27 using base::TimeDelta;
28 using chromeos_update_engine::ErrorCode;
29 using chromeos_update_engine::InstallPlan;
30 using std::string;
31
32 namespace chromeos_update_manager {
33
34 constexpr TimeDelta kHour = TimeDelta::FromHours(1);
35 constexpr TimeDelta kMinute = TimeDelta::FromMinutes(1);
36
37 const WeeklyTimeIntervalVector kTestIntervals{
38 // Monday 10:15 AM to Monday 3:30 PM.
39 WeeklyTimeInterval(WeeklyTime(1, kHour * 10 + kMinute * 15),
40 WeeklyTime(1, kHour * 15 + kMinute * 30)),
41 // Wednesday 8:30 PM to Thursday 8:40 AM.
42 WeeklyTimeInterval(WeeklyTime(3, kHour * 20 + kMinute * 30),
43 WeeklyTime(4, kHour * 8 + kMinute * 40)),
44 };
45
46 class UmUpdateTimeRestrictionsPolicyImplTest : public UmPolicyTestBase {
47 protected:
UmUpdateTimeRestrictionsPolicyImplTest()48 UmUpdateTimeRestrictionsPolicyImplTest() {
49 policy_ = std::make_unique<UpdateTimeRestrictionsPolicyImpl>();
50 }
51
TestPolicy(const Time::Exploded & exploded,const WeeklyTimeIntervalVector & test_intervals,const EvalStatus & expected_value,bool kiosk)52 void TestPolicy(const Time::Exploded& exploded,
53 const WeeklyTimeIntervalVector& test_intervals,
54 const EvalStatus& expected_value,
55 bool kiosk) {
56 if (kiosk)
57 fake_state_.device_policy_provider()
58 ->var_auto_launched_kiosk_app_id()
59 ->reset(new string("myapp"));
60
61 Time time;
62 EXPECT_TRUE(Time::FromLocalExploded(exploded, &time));
63 fake_clock_.SetWallclockTime(time);
64 SetUpDefaultTimeProvider();
65 fake_state_.device_policy_provider()
66 ->var_disallowed_time_intervals()
67 ->reset(new WeeklyTimeIntervalVector(test_intervals));
68 ErrorCode result;
69 InstallPlan install_plan;
70 ExpectPolicyStatus(
71 expected_value, &Policy::UpdateCanBeApplied, &result, &install_plan);
72 if (expected_value == EvalStatus::kSucceeded)
73 EXPECT_EQ(result, ErrorCode::kOmahaUpdateDeferredPerPolicy);
74 }
75 };
76
77 // If there are no intervals, then the check should always return kContinue.
TEST_F(UmUpdateTimeRestrictionsPolicyImplTest,NoIntervalsSetTest)78 TEST_F(UmUpdateTimeRestrictionsPolicyImplTest, NoIntervalsSetTest) {
79 Time::Exploded random_time{2018, 7, 1, 9, 12, 30, 0, 0};
80 TestPolicy(random_time,
81 WeeklyTimeIntervalVector(),
82 EvalStatus::kContinue,
83 /* kiosk = */ true);
84 }
85
86 // Check that all intervals are checked.
TEST_F(UmUpdateTimeRestrictionsPolicyImplTest,TimeInRange)87 TEST_F(UmUpdateTimeRestrictionsPolicyImplTest, TimeInRange) {
88 // Monday, July 9th 2018 12:30 PM.
89 Time::Exploded first_interval_time{2018, 7, 1, 9, 12, 30, 0, 0};
90 TestPolicy(first_interval_time,
91 kTestIntervals,
92 EvalStatus::kSucceeded,
93 /* kiosk = */ true);
94
95 // Check second interval.
96 // Thursday, July 12th 2018 4:30 AM.
97 Time::Exploded second_interval_time{2018, 7, 4, 12, 4, 30, 0, 0};
98 TestPolicy(second_interval_time,
99 kTestIntervals,
100 EvalStatus::kSucceeded,
101 /* kiosk = */ true);
102 }
103
TEST_F(UmUpdateTimeRestrictionsPolicyImplTest,TimeOutOfRange)104 TEST_F(UmUpdateTimeRestrictionsPolicyImplTest, TimeOutOfRange) {
105 // Monday, July 9th 2018 6:30 PM.
106 Time::Exploded out_of_range_time{2018, 7, 1, 9, 18, 30, 0, 0};
107 TestPolicy(out_of_range_time,
108 kTestIntervals,
109 EvalStatus::kContinue,
110 /* kiosk = */ true);
111 }
112
TEST_F(UmUpdateTimeRestrictionsPolicyImplTest,NoKioskDisablesPolicy)113 TEST_F(UmUpdateTimeRestrictionsPolicyImplTest, NoKioskDisablesPolicy) {
114 Time::Exploded in_range_time{2018, 7, 1, 9, 12, 30, 0, 0};
115 TestPolicy(in_range_time,
116 kTestIntervals,
117 EvalStatus::kContinue,
118 /* kiosk = */ false);
119 }
120 } // namespace chromeos_update_manager
121