Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / policy / enterprise_install_attributes_unittest.cc
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/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"
18
19 namespace policy {
20
21 namespace cryptohome_util = chromeos::cryptohome_util;
22
23 namespace {
24
25 void CopyLockResult(base::RunLoop* loop,
26                     EnterpriseInstallAttributes::LockResult* out,
27                     EnterpriseInstallAttributes::LockResult result) {
28   *out = result;
29   loop->Quit();
30 }
31
32 }  // namespace
33
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";
38
39 class 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
88 TEST_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
114 TEST_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
124 TEST_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
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(
140                 kTestUser,
141                 DEVICE_MODE_ENTERPRISE,
142                 kTestDeviceId));
143   EXPECT_EQ(kTestDomain, install_attributes_.GetDomain());
144 }
145
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(
151                 kTestUser,
152                 DEVICE_MODE_ENTERPRISE,
153                 kTestDeviceId));
154   EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser());
155 }
156
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(
162                 kTestUser,
163                 DEVICE_MODE_ENTERPRISE,
164                 kTestDeviceId));
165   EXPECT_EQ(kTestDeviceId, install_attributes_.GetDeviceId());
166 }
167
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(
173                 kTestUser,
174                 DEVICE_MODE_RETAIL_KIOSK,
175                 kTestDeviceId));
176   EXPECT_EQ(DEVICE_MODE_RETAIL_KIOSK,
177             install_attributes_.GetMode());
178 }
179
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());
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
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(
199                 std::string(),
200                 DEVICE_MODE_CONSUMER_KIOSK_AUTOLAUNCH,
201                 std::string()));
202
203   ASSERT_FALSE(cryptohome_util::InstallAttributesIsFirstInstall());
204   EXPECT_EQ(DEVICE_MODE_CONSUMER_KIOSK_AUTOLAUNCH,
205             install_attributes_.GetMode());
206   ASSERT_TRUE(install_attributes_.IsConsumerKioskDeviceWithAutoLaunch());
207 }
208
209 TEST_F(EnterpriseInstallAttributesTest, DeviceLockedFromOlderVersion) {
210   install_attributes_.ReadCacheFile(GetTempPath());
211   EXPECT_EQ(DEVICE_MODE_PENDING, install_attributes_.GetMode());
212   // Lock the attributes as if it was done from older Chrome version.
213   ASSERT_TRUE(cryptohome_util::InstallAttributesSet(
214       EnterpriseInstallAttributes::kAttrEnterpriseOwned, "true"));
215   ASSERT_TRUE(cryptohome_util::InstallAttributesSet(
216       EnterpriseInstallAttributes::kAttrEnterpriseUser, kTestUser));
217   ASSERT_TRUE(cryptohome_util::InstallAttributesFinalize());
218   base::RunLoop loop;
219   install_attributes_.ReadImmutableAttributes(base::Bind(loop.QuitClosure()));
220   loop.Run();
221
222   ASSERT_FALSE(cryptohome_util::InstallAttributesIsFirstInstall());
223   EXPECT_EQ(DEVICE_MODE_ENTERPRISE, install_attributes_.GetMode());
224   EXPECT_EQ(kTestDomain, install_attributes_.GetDomain());
225   EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser());
226   EXPECT_EQ("", install_attributes_.GetDeviceId());
227 }
228
229 TEST_F(EnterpriseInstallAttributesTest, ReadCacheFile) {
230   cryptohome::SerializedInstallAttributes install_attrs_proto;
231   SetAttribute(&install_attrs_proto,
232                EnterpriseInstallAttributes::kAttrEnterpriseOwned, "true");
233   SetAttribute(&install_attrs_proto,
234                EnterpriseInstallAttributes::kAttrEnterpriseUser, kTestUser);
235   const std::string blob(install_attrs_proto.SerializeAsString());
236   ASSERT_EQ(static_cast<int>(blob.size()),
237             file_util::WriteFile(GetTempPath(), blob.c_str(), blob.size()));
238   install_attributes_.ReadCacheFile(GetTempPath());
239   EXPECT_EQ(DEVICE_MODE_ENTERPRISE, install_attributes_.GetMode());
240   EXPECT_EQ(kTestDomain, install_attributes_.GetDomain());
241   EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser());
242   EXPECT_EQ("", install_attributes_.GetDeviceId());
243 }
244
245 TEST_F(EnterpriseInstallAttributesTest, ReadCacheFileForConsumerKiosk) {
246   cryptohome::SerializedInstallAttributes install_attrs_proto;
247   SetAttribute(&install_attrs_proto,
248                EnterpriseInstallAttributes::kAttrConsumerKioskEnabled, "true");
249   const std::string blob(install_attrs_proto.SerializeAsString());
250   ASSERT_EQ(static_cast<int>(blob.size()),
251             file_util::WriteFile(GetTempPath(), blob.c_str(), blob.size()));
252   install_attributes_.ReadCacheFile(GetTempPath());
253   EXPECT_EQ(DEVICE_MODE_CONSUMER_KIOSK_AUTOLAUNCH,
254             install_attributes_.GetMode());
255   EXPECT_EQ("", install_attributes_.GetDomain());
256   EXPECT_EQ("", install_attributes_.GetRegistrationUser());
257   EXPECT_EQ("", install_attributes_.GetDeviceId());
258 }
259
260 }  // namespace policy