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

Unified Diff: chrome/browser/chromeos/settings/device_settings_provider_unittest.cc

Issue 10832035: Switch from SignedSettings to DeviceSettingsService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/settings/device_settings_provider_unittest.cc
diff --git a/chrome/browser/chromeos/settings/device_settings_provider_unittest.cc b/chrome/browser/chromeos/settings/device_settings_provider_unittest.cc
index e05c24c76511516a6d37fc8616d4ae734d505f9f..8943eee3e0d2526cc3acb3b258767f7b2f8498ed 100644
--- a/chrome/browser/chromeos/settings/device_settings_provider_unittest.cc
+++ b/chrome/browser/chromeos/settings/device_settings_provider_unittest.cc
@@ -7,13 +7,14 @@
#include <string>
#include "base/bind.h"
+#include "base/callback.h"
#include "base/message_loop.h"
#include "base/values.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
-#include "chrome/browser/chromeos/login/mock_user_manager.h"
#include "chrome/browser/chromeos/settings/cros_settings_names.h"
-#include "chrome/browser/chromeos/settings/mock_signed_settings_helper.h"
-#include "chrome/browser/chromeos/settings/ownership_service.h"
+#include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
+#include "chrome/browser/chromeos/settings/mock_owner_key_util.h"
+#include "chrome/browser/policy/policy_builder.h"
#include "chrome/browser/policy/proto/chrome_device_policy.pb.h"
#include "chrome/browser/policy/proto/device_management_backend.pb.h"
#include "chrome/test/base/testing_browser_process.h"
@@ -23,96 +24,82 @@
#include "testing/gtest/include/gtest/gtest.h"
namespace em = enterprise_management;
+
namespace chromeos {
using ::testing::AnyNumber;
using ::testing::Mock;
-using ::testing::Return;
-using ::testing::SaveArg;
using ::testing::_;
class DeviceSettingsProviderTest: public testing::Test {
-public:
+ public:
MOCK_METHOD1(SettingChanged, void(const std::string&));
MOCK_METHOD0(GetTrustedCallback, void(void));
-protected:
+ protected:
DeviceSettingsProviderTest()
: message_loop_(MessageLoop::TYPE_UI),
ui_thread_(content::BrowserThread::UI, &message_loop_),
file_thread_(content::BrowserThread::FILE, &message_loop_),
- local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)) {
- }
-
- virtual ~DeviceSettingsProviderTest() {
- }
+ local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)),
+ owner_key_util_(new MockOwnerKeyUtil()) {}
virtual void SetUp() OVERRIDE {
- PrepareEmptyPolicy();
+ policy_.payload().mutable_metrics_enabled()->set_metrics_enabled(false);
+ policy_.Build();
- EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_))
- .WillRepeatedly(
- MockSignedSettingsHelperRetrievePolicy(SignedSettings::SUCCESS,
- policy_blob_));
- EXPECT_CALL(signed_settings_helper_, StartStorePolicyOp(_,_))
- .WillRepeatedly(DoAll(
- SaveArg<0>(&policy_blob_),
- MockSignedSettingsHelperStorePolicy(SignedSettings::SUCCESS)));
+ device_settings_test_helper_.set_policy_blob(policy_.GetBlob());
- EXPECT_CALL(*mock_user_manager_.user_manager(), IsCurrentUserOwner())
- .WillRepeatedly(Return(true));
+ device_settings_service_.Initialize(&device_settings_test_helper_,
+ owner_key_util_);
EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
provider_.reset(
new DeviceSettingsProvider(
base::Bind(&DeviceSettingsProviderTest::SettingChanged,
base::Unretained(this)),
- &signed_settings_helper_));
- provider_->set_ownership_status(OwnershipService::OWNERSHIP_TAKEN);
-
- // To prevent flooding the logs.
- provider_->set_retries_left(1);
- provider_->Reload();
+ &device_settings_service_));
Mock::VerifyAndClearExpectations(this);
}
- void PrepareEmptyPolicy() {
- em::PolicyData policy;
- em::ChromeDeviceSettingsProto pol;
- // Set metrics to disabled to prevent us from running into code that is not
- // mocked.
- pol.mutable_metrics_enabled()->set_metrics_enabled(false);
- policy.set_policy_type(chromeos::kDevicePolicyType);
- policy.set_username("me@owner");
- policy.set_policy_value(pol.SerializeAsString());
- // Wipe the signed settings store.
- policy_blob_.set_policy_data(policy.SerializeAsString());
- policy_blob_.set_policy_data_signature("false");
+ virtual void TearDown() OVERRIDE {
+ device_settings_service_.Shutdown();
}
- em::PolicyFetchResponse policy_blob_;
-
- scoped_ptr<DeviceSettingsProvider> provider_;
-
MessageLoop message_loop_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread file_thread_;
+ ScopedStubCrosEnabler stub_cros_enabler_;
+
ScopedTestingLocalState local_state_;
- MockSignedSettingsHelper signed_settings_helper_;
+ DeviceSettingsTestHelper device_settings_test_helper_;
+ scoped_refptr<MockOwnerKeyUtil> owner_key_util_;
- ScopedStubCrosEnabler stub_cros_enabler_;
- ScopedMockUserManagerEnabler mock_user_manager_;
+ DeviceSettingsService device_settings_service_;
+
+ policy::DevicePolicyBuilder policy_;
+
+ scoped_ptr<DeviceSettingsProvider> provider_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(DeviceSettingsProviderTest);
};
TEST_F(DeviceSettingsProviderTest, InitializationTest) {
+ owner_key_util_->SetPublicKeyFromPrivateKey(policy_.signing_key());
+
+ // Have the service load a settings blob.
+ EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
+ device_settings_service_.Load();
+ device_settings_test_helper_.Flush();
+ Mock::VerifyAndClearExpectations(this);
+
// Verify that the policy blob has been correctly parsed and trusted.
// The trusted flag should be set before the call to PrepareTrustedValues.
EXPECT_EQ(CrosSettingsProvider::TRUSTED,
- provider_->PrepareTrustedValues(
- base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
- base::Unretained(this))));
+ provider_->PrepareTrustedValues(base::Closure()));
const base::Value* value = provider_->Get(kStatsReportingPref);
ASSERT_TRUE(value);
bool bool_value;
@@ -121,16 +108,13 @@ TEST_F(DeviceSettingsProviderTest, InitializationTest) {
}
TEST_F(DeviceSettingsProviderTest, InitializationTestUnowned) {
- // No calls to the SignedSettingsHelper should occur in this case!
- Mock::VerifyAndClear(&signed_settings_helper_);
+ // Have the service check the key.
+ device_settings_service_.Load();
+ device_settings_test_helper_.Flush();
- provider_->set_ownership_status(OwnershipService::OWNERSHIP_NONE);
- provider_->Reload();
// The trusted flag should be set before the call to PrepareTrustedValues.
EXPECT_EQ(CrosSettingsProvider::TRUSTED,
- provider_->PrepareTrustedValues(
- base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
- base::Unretained(this))));
+ provider_->PrepareTrustedValues(base::Closure()));
const base::Value* value = provider_->Get(kReleaseChannel);
ASSERT_TRUE(value);
std::string string_value;
@@ -138,26 +122,38 @@ TEST_F(DeviceSettingsProviderTest, InitializationTestUnowned) {
EXPECT_TRUE(string_value.empty());
// Sets should succeed though and be readable from the cache.
+ EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
EXPECT_CALL(*this, SettingChanged(kReleaseChannel)).Times(1);
base::StringValue new_value("stable-channel");
provider_->Set(kReleaseChannel, new_value);
- // Do one more reload here to make sure we don't flip randomly between stores.
- provider_->Reload();
- // Verify the change has not been applied.
+ Mock::VerifyAndClearExpectations(this);
+
+ // This shouldn't trigger a write.
+ device_settings_test_helper_.set_policy_blob(std::string());
+ device_settings_test_helper_.Flush();
+ EXPECT_EQ(std::string(), device_settings_test_helper_.policy_blob());
+
+ // Verify the change has been applied.
const base::Value* saved_value = provider_->Get(kReleaseChannel);
ASSERT_TRUE(saved_value);
EXPECT_TRUE(saved_value->GetAsString(&string_value));
ASSERT_EQ("stable-channel", string_value);
- Mock::VerifyAndClearExpectations(this);
}
TEST_F(DeviceSettingsProviderTest, SetPrefFailed) {
- EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
// If we are not the owner no sets should work.
- EXPECT_CALL(*mock_user_manager_.user_manager(), IsCurrentUserOwner())
- .WillOnce(Return(false));
+ owner_key_util_->SetPublicKeyFromPrivateKey(policy_.signing_key());
+
base::FundamentalValue value(true);
+ EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
provider_->Set(kStatsReportingPref, value);
+ Mock::VerifyAndClearExpectations(this);
+
+ // This shouldn't trigger a write.
+ device_settings_test_helper_.set_policy_blob(std::string());
+ device_settings_test_helper_.Flush();
+ EXPECT_EQ(std::string(), device_settings_test_helper_.policy_blob());
+
// Verify the change has not been applied.
const base::Value* saved_value = provider_->Get(kStatsReportingPref);
ASSERT_TRUE(saved_value);
@@ -167,10 +163,26 @@ TEST_F(DeviceSettingsProviderTest, SetPrefFailed) {
}
TEST_F(DeviceSettingsProviderTest, SetPrefSucceed) {
- EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
+ owner_key_util_->SetPrivateKey(policy_.signing_key());
+ device_settings_service_.SetUsername(policy_.policy_data().username());
+ device_settings_test_helper_.Flush();
+
base::FundamentalValue value(true);
+ EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
+ EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
provider_->Set(kStatsReportingPref, value);
- // Verify the change has not been applied.
+ Mock::VerifyAndClearExpectations(this);
+
+ // Process the store.
+ device_settings_test_helper_.set_policy_blob(std::string());
+ device_settings_test_helper_.Flush();
+
+ // Verify that the device policy has been adjusted.
+ ASSERT_TRUE(device_settings_service_.device_settings());
+ EXPECT_TRUE(device_settings_service_.device_settings()->
+ metrics_enabled().metrics_enabled());
+
+ // Verify the change has been applied.
const base::Value* saved_value = provider_->Get(kStatsReportingPref);
ASSERT_TRUE(saved_value);
bool bool_value;
@@ -178,84 +190,64 @@ TEST_F(DeviceSettingsProviderTest, SetPrefSucceed) {
EXPECT_TRUE(bool_value);
}
-TEST_F(DeviceSettingsProviderTest, PolicyRetrievalFailedBadSingature) {
- // No calls to the SignedSettingsHelper should occur in this case!
- Mock::VerifyAndClear(&signed_settings_helper_);
- EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_))
- .WillRepeatedly(
- MockSignedSettingsHelperRetrievePolicy(
- SignedSettings::BAD_SIGNATURE,
- policy_blob_));
- provider_->Reload();
- // Verify that the cache policy blob is not "trusted".
- EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
- provider_->PrepareTrustedValues(
- base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
- base::Unretained(this))));
-}
+TEST_F(DeviceSettingsProviderTest, PolicyRetrievalFailedBadSignature) {
+ owner_key_util_->SetPublicKeyFromPrivateKey(policy_.signing_key());
+ policy_.policy().set_policy_data_signature("bad signature");
+ device_settings_test_helper_.set_policy_blob(policy_.GetBlob());
+
+ device_settings_service_.Load();
+ device_settings_test_helper_.Flush();
-TEST_F(DeviceSettingsProviderTest, PolicyRetrievalOperationFailedPermanently) {
- // No calls to the SignedSettingsHelper should occur in this case!
- Mock::VerifyAndClear(&signed_settings_helper_);
- EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_))
- .WillRepeatedly(
- MockSignedSettingsHelperRetrievePolicy(
- SignedSettings::OPERATION_FAILED,
- policy_blob_));
- provider_->Reload();
- // Verify that the cache policy blob is not "trusted".
+ // Verify that the cached settings blob is not "trusted".
+ EXPECT_EQ(DeviceSettingsService::STORE_VALIDATION_ERROR,
+ device_settings_service_.status());
EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
- provider_->PrepareTrustedValues(
- base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
- base::Unretained(this))));
+ provider_->PrepareTrustedValues(base::Closure()));
}
-TEST_F(DeviceSettingsProviderTest, PolicyRetrievalOperationFailedOnce) {
- // No calls to the SignedSettingsHelper should occur in this case!
- Mock::VerifyAndClear(&signed_settings_helper_);
- EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_))
- .WillOnce(
- MockSignedSettingsHelperRetrievePolicy(
- SignedSettings::OPERATION_FAILED,
- policy_blob_))
- .WillRepeatedly(
- MockSignedSettingsHelperRetrievePolicy(
- SignedSettings::SUCCESS,
- policy_blob_));
- // Should be trusted after an automatic reload.
- provider_->Reload();
- // Verify that the cache policy blob is not "trusted".
- EXPECT_EQ(CrosSettingsProvider::TRUSTED,
- provider_->PrepareTrustedValues(
- base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
- base::Unretained(this))));
+TEST_F(DeviceSettingsProviderTest, PolicyRetrievalNoPolicy) {
+ owner_key_util_->SetPublicKeyFromPrivateKey(policy_.signing_key());
+ device_settings_test_helper_.set_policy_blob(std::string());
+
+ device_settings_service_.Load();
+ device_settings_test_helper_.Flush();
+
+ // Verify that the cached settings blob is not "trusted".
+ EXPECT_EQ(DeviceSettingsService::STORE_NO_POLICY,
+ device_settings_service_.status());
+ EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
+ provider_->PrepareTrustedValues(base::Closure()));
}
TEST_F(DeviceSettingsProviderTest, PolicyFailedPermanentlyNotification) {
- Mock::VerifyAndClear(&signed_settings_helper_);
- EXPECT_CALL(signed_settings_helper_, StartRetrievePolicyOp(_))
- .WillRepeatedly(
- MockSignedSettingsHelperRetrievePolicy(
- SignedSettings::OPERATION_FAILED,
- policy_blob_));
-
- provider_->set_trusted_status(CrosSettingsProvider::TEMPORARILY_UNTRUSTED);
+ owner_key_util_->SetPublicKeyFromPrivateKey(policy_.signing_key());
+ device_settings_test_helper_.set_policy_blob(std::string());
+
EXPECT_CALL(*this, GetTrustedCallback());
EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED,
provider_->PrepareTrustedValues(
base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
base::Unretained(this))));
- provider_->Reload();
+
+ device_settings_service_.Load();
+ device_settings_test_helper_.Flush();
+ Mock::VerifyAndClearExpectations(this);
+
+ EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
+ provider_->PrepareTrustedValues(base::Closure()));
}
TEST_F(DeviceSettingsProviderTest, PolicyLoadNotification) {
- provider_->set_trusted_status(CrosSettingsProvider::TEMPORARILY_UNTRUSTED);
EXPECT_CALL(*this, GetTrustedCallback());
+
EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED,
provider_->PrepareTrustedValues(
base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
base::Unretained(this))));
- provider_->Reload();
+
+ device_settings_service_.Load();
+ device_settings_test_helper_.Flush();
+ Mock::VerifyAndClearExpectations(this);
}
} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698