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

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