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