| OLD | NEW |
| (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/policy/device_cloud_policy_store_chromeos.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/compiler_specific.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop.h" |
| 14 #include "chrome/browser/chromeos/cros/mock_cryptohome_library.h" |
| 15 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 16 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" |
| 17 #include "chrome/browser/chromeos/settings/mock_owner_key_util.h" |
| 18 #include "chrome/browser/policy/enterprise_install_attributes.h" |
| 19 #include "chrome/browser/policy/policy_builder.h" |
| 20 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" |
| 21 #include "chromeos/dbus/mock_session_manager_client.h" |
| 22 #include "content/public/test/test_browser_thread.h" |
| 23 #include "policy/policy_constants.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 |
| 26 namespace policy { |
| 27 |
| 28 class DeviceCloudPolicyStoreChromeOSTest : public testing::Test { |
| 29 protected: |
| 30 DeviceCloudPolicyStoreChromeOSTest() |
| 31 : loop_(MessageLoop::TYPE_UI), |
| 32 ui_thread_(content::BrowserThread::UI, &loop_), |
| 33 file_thread_(content::BrowserThread::FILE, &loop_), |
| 34 owner_key_util_(new chromeos::MockOwnerKeyUtil()), |
| 35 cryptohome_library_(chromeos::CryptohomeLibrary::GetImpl(true)), |
| 36 install_attributes_( |
| 37 new EnterpriseInstallAttributes(cryptohome_library_.get())), |
| 38 store_(new DeviceCloudPolicyStoreChromeOS(&device_settings_service_, |
| 39 install_attributes_.get())) {} |
| 40 |
| 41 virtual void SetUp() OVERRIDE { |
| 42 device_settings_service_.Initialize(&device_settings_test_helper_, |
| 43 owner_key_util_); |
| 44 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, |
| 45 install_attributes_->LockDevice(PolicyBuilder::kFakeUsername, |
| 46 DEVICE_MODE_ENTERPRISE, |
| 47 PolicyBuilder::kFakeDeviceId)); |
| 48 |
| 49 policy_.payload().mutable_metrics_enabled()->set_metrics_enabled(true); |
| 50 policy_.Build(); |
| 51 |
| 52 owner_key_util_->SetPublicKeyFromPrivateKey(policy_.signing_key()); |
| 53 device_settings_test_helper_.set_policy_blob(policy_.GetBlob()); |
| 54 } |
| 55 |
| 56 void ExpectFailure(CloudPolicyStore::Status expected_status) { |
| 57 EXPECT_EQ(expected_status, store_->status()); |
| 58 EXPECT_TRUE(store_->is_initialized()); |
| 59 EXPECT_FALSE(store_->has_policy()); |
| 60 EXPECT_FALSE(store_->is_managed()); |
| 61 } |
| 62 |
| 63 void ExpectSuccess() { |
| 64 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); |
| 65 EXPECT_TRUE(store_->is_initialized()); |
| 66 EXPECT_TRUE(store_->has_policy()); |
| 67 EXPECT_TRUE(store_->is_managed()); |
| 68 EXPECT_TRUE(store_->policy()); |
| 69 base::FundamentalValue expected(true); |
| 70 EXPECT_TRUE( |
| 71 base::Value::Equals(&expected, |
| 72 store_->policy_map().GetValue( |
| 73 key::kDeviceMetricsReportingEnabled))); |
| 74 } |
| 75 |
| 76 void PrepareExistingPolicy() { |
| 77 store_->Load(); |
| 78 device_settings_test_helper_.Flush(); |
| 79 ExpectSuccess(); |
| 80 |
| 81 policy_.set_new_signing_key(scoped_ptr<crypto::RSAPrivateKey>()); |
| 82 policy_.Build(); |
| 83 } |
| 84 |
| 85 void PrepareNewSigningKey() { |
| 86 policy_.set_new_signing_key(PolicyBuilder::CreateTestNewSigningKey()); |
| 87 policy_.Build(); |
| 88 owner_key_util_->SetPublicKeyFromPrivateKey(policy_.new_signing_key()); |
| 89 } |
| 90 |
| 91 void ResetToNonEnterprise() { |
| 92 store_.reset(); |
| 93 cryptohome_library_->InstallAttributesSet("enterprise.owned", |
| 94 std::string()); |
| 95 install_attributes_.reset( |
| 96 new EnterpriseInstallAttributes(cryptohome_library_.get())); |
| 97 store_.reset(new DeviceCloudPolicyStoreChromeOS(&device_settings_service_, |
| 98 install_attributes_.get())); |
| 99 } |
| 100 |
| 101 MessageLoop loop_; |
| 102 content::TestBrowserThread ui_thread_; |
| 103 content::TestBrowserThread file_thread_; |
| 104 |
| 105 chromeos::DeviceSettingsTestHelper device_settings_test_helper_; |
| 106 DevicePolicyBuilder policy_; |
| 107 |
| 108 scoped_refptr<chromeos::MockOwnerKeyUtil> owner_key_util_; |
| 109 chromeos::DeviceSettingsService device_settings_service_; |
| 110 scoped_ptr<chromeos::CryptohomeLibrary> cryptohome_library_; |
| 111 scoped_ptr<EnterpriseInstallAttributes> install_attributes_; |
| 112 |
| 113 scoped_ptr<DeviceCloudPolicyStoreChromeOS> store_; |
| 114 |
| 115 private: |
| 116 DISALLOW_COPY_AND_ASSIGN(DeviceCloudPolicyStoreChromeOSTest); |
| 117 }; |
| 118 |
| 119 TEST_F(DeviceCloudPolicyStoreChromeOSTest, LoadNoKey) { |
| 120 std::vector<uint8> empty_key; |
| 121 owner_key_util_->SetPublicKey(empty_key); |
| 122 store_->Load(); |
| 123 device_settings_test_helper_.Flush(); |
| 124 ExpectFailure(CloudPolicyStore::STATUS_BAD_STATE); |
| 125 } |
| 126 |
| 127 TEST_F(DeviceCloudPolicyStoreChromeOSTest, LoadNoPolicy) { |
| 128 device_settings_test_helper_.set_policy_blob(std::string()); |
| 129 store_->Load(); |
| 130 device_settings_test_helper_.Flush(); |
| 131 ExpectFailure(CloudPolicyStore::STATUS_LOAD_ERROR); |
| 132 } |
| 133 |
| 134 TEST_F(DeviceCloudPolicyStoreChromeOSTest, LoadNotEnterprise) { |
| 135 ResetToNonEnterprise(); |
| 136 store_->Load(); |
| 137 device_settings_test_helper_.Flush(); |
| 138 ExpectFailure(CloudPolicyStore::STATUS_BAD_STATE); |
| 139 } |
| 140 |
| 141 TEST_F(DeviceCloudPolicyStoreChromeOSTest, LoadSuccess) { |
| 142 store_->Load(); |
| 143 device_settings_test_helper_.Flush(); |
| 144 ExpectSuccess(); |
| 145 } |
| 146 |
| 147 TEST_F(DeviceCloudPolicyStoreChromeOSTest, StoreSuccess) { |
| 148 PrepareExistingPolicy(); |
| 149 store_->Store(policy_.policy()); |
| 150 device_settings_test_helper_.Flush(); |
| 151 ExpectSuccess(); |
| 152 } |
| 153 |
| 154 TEST_F(DeviceCloudPolicyStoreChromeOSTest, StoreNoSignature) { |
| 155 PrepareExistingPolicy(); |
| 156 policy_.policy().clear_policy_data_signature(); |
| 157 store_->Store(policy_.policy()); |
| 158 device_settings_test_helper_.Flush(); |
| 159 EXPECT_EQ(CloudPolicyStore::STATUS_VALIDATION_ERROR, store_->status()); |
| 160 EXPECT_EQ(CloudPolicyValidatorBase::VALIDATION_BAD_SIGNATURE, |
| 161 store_->validation_status()); |
| 162 } |
| 163 |
| 164 TEST_F(DeviceCloudPolicyStoreChromeOSTest, StoreBadSignature) { |
| 165 PrepareExistingPolicy(); |
| 166 policy_.policy().set_policy_data_signature("invalid"); |
| 167 store_->Store(policy_.policy()); |
| 168 device_settings_test_helper_.Flush(); |
| 169 EXPECT_EQ(CloudPolicyStore::STATUS_VALIDATION_ERROR, store_->status()); |
| 170 EXPECT_EQ(CloudPolicyValidatorBase::VALIDATION_BAD_SIGNATURE, |
| 171 store_->validation_status()); |
| 172 } |
| 173 |
| 174 TEST_F(DeviceCloudPolicyStoreChromeOSTest, StoreKeyRotation) { |
| 175 PrepareExistingPolicy(); |
| 176 policy_.set_new_signing_key(PolicyBuilder::CreateTestNewSigningKey()); |
| 177 policy_.Build(); |
| 178 store_->Store(policy_.policy()); |
| 179 device_settings_test_helper_.FlushLoops(); |
| 180 device_settings_test_helper_.FlushStore(); |
| 181 owner_key_util_->SetPublicKeyFromPrivateKey(policy_.new_signing_key()); |
| 182 device_settings_service_.OwnerKeySet(true); |
| 183 device_settings_test_helper_.Flush(); |
| 184 ExpectSuccess(); |
| 185 } |
| 186 |
| 187 TEST_F(DeviceCloudPolicyStoreChromeOSTest, InstallInitialPolicySuccess) { |
| 188 PrepareNewSigningKey(); |
| 189 store_->InstallInitialPolicy(policy_.policy()); |
| 190 device_settings_test_helper_.Flush(); |
| 191 ExpectSuccess(); |
| 192 } |
| 193 |
| 194 TEST_F(DeviceCloudPolicyStoreChromeOSTest, InstallInitialPolicyNoSignature) { |
| 195 PrepareNewSigningKey(); |
| 196 policy_.policy().clear_policy_data_signature(); |
| 197 store_->InstallInitialPolicy(policy_.policy()); |
| 198 device_settings_test_helper_.Flush(); |
| 199 ExpectFailure(CloudPolicyStore::STATUS_VALIDATION_ERROR); |
| 200 EXPECT_EQ(CloudPolicyValidatorBase::VALIDATION_BAD_SIGNATURE, |
| 201 store_->validation_status()); |
| 202 } |
| 203 |
| 204 TEST_F(DeviceCloudPolicyStoreChromeOSTest, InstallInitialPolicyNoKey) { |
| 205 PrepareNewSigningKey(); |
| 206 policy_.policy().clear_new_public_key(); |
| 207 store_->InstallInitialPolicy(policy_.policy()); |
| 208 device_settings_test_helper_.Flush(); |
| 209 ExpectFailure(CloudPolicyStore::STATUS_VALIDATION_ERROR); |
| 210 EXPECT_EQ(CloudPolicyValidatorBase::VALIDATION_BAD_SIGNATURE, |
| 211 store_->validation_status()); |
| 212 } |
| 213 |
| 214 TEST_F(DeviceCloudPolicyStoreChromeOSTest, InstallInitialPolicyNotEnterprise) { |
| 215 PrepareNewSigningKey(); |
| 216 ResetToNonEnterprise(); |
| 217 store_->InstallInitialPolicy(policy_.policy()); |
| 218 device_settings_test_helper_.Flush(); |
| 219 ExpectFailure(CloudPolicyStore::STATUS_BAD_STATE); |
| 220 } |
| 221 |
| 222 } // namespace policy |
| OLD | NEW |