enterprise_install_attributes_unittest.cc revision 3551c9c881056c480085172ff9840cab31610854
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/policy/enterprise_install_attributes.h" 6 7#include "base/bind.h" 8#include "base/file_util.h" 9#include "base/files/scoped_temp_dir.h" 10#include "base/memory/scoped_ptr.h" 11#include "base/run_loop.h" 12#include "chrome/browser/policy/proto/chromeos/install_attributes.pb.h" 13#include "chromeos/cryptohome/cryptohome_library.h" 14#include "chromeos/dbus/cryptohome_client.h" 15#include "google_apis/gaia/gaia_auth_util.h" 16#include "testing/gtest/include/gtest/gtest.h" 17 18namespace policy { 19 20namespace { 21 22void CopyLockResult(base::RunLoop* loop, 23 EnterpriseInstallAttributes::LockResult* out, 24 EnterpriseInstallAttributes::LockResult result) { 25 *out = result; 26 loop->Quit(); 27} 28 29} // namespace 30 31static const char kTestUser[] = "test@example.com"; 32static const char kTestUserCanonicalize[] = "UPPER.CASE@example.com"; 33static const char kTestDomain[] = "example.com"; 34static const char kTestDeviceId[] = "133750519"; 35 36class EnterpriseInstallAttributesTest : public testing::Test { 37 protected: 38 EnterpriseInstallAttributesTest() 39 : cryptohome_(chromeos::CryptohomeLibrary::GetTestImpl()), 40 stub_cryptohome_client_(chromeos::CryptohomeClient::Create( 41 chromeos::STUB_DBUS_CLIENT_IMPLEMENTATION, NULL)), 42 install_attributes_(cryptohome_.get(), stub_cryptohome_client_.get()) {} 43 44 virtual void SetUp() OVERRIDE { 45 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 46 } 47 48 base::FilePath GetTempPath() const { 49 return temp_dir_.path().Append("install_attrs_test"); 50 } 51 52 void SetAttribute( 53 cryptohome::SerializedInstallAttributes* install_attrs_proto, 54 const std::string& name, 55 const std::string& value) { 56 cryptohome::SerializedInstallAttributes::Attribute* attribute; 57 attribute = install_attrs_proto->add_attributes(); 58 attribute->set_name(name); 59 attribute->set_value(value); 60 } 61 62 base::MessageLoopForUI message_loop_; 63 base::ScopedTempDir temp_dir_; 64 scoped_ptr<chromeos::CryptohomeLibrary> cryptohome_; 65 scoped_ptr<chromeos::CryptohomeClient> stub_cryptohome_client_; 66 EnterpriseInstallAttributes install_attributes_; 67 68 EnterpriseInstallAttributes::LockResult LockDeviceAndWaitForResult( 69 const std::string& user, 70 DeviceMode device_mode, 71 const std::string& device_id) { 72 base::RunLoop loop; 73 EnterpriseInstallAttributes::LockResult result; 74 install_attributes_.LockDevice(user, device_mode, device_id, 75 base::Bind(&CopyLockResult, &loop, &result)); 76 loop.Run(); 77 return result; 78 } 79}; 80 81TEST_F(EnterpriseInstallAttributesTest, Lock) { 82 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, 83 LockDeviceAndWaitForResult( 84 kTestUser, 85 DEVICE_MODE_ENTERPRISE, 86 kTestDeviceId)); 87 88 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, 89 LockDeviceAndWaitForResult( 90 kTestUser, 91 DEVICE_MODE_ENTERPRISE, 92 kTestDeviceId)); 93 // Another user from the same domain should also succeed. 94 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, 95 LockDeviceAndWaitForResult( 96 "test1@example.com", 97 DEVICE_MODE_ENTERPRISE, 98 kTestDeviceId)); 99 // But another domain should fail. 100 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_WRONG_USER, 101 LockDeviceAndWaitForResult( 102 "test@bluebears.com", 103 DEVICE_MODE_ENTERPRISE, 104 kTestDeviceId)); 105} 106 107TEST_F(EnterpriseInstallAttributesTest, LockCanonicalize) { 108 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, 109 LockDeviceAndWaitForResult( 110 kTestUserCanonicalize, 111 DEVICE_MODE_ENTERPRISE, 112 kTestDeviceId)); 113 EXPECT_EQ(gaia::CanonicalizeEmail(kTestUserCanonicalize), 114 install_attributes_.GetRegistrationUser()); 115} 116 117TEST_F(EnterpriseInstallAttributesTest, IsEnterpriseDevice) { 118 install_attributes_.ReadCacheFile(GetTempPath()); 119 EXPECT_FALSE(install_attributes_.IsEnterpriseDevice()); 120 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, 121 LockDeviceAndWaitForResult( 122 kTestUser, 123 DEVICE_MODE_ENTERPRISE, 124 kTestDeviceId)); 125 EXPECT_TRUE(install_attributes_.IsEnterpriseDevice()); 126} 127 128TEST_F(EnterpriseInstallAttributesTest, GetDomain) { 129 install_attributes_.ReadCacheFile(GetTempPath()); 130 EXPECT_EQ(std::string(), install_attributes_.GetDomain()); 131 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, 132 LockDeviceAndWaitForResult( 133 kTestUser, 134 DEVICE_MODE_ENTERPRISE, 135 kTestDeviceId)); 136 EXPECT_EQ(kTestDomain, install_attributes_.GetDomain()); 137} 138 139TEST_F(EnterpriseInstallAttributesTest, GetRegistrationUser) { 140 install_attributes_.ReadCacheFile(GetTempPath()); 141 EXPECT_EQ(std::string(), install_attributes_.GetRegistrationUser()); 142 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, 143 LockDeviceAndWaitForResult( 144 kTestUser, 145 DEVICE_MODE_ENTERPRISE, 146 kTestDeviceId)); 147 EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser()); 148} 149 150TEST_F(EnterpriseInstallAttributesTest, GetDeviceId) { 151 install_attributes_.ReadCacheFile(GetTempPath()); 152 EXPECT_EQ(std::string(), install_attributes_.GetDeviceId()); 153 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, 154 LockDeviceAndWaitForResult( 155 kTestUser, 156 DEVICE_MODE_ENTERPRISE, 157 kTestDeviceId)); 158 EXPECT_EQ(kTestDeviceId, install_attributes_.GetDeviceId()); 159} 160 161TEST_F(EnterpriseInstallAttributesTest, GetMode) { 162 install_attributes_.ReadCacheFile(GetTempPath()); 163 EXPECT_EQ(DEVICE_MODE_PENDING, install_attributes_.GetMode()); 164 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, 165 LockDeviceAndWaitForResult( 166 kTestUser, 167 DEVICE_MODE_RETAIL_KIOSK, 168 kTestDeviceId)); 169 EXPECT_EQ(DEVICE_MODE_RETAIL_KIOSK, 170 install_attributes_.GetMode()); 171} 172 173TEST_F(EnterpriseInstallAttributesTest, ConsumerDevice) { 174 install_attributes_.ReadCacheFile(GetTempPath()); 175 EXPECT_EQ(DEVICE_MODE_PENDING, install_attributes_.GetMode()); 176 // Lock the attributes empty. 177 ASSERT_TRUE(cryptohome_->InstallAttributesFinalize()); 178 base::RunLoop loop; 179 install_attributes_.ReadImmutableAttributes(base::Bind(loop.QuitClosure())); 180 loop.Run(); 181 182 ASSERT_FALSE(cryptohome_->InstallAttributesIsFirstInstall()); 183 EXPECT_EQ(DEVICE_MODE_CONSUMER, install_attributes_.GetMode()); 184} 185 186TEST_F(EnterpriseInstallAttributesTest, ConsumerKioskDevice) { 187 install_attributes_.ReadCacheFile(GetTempPath()); 188 EXPECT_EQ(DEVICE_MODE_PENDING, install_attributes_.GetMode()); 189 // Lock the attributes for consumer kiosk. 190 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, 191 LockDeviceAndWaitForResult( 192 std::string(), 193 DEVICE_MODE_CONSUMER_KIOSK, 194 std::string())); 195 196 ASSERT_FALSE(cryptohome_->InstallAttributesIsFirstInstall()); 197 EXPECT_EQ(DEVICE_MODE_CONSUMER_KIOSK, install_attributes_.GetMode()); 198 ASSERT_TRUE(install_attributes_.IsConsumerKioskDevice()); 199} 200 201TEST_F(EnterpriseInstallAttributesTest, DeviceLockedFromOlderVersion) { 202 install_attributes_.ReadCacheFile(GetTempPath()); 203 EXPECT_EQ(DEVICE_MODE_PENDING, install_attributes_.GetMode()); 204 // Lock the attributes as if it was done from older Chrome version. 205 ASSERT_TRUE(cryptohome_->InstallAttributesSet( 206 EnterpriseInstallAttributes::kAttrEnterpriseOwned, "true")); 207 ASSERT_TRUE(cryptohome_->InstallAttributesSet( 208 EnterpriseInstallAttributes::kAttrEnterpriseUser, kTestUser)); 209 ASSERT_TRUE(cryptohome_->InstallAttributesFinalize()); 210 base::RunLoop loop; 211 install_attributes_.ReadImmutableAttributes(base::Bind(loop.QuitClosure())); 212 loop.Run(); 213 214 ASSERT_FALSE(cryptohome_->InstallAttributesIsFirstInstall()); 215 EXPECT_EQ(DEVICE_MODE_ENTERPRISE, install_attributes_.GetMode()); 216 EXPECT_EQ(kTestDomain, install_attributes_.GetDomain()); 217 EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser()); 218 EXPECT_EQ("", install_attributes_.GetDeviceId()); 219} 220 221TEST_F(EnterpriseInstallAttributesTest, ReadCacheFile) { 222 cryptohome::SerializedInstallAttributes install_attrs_proto; 223 SetAttribute(&install_attrs_proto, 224 EnterpriseInstallAttributes::kAttrEnterpriseOwned, "true"); 225 SetAttribute(&install_attrs_proto, 226 EnterpriseInstallAttributes::kAttrEnterpriseUser, kTestUser); 227 const std::string blob(install_attrs_proto.SerializeAsString()); 228 ASSERT_EQ(static_cast<int>(blob.size()), 229 file_util::WriteFile(GetTempPath(), blob.c_str(), blob.size())); 230 install_attributes_.ReadCacheFile(GetTempPath()); 231 EXPECT_EQ(DEVICE_MODE_ENTERPRISE, install_attributes_.GetMode()); 232 EXPECT_EQ(kTestDomain, install_attributes_.GetDomain()); 233 EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser()); 234 EXPECT_EQ("", install_attributes_.GetDeviceId()); 235} 236 237TEST_F(EnterpriseInstallAttributesTest, ReadCacheFileForConsumerKiosk) { 238 cryptohome::SerializedInstallAttributes install_attrs_proto; 239 SetAttribute(&install_attrs_proto, 240 EnterpriseInstallAttributes::kAttrConsumerKioskEnabled, "true"); 241 const std::string blob(install_attrs_proto.SerializeAsString()); 242 ASSERT_EQ(static_cast<int>(blob.size()), 243 file_util::WriteFile(GetTempPath(), blob.c_str(), blob.size())); 244 install_attributes_.ReadCacheFile(GetTempPath()); 245 EXPECT_EQ(DEVICE_MODE_CONSUMER_KIOSK, install_attributes_.GetMode()); 246 EXPECT_EQ("", install_attributes_.GetDomain()); 247 EXPECT_EQ("", install_attributes_.GetRegistrationUser()); 248 EXPECT_EQ("", install_attributes_.GetDeviceId()); 249} 250 251} // namespace policy 252