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

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

Issue 10918027: Revert 154457 - Switch from SignedSettings to DeviceSettingsService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: 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"
11 #include "base/message_loop.h" 10 #include "base/message_loop.h"
12 #include "base/values.h" 11 #include "base/values.h"
13 #include "chrome/browser/chromeos/cros/cros_library.h" 12 #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/device_settings_test_helper.h" 15 #include "chrome/browser/chromeos/settings/mock_signed_settings_helper.h"
16 #include "chrome/browser/chromeos/settings/mock_owner_key_util.h" 16 #include "chrome/browser/chromeos/settings/ownership_service.h"
17 #include "chrome/browser/policy/policy_builder.h"
18 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" 17 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h"
19 #include "chrome/browser/policy/proto/device_management_backend.pb.h" 18 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
20 #include "chrome/test/base/testing_browser_process.h" 19 #include "chrome/test/base/testing_browser_process.h"
21 #include "chrome/test/base/testing_pref_service.h" 20 #include "chrome/test/base/testing_pref_service.h"
22 #include "content/public/test/test_browser_thread.h" 21 #include "content/public/test/test_browser_thread.h"
23 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
25 24
26 namespace em = enterprise_management; 25 namespace em = enterprise_management;
27
28 namespace chromeos { 26 namespace chromeos {
29 27
30 using ::testing::AnyNumber; 28 using ::testing::AnyNumber;
31 using ::testing::Mock; 29 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 owner_key_util_(new MockOwnerKeyUtil()) {} 45 }
46
47 virtual ~DeviceSettingsProviderTest() {
48 }
46 49
47 virtual void SetUp() OVERRIDE { 50 virtual void SetUp() OVERRIDE {
48 policy_.payload().mutable_metrics_enabled()->set_metrics_enabled(false); 51 PrepareEmptyPolicy();
49 policy_.Build();
50 52
51 device_settings_test_helper_.set_policy_blob(policy_.GetBlob()); 53 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_))
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)));
52 61
53 device_settings_service_.Initialize(&device_settings_test_helper_, 62 EXPECT_CALL(*mock_user_manager_.user_manager(), IsCurrentUserOwner())
54 owner_key_util_); 63 .WillRepeatedly(Return(true));
55 64
56 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber()); 65 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
57 provider_.reset( 66 provider_.reset(
58 new DeviceSettingsProvider( 67 new DeviceSettingsProvider(
59 base::Bind(&DeviceSettingsProviderTest::SettingChanged, 68 base::Bind(&DeviceSettingsProviderTest::SettingChanged,
60 base::Unretained(this)), 69 base::Unretained(this)),
61 &device_settings_service_)); 70 &signed_settings_helper_));
71 provider_->set_ownership_status(OwnershipService::OWNERSHIP_TAKEN);
72
73 // To prevent flooding the logs.
74 provider_->set_retries_left(1);
75 provider_->Reload();
62 Mock::VerifyAndClearExpectations(this); 76 Mock::VerifyAndClearExpectations(this);
63 } 77 }
64 78
65 virtual void TearDown() OVERRIDE { 79 void PrepareEmptyPolicy() {
66 device_settings_service_.Shutdown(); 80 em::PolicyData policy;
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");
67 } 91 }
68 92
93 em::PolicyFetchResponse policy_blob_;
94
95 scoped_ptr<DeviceSettingsProvider> provider_;
96
69 MessageLoop message_loop_; 97 MessageLoop message_loop_;
70 content::TestBrowserThread ui_thread_; 98 content::TestBrowserThread ui_thread_;
71 content::TestBrowserThread file_thread_; 99 content::TestBrowserThread file_thread_;
72 100
73 ScopedStubCrosEnabler stub_cros_enabler_;
74
75 ScopedTestingLocalState local_state_; 101 ScopedTestingLocalState local_state_;
76 102
77 DeviceSettingsTestHelper device_settings_test_helper_; 103 MockSignedSettingsHelper signed_settings_helper_;
78 scoped_refptr<MockOwnerKeyUtil> owner_key_util_;
79 104
80 DeviceSettingsService device_settings_service_; 105 ScopedStubCrosEnabler stub_cros_enabler_;
81 106 ScopedMockUserManagerEnabler mock_user_manager_;
82 policy::DevicePolicyBuilder policy_;
83
84 scoped_ptr<DeviceSettingsProvider> provider_;
85
86 private:
87 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsProviderTest);
88 }; 107 };
89 108
90 TEST_F(DeviceSettingsProviderTest, InitializationTest) { 109 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
99 // Verify that the policy blob has been correctly parsed and trusted. 110 // Verify that the policy blob has been correctly parsed and trusted.
100 // The trusted flag should be set before the call to PrepareTrustedValues. 111 // The trusted flag should be set before the call to PrepareTrustedValues.
101 EXPECT_EQ(CrosSettingsProvider::TRUSTED, 112 EXPECT_EQ(CrosSettingsProvider::TRUSTED,
102 provider_->PrepareTrustedValues(base::Closure())); 113 provider_->PrepareTrustedValues(
114 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
115 base::Unretained(this))));
103 const base::Value* value = provider_->Get(kStatsReportingPref); 116 const base::Value* value = provider_->Get(kStatsReportingPref);
104 ASSERT_TRUE(value); 117 ASSERT_TRUE(value);
105 bool bool_value; 118 bool bool_value;
106 EXPECT_TRUE(value->GetAsBoolean(&bool_value)); 119 EXPECT_TRUE(value->GetAsBoolean(&bool_value));
107 EXPECT_FALSE(bool_value); 120 EXPECT_FALSE(bool_value);
108 } 121 }
109 122
110 TEST_F(DeviceSettingsProviderTest, InitializationTestUnowned) { 123 TEST_F(DeviceSettingsProviderTest, InitializationTestUnowned) {
111 // Have the service check the key. 124 // No calls to the SignedSettingsHelper should occur in this case!
112 device_settings_service_.Load(); 125 Mock::VerifyAndClear(&signed_settings_helper_);
113 device_settings_test_helper_.Flush();
114 126
127 provider_->set_ownership_status(OwnershipService::OWNERSHIP_NONE);
128 provider_->Reload();
115 // The trusted flag should be set before the call to PrepareTrustedValues. 129 // The trusted flag should be set before the call to PrepareTrustedValues.
116 EXPECT_EQ(CrosSettingsProvider::TRUSTED, 130 EXPECT_EQ(CrosSettingsProvider::TRUSTED,
117 provider_->PrepareTrustedValues(base::Closure())); 131 provider_->PrepareTrustedValues(
132 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
133 base::Unretained(this))));
118 const base::Value* value = provider_->Get(kReleaseChannel); 134 const base::Value* value = provider_->Get(kReleaseChannel);
119 ASSERT_TRUE(value); 135 ASSERT_TRUE(value);
120 std::string string_value; 136 std::string string_value;
121 EXPECT_TRUE(value->GetAsString(&string_value)); 137 EXPECT_TRUE(value->GetAsString(&string_value));
122 EXPECT_TRUE(string_value.empty()); 138 EXPECT_TRUE(string_value.empty());
123 139
124 // Sets should succeed though and be readable from the cache. 140 // Sets should succeed though and be readable from the cache.
125 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
126 EXPECT_CALL(*this, SettingChanged(kReleaseChannel)).Times(1); 141 EXPECT_CALL(*this, SettingChanged(kReleaseChannel)).Times(1);
127 base::StringValue new_value("stable-channel"); 142 base::StringValue new_value("stable-channel");
128 provider_->Set(kReleaseChannel, new_value); 143 provider_->Set(kReleaseChannel, new_value);
129 Mock::VerifyAndClearExpectations(this); 144 // Do one more reload here to make sure we don't flip randomly between stores.
130 145 provider_->Reload();
131 // This shouldn't trigger a write. 146 // Verify the change has not been applied.
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.
137 const base::Value* saved_value = provider_->Get(kReleaseChannel); 147 const base::Value* saved_value = provider_->Get(kReleaseChannel);
138 ASSERT_TRUE(saved_value); 148 ASSERT_TRUE(saved_value);
139 EXPECT_TRUE(saved_value->GetAsString(&string_value)); 149 EXPECT_TRUE(saved_value->GetAsString(&string_value));
140 ASSERT_EQ("stable-channel", string_value); 150 ASSERT_EQ("stable-channel", string_value);
151 Mock::VerifyAndClearExpectations(this);
141 } 152 }
142 153
143 TEST_F(DeviceSettingsProviderTest, SetPrefFailed) { 154 TEST_F(DeviceSettingsProviderTest, SetPrefFailed) {
155 EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
144 // If we are not the owner no sets should work. 156 // If we are not the owner no sets should work.
145 owner_key_util_->SetPublicKeyFromPrivateKey(policy_.signing_key()); 157 EXPECT_CALL(*mock_user_manager_.user_manager(), IsCurrentUserOwner())
146 158 .WillOnce(Return(false));
147 base::FundamentalValue value(true); 159 base::FundamentalValue value(true);
148 EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
149 provider_->Set(kStatsReportingPref, value); 160 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
157 // Verify the change has not been applied. 161 // Verify the change has not been applied.
158 const base::Value* saved_value = provider_->Get(kStatsReportingPref); 162 const base::Value* saved_value = provider_->Get(kStatsReportingPref);
159 ASSERT_TRUE(saved_value); 163 ASSERT_TRUE(saved_value);
160 bool bool_value; 164 bool bool_value;
161 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value)); 165 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value));
162 EXPECT_FALSE(bool_value); 166 EXPECT_FALSE(bool_value);
163 } 167 }
164 168
165 TEST_F(DeviceSettingsProviderTest, SetPrefSucceed) { 169 TEST_F(DeviceSettingsProviderTest, SetPrefSucceed) {
166 owner_key_util_->SetPrivateKey(policy_.signing_key()); 170 EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
167 device_settings_service_.SetUsername(policy_.policy_data().username());
168 device_settings_test_helper_.Flush();
169
170 base::FundamentalValue value(true); 171 base::FundamentalValue value(true);
171 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
172 EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
173 provider_->Set(kStatsReportingPref, value); 172 provider_->Set(kStatsReportingPref, value);
174 Mock::VerifyAndClearExpectations(this); 173 // Verify the change has not been applied.
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.
186 const base::Value* saved_value = provider_->Get(kStatsReportingPref); 174 const base::Value* saved_value = provider_->Get(kStatsReportingPref);
187 ASSERT_TRUE(saved_value); 175 ASSERT_TRUE(saved_value);
188 bool bool_value; 176 bool bool_value;
189 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value)); 177 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value));
190 EXPECT_TRUE(bool_value); 178 EXPECT_TRUE(bool_value);
191 } 179 }
192 180
193 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalFailedBadSignature) { 181 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalFailedBadSingature) {
194 owner_key_util_->SetPublicKeyFromPrivateKey(policy_.signing_key()); 182 // No calls to the SignedSettingsHelper should occur in this case!
195 policy_.policy().set_policy_data_signature("bad signature"); 183 Mock::VerifyAndClear(&signed_settings_helper_);
196 device_settings_test_helper_.set_policy_blob(policy_.GetBlob()); 184 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_))
197 185 .WillRepeatedly(
198 device_settings_service_.Load(); 186 MockSignedSettingsHelperRetrievePolicy(
199 device_settings_test_helper_.Flush(); 187 SignedSettings::BAD_SIGNATURE,
200 188 policy_blob_));
201 // Verify that the cached settings blob is not "trusted". 189 provider_->Reload();
202 EXPECT_EQ(DeviceSettingsService::STORE_VALIDATION_ERROR, 190 // Verify that the cache policy blob is not "trusted".
203 device_settings_service_.status());
204 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED, 191 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
205 provider_->PrepareTrustedValues(base::Closure())); 192 provider_->PrepareTrustedValues(
193 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
194 base::Unretained(this))));
206 } 195 }
207 196
208 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalNoPolicy) { 197 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalOperationFailedPermanently) {
209 owner_key_util_->SetPublicKeyFromPrivateKey(policy_.signing_key()); 198 // No calls to the SignedSettingsHelper should occur in this case!
210 device_settings_test_helper_.set_policy_blob(std::string()); 199 Mock::VerifyAndClear(&signed_settings_helper_);
200 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_))
201 .WillRepeatedly(
202 MockSignedSettingsHelperRetrievePolicy(
203 SignedSettings::OPERATION_FAILED,
204 policy_blob_));
205 provider_->Reload();
206 // Verify that the cache policy blob is not "trusted".
207 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
208 provider_->PrepareTrustedValues(
209 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
210 base::Unretained(this))));
211 }
211 212
212 device_settings_service_.Load(); 213 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalOperationFailedOnce) {
213 device_settings_test_helper_.Flush(); 214 // No calls to the SignedSettingsHelper should occur in this case!
214 215 Mock::VerifyAndClear(&signed_settings_helper_);
215 // Verify that the cached settings blob is not "trusted". 216 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_))
216 EXPECT_EQ(DeviceSettingsService::STORE_NO_POLICY, 217 .WillOnce(
217 device_settings_service_.status()); 218 MockSignedSettingsHelperRetrievePolicy(
218 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED, 219 SignedSettings::OPERATION_FAILED,
219 provider_->PrepareTrustedValues(base::Closure())); 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))));
220 } 232 }
221 233
222 TEST_F(DeviceSettingsProviderTest, PolicyFailedPermanentlyNotification) { 234 TEST_F(DeviceSettingsProviderTest, PolicyFailedPermanentlyNotification) {
223 owner_key_util_->SetPublicKeyFromPrivateKey(policy_.signing_key()); 235 Mock::VerifyAndClear(&signed_settings_helper_);
224 device_settings_test_helper_.set_policy_blob(std::string()); 236 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_))
237 .WillRepeatedly(
238 MockSignedSettingsHelperRetrievePolicy(
239 SignedSettings::OPERATION_FAILED,
240 policy_blob_));
225 241
242 provider_->set_trusted_status(CrosSettingsProvider::TEMPORARILY_UNTRUSTED);
226 EXPECT_CALL(*this, GetTrustedCallback()); 243 EXPECT_CALL(*this, GetTrustedCallback());
227 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED, 244 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED,
228 provider_->PrepareTrustedValues( 245 provider_->PrepareTrustedValues(
229 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback, 246 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
230 base::Unretained(this)))); 247 base::Unretained(this))));
231 248 provider_->Reload();
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()));
238 } 249 }
239 250
240 TEST_F(DeviceSettingsProviderTest, PolicyLoadNotification) { 251 TEST_F(DeviceSettingsProviderTest, PolicyLoadNotification) {
252 provider_->set_trusted_status(CrosSettingsProvider::TEMPORARILY_UNTRUSTED);
241 EXPECT_CALL(*this, GetTrustedCallback()); 253 EXPECT_CALL(*this, GetTrustedCallback());
242
243 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED, 254 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED,
244 provider_->PrepareTrustedValues( 255 provider_->PrepareTrustedValues(
245 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback, 256 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
246 base::Unretained(this)))); 257 base::Unretained(this))));
247 258 provider_->Reload();
248 device_settings_service_.Load();
249 device_settings_test_helper_.Flush();
250 Mock::VerifyAndClearExpectations(this);
251 } 259 }
252 260
253 } // namespace chromeos 261 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/settings/device_settings_provider.cc ('k') | chrome/browser/chromeos/settings/owner_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698