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