Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1112)

Side by Side Diff: chrome/browser/chromeos/settings/device_settings_provider_unittest.cc

Issue 10832035: Switch from SignedSettings to DeviceSettingsService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/settings/device_settings_provider.h" 5 #include "chrome/browser/chromeos/settings/device_settings_provider.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h"
10 #include "base/message_loop.h" 11 #include "base/message_loop.h"
11 #include "base/values.h" 12 #include "base/values.h"
12 #include "chrome/browser/chromeos/cros/cros_library.h" 13 #include "chrome/browser/chromeos/cros/cros_library.h"
13 #include "chrome/browser/chromeos/login/mock_user_manager.h"
14 #include "chrome/browser/chromeos/settings/cros_settings_names.h" 14 #include "chrome/browser/chromeos/settings/cros_settings_names.h"
15 #include "chrome/browser/chromeos/settings/mock_signed_settings_helper.h" 15 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
16 #include "chrome/browser/chromeos/settings/ownership_service.h" 16 #include "chrome/browser/chromeos/settings/mock_owner_key_util.h"
17 #include "chrome/browser/policy/policy_builder.h"
17 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" 18 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h"
18 #include "chrome/browser/policy/proto/device_management_backend.pb.h" 19 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
19 #include "chrome/test/base/testing_browser_process.h" 20 #include "chrome/test/base/testing_browser_process.h"
20 #include "chrome/test/base/testing_pref_service.h" 21 #include "chrome/test/base/testing_pref_service.h"
21 #include "content/public/test/test_browser_thread.h" 22 #include "content/public/test/test_browser_thread.h"
22 #include "testing/gmock/include/gmock/gmock.h" 23 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
24 25
25 namespace em = enterprise_management; 26 namespace em = enterprise_management;
27
26 namespace chromeos { 28 namespace chromeos {
27 29
28 using ::testing::AnyNumber; 30 using ::testing::AnyNumber;
29 using ::testing::Mock; 31 using ::testing::Mock;
30 using ::testing::Return;
31 using ::testing::SaveArg;
32 using ::testing::_; 32 using ::testing::_;
33 33
34 class DeviceSettingsProviderTest: public testing::Test { 34 class DeviceSettingsProviderTest: public testing::Test {
35 public: 35 public:
36 MOCK_METHOD1(SettingChanged, void(const std::string&)); 36 MOCK_METHOD1(SettingChanged, void(const std::string&));
37 MOCK_METHOD0(GetTrustedCallback, void(void)); 37 MOCK_METHOD0(GetTrustedCallback, void(void));
38 38
39 protected: 39 protected:
40 DeviceSettingsProviderTest() 40 DeviceSettingsProviderTest()
41 : message_loop_(MessageLoop::TYPE_UI), 41 : message_loop_(MessageLoop::TYPE_UI),
42 ui_thread_(content::BrowserThread::UI, &message_loop_), 42 ui_thread_(content::BrowserThread::UI, &message_loop_),
43 file_thread_(content::BrowserThread::FILE, &message_loop_), 43 file_thread_(content::BrowserThread::FILE, &message_loop_),
44 local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)) { 44 local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)),
45 } 45 owner_key_util_(new MockOwnerKeyUtil()) {}
46
47 virtual ~DeviceSettingsProviderTest() {
48 }
49 46
50 virtual void SetUp() OVERRIDE { 47 virtual void SetUp() OVERRIDE {
51 PrepareEmptyPolicy(); 48 policy_.payload().mutable_metrics_enabled()->set_metrics_enabled(false);
49 policy_.Build();
52 50
53 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)) 51 device_settings_test_helper_.set_policy_blob(policy_.GetBlob());
54 .WillRepeatedly(
55 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS,
56 policy_blob_));
57 EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_,_))
58 .WillRepeatedly(DoAll(
59 SaveArg<0>(&policy_blob_),
60 MockSignedSettingsHelperStorePolicy(SignedSettings::SUCCESS)));
61 52
62 EXPECT_CALL(*mock_user_manager_.user_manager(), IsCurrentUserOwner()) 53 device_settings_service_.Initialize(&device_settings_test_helper_,
63 .WillRepeatedly(Return(true)); 54 owner_key_util_);
64 55
65 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber()); 56 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
66 provider_.reset( 57 provider_.reset(
67 new DeviceSettingsProvider( 58 new DeviceSettingsProvider(
68 base::Bind(&DeviceSettingsProviderTest::SettingChanged, 59 base::Bind(&DeviceSettingsProviderTest::SettingChanged,
69 base::Unretained(this)), 60 base::Unretained(this)),
70 &signed_settings_helper_)); 61 &device_settings_service_));
71 provider_->set_ownership_status(OwnershipService::OWNERSHIP_TAKEN);
72
73 // To prevent flooding the logs.
74 provider_->set_retries_left(1);
75 provider_->Reload();
76 Mock::VerifyAndClearExpectations(this); 62 Mock::VerifyAndClearExpectations(this);
77 } 63 }
78 64
79 void PrepareEmptyPolicy() { 65 virtual void TearDown() OVERRIDE {
80 em::PolicyData policy; 66 device_settings_service_.Shutdown();
81 em::ChromeDeviceSettingsProto pol;
82 // Set metrics to disabled to prevent us from running into code that is not
83 // mocked.
84 pol.mutable_metrics_enabled()->set_metrics_enabled(false);
85 policy.set_policy_type(chromeos::kDevicePolicyType);
86 policy.set_username("me@owner");
87 policy.set_policy_value(pol.SerializeAsString());
88 // Wipe the signed settings store.
89 policy_blob_.set_policy_data(policy.SerializeAsString());
90 policy_blob_.set_policy_data_signature("false");
91 } 67 }
92 68
93 em::PolicyFetchResponse policy_blob_;
94
95 scoped_ptr<DeviceSettingsProvider> provider_;
96
97 MessageLoop message_loop_; 69 MessageLoop message_loop_;
98 content::TestBrowserThread ui_thread_; 70 content::TestBrowserThread ui_thread_;
99 content::TestBrowserThread file_thread_; 71 content::TestBrowserThread file_thread_;
100 72
73 ScopedStubCrosEnabler stub_cros_enabler_;
74
101 ScopedTestingLocalState local_state_; 75 ScopedTestingLocalState local_state_;
102 76
103 MockSignedSettingsHelper signed_settings_helper_; 77 DeviceSettingsTestHelper device_settings_test_helper_;
78 scoped_refptr<MockOwnerKeyUtil> owner_key_util_;
104 79
105 ScopedStubCrosEnabler stub_cros_enabler_; 80 DeviceSettingsService device_settings_service_;
106 ScopedMockUserManagerEnabler mock_user_manager_; 81
82 policy::DevicePolicyBuilder policy_;
83
84 scoped_ptr<DeviceSettingsProvider> provider_;
85
86 private:
87 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsProviderTest);
107 }; 88 };
108 89
109 TEST_F(DeviceSettingsProviderTest, InitializationTest) { 90 TEST_F(DeviceSettingsProviderTest, InitializationTest) {
91 owner_key_util_->SetPublicKeyFromPrivateKey(policy_.signing_key());
92
93 // Have the service load a settings blob.
94 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
95 device_settings_service_.Load();
96 device_settings_test_helper_.Flush();
97 Mock::VerifyAndClearExpectations(this);
98
110 // Verify that the policy blob has been correctly parsed and trusted. 99 // Verify that the policy blob has been correctly parsed and trusted.
111 // The trusted flag should be set before the call to PrepareTrustedValues. 100 // The trusted flag should be set before the call to PrepareTrustedValues.
112 EXPECT_EQ(CrosSettingsProvider::TRUSTED, 101 EXPECT_EQ(CrosSettingsProvider::TRUSTED,
113 provider_->PrepareTrustedValues( 102 provider_->PrepareTrustedValues(base::Closure()));
114 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
115 base::Unretained(this))));
116 const base::Value* value = provider_->Get(kStatsReportingPref); 103 const base::Value* value = provider_->Get(kStatsReportingPref);
117 ASSERT_TRUE(value); 104 ASSERT_TRUE(value);
118 bool bool_value; 105 bool bool_value;
119 EXPECT_TRUE(value->GetAsBoolean(&bool_value)); 106 EXPECT_TRUE(value->GetAsBoolean(&bool_value));
120 EXPECT_FALSE(bool_value); 107 EXPECT_FALSE(bool_value);
121 } 108 }
122 109
123 TEST_F(DeviceSettingsProviderTest, InitializationTestUnowned) { 110 TEST_F(DeviceSettingsProviderTest, InitializationTestUnowned) {
124 // No calls to the SignedSettingsHelper should occur in this case! 111 // Have the service check the key.
125 Mock::VerifyAndClear(&signed_settings_helper_); 112 device_settings_service_.Load();
113 device_settings_test_helper_.Flush();
126 114
127 provider_->set_ownership_status(OwnershipService::OWNERSHIP_NONE);
128 provider_->Reload();
129 // The trusted flag should be set before the call to PrepareTrustedValues. 115 // The trusted flag should be set before the call to PrepareTrustedValues.
130 EXPECT_EQ(CrosSettingsProvider::TRUSTED, 116 EXPECT_EQ(CrosSettingsProvider::TRUSTED,
131 provider_->PrepareTrustedValues( 117 provider_->PrepareTrustedValues(base::Closure()));
132 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
133 base::Unretained(this))));
134 const base::Value* value = provider_->Get(kReleaseChannel); 118 const base::Value* value = provider_->Get(kReleaseChannel);
135 ASSERT_TRUE(value); 119 ASSERT_TRUE(value);
136 std::string string_value; 120 std::string string_value;
137 EXPECT_TRUE(value->GetAsString(&string_value)); 121 EXPECT_TRUE(value->GetAsString(&string_value));
138 EXPECT_TRUE(string_value.empty()); 122 EXPECT_TRUE(string_value.empty());
139 123
140 // Sets should succeed though and be readable from the cache. 124 // Sets should succeed though and be readable from the cache.
125 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
141 EXPECT_CALL(*this, SettingChanged(kReleaseChannel)).Times(1); 126 EXPECT_CALL(*this, SettingChanged(kReleaseChannel)).Times(1);
142 base::StringValue new_value("stable-channel"); 127 base::StringValue new_value("stable-channel");
143 provider_->Set(kReleaseChannel, new_value); 128 provider_->Set(kReleaseChannel, new_value);
144 // Do one more reload here to make sure we don't flip randomly between stores. 129 Mock::VerifyAndClearExpectations(this);
145 provider_->Reload(); 130
146 // Verify the change has not been applied. 131 // This shouldn't trigger a write.
132 device_settings_test_helper_.set_policy_blob(std::string());
133 device_settings_test_helper_.Flush();
134 EXPECT_EQ(std::string(), device_settings_test_helper_.policy_blob());
135
136 // Verify the change has been applied.
147 const base::Value* saved_value = provider_->Get(kReleaseChannel); 137 const base::Value* saved_value = provider_->Get(kReleaseChannel);
148 ASSERT_TRUE(saved_value); 138 ASSERT_TRUE(saved_value);
149 EXPECT_TRUE(saved_value->GetAsString(&string_value)); 139 EXPECT_TRUE(saved_value->GetAsString(&string_value));
150 ASSERT_EQ("stable-channel", string_value); 140 ASSERT_EQ("stable-channel", string_value);
151 Mock::VerifyAndClearExpectations(this);
152 } 141 }
153 142
154 TEST_F(DeviceSettingsProviderTest, SetPrefFailed) { 143 TEST_F(DeviceSettingsProviderTest, SetPrefFailed) {
144 // If we are not the owner no sets should work.
145 owner_key_util_->SetPublicKeyFromPrivateKey(policy_.signing_key());
146
147 base::FundamentalValue value(true);
155 EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1); 148 EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
156 // If we are not the owner no sets should work.
157 EXPECT_CALL(*mock_user_manager_.user_manager(), IsCurrentUserOwner())
158 .WillOnce(Return(false));
159 base::FundamentalValue value(true);
160 provider_->Set(kStatsReportingPref, value); 149 provider_->Set(kStatsReportingPref, value);
150 Mock::VerifyAndClearExpectations(this);
151
152 // This shouldn't trigger a write.
153 device_settings_test_helper_.set_policy_blob(std::string());
154 device_settings_test_helper_.Flush();
155 EXPECT_EQ(std::string(), device_settings_test_helper_.policy_blob());
156
161 // Verify the change has not been applied. 157 // Verify the change has not been applied.
162 const base::Value* saved_value = provider_->Get(kStatsReportingPref); 158 const base::Value* saved_value = provider_->Get(kStatsReportingPref);
163 ASSERT_TRUE(saved_value); 159 ASSERT_TRUE(saved_value);
164 bool bool_value; 160 bool bool_value;
165 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value)); 161 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value));
166 EXPECT_FALSE(bool_value); 162 EXPECT_FALSE(bool_value);
167 } 163 }
168 164
169 TEST_F(DeviceSettingsProviderTest, SetPrefSucceed) { 165 TEST_F(DeviceSettingsProviderTest, SetPrefSucceed) {
166 owner_key_util_->SetPrivateKey(policy_.signing_key());
167 device_settings_service_.SetUsername(policy_.policy_data().username());
168 device_settings_test_helper_.Flush();
169
170 base::FundamentalValue value(true);
171 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
170 EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1); 172 EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
171 base::FundamentalValue value(true);
172 provider_->Set(kStatsReportingPref, value); 173 provider_->Set(kStatsReportingPref, value);
173 // Verify the change has not been applied. 174 Mock::VerifyAndClearExpectations(this);
175
176 // Process the store.
177 device_settings_test_helper_.set_policy_blob(std::string());
178 device_settings_test_helper_.Flush();
179
180 // Verify that the device policy has been adjusted.
181 ASSERT_TRUE(device_settings_service_.device_settings());
182 EXPECT_TRUE(device_settings_service_.device_settings()->
183 metrics_enabled().metrics_enabled());
184
185 // Verify the change has been applied.
174 const base::Value* saved_value = provider_->Get(kStatsReportingPref); 186 const base::Value* saved_value = provider_->Get(kStatsReportingPref);
175 ASSERT_TRUE(saved_value); 187 ASSERT_TRUE(saved_value);
176 bool bool_value; 188 bool bool_value;
177 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value)); 189 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value));
178 EXPECT_TRUE(bool_value); 190 EXPECT_TRUE(bool_value);
179 } 191 }
180 192
181 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalFailedBadSingature) { 193 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalFailedBadSignature) {
182 // No calls to the SignedSettingsHelper should occur in this case! 194 owner_key_util_->SetPublicKeyFromPrivateKey(policy_.signing_key());
183 Mock::VerifyAndClear(&signed_settings_helper_); 195 policy_.policy().set_policy_data_signature("bad signature");
184 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)) 196 device_settings_test_helper_.set_policy_blob(policy_.GetBlob());
185 .WillRepeatedly( 197
186 MockSignedSettingsHelperRetrievePolicy( 198 device_settings_service_.Load();
187 SignedSettings::BAD_SIGNATURE, 199 device_settings_test_helper_.Flush();
188 policy_blob_)); 200
189 provider_->Reload(); 201 // Verify that the cached settings blob is not "trusted".
190 // Verify that the cache policy blob is not "trusted". 202 EXPECT_EQ(DeviceSettingsService::STORE_VALIDATION_ERROR,
203 device_settings_service_.status());
191 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED, 204 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
192 provider_->PrepareTrustedValues( 205 provider_->PrepareTrustedValues(base::Closure()));
193 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
194 base::Unretained(this))));
195 } 206 }
196 207
197 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalOperationFailedPermanently) { 208 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalNoPolicy) {
198 // No calls to the SignedSettingsHelper should occur in this case! 209 owner_key_util_->SetPublicKeyFromPrivateKey(policy_.signing_key());
199 Mock::VerifyAndClear(&signed_settings_helper_); 210 device_settings_test_helper_.set_policy_blob(std::string());
200 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)) 211
201 .WillRepeatedly( 212 device_settings_service_.Load();
202 MockSignedSettingsHelperRetrievePolicy( 213 device_settings_test_helper_.Flush();
203 SignedSettings::OPERATION_FAILED, 214
204 policy_blob_)); 215 // Verify that the cached settings blob is not "trusted".
205 provider_->Reload(); 216 EXPECT_EQ(DeviceSettingsService::STORE_NO_POLICY,
206 // Verify that the cache policy blob is not "trusted". 217 device_settings_service_.status());
207 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED, 218 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
208 provider_->PrepareTrustedValues( 219 provider_->PrepareTrustedValues(base::Closure()));
209 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
210 base::Unretained(this))));
211 }
212
213 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalOperationFailedOnce) {
214 // No calls to the SignedSettingsHelper should occur in this case!
215 Mock::VerifyAndClear(&signed_settings_helper_);
216 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_))
217 .WillOnce(
218 MockSignedSettingsHelperRetrievePolicy(
219 SignedSettings::OPERATION_FAILED,
220 policy_blob_))
221 .WillRepeatedly(
222 MockSignedSettingsHelperRetrievePolicy(
223 SignedSettings::SUCCESS,
224 policy_blob_));
225 // Should be trusted after an automatic reload.
226 provider_->Reload();
227 // Verify that the cache policy blob is not "trusted".
228 EXPECT_EQ(CrosSettingsProvider::TRUSTED,
229 provider_->PrepareTrustedValues(
230 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
231 base::Unretained(this))));
232 } 220 }
233 221
234 TEST_F(DeviceSettingsProviderTest, PolicyFailedPermanentlyNotification) { 222 TEST_F(DeviceSettingsProviderTest, PolicyFailedPermanentlyNotification) {
235 Mock::VerifyAndClear(&signed_settings_helper_); 223 owner_key_util_->SetPublicKeyFromPrivateKey(policy_.signing_key());
236 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_)) 224 device_settings_test_helper_.set_policy_blob(std::string());
237 .WillRepeatedly(
238 MockSignedSettingsHelperRetrievePolicy(
239 SignedSettings::OPERATION_FAILED,
240 policy_blob_));
241 225
242 provider_->set_trusted_status(CrosSettingsProvider::TEMPORARILY_UNTRUSTED);
243 EXPECT_CALL(*this, GetTrustedCallback()); 226 EXPECT_CALL(*this, GetTrustedCallback());
244 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED, 227 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED,
245 provider_->PrepareTrustedValues( 228 provider_->PrepareTrustedValues(
246 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback, 229 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
247 base::Unretained(this)))); 230 base::Unretained(this))));
248 provider_->Reload(); 231
232 device_settings_service_.Load();
233 device_settings_test_helper_.Flush();
234 Mock::VerifyAndClearExpectations(this);
235
236 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
237 provider_->PrepareTrustedValues(base::Closure()));
249 } 238 }
250 239
251 TEST_F(DeviceSettingsProviderTest, PolicyLoadNotification) { 240 TEST_F(DeviceSettingsProviderTest, PolicyLoadNotification) {
252 provider_->set_trusted_status(CrosSettingsProvider::TEMPORARILY_UNTRUSTED);
253 EXPECT_CALL(*this, GetTrustedCallback()); 241 EXPECT_CALL(*this, GetTrustedCallback());
242
254 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED, 243 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED,
255 provider_->PrepareTrustedValues( 244 provider_->PrepareTrustedValues(
256 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback, 245 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
257 base::Unretained(this)))); 246 base::Unretained(this))));
258 provider_->Reload(); 247
248 device_settings_service_.Load();
249 device_settings_test_helper_.Flush();
250 Mock::VerifyAndClearExpectations(this);
259 } 251 }
260 252
261 } // namespace chromeos 253 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698