tanzh
2024-09-27 0179a5c4855513427205007d983ec47eba6a13f6
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
//
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
 
#include "update_engine/update_manager/update_time_restrictions_policy_impl.h"
 
#include <memory>
 
#include <base/time/time.h>
 
#include "update_engine/update_manager/policy_test_utils.h"
#include "update_engine/update_manager/weekly_time.h"
 
using base::Time;
using base::TimeDelta;
using chromeos_update_engine::ErrorCode;
using chromeos_update_engine::InstallPlan;
using std::string;
 
namespace chromeos_update_manager {
 
constexpr TimeDelta kHour = TimeDelta::FromHours(1);
constexpr TimeDelta kMinute = TimeDelta::FromMinutes(1);
 
const WeeklyTimeIntervalVector kTestIntervals{
    // Monday 10:15 AM to Monday 3:30 PM.
    WeeklyTimeInterval(WeeklyTime(1, kHour * 10 + kMinute * 15),
                       WeeklyTime(1, kHour * 15 + kMinute * 30)),
    // Wednesday 8:30 PM to Thursday 8:40 AM.
    WeeklyTimeInterval(WeeklyTime(3, kHour * 20 + kMinute * 30),
                       WeeklyTime(4, kHour * 8 + kMinute * 40)),
};
 
class UmUpdateTimeRestrictionsPolicyImplTest : public UmPolicyTestBase {
 protected:
  UmUpdateTimeRestrictionsPolicyImplTest() {
    policy_ = std::make_unique<UpdateTimeRestrictionsPolicyImpl>();
  }
 
  void TestPolicy(const Time::Exploded& exploded,
                  const WeeklyTimeIntervalVector& test_intervals,
                  const EvalStatus& expected_value,
                  bool kiosk) {
    if (kiosk)
      fake_state_.device_policy_provider()
          ->var_auto_launched_kiosk_app_id()
          ->reset(new string("myapp"));
 
    Time time;
    EXPECT_TRUE(Time::FromLocalExploded(exploded, &time));
    fake_clock_.SetWallclockTime(time);
    SetUpDefaultTimeProvider();
    fake_state_.device_policy_provider()
        ->var_disallowed_time_intervals()
        ->reset(new WeeklyTimeIntervalVector(test_intervals));
    ErrorCode result;
    InstallPlan install_plan;
    ExpectPolicyStatus(
        expected_value, &Policy::UpdateCanBeApplied, &result, &install_plan);
    if (expected_value == EvalStatus::kSucceeded)
      EXPECT_EQ(result, ErrorCode::kOmahaUpdateDeferredPerPolicy);
  }
};
 
// If there are no intervals, then the check should always return kContinue.
TEST_F(UmUpdateTimeRestrictionsPolicyImplTest, NoIntervalsSetTest) {
  Time::Exploded random_time{2018, 7, 1, 9, 12, 30, 0, 0};
  TestPolicy(random_time,
             WeeklyTimeIntervalVector(),
             EvalStatus::kContinue,
             /* kiosk = */ true);
}
 
// Check that all intervals are checked.
TEST_F(UmUpdateTimeRestrictionsPolicyImplTest, TimeInRange) {
  // Monday, July 9th 2018 12:30 PM.
  Time::Exploded first_interval_time{2018, 7, 1, 9, 12, 30, 0, 0};
  TestPolicy(first_interval_time,
             kTestIntervals,
             EvalStatus::kSucceeded,
             /* kiosk = */ true);
 
  // Check second interval.
  // Thursday, July 12th 2018 4:30 AM.
  Time::Exploded second_interval_time{2018, 7, 4, 12, 4, 30, 0, 0};
  TestPolicy(second_interval_time,
             kTestIntervals,
             EvalStatus::kSucceeded,
             /* kiosk = */ true);
}
 
TEST_F(UmUpdateTimeRestrictionsPolicyImplTest, TimeOutOfRange) {
  // Monday, July 9th 2018 6:30 PM.
  Time::Exploded out_of_range_time{2018, 7, 1, 9, 18, 30, 0, 0};
  TestPolicy(out_of_range_time,
             kTestIntervals,
             EvalStatus::kContinue,
             /* kiosk = */ true);
}
 
TEST_F(UmUpdateTimeRestrictionsPolicyImplTest, NoKioskDisablesPolicy) {
  Time::Exploded in_range_time{2018, 7, 1, 9, 12, 30, 0, 0};
  TestPolicy(in_range_time,
             kTestIntervals,
             EvalStatus::kContinue,
             /* kiosk = */ false);
}
}  // namespace chromeos_update_manager