| 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/enterprise_install_attributes.h" | |
| 6 | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/files/scoped_temp_dir.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "chrome/browser/chromeos/cros/cryptohome_library.h" | |
| 11 #include "chrome/browser/policy/proto/install_attributes.pb.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace policy { | |
| 15 | |
| 16 static const char kTestUser[] = "test@example.com"; | |
| 17 static const char kTestDomain[] = "example.com"; | |
| 18 static const char kTestDeviceId[] = "133750519"; | |
| 19 | |
| 20 static const char kAttrEnterpriseOwned[] = "enterprise.owned"; | |
| 21 static const char kAttrEnterpriseUser[] = "enterprise.user"; | |
| 22 | |
| 23 class EnterpriseInstallAttributesTest : public testing::Test { | |
| 24 protected: | |
| 25 EnterpriseInstallAttributesTest() | |
| 26 : cryptohome_(chromeos::CryptohomeLibrary::GetImpl(true)), | |
| 27 install_attributes_(cryptohome_.get()) {} | |
| 28 | |
| 29 virtual void SetUp() OVERRIDE { | |
| 30 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 31 } | |
| 32 | |
| 33 FilePath GetTempPath() const { | |
| 34 return temp_dir_.path().Append("install_attrs_test"); | |
| 35 } | |
| 36 | |
| 37 void SetAttribute( | |
| 38 cryptohome::SerializedInstallAttributes* install_attrs_proto, | |
| 39 const std::string& name, | |
| 40 const std::string& value) { | |
| 41 cryptohome::SerializedInstallAttributes::Attribute* attribute; | |
| 42 attribute = install_attrs_proto->add_attributes(); | |
| 43 attribute->set_name(name); | |
| 44 attribute->set_value(value); | |
| 45 } | |
| 46 | |
| 47 base::ScopedTempDir temp_dir_; | |
| 48 scoped_ptr<chromeos::CryptohomeLibrary> cryptohome_; | |
| 49 EnterpriseInstallAttributes install_attributes_; | |
| 50 }; | |
| 51 | |
| 52 TEST_F(EnterpriseInstallAttributesTest, Lock) { | |
| 53 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | |
| 54 install_attributes_.LockDevice( | |
| 55 kTestUser, | |
| 56 DEVICE_MODE_ENTERPRISE, | |
| 57 kTestDeviceId)); | |
| 58 | |
| 59 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | |
| 60 install_attributes_.LockDevice( | |
| 61 kTestUser, | |
| 62 DEVICE_MODE_ENTERPRISE, | |
| 63 kTestDeviceId)); | |
| 64 // Another user from the4 same domain should also succeed. | |
| 65 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | |
| 66 install_attributes_.LockDevice( | |
| 67 "test1@example.com", | |
| 68 DEVICE_MODE_ENTERPRISE, | |
| 69 kTestDeviceId)); | |
| 70 // But another domain should fail. | |
| 71 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_WRONG_USER, | |
| 72 install_attributes_.LockDevice( | |
| 73 "test@bluebears.com", | |
| 74 DEVICE_MODE_ENTERPRISE, | |
| 75 kTestDeviceId)); | |
| 76 } | |
| 77 | |
| 78 TEST_F(EnterpriseInstallAttributesTest, IsEnterpriseDevice) { | |
| 79 install_attributes_.ReadCacheFile(GetTempPath()); | |
| 80 EXPECT_FALSE(install_attributes_.IsEnterpriseDevice()); | |
| 81 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | |
| 82 install_attributes_.LockDevice( | |
| 83 kTestUser, | |
| 84 DEVICE_MODE_ENTERPRISE, | |
| 85 kTestDeviceId)); | |
| 86 EXPECT_TRUE(install_attributes_.IsEnterpriseDevice()); | |
| 87 } | |
| 88 | |
| 89 TEST_F(EnterpriseInstallAttributesTest, GetDomain) { | |
| 90 install_attributes_.ReadCacheFile(GetTempPath()); | |
| 91 EXPECT_EQ(std::string(), install_attributes_.GetDomain()); | |
| 92 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | |
| 93 install_attributes_.LockDevice( | |
| 94 kTestUser, | |
| 95 DEVICE_MODE_ENTERPRISE, | |
| 96 kTestDeviceId)); | |
| 97 EXPECT_EQ(kTestDomain, install_attributes_.GetDomain()); | |
| 98 } | |
| 99 | |
| 100 TEST_F(EnterpriseInstallAttributesTest, GetRegistrationUser) { | |
| 101 install_attributes_.ReadCacheFile(GetTempPath()); | |
| 102 EXPECT_EQ(std::string(), install_attributes_.GetRegistrationUser()); | |
| 103 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | |
| 104 install_attributes_.LockDevice( | |
| 105 kTestUser, | |
| 106 DEVICE_MODE_ENTERPRISE, | |
| 107 kTestDeviceId)); | |
| 108 EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser()); | |
| 109 } | |
| 110 | |
| 111 TEST_F(EnterpriseInstallAttributesTest, GetDeviceId) { | |
| 112 install_attributes_.ReadCacheFile(GetTempPath()); | |
| 113 EXPECT_EQ(std::string(), install_attributes_.GetDeviceId()); | |
| 114 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | |
| 115 install_attributes_.LockDevice( | |
| 116 kTestUser, | |
| 117 DEVICE_MODE_ENTERPRISE, | |
| 118 kTestDeviceId)); | |
| 119 EXPECT_EQ(kTestDeviceId, install_attributes_.GetDeviceId()); | |
| 120 } | |
| 121 | |
| 122 TEST_F(EnterpriseInstallAttributesTest, GetMode) { | |
| 123 install_attributes_.ReadCacheFile(GetTempPath()); | |
| 124 EXPECT_EQ(DEVICE_MODE_PENDING, install_attributes_.GetMode()); | |
| 125 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | |
| 126 install_attributes_.LockDevice( | |
| 127 kTestUser, | |
| 128 DEVICE_MODE_KIOSK, | |
| 129 kTestDeviceId)); | |
| 130 EXPECT_EQ(DEVICE_MODE_KIOSK, | |
| 131 install_attributes_.GetMode()); | |
| 132 } | |
| 133 | |
| 134 TEST_F(EnterpriseInstallAttributesTest, ConsumerDevice) { | |
| 135 install_attributes_.ReadCacheFile(GetTempPath()); | |
| 136 EXPECT_EQ(DEVICE_MODE_PENDING, install_attributes_.GetMode()); | |
| 137 // Lock the attributes empty. | |
| 138 ASSERT_TRUE(cryptohome_->InstallAttributesFinalize()); | |
| 139 install_attributes_.ReadImmutableAttributes(); | |
| 140 ASSERT_FALSE(cryptohome_->InstallAttributesIsFirstInstall()); | |
| 141 EXPECT_EQ(DEVICE_MODE_CONSUMER, install_attributes_.GetMode()); | |
| 142 } | |
| 143 | |
| 144 TEST_F(EnterpriseInstallAttributesTest, DeviceLockedFromOlderVersion) { | |
| 145 install_attributes_.ReadCacheFile(GetTempPath()); | |
| 146 EXPECT_EQ(DEVICE_MODE_PENDING, install_attributes_.GetMode()); | |
| 147 // Lock the attributes as if it was done from older Chrome version. | |
| 148 ASSERT_TRUE(cryptohome_->InstallAttributesSet(kAttrEnterpriseOwned, "true")); | |
| 149 ASSERT_TRUE(cryptohome_->InstallAttributesSet(kAttrEnterpriseUser, | |
| 150 kTestUser)); | |
| 151 ASSERT_TRUE(cryptohome_->InstallAttributesFinalize()); | |
| 152 install_attributes_.ReadImmutableAttributes(); | |
| 153 ASSERT_FALSE(cryptohome_->InstallAttributesIsFirstInstall()); | |
| 154 EXPECT_EQ(DEVICE_MODE_ENTERPRISE, install_attributes_.GetMode()); | |
| 155 EXPECT_EQ(kTestDomain, install_attributes_.GetDomain()); | |
| 156 EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser()); | |
| 157 EXPECT_EQ("", install_attributes_.GetDeviceId()); | |
| 158 } | |
| 159 | |
| 160 TEST_F(EnterpriseInstallAttributesTest, ReadCacheFile) { | |
| 161 cryptohome::SerializedInstallAttributes install_attrs_proto; | |
| 162 SetAttribute(&install_attrs_proto, kAttrEnterpriseOwned, "true"); | |
| 163 SetAttribute(&install_attrs_proto, kAttrEnterpriseUser, kTestUser); | |
| 164 const std::string blob(install_attrs_proto.SerializeAsString()); | |
| 165 ASSERT_EQ(static_cast<int>(blob.size()), | |
| 166 file_util::WriteFile(GetTempPath(), blob.c_str(), blob.size())); | |
| 167 install_attributes_.ReadCacheFile(GetTempPath()); | |
| 168 EXPECT_EQ(DEVICE_MODE_ENTERPRISE, install_attributes_.GetMode()); | |
| 169 EXPECT_EQ(kTestDomain, install_attributes_.GetDomain()); | |
| 170 EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser()); | |
| 171 EXPECT_EQ("", install_attributes_.GetDeviceId()); | |
| 172 } | |
| 173 | |
| 174 } // namespace policy | |
| OLD | NEW |