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

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

Issue 9703115: Add DeviceSettingProvider unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed documentation. Created 8 years, 9 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
(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"
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 alphabetize
pastarmovj 2012/03/21 15:23:27 Done.
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"
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 needed?
pastarmovj 2012/03/21 15:23:27 Done.
21 #include "chrome/browser/metrics/metrics_service.h"
22 #include "chrome/browser/policy/browser_policy_connector.h"
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 needed?
pastarmovj 2012/03/21 15:23:27 Done.
23 #include "chrome/browser/policy/cloud_policy_data_store.h"
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 needed?
pastarmovj 2012/03/21 15:23:27 Done.
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;
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 You're never reading |notifier_called_|...
pastarmovj 2012/03/21 15:23:27 Done.
45 }
46
47 void GetTrustedCallback() {
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 Both SettingChanged and GetTrustedCallback (which
pastarmovj 2012/03/21 15:23:27 Done.
48 trusted_called_ = true;
49 }
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 newline
pastarmovj 2012/03/21 15:23:27 Done.
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_.reset(new MockUserManager());
81 old_user_manager_ = UserManager::Set(mock_user_manager_.get());
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 UserManager::Set(old_user_manager_);
99 }
100
101 void PrepareEmptyPolicy() {
102 em::PolicyData policy;
103 em::ChromeDeviceSettingsProto pol;
104 // Set metrics to disabled to prevent us from running into code that is not
105 // mocked.
106 pol.mutable_metrics_enabled()->set_metrics_enabled(false);
107 policy.set_policy_type(chromeos::kDevicePolicyType);
108 policy.set_username("me@owner");
109 policy.set_policy_value(pol.SerializeAsString());
110 // Wipe the signed settings store.
111 policy_blob_.set_policy_data(policy.SerializeAsString());
112 policy_blob_.set_policy_data_signature("false");
113 }
114
115 em::PolicyFetchResponse policy_blob_;
116
117 scoped_ptr<DeviceSettingsProvider> provider_;
118
119 MessageLoop message_loop_;
120 content::TestBrowserThread ui_thread_;
121 content::TestBrowserThread file_thread_;
122
123 base::WeakPtrFactory<DeviceSettingsProviderTest> pointer_factory_;
124
125 ScopedTestingLocalState local_state_;
126
127 MockSignedSettingsHelper signed_settings_helper_;
128 scoped_ptr<MockUserManager> mock_user_manager_;
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 Does this need the scoped_ptr wrapping?
pastarmovj 2012/03/21 15:23:27 Yes after my UserManager CL that Nikita is reviewi
129 UserManager* old_user_manager_;
130
131 ScopedStubCrosEnabler stub_cros_enabler_;
132
133 MetricsService* metrics_service_;
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 Seems there is no reason to keep this pointer in a
pastarmovj 2012/03/21 15:23:27 True fixed.
134
135 bool trusted_called_;
136 bool notifier_called_;
137 };
138
139 TEST_F(DeviceSettingsProviderTest, InitializationTest) {
140 // Verify that the policy blob has been correctly parsed and trusted.
141 ASSERT_TRUE(provider_->GetTrusted(
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 EXPECT_TRUE seems to be good enough
pastarmovj 2012/03/21 15:23:27 Done.
142 kStatsReportingPref,
143 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
144 pointer_factory_.GetWeakPtr())));
145 // The trusted flag should be established already prior to calling GetTrusted.
146 message_loop_.RunAllPending();
147 ASSERT_FALSE(trusted_called_);
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 same here.
pastarmovj 2012/03/21 15:23:27 Line is gone.
148 const base::Value* value = provider_->Get(kStatsReportingPref);
149 ASSERT_TRUE(value);
150 bool bool_value;
151 ASSERT_TRUE(value->GetAsBoolean(&bool_value));
152 ASSERT_FALSE(bool_value);
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 and here and below
pastarmovj 2012/03/21 15:23:27 Done.
153 }
154
155 TEST_F(DeviceSettingsProviderTest, InitializationTestUnowned) {
156 // No calls to the SignedSettingsHelper shoud occur in this case!
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 s/shoud/should/ also below
pastarmovj 2012/03/21 15:23:27 Done.
157 Mock::VerifyAndClear(&signed_settings_helper_);
158
159 provider_->set_ownership_status(OwnershipService::OWNERSHIP_NONE);
160 provider_->Reload();
161 // Verify that the cache policy blob is "trusted".
162 ASSERT_TRUE(provider_->GetTrusted(
163 kReleaseChannel,
164 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
165 pointer_factory_.GetWeakPtr())));
166 // The trusted flag should be established already prior to calling GetTrusted.
167 message_loop_.RunAllPending();
168 ASSERT_FALSE(trusted_called_);
169 const base::Value* value = provider_->Get(kReleaseChannel);
170 ASSERT_TRUE(value);
171 std::string string_value;
172 ASSERT_TRUE(value->GetAsString(&string_value));
173 ASSERT_TRUE(string_value.empty());
174
175 // Sets should succeed though and be readable from the cache.
176 base::StringValue new_value("stable-channel");
177 provider_->Set(kReleaseChannel, new_value);
178 // Do one more reload here to make sure we don't flip randomly between stores.
179 provider_->Reload();
180 // Verify the change has not been apply.
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 s/apply/applied/
pastarmovj 2012/03/21 15:23:27 Done.
181 const base::Value* saved_value = provider_->Get(kReleaseChannel);
182 ASSERT_TRUE(saved_value);
183 ASSERT_TRUE(saved_value->GetAsString(&string_value));
184 ASSERT_EQ("stable-channel", string_value);
185 }
186
187 TEST_F(DeviceSettingsProviderTest, SetPrefFailed) {
188 // If we are not the owner no sets should work.
189 EXPECT_CALL(*mock_user_manager_, IsCurrentUserOwner())
190 .WillOnce(Return(false));
191 base::FundamentalValue value(true);
192 provider_->Set(kStatsReportingPref, value);
193 // Verify the change has not been apply.
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 s/apply/applied/ also below.
pastarmovj 2012/03/21 15:23:27 Done.
194 const base::Value* saved_value = provider_->Get(kStatsReportingPref);
195 ASSERT_TRUE(saved_value);
196 bool bool_value;
197 ASSERT_TRUE(saved_value->GetAsBoolean(&bool_value));
198 ASSERT_FALSE(bool_value);
199 }
200
201 TEST_F(DeviceSettingsProviderTest, SetPrefSucceed) {
202 base::FundamentalValue value(true);
203 provider_->Set(kStatsReportingPref, value);
204 // Verify the change has not been apply.
205 const base::Value* saved_value = provider_->Get(kStatsReportingPref);
206 ASSERT_TRUE(saved_value);
207 bool bool_value;
208 ASSERT_TRUE(saved_value->GetAsBoolean(&bool_value));
209 ASSERT_TRUE(bool_value);
210 }
211
212 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalFailed) {
213 // No calls to the SignedSettingsHelper shoud occur in this case!
214 Mock::VerifyAndClear(&signed_settings_helper_);
215 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_))
216 .Times(AnyNumber())
217 .WillRepeatedly(
218 MockSignedSettingsHelperRetrievePolicy(
219 SignedSettings::BAD_SIGNATURE,
220 policy_blob_));
221 provider_->Reload();
222 // Verify that the cache policy blob is "trusted".
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 Don't you initialize with OWNERSHIP_TAKEN? Shouldn
pastarmovj 2012/03/21 15:23:27 True and this is what the code does but the commen
223 ASSERT_FALSE(provider_->GetTrusted(
224 kReleaseChannel,
225 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
226 pointer_factory_.GetWeakPtr())));
227 // The trusted flag should be established already prior to calling GetTrusted.
228 message_loop_.RunAllPending();
229 ASSERT_FALSE(trusted_called_);
230 }
231
232 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalFailed2) {
Mattias Nissler (ping if slow) 2012/03/16 15:21:11 Can you make the naming of the test reflect the di
pastarmovj 2012/03/21 15:23:27 Done.
233 // No calls to the SignedSettingsHelper shoud occur in this case!
234 Mock::VerifyAndClear(&signed_settings_helper_);
235 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_))
236 .Times(AnyNumber())
237 .WillRepeatedly(
238 MockSignedSettingsHelperRetrievePolicy(
239 SignedSettings::OPERATION_FAILED,
240 policy_blob_));
241 provider_->Reload();
242 // Verify that the cache policy blob is "trusted".
243 ASSERT_FALSE(provider_->GetTrusted(
244 kReleaseChannel,
245 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
246 pointer_factory_.GetWeakPtr())));
247 // The trusted flag should be established already prior to calling GetTrusted.
248 message_loop_.RunAllPending();
249 ASSERT_FALSE(trusted_called_);
250 }
251
252 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698