enterprise_install_attributes_unittest.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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_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"
18
19namespace policy {
20
21namespace cryptohome_util = chromeos::cryptohome_util;
22
23namespace {
24
25void CopyLockResult(base::RunLoop* loop,
26                    EnterpriseInstallAttributes::LockResult* out,
27                    EnterpriseInstallAttributes::LockResult result) {
28  *out = result;
29  loop->Quit();
30}
31
32}  // namespace
33
34static const char kTestUser[] = "test@example.com";
35static const char kTestUserCanonicalize[] = "UPPER.CASE@example.com";
36static const char kTestDomain[] = "example.com";
37static const char kTestDeviceId[] = "133750519";
38
39class EnterpriseInstallAttributesTest : public testing::Test {
40 protected:
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 */);
45  }
46
47  virtual void SetUp() OVERRIDE {
48    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
49    chromeos::DBusThreadManager::InitializeWithStub();
50  }
51
52  virtual void TearDown() OVERRIDE {
53    chromeos::DBusThreadManager::Shutdown();
54  }
55
56  base::FilePath GetTempPath() const {
57    return temp_dir_.path().Append("install_attrs_test");
58  }
59
60  void SetAttribute(
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);
68  }
69
70  base::MessageLoopForUI message_loop_;
71  base::ScopedTempDir temp_dir_;
72  scoped_ptr<chromeos::FakeCryptohomeClient> fake_cryptohome_client_;
73  EnterpriseInstallAttributes install_attributes_;
74
75  EnterpriseInstallAttributes::LockResult LockDeviceAndWaitForResult(
76      const std::string& user,
77      DeviceMode device_mode,
78      const std::string& device_id) {
79    base::RunLoop loop;
80    EnterpriseInstallAttributes::LockResult result;
81    install_attributes_.LockDevice(user, device_mode, device_id,
82                                   base::Bind(&CopyLockResult, &loop, &result));
83    loop.Run();
84    return result;
85  }
86};
87
88TEST_F(EnterpriseInstallAttributesTest, Lock) {
89  EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
90            LockDeviceAndWaitForResult(
91                kTestUser,
92                DEVICE_MODE_ENTERPRISE,
93                kTestDeviceId));
94
95  EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
96            LockDeviceAndWaitForResult(
97                kTestUser,
98                DEVICE_MODE_ENTERPRISE,
99                kTestDeviceId));
100  // Another user from the same domain should also succeed.
101  EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
102            LockDeviceAndWaitForResult(
103                "test1@example.com",
104                DEVICE_MODE_ENTERPRISE,
105                kTestDeviceId));
106  // But another domain should fail.
107  EXPECT_EQ(EnterpriseInstallAttributes::LOCK_WRONG_USER,
108            LockDeviceAndWaitForResult(
109                "test@bluebears.com",
110                DEVICE_MODE_ENTERPRISE,
111                kTestDeviceId));
112}
113
114TEST_F(EnterpriseInstallAttributesTest, LockCanonicalize) {
115  EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
116            LockDeviceAndWaitForResult(
117                kTestUserCanonicalize,
118                DEVICE_MODE_ENTERPRISE,
119                kTestDeviceId));
120  EXPECT_EQ(gaia::CanonicalizeEmail(kTestUserCanonicalize),
121            install_attributes_.GetRegistrationUser());
122}
123
124TEST_F(EnterpriseInstallAttributesTest, IsEnterpriseDevice) {
125  install_attributes_.ReadCacheFile(GetTempPath());
126  EXPECT_FALSE(install_attributes_.IsEnterpriseDevice());
127  ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
128            LockDeviceAndWaitForResult(
129                kTestUser,
130                DEVICE_MODE_ENTERPRISE,
131                kTestDeviceId));
132  EXPECT_TRUE(install_attributes_.IsEnterpriseDevice());
133}
134
135TEST_F(EnterpriseInstallAttributesTest, GetDomain) {
136  install_attributes_.ReadCacheFile(GetTempPath());
137  EXPECT_EQ(std::string(), install_attributes_.GetDomain());
138  ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
139            LockDeviceAndWaitForResult(
140                kTestUser,
141                DEVICE_MODE_ENTERPRISE,
142                kTestDeviceId));
143  EXPECT_EQ(kTestDomain, install_attributes_.GetDomain());
144}
145
146TEST_F(EnterpriseInstallAttributesTest, GetRegistrationUser) {
147  install_attributes_.ReadCacheFile(GetTempPath());
148  EXPECT_EQ(std::string(), install_attributes_.GetRegistrationUser());
149  ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
150            LockDeviceAndWaitForResult(
151                kTestUser,
152                DEVICE_MODE_ENTERPRISE,
153                kTestDeviceId));
154  EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser());
155}
156
157TEST_F(EnterpriseInstallAttributesTest, GetDeviceId) {
158  install_attributes_.ReadCacheFile(GetTempPath());
159  EXPECT_EQ(std::string(), install_attributes_.GetDeviceId());
160  ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS,
161            LockDeviceAndWaitForResult(
162                kTestUser,
163                DEVICE_MODE_ENTERPRISE,
164                kTestDeviceId));
165  EXPECT_EQ(kTestDeviceId, install_attributes_.GetDeviceId());
166}
167
168TEST_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(
173                kTestUser,
174                DEVICE_MODE_RETAIL_KIOSK,
175                kTestDeviceId));
176  EXPECT_EQ(DEVICE_MODE_RETAIL_KIOSK,
177            install_attributes_.GetMode());
178}
179
180TEST_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());
185  base::RunLoop loop;
186  install_attributes_.ReadImmutableAttributes(base::Bind(loop.QuitClosure()));
187  loop.Run();
188
189  ASSERT_FALSE(cryptohome_util::InstallAttributesIsFirstInstall());
190  EXPECT_EQ(DEVICE_MODE_CONSUMER, install_attributes_.GetMode());
191}
192
193TEST_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(
199                std::string(),
200                DEVICE_MODE_CONSUMER_KIOSK,
201                std::string()));
202
203  ASSERT_FALSE(cryptohome_util::InstallAttributesIsFirstInstall());
204  EXPECT_EQ(DEVICE_MODE_CONSUMER_KIOSK, install_attributes_.GetMode());
205  ASSERT_TRUE(install_attributes_.IsConsumerKioskDevice());
206}
207
208TEST_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());
217  base::RunLoop loop;
218  install_attributes_.ReadImmutableAttributes(base::Bind(loop.QuitClosure()));
219  loop.Run();
220
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());
226}
227
228TEST_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());
242}
243
244TEST_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());
256}
257
258}  // namespace policy
259