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.
5 #include "chrome/browser/chromeos/policy/enterprise_install_attributes.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/chromeos/policy/proto/install_attributes.pb.h"
13 #include "chromeos/cryptohome/cryptohome_util.h"
14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "chromeos/dbus/fake_cryptohome_client.h"
16 #include "google_apis/gaia/gaia_auth_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
21 namespace cryptohome_util = chromeos::cryptohome_util;
25 void CopyLockResult(base::RunLoop* loop,
26 EnterpriseInstallAttributes::LockResult* out,
27 EnterpriseInstallAttributes::LockResult result) {
34 static const char kTestUser[] = "test@example.com";
35 static const char kTestUserCanonicalize[] = "UPPER.CASE@example.com";
36 static const char kTestDomain[] = "example.com";
37 static const char kTestDeviceId[] = "133750519";
39 class EnterpriseInstallAttributesTest : public testing::Test {
41 EnterpriseInstallAttributesTest()
42 : fake_cryptohome_client_(new chromeos::FakeCryptohomeClient()),
43 install_attributes_(fake_cryptohome_client_.get()) {
44 fake_cryptohome_client_->Init(NULL /* no dbus::Bus */);
47 virtual void SetUp() OVERRIDE {
48 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
49 chromeos::DBusThreadManager::InitializeWithStub();
52 virtual void TearDown() OVERRIDE {
53 chromeos::DBusThreadManager::Shutdown();
56 base::FilePath GetTempPath() const {
57 return temp_dir_.path().Append("install_attrs_test");
61 cryptohome::SerializedInstallAttributes* install_attrs_proto,
62 const std::string& name,
63 const std::string& value) {
64 cryptohome::SerializedInstallAttributes::Attribute* attribute;
65 attribute = install_attrs_proto->add_attributes();
66 attribute->set_name(name);
67 attribute->set_value(value);
70 base::MessageLoopForUI message_loop_;
71 base::ScopedTempDir temp_dir_;
72 scoped_ptr<chromeos::FakeCryptohomeClient> fake_cryptohome_client_;
73 EnterpriseInstallAttributes install_attributes_;
75 EnterpriseInstallAttributes::LockResult LockDeviceAndWaitForResult(
76 const std::string& user,
77 DeviceMode device_mode,
78 const std::string& device_id) {
80 EnterpriseInstallAttributes::LockResult result;
81 install_attributes_.LockDevice(user, device_mode, device_id,
82 base::Bind(&CopyLockResult, &loop, &result));
88 TEST_F(EnterpriseInstallAttributesTest, Lock) {
89 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
90 LockDeviceAndWaitForResult(
92 DEVICE_MODE_ENTERPRISE,
95 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
96 LockDeviceAndWaitForResult(
98 DEVICE_MODE_ENTERPRISE,
100 // Another user from the same domain should also succeed.
101 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
102 LockDeviceAndWaitForResult(
104 DEVICE_MODE_ENTERPRISE,
106 // But another domain should fail.
107 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_WRONG_USER,
108 LockDeviceAndWaitForResult(
109 "test@bluebears.com",
110 DEVICE_MODE_ENTERPRISE,
114 TEST_F(EnterpriseInstallAttributesTest, LockCanonicalize) {
115 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
116 LockDeviceAndWaitForResult(
117 kTestUserCanonicalize,
118 DEVICE_MODE_ENTERPRISE,
120 EXPECT_EQ(gaia::CanonicalizeEmail(kTestUserCanonicalize),
121 install_attributes_.GetRegistrationUser());
124 TEST_F(EnterpriseInstallAttributesTest, IsEnterpriseDevice) {
125 install_attributes_.ReadCacheFile(GetTempPath());
126 EXPECT_FALSE(install_attributes_.IsEnterpriseDevice());
127 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
128 LockDeviceAndWaitForResult(
130 DEVICE_MODE_ENTERPRISE,
132 EXPECT_TRUE(install_attributes_.IsEnterpriseDevice());
135 TEST_F(EnterpriseInstallAttributesTest, GetDomain) {
136 install_attributes_.ReadCacheFile(GetTempPath());
137 EXPECT_EQ(std::string(), install_attributes_.GetDomain());
138 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
139 LockDeviceAndWaitForResult(
141 DEVICE_MODE_ENTERPRISE,
143 EXPECT_EQ(kTestDomain, install_attributes_.GetDomain());
146 TEST_F(EnterpriseInstallAttributesTest, GetRegistrationUser) {
147 install_attributes_.ReadCacheFile(GetTempPath());
148 EXPECT_EQ(std::string(), install_attributes_.GetRegistrationUser());
149 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
150 LockDeviceAndWaitForResult(
152 DEVICE_MODE_ENTERPRISE,
154 EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser());
157 TEST_F(EnterpriseInstallAttributesTest, GetDeviceId) {
158 install_attributes_.ReadCacheFile(GetTempPath());
159 EXPECT_EQ(std::string(), install_attributes_.GetDeviceId());
160 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
161 LockDeviceAndWaitForResult(
163 DEVICE_MODE_ENTERPRISE,
165 EXPECT_EQ(kTestDeviceId, install_attributes_.GetDeviceId());
168 TEST_F(EnterpriseInstallAttributesTest, GetMode) {
169 install_attributes_.ReadCacheFile(GetTempPath());
170 EXPECT_EQ(DEVICE_MODE_PENDING, install_attributes_.GetMode());
171 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
172 LockDeviceAndWaitForResult(
174 DEVICE_MODE_RETAIL_KIOSK,
176 EXPECT_EQ(DEVICE_MODE_RETAIL_KIOSK,
177 install_attributes_.GetMode());
180 TEST_F(EnterpriseInstallAttributesTest, ConsumerDevice) {
181 install_attributes_.ReadCacheFile(GetTempPath());
182 EXPECT_EQ(DEVICE_MODE_PENDING, install_attributes_.GetMode());
183 // Lock the attributes empty.
184 ASSERT_TRUE(cryptohome_util::InstallAttributesFinalize());
186 install_attributes_.ReadImmutableAttributes(base::Bind(loop.QuitClosure()));
189 ASSERT_FALSE(cryptohome_util::InstallAttributesIsFirstInstall());
190 EXPECT_EQ(DEVICE_MODE_CONSUMER, install_attributes_.GetMode());
193 TEST_F(EnterpriseInstallAttributesTest, ConsumerKioskDevice) {
194 install_attributes_.ReadCacheFile(GetTempPath());
195 EXPECT_EQ(DEVICE_MODE_PENDING, install_attributes_.GetMode());
196 // Lock the attributes for consumer kiosk.
197 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
198 LockDeviceAndWaitForResult(
200 DEVICE_MODE_CONSUMER_KIOSK,
203 ASSERT_FALSE(cryptohome_util::InstallAttributesIsFirstInstall());
204 EXPECT_EQ(DEVICE_MODE_CONSUMER_KIOSK, install_attributes_.GetMode());
205 ASSERT_TRUE(install_attributes_.IsConsumerKioskDevice());
208 TEST_F(EnterpriseInstallAttributesTest, DeviceLockedFromOlderVersion) {
209 install_attributes_.ReadCacheFile(GetTempPath());
210 EXPECT_EQ(DEVICE_MODE_PENDING, install_attributes_.GetMode());
211 // Lock the attributes as if it was done from older Chrome version.
212 ASSERT_TRUE(cryptohome_util::InstallAttributesSet(
213 EnterpriseInstallAttributes::kAttrEnterpriseOwned, "true"));
214 ASSERT_TRUE(cryptohome_util::InstallAttributesSet(
215 EnterpriseInstallAttributes::kAttrEnterpriseUser, kTestUser));
216 ASSERT_TRUE(cryptohome_util::InstallAttributesFinalize());
218 install_attributes_.ReadImmutableAttributes(base::Bind(loop.QuitClosure()));
221 ASSERT_FALSE(cryptohome_util::InstallAttributesIsFirstInstall());
222 EXPECT_EQ(DEVICE_MODE_ENTERPRISE, install_attributes_.GetMode());
223 EXPECT_EQ(kTestDomain, install_attributes_.GetDomain());
224 EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser());
225 EXPECT_EQ("", install_attributes_.GetDeviceId());
228 TEST_F(EnterpriseInstallAttributesTest, ReadCacheFile) {
229 cryptohome::SerializedInstallAttributes install_attrs_proto;
230 SetAttribute(&install_attrs_proto,
231 EnterpriseInstallAttributes::kAttrEnterpriseOwned, "true");
232 SetAttribute(&install_attrs_proto,
233 EnterpriseInstallAttributes::kAttrEnterpriseUser, kTestUser);
234 const std::string blob(install_attrs_proto.SerializeAsString());
235 ASSERT_EQ(static_cast<int>(blob.size()),
236 file_util::WriteFile(GetTempPath(), blob.c_str(), blob.size()));
237 install_attributes_.ReadCacheFile(GetTempPath());
238 EXPECT_EQ(DEVICE_MODE_ENTERPRISE, install_attributes_.GetMode());
239 EXPECT_EQ(kTestDomain, install_attributes_.GetDomain());
240 EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser());
241 EXPECT_EQ("", install_attributes_.GetDeviceId());
244 TEST_F(EnterpriseInstallAttributesTest, ReadCacheFileForConsumerKiosk) {
245 cryptohome::SerializedInstallAttributes install_attrs_proto;
246 SetAttribute(&install_attrs_proto,
247 EnterpriseInstallAttributes::kAttrConsumerKioskEnabled, "true");
248 const std::string blob(install_attrs_proto.SerializeAsString());
249 ASSERT_EQ(static_cast<int>(blob.size()),
250 file_util::WriteFile(GetTempPath(), blob.c_str(), blob.size()));
251 install_attributes_.ReadCacheFile(GetTempPath());
252 EXPECT_EQ(DEVICE_MODE_CONSUMER_KIOSK, install_attributes_.GetMode());
253 EXPECT_EQ("", install_attributes_.GetDomain());
254 EXPECT_EQ("", install_attributes_.GetRegistrationUser());
255 EXPECT_EQ("", install_attributes_.GetDeviceId());
258 } // namespace policy