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