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

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: Addressed comments and removed the MetricsService changes from this CL. 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/device_settings_provider.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/cros/cros_library.h"
15 #include "chrome/browser/chromeos/cros_settings_names.h"
16 #include "chrome/browser/chromeos/login/mock_signed_settings_helper.h"
17 #include "chrome/browser/chromeos/login/mock_user_manager.h"
18 #include "chrome/browser/chromeos/login/ownership_service.h"
19 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h"
20 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
21 #include "chrome/test/base/testing_browser_process.h"
22 #include "chrome/test/base/testing_pref_service.h"
23 #include "content/test/test_browser_thread.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26
27 namespace em = enterprise_management;
28 namespace chromeos {
29
30 using ::testing::_;
31 using ::testing::AnyNumber;
32 using ::testing::Mock;
33 using ::testing::Return;
34 using ::testing::SaveArg;
35
36 class DeviceSettingsProviderTest: public testing::Test {
37 public:
38 MOCK_METHOD1(SettingChanged, void(const std::string&));
39 MOCK_METHOD0(GetTrustedCallback, void(void));
40
41 protected:
42 DeviceSettingsProviderTest()
43 : message_loop_(MessageLoop::TYPE_UI),
44 ui_thread_(content::BrowserThread::UI, &message_loop_),
45 file_thread_(content::BrowserThread::FILE, &message_loop_),
46 pointer_factory_(this),
47 local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)) {
48 }
49
50 virtual ~DeviceSettingsProviderTest() {
51 }
52
53 virtual void SetUp() {
54 PrepareEmptyPolicy();
55
56 EXPECT_CALL(*this, SettingChanged(_))
57 .Times(AnyNumber());
58
59 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_))
60 .Times(AnyNumber())
61 .WillRepeatedly(
62 MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS,
63 policy_blob_));
64 EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_,_))
65 .Times(AnyNumber())
66 .WillRepeatedly(DoAll(
67 SaveArg<0>(&policy_blob_),
68 MockSignedSettingsHelperStorePolicy(SignedSettings::SUCCESS)));
69
70 mock_user_manager_.reset(new MockUserManager());
71 old_user_manager_ = UserManager::Set(mock_user_manager_.get());
72 EXPECT_CALL(*mock_user_manager_, IsCurrentUserOwner())
73 .Times(AnyNumber())
74 .WillRepeatedly(Return(true));
75
76 provider_.reset(
77 new DeviceSettingsProvider(
78 base::Bind(&DeviceSettingsProviderTest::SettingChanged,
79 pointer_factory_.GetWeakPtr()),
Mattias Nissler (ping if slow) 2012/03/22 10:59:33 You could actually ditch the WeakPtrFactory and us
80 &signed_settings_helper_));
81 provider_->set_ownership_status(OwnershipService::OWNERSHIP_TAKEN);
82 provider_->Reload();
83 }
84
85 virtual void TearDown() {
86 UserManager::Set(old_user_manager_);
87 }
88
89 void PrepareEmptyPolicy() {
90 em::PolicyData policy;
91 em::ChromeDeviceSettingsProto pol;
92 // Set metrics to disabled to prevent us from running into code that is not
93 // mocked.
94 pol.mutable_metrics_enabled()->set_metrics_enabled(false);
95 policy.set_policy_type(chromeos::kDevicePolicyType);
96 policy.set_username("me@owner");
97 policy.set_policy_value(pol.SerializeAsString());
98 // Wipe the signed settings store.
99 policy_blob_.set_policy_data(policy.SerializeAsString());
100 policy_blob_.set_policy_data_signature("false");
101 }
102
103 em::PolicyFetchResponse policy_blob_;
104
105 scoped_ptr<DeviceSettingsProvider> provider_;
106
107 MessageLoop message_loop_;
108 content::TestBrowserThread ui_thread_;
109 content::TestBrowserThread file_thread_;
110
111 base::WeakPtrFactory<DeviceSettingsProviderTest> pointer_factory_;
112
113 ScopedTestingLocalState local_state_;
114
115 MockSignedSettingsHelper signed_settings_helper_;
116 scoped_ptr<MockUserManager> mock_user_manager_;
117 UserManager* old_user_manager_;
118
119 ScopedStubCrosEnabler stub_cros_enabler_;
120 };
121
122 TEST_F(DeviceSettingsProviderTest, InitializationTest) {
123 // Verify that the policy blob has been correctly parsed and trusted.
124 EXPECT_TRUE(provider_->PrepareTrustedValues(
125 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
126 pointer_factory_.GetWeakPtr())));
127 // The trusted flag should be established already prior to calling GetTrusted.
128 message_loop_.RunAllPending();
129 const base::Value* value = provider_->Get(kStatsReportingPref);
130 ASSERT_TRUE(value);
131 bool bool_value;
132 EXPECT_TRUE(value->GetAsBoolean(&bool_value));
133 EXPECT_FALSE(bool_value);
134 }
135
136 TEST_F(DeviceSettingsProviderTest, InitializationTestUnowned) {
137 // No calls to the SignedSettingsHelper should occur in this case!
138 Mock::VerifyAndClear(&signed_settings_helper_);
139
140 provider_->set_ownership_status(OwnershipService::OWNERSHIP_NONE);
141 provider_->Reload();
142 // Verify that the cache policy blob is "trusted".
143 EXPECT_TRUE(provider_->PrepareTrustedValues(
144 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
145 pointer_factory_.GetWeakPtr())));
146 // The trusted flag should be established already prior to calling GetTrusted.
147 message_loop_.RunAllPending();
148 const base::Value* value = provider_->Get(kReleaseChannel);
149 ASSERT_TRUE(value);
150 std::string string_value;
151 EXPECT_TRUE(value->GetAsString(&string_value));
152 EXPECT_TRUE(string_value.empty());
153
154 // Sets should succeed though and be readable from the cache.
155 base::StringValue new_value("stable-channel");
156 provider_->Set(kReleaseChannel, new_value);
157 // Do one more reload here to make sure we don't flip randomly between stores.
158 provider_->Reload();
159 // Verify the change has not been applied.
160 const base::Value* saved_value = provider_->Get(kReleaseChannel);
161 ASSERT_TRUE(saved_value);
162 EXPECT_TRUE(saved_value->GetAsString(&string_value));
163 ASSERT_EQ("stable-channel", string_value);
164 }
165
166 TEST_F(DeviceSettingsProviderTest, SetPrefFailed) {
167 // If we are not the owner no sets should work.
168 EXPECT_CALL(*mock_user_manager_, IsCurrentUserOwner())
169 .WillOnce(Return(false));
170 base::FundamentalValue value(true);
171 provider_->Set(kStatsReportingPref, value);
172 // Verify the change has not been applied.
173 const base::Value* saved_value = provider_->Get(kStatsReportingPref);
174 ASSERT_TRUE(saved_value);
175 bool bool_value;
176 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value));
177 EXPECT_FALSE(bool_value);
178 }
179
180 TEST_F(DeviceSettingsProviderTest, SetPrefSucceed) {
181 base::FundamentalValue value(true);
182 provider_->Set(kStatsReportingPref, value);
183 // Verify the change has not been applied.
184 const base::Value* saved_value = provider_->Get(kStatsReportingPref);
185 ASSERT_TRUE(saved_value);
186 bool bool_value;
187 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value));
188 EXPECT_TRUE(bool_value);
189 }
190
191 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalFailedBadSingature) {
192 // No calls to the SignedSettingsHelper should occur in this case!
193 Mock::VerifyAndClear(&signed_settings_helper_);
194 EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_))
195 .Times(AnyNumber())
196 .WillRepeatedly(
197 MockSignedSettingsHelperRetrievePolicy(
198 SignedSettings::BAD_SIGNATURE,
199 policy_blob_));
200 provider_->Reload();
201 // Verify that the cache policy blob is not "trusted".
202 EXPECT_FALSE(provider_->PrepareTrustedValues(
203 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
204 pointer_factory_.GetWeakPtr())));
205 // The trusted flag should be established already prior to calling GetTrusted.
206 message_loop_.RunAllPending();
207 }
208
209 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalOperationFailed) {
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 .Times(AnyNumber())
214 .WillRepeatedly(
215 MockSignedSettingsHelperRetrievePolicy(
216 SignedSettings::OPERATION_FAILED,
217 policy_blob_));
218 provider_->Reload();
219 // Verify that the cache policy blob is not "trusted".
220 EXPECT_FALSE(provider_->PrepareTrustedValues(
221 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
222 pointer_factory_.GetWeakPtr())));
223 // The trusted flag should be established already prior to calling GetTrusted.
224 message_loop_.RunAllPending();
225 }
226
227 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/device_settings_provider.cc ('k') | chrome/browser/ui/options/options_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698