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 #include <kiosk-app/dbus-proxies.h>
25 #include <kiosk-app/dbus-proxy-mocks.h>
26
27 #include "update_engine/common/fake_boot_control.h"
28 #include "update_engine/common/fake_hardware.h"
29 #include "update_engine/cros/fake_system_state.h"
30 #include "update_engine/update_manager/umtest_utils.h"
31
32 using chromeos_update_engine::FakeSystemState;
33 using org::chromium::KioskAppServiceInterfaceProxyMock;
34 using std::unique_ptr;
35 using testing::_;
36 using testing::DoAll;
37 using testing::Return;
38 using testing::SetArgPointee;
39
40 namespace {
41 const char kRequiredPlatformVersion[] = "1234.0.0";
42 } // namespace
43
44 namespace chromeos_update_manager {
45
46 class UmRealSystemProviderTest : public ::testing::Test {
47 protected:
SetUp()48 void SetUp() override {
49 FakeSystemState::CreateInstance();
50 kiosk_app_proxy_mock_.reset(new KioskAppServiceInterfaceProxyMock());
51 ON_CALL(*kiosk_app_proxy_mock_, GetRequiredPlatformVersion(_, _, _))
52 .WillByDefault(
53 DoAll(SetArgPointee<0>(kRequiredPlatformVersion), Return(true)));
54
55 provider_.reset(new RealSystemProvider(kiosk_app_proxy_mock_.get()));
56 EXPECT_TRUE(provider_->Init());
57 }
58
59 unique_ptr<RealSystemProvider> provider_;
60
61 unique_ptr<KioskAppServiceInterfaceProxyMock> kiosk_app_proxy_mock_;
62 };
63
TEST_F(UmRealSystemProviderTest,InitTest)64 TEST_F(UmRealSystemProviderTest, InitTest) {
65 EXPECT_NE(nullptr, provider_->var_is_normal_boot_mode());
66 EXPECT_NE(nullptr, provider_->var_is_official_build());
67 EXPECT_NE(nullptr, provider_->var_is_oobe_complete());
68 EXPECT_NE(nullptr, provider_->var_kiosk_required_platform_version());
69 EXPECT_NE(nullptr, provider_->var_chromeos_version());
70 }
71
TEST_F(UmRealSystemProviderTest,IsOOBECompleteTrue)72 TEST_F(UmRealSystemProviderTest, IsOOBECompleteTrue) {
73 FakeSystemState::Get()->fake_hardware()->SetIsOOBEComplete(base::Time());
74 UmTestUtils::ExpectVariableHasValue(true, provider_->var_is_oobe_complete());
75 }
76
TEST_F(UmRealSystemProviderTest,IsOOBECompleteFalse)77 TEST_F(UmRealSystemProviderTest, IsOOBECompleteFalse) {
78 FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
79 UmTestUtils::ExpectVariableHasValue(false, provider_->var_is_oobe_complete());
80 }
81
TEST_F(UmRealSystemProviderTest,VersionFromRequestParams)82 TEST_F(UmRealSystemProviderTest, VersionFromRequestParams) {
83 FakeSystemState::Get()->request_params()->set_app_version("1.2.3");
84 // Call |Init| again to pick up the version.
85 EXPECT_TRUE(provider_->Init());
86
87 base::Version version("1.2.3");
88 UmTestUtils::ExpectVariableHasValue(version,
89 provider_->var_chromeos_version());
90 }
91
TEST_F(UmRealSystemProviderTest,KioskRequiredPlatformVersion)92 TEST_F(UmRealSystemProviderTest, KioskRequiredPlatformVersion) {
93 UmTestUtils::ExpectVariableHasValue(
94 std::string(kRequiredPlatformVersion),
95 provider_->var_kiosk_required_platform_version());
96 }
97
TEST_F(UmRealSystemProviderTest,KioskRequiredPlatformVersionFailure)98 TEST_F(UmRealSystemProviderTest, KioskRequiredPlatformVersionFailure) {
99 EXPECT_CALL(*kiosk_app_proxy_mock_, GetRequiredPlatformVersion(_, _, _))
100 .WillOnce(Return(false));
101
102 UmTestUtils::ExpectVariableNotSet(
103 provider_->var_kiosk_required_platform_version());
104 }
105
TEST_F(UmRealSystemProviderTest,KioskRequiredPlatformVersionRecoveryFromFailure)106 TEST_F(UmRealSystemProviderTest,
107 KioskRequiredPlatformVersionRecoveryFromFailure) {
108 EXPECT_CALL(*kiosk_app_proxy_mock_, GetRequiredPlatformVersion(_, _, _))
109 .WillOnce(Return(false));
110 UmTestUtils::ExpectVariableNotSet(
111 provider_->var_kiosk_required_platform_version());
112 testing::Mock::VerifyAndClearExpectations(kiosk_app_proxy_mock_.get());
113
114 EXPECT_CALL(*kiosk_app_proxy_mock_, GetRequiredPlatformVersion(_, _, _))
115 .WillOnce(
116 DoAll(SetArgPointee<0>(kRequiredPlatformVersion), Return(true)));
117 UmTestUtils::ExpectVariableHasValue(
118 std::string(kRequiredPlatformVersion),
119 provider_->var_kiosk_required_platform_version());
120 }
121
TEST_F(UmRealSystemProviderTest,KioskRequiredPlatformVersionRepeatedFailure)122 TEST_F(UmRealSystemProviderTest, KioskRequiredPlatformVersionRepeatedFailure) {
123 // Simulate unreadable platform version. The variable should return a
124 // null pointer |kRetryPollVariableMaxRetry| times and then return an empty
125 // string to indicate that it gave up.
126 constexpr int kNumMethodCalls = 5;
127 EXPECT_CALL(*kiosk_app_proxy_mock_, GetRequiredPlatformVersion)
128 .Times(kNumMethodCalls + 1)
129 .WillRepeatedly(Return(false));
130 for (int i = 0; i < kNumMethodCalls; ++i) {
131 UmTestUtils::ExpectVariableNotSet(
132 provider_->var_kiosk_required_platform_version());
133 }
134 UmTestUtils::ExpectVariableHasValue(
135 std::string(""), provider_->var_kiosk_required_platform_version());
136 }
137
138 } // namespace chromeos_update_manager
139