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

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

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

Powered by Google App Engine
This is Rietveld 408576698