OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/login/signed_settings.h" | |
6 | |
7 #include <map> | |
8 #include <string> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/message_loop.h" | |
13 #include "base/values.h" | |
14 #include "chrome/browser/chromeos/device_settings_provider.h" | |
15 #include "chrome/browser/chromeos/cros/cros_library.h" | |
16 #include "chrome/browser/chromeos/cros_settings_names.h" | |
17 #include "chrome/browser/chromeos/login/mock_signed_settings_helper.h" | |
18 #include "chrome/browser/chromeos/login/mock_user_manager.h" | |
19 #include "chrome/browser/chromeos/login/ownership_service.h" | |
20 #include "chrome/browser/chromeos/login/signed_settings_cache.h" | |
21 #include "chrome/browser/metrics/metrics_service.h" | |
22 #include "chrome/browser/policy/browser_policy_connector.h" | |
23 #include "chrome/browser/policy/cloud_policy_data_store.h" | |
24 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" | |
25 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
26 #include "chrome/test/base/testing_browser_process.h" | |
27 #include "chrome/test/base/testing_pref_service.h" | |
28 #include "content/test/test_browser_thread.h" | |
29 #include "testing/gmock/include/gmock/gmock.h" | |
30 #include "testing/gtest/include/gtest/gtest.h" | |
31 | |
32 namespace em = enterprise_management; | |
33 namespace chromeos { | |
34 | |
35 using ::testing::_; | |
36 using ::testing::AnyNumber; | |
37 using ::testing::Mock; | |
38 using ::testing::Return; | |
39 using ::testing::SaveArg; | |
40 | |
41 class DeviceSettingsProviderTest: public testing::Test { | |
42 public: | |
43 void SettingChanged(const std::string& name) { | |
44 notifier_called_ = true; | |
45 } | |
46 | |
47 void GetTrustedCallback() { | |
48 trusted_called_ = true; | |
49 } | |
50 protected: | |
51 DeviceSettingsProviderTest() | |
52 : message_loop_(MessageLoop::TYPE_UI), | |
53 ui_thread_(content::BrowserThread::UI, &message_loop_), | |
54 file_thread_(content::BrowserThread::FILE, &message_loop_), | |
55 pointer_factory_(this), | |
56 local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)) { | |
57 } | |
58 | |
59 virtual ~DeviceSettingsProviderTest() { | |
60 } | |
61 | |
62 virtual void SetUp() { | |
63 metrics_service_ = new MetricsService; | |
64 static_cast<TestingBrowserProcess*>(g_browser_process)->SetMetricsService( | |
65 metrics_service_); | |
66 | |
67 PrepareEmptyPolicy(); | |
68 | |
69 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)) | |
70 .Times(AnyNumber()) | |
71 .WillRepeatedly( | |
72 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS, | |
73 policy_blob_)); | |
74 EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_,_)) | |
75 .Times(AnyNumber()) | |
76 .WillRepeatedly(DoAll( | |
77 SaveArg<0>(&policy_blob_), | |
78 MockSignedSettingsHelperStorePolicy(SignedSettings::SUCCESS))); | |
79 | |
80 mock_user_manager_ = new MockUserManager(); | |
81 UserManager::Set(mock_user_manager_); | |
82 EXPECT_CALL(*mock_user_manager_, IsCurrentUserOwner()) | |
83 .Times(AnyNumber()) | |
84 .WillRepeatedly(Return(true)); | |
85 | |
86 provider_.reset( | |
87 new DeviceSettingsProvider( | |
88 base::Bind(&DeviceSettingsProviderTest::SettingChanged, | |
89 pointer_factory_.GetWeakPtr()), | |
90 &signed_settings_helper_, | |
91 OwnershipService::OWNERSHIP_TAKEN)); | |
92 | |
93 trusted_called_ = false; | |
94 notifier_called_ = false; | |
95 } | |
96 | |
97 virtual void TearDown() { | |
98 } | |
99 | |
100 void PrepareEmptyPolicy() { | |
101 em::PolicyData policy; | |
102 em::ChromeDeviceSettingsProto pol; | |
103 // Set metrics to disabled to prevent us from running into code that is not | |
104 // mocked. | |
105 pol.mutable_metrics_enabled()->set_metrics_enabled(false); | |
106 policy.set_policy_type(chromeos::kDevicePolicyType); | |
107 policy.set_username("me@owner"); | |
108 policy.set_policy_value(pol.SerializeAsString()); | |
109 // Wipe the signed settings store. | |
110 policy_blob_.set_policy_data(policy.SerializeAsString()); | |
111 policy_blob_.set_policy_data_signature("false"); | |
112 } | |
113 | |
114 em::PolicyFetchResponse policy_blob_; | |
115 | |
116 scoped_ptr<DeviceSettingsProvider> provider_; | |
117 | |
118 MessageLoop message_loop_; | |
119 content::TestBrowserThread ui_thread_; | |
120 content::TestBrowserThread file_thread_; | |
121 | |
122 base::WeakPtrFactory<DeviceSettingsProviderTest> pointer_factory_; | |
123 | |
124 ScopedTestingLocalState local_state_; | |
125 | |
126 MockSignedSettingsHelper signed_settings_helper_; | |
127 MockUserManager* mock_user_manager_; | |
128 | |
129 ScopedStubCrosEnabler stub_cros_enabler_; | |
130 | |
131 MetricsService* metrics_service_; | |
132 | |
133 bool trusted_called_; | |
134 bool notifier_called_; | |
135 }; | |
136 | |
137 TEST_F(DeviceSettingsProviderTest, InitializationTest) { | |
138 // Verify that the policy blob has been correctly parsed and trusted. | |
139 ASSERT_TRUE(provider_->GetTrusted( | |
140 kStatsReportingPref, | |
141 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback, | |
142 pointer_factory_.GetWeakPtr()))); | |
143 // The trusted flag should be established already prior to calling GetTrusted. | |
144 message_loop_.RunAllPending(); | |
145 ASSERT_FALSE(trusted_called_); | |
146 const base::Value* value = provider_->Get(kStatsReportingPref); | |
147 ASSERT_TRUE(value); | |
148 bool bool_value; | |
149 ASSERT_TRUE(value->GetAsBoolean(&bool_value)); | |
150 ASSERT_FALSE(bool_value); | |
151 } | |
152 | |
153 TEST_F(DeviceSettingsProviderTest, InitializationTestUnowned) { | |
154 // No calls to the SignedSettingsHelper shoud occur in this case! | |
155 Mock::VerifyAndClear(&signed_settings_helper_); | |
156 | |
157 provider_->set_ownership_status(OwnershipService::OWNERSHIP_NONE); | |
158 provider_->Reload(); | |
159 // Verify that the cache policy blob is "trusted". | |
160 ASSERT_TRUE(provider_->GetTrusted( | |
161 kReleaseChannel, | |
162 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback, | |
163 pointer_factory_.GetWeakPtr()))); | |
164 // The trusted flag should be established already prior to calling GetTrusted. | |
165 message_loop_.RunAllPending(); | |
166 ASSERT_FALSE(trusted_called_); | |
167 const base::Value* value = provider_->Get(kReleaseChannel); | |
168 ASSERT_TRUE(value); | |
169 std::string string_value; | |
170 ASSERT_TRUE(value->GetAsString(&string_value)); | |
171 ASSERT_TRUE(string_value.empty()); | |
172 | |
173 // Sets should succeed though and be readable from the cache. | |
174 base::StringValue new_value("stable-channel"); | |
175 provider_->Set(kReleaseChannel, new_value); | |
176 // Do one more reload here to make sure we don't flip randomly between stores. | |
177 provider_->Reload(); | |
178 // Verify the change has not been apply. | |
179 const base::Value* saved_value = provider_->Get(kReleaseChannel); | |
180 ASSERT_TRUE(saved_value); | |
181 ASSERT_TRUE(saved_value->GetAsString(&string_value)); | |
182 ASSERT_EQ("stable-channel", string_value); | |
183 } | |
184 | |
185 TEST_F(DeviceSettingsProviderTest, SetPrefFailed) { | |
186 // If we are not the owner no sets should work. | |
187 EXPECT_CALL(*mock_user_manager_, IsCurrentUserOwner()) | |
188 .WillOnce(Return(false)); | |
189 base::FundamentalValue value(true); | |
190 provider_->Set(kStatsReportingPref, value); | |
191 // Verify the change has not been apply. | |
192 const base::Value* saved_value = provider_->Get(kStatsReportingPref); | |
193 ASSERT_TRUE(saved_value); | |
194 bool bool_value; | |
195 ASSERT_TRUE(saved_value->GetAsBoolean(&bool_value)); | |
196 ASSERT_FALSE(bool_value); | |
197 } | |
198 | |
199 TEST_F(DeviceSettingsProviderTest, SetPrefSucceed) { | |
200 base::FundamentalValue value(true); | |
201 provider_->Set(kStatsReportingPref, value); | |
202 // Verify the change has not been apply. | |
203 const base::Value* saved_value = provider_->Get(kStatsReportingPref); | |
204 ASSERT_TRUE(saved_value); | |
205 bool bool_value; | |
206 ASSERT_TRUE(saved_value->GetAsBoolean(&bool_value)); | |
207 ASSERT_TRUE(bool_value); | |
208 } | |
209 | |
210 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalFailed) { | |
211 // No calls to the SignedSettingsHelper shoud occur in this case! | |
Chris Masone
2012/03/13 16:45:53
This comment copy-pasted?
Something more like "Cl
pastarmovj
2012/03/22 11:48:01
This file is not part of this CL anymore. It was s
| |
212 Mock::VerifyAndClear(&signed_settings_helper_); | |
213 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)) | |
214 .Times(AnyNumber()) | |
215 .WillRepeatedly( | |
216 MockSignedSettingsHelperRetrievePolicy( | |
217 SignedSettings::BAD_SIGNATURE, | |
218 policy_blob_)); | |
219 provider_->Reload(); | |
220 // Verify that the cache policy blob is "trusted". | |
221 ASSERT_FALSE(provider_->GetTrusted( | |
222 kReleaseChannel, | |
223 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback, | |
224 pointer_factory_.GetWeakPtr()))); | |
225 // The trusted flag should be established already prior to calling GetTrusted. | |
226 message_loop_.RunAllPending(); | |
227 ASSERT_FALSE(trusted_called_); | |
228 } | |
229 | |
230 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalFailed2) { | |
231 // No calls to the SignedSettingsHelper shoud occur in this case! | |
Chris Masone
2012/03/13 16:45:53
Same comment as above
| |
232 Mock::VerifyAndClear(&signed_settings_helper_); | |
233 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)) | |
234 .Times(AnyNumber()) | |
235 .WillRepeatedly( | |
236 MockSignedSettingsHelperRetrievePolicy( | |
237 SignedSettings::OPERATION_FAILED, | |
238 policy_blob_)); | |
239 provider_->Reload(); | |
240 // Verify that the cache policy blob is "trusted". | |
241 ASSERT_FALSE(provider_->GetTrusted( | |
242 kReleaseChannel, | |
243 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback, | |
244 pointer_factory_.GetWeakPtr()))); | |
245 // The trusted flag should be established already prior to calling GetTrusted. | |
246 message_loop_.RunAllPending(); | |
247 ASSERT_FALSE(trusted_called_); | |
248 } | |
249 | |
250 } // namespace chromeos | |
OLD | NEW |