1//
2// Copyright (C) 2014 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/real_system_provider.h"
18
19#include <memory>
20
21#include <base/time/time.h>
22#include <gmock/gmock.h>
23#include <gtest/gtest.h>
24
25#include "update_engine/common/fake_boot_control.h"
26#include "update_engine/common/fake_hardware.h"
27#include "update_engine/update_manager/umtest_utils.h"
28#if USE_CHROME_KIOSK_APP
29#include "libcros/dbus-proxies.h"
30#include "libcros/dbus-proxy-mocks.h"
31
32using org::chromium::LibCrosServiceInterfaceProxyMock;
33#endif  // USE_CHROME_KIOSK_APP
34using std::unique_ptr;
35using testing::_;
36using testing::DoAll;
37using testing::Return;
38using testing::SetArgPointee;
39
40#if USE_CHROME_KIOSK_APP
41namespace {
42const char kRequiredPlatformVersion[] ="1234.0.0";
43}  // namespace
44#endif  // USE_CHROME_KIOSK_APP
45
46namespace chromeos_update_manager {
47
48class UmRealSystemProviderTest : public ::testing::Test {
49 protected:
50  void SetUp() override {
51#if USE_CHROME_KIOSK_APP
52    libcros_proxy_mock_.reset(new LibCrosServiceInterfaceProxyMock());
53    ON_CALL(*libcros_proxy_mock_,
54            GetKioskAppRequiredPlatformVersion(_, _, _))
55        .WillByDefault(
56            DoAll(SetArgPointee<0>(kRequiredPlatformVersion), Return(true)));
57
58    provider_.reset(new RealSystemProvider(
59        &fake_hardware_, &fake_boot_control_, libcros_proxy_mock_.get()));
60#else
61    provider_.reset(
62        new RealSystemProvider(&fake_hardware_, &fake_boot_control_, nullptr));
63#endif  // USE_CHROME_KIOSK_APP
64    EXPECT_TRUE(provider_->Init());
65  }
66
67  chromeos_update_engine::FakeHardware fake_hardware_;
68  chromeos_update_engine::FakeBootControl fake_boot_control_;
69  unique_ptr<RealSystemProvider> provider_;
70
71#if USE_CHROME_KIOSK_APP
72  unique_ptr<LibCrosServiceInterfaceProxyMock> libcros_proxy_mock_;
73#endif  // USE_CHROME_KIOSK_APP
74};
75
76TEST_F(UmRealSystemProviderTest, InitTest) {
77  EXPECT_NE(nullptr, provider_->var_is_normal_boot_mode());
78  EXPECT_NE(nullptr, provider_->var_is_official_build());
79  EXPECT_NE(nullptr, provider_->var_is_oobe_complete());
80  EXPECT_NE(nullptr, provider_->var_kiosk_required_platform_version());
81}
82
83TEST_F(UmRealSystemProviderTest, IsOOBECompleteTrue) {
84  fake_hardware_.SetIsOOBEComplete(base::Time());
85  UmTestUtils::ExpectVariableHasValue(true, provider_->var_is_oobe_complete());
86}
87
88TEST_F(UmRealSystemProviderTest, IsOOBECompleteFalse) {
89  fake_hardware_.UnsetIsOOBEComplete();
90  UmTestUtils::ExpectVariableHasValue(false, provider_->var_is_oobe_complete());
91}
92
93#if USE_CHROME_KIOSK_APP
94TEST_F(UmRealSystemProviderTest, KioskRequiredPlatformVersion) {
95  UmTestUtils::ExpectVariableHasValue(
96      std::string(kRequiredPlatformVersion),
97      provider_->var_kiosk_required_platform_version());
98}
99
100TEST_F(UmRealSystemProviderTest, KioskRequiredPlatformVersionFailure) {
101  EXPECT_CALL(*libcros_proxy_mock_,
102              GetKioskAppRequiredPlatformVersion(_, _, _))
103      .WillOnce(Return(false));
104
105  UmTestUtils::ExpectVariableNotSet(
106      provider_->var_kiosk_required_platform_version());
107}
108
109TEST_F(UmRealSystemProviderTest,
110       KioskRequiredPlatformVersionRecoveryFromFailure) {
111  EXPECT_CALL(*libcros_proxy_mock_,
112              GetKioskAppRequiredPlatformVersion(_, _, _))
113      .WillOnce(Return(false));
114  UmTestUtils::ExpectVariableNotSet(
115      provider_->var_kiosk_required_platform_version());
116  testing::Mock::VerifyAndClearExpectations(libcros_proxy_mock_.get());
117
118  EXPECT_CALL(*libcros_proxy_mock_,
119              GetKioskAppRequiredPlatformVersion(_, _, _))
120      .WillOnce(
121          DoAll(SetArgPointee<0>(kRequiredPlatformVersion), Return(true)));
122  UmTestUtils::ExpectVariableHasValue(
123      std::string(kRequiredPlatformVersion),
124      provider_->var_kiosk_required_platform_version());
125}
126#else
127TEST_F(UmRealSystemProviderTest, KioskRequiredPlatformVersion) {
128  UmTestUtils::ExpectVariableHasValue(
129      std::string(), provider_->var_kiosk_required_platform_version());
130}
131#endif  // USE_CHROME_KIOSK_APP
132
133}  // namespace chromeos_update_manager
134