OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/policy/enterprise_install_attributes.h" | 5 #include "chrome/browser/policy/enterprise_install_attributes.h" |
6 | 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" |
7 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
8 #include "chrome/browser/chromeos/cros/cryptohome_library.h" | 10 #include "chrome/browser/chromeos/cros/cryptohome_library.h" |
| 11 #include "chrome/browser/policy/proto/install_attributes.pb.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
10 | 13 |
11 namespace policy { | 14 namespace policy { |
12 | 15 |
13 static const char kTestUser[] = "test@example.com"; | 16 static const char kTestUser[] = "test@example.com"; |
14 static const char kTestDomain[] = "example.com"; | 17 static const char kTestDomain[] = "example.com"; |
15 static const char kTestDeviceId[] = "133750519"; | 18 static const char kTestDeviceId[] = "133750519"; |
16 | 19 |
17 static const char kAttrEnterpriseOwned[] = "enterprise.owned"; | 20 static const char kAttrEnterpriseOwned[] = "enterprise.owned"; |
18 static const char kAttrEnterpriseUser[] = "enterprise.user"; | 21 static const char kAttrEnterpriseUser[] = "enterprise.user"; |
19 | 22 |
20 class EnterpriseInstallAttributesTest : public testing::Test { | 23 class EnterpriseInstallAttributesTest : public testing::Test { |
21 protected: | 24 protected: |
22 EnterpriseInstallAttributesTest() | 25 EnterpriseInstallAttributesTest() |
23 : cryptohome_(chromeos::CryptohomeLibrary::GetImpl(true)), | 26 : cryptohome_(chromeos::CryptohomeLibrary::GetImpl(true)), |
24 install_attributes_(cryptohome_.get()) {} | 27 install_attributes_(cryptohome_.get()) {} |
25 | 28 |
| 29 virtual void SetUp() OVERRIDE { |
| 30 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 31 } |
| 32 |
| 33 FilePath GetTempPath() const { |
| 34 return temp_dir_.path().Append("install_attrs_test"); |
| 35 } |
| 36 |
| 37 void SetAttribute( |
| 38 cryptohome::SerializedInstallAttributes* install_attrs_proto, |
| 39 const std::string& name, |
| 40 const std::string& value) { |
| 41 cryptohome::SerializedInstallAttributes::Attribute* attribute; |
| 42 attribute = install_attrs_proto->add_attributes(); |
| 43 attribute->set_name(name); |
| 44 attribute->set_value(value); |
| 45 } |
| 46 |
| 47 base::ScopedTempDir temp_dir_; |
26 scoped_ptr<chromeos::CryptohomeLibrary> cryptohome_; | 48 scoped_ptr<chromeos::CryptohomeLibrary> cryptohome_; |
27 EnterpriseInstallAttributes install_attributes_; | 49 EnterpriseInstallAttributes install_attributes_; |
28 }; | 50 }; |
29 | 51 |
30 TEST_F(EnterpriseInstallAttributesTest, Lock) { | 52 TEST_F(EnterpriseInstallAttributesTest, Lock) { |
31 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | 53 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, |
32 install_attributes_.LockDevice( | 54 install_attributes_.LockDevice( |
33 kTestUser, | 55 kTestUser, |
34 DEVICE_MODE_ENTERPRISE, | 56 DEVICE_MODE_ENTERPRISE, |
35 kTestDeviceId)); | 57 kTestDeviceId)); |
(...skipping 11 matching lines...) Expand all Loading... |
47 kTestDeviceId)); | 69 kTestDeviceId)); |
48 // But another domain should fail. | 70 // But another domain should fail. |
49 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_WRONG_USER, | 71 EXPECT_EQ(EnterpriseInstallAttributes::LOCK_WRONG_USER, |
50 install_attributes_.LockDevice( | 72 install_attributes_.LockDevice( |
51 "test@bluebears.com", | 73 "test@bluebears.com", |
52 DEVICE_MODE_ENTERPRISE, | 74 DEVICE_MODE_ENTERPRISE, |
53 kTestDeviceId)); | 75 kTestDeviceId)); |
54 } | 76 } |
55 | 77 |
56 TEST_F(EnterpriseInstallAttributesTest, IsEnterpriseDevice) { | 78 TEST_F(EnterpriseInstallAttributesTest, IsEnterpriseDevice) { |
| 79 install_attributes_.ReadCacheFile(GetTempPath()); |
57 EXPECT_FALSE(install_attributes_.IsEnterpriseDevice()); | 80 EXPECT_FALSE(install_attributes_.IsEnterpriseDevice()); |
58 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | 81 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, |
59 install_attributes_.LockDevice( | 82 install_attributes_.LockDevice( |
60 kTestUser, | 83 kTestUser, |
61 DEVICE_MODE_ENTERPRISE, | 84 DEVICE_MODE_ENTERPRISE, |
62 kTestDeviceId)); | 85 kTestDeviceId)); |
63 EXPECT_TRUE(install_attributes_.IsEnterpriseDevice()); | 86 EXPECT_TRUE(install_attributes_.IsEnterpriseDevice()); |
64 } | 87 } |
65 | 88 |
66 TEST_F(EnterpriseInstallAttributesTest, GetDomain) { | 89 TEST_F(EnterpriseInstallAttributesTest, GetDomain) { |
| 90 install_attributes_.ReadCacheFile(GetTempPath()); |
67 EXPECT_EQ(std::string(), install_attributes_.GetDomain()); | 91 EXPECT_EQ(std::string(), install_attributes_.GetDomain()); |
68 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | 92 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, |
69 install_attributes_.LockDevice( | 93 install_attributes_.LockDevice( |
70 kTestUser, | 94 kTestUser, |
71 DEVICE_MODE_ENTERPRISE, | 95 DEVICE_MODE_ENTERPRISE, |
72 kTestDeviceId)); | 96 kTestDeviceId)); |
73 EXPECT_EQ(kTestDomain, install_attributes_.GetDomain()); | 97 EXPECT_EQ(kTestDomain, install_attributes_.GetDomain()); |
74 } | 98 } |
75 | 99 |
76 TEST_F(EnterpriseInstallAttributesTest, GetRegistrationUser) { | 100 TEST_F(EnterpriseInstallAttributesTest, GetRegistrationUser) { |
| 101 install_attributes_.ReadCacheFile(GetTempPath()); |
77 EXPECT_EQ(std::string(), install_attributes_.GetRegistrationUser()); | 102 EXPECT_EQ(std::string(), install_attributes_.GetRegistrationUser()); |
78 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | 103 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, |
79 install_attributes_.LockDevice( | 104 install_attributes_.LockDevice( |
80 kTestUser, | 105 kTestUser, |
81 DEVICE_MODE_ENTERPRISE, | 106 DEVICE_MODE_ENTERPRISE, |
82 kTestDeviceId)); | 107 kTestDeviceId)); |
83 EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser()); | 108 EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser()); |
84 } | 109 } |
85 | 110 |
86 TEST_F(EnterpriseInstallAttributesTest, GetDeviceId) { | 111 TEST_F(EnterpriseInstallAttributesTest, GetDeviceId) { |
| 112 install_attributes_.ReadCacheFile(GetTempPath()); |
87 EXPECT_EQ(std::string(), install_attributes_.GetDeviceId()); | 113 EXPECT_EQ(std::string(), install_attributes_.GetDeviceId()); |
88 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | 114 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, |
89 install_attributes_.LockDevice( | 115 install_attributes_.LockDevice( |
90 kTestUser, | 116 kTestUser, |
91 DEVICE_MODE_ENTERPRISE, | 117 DEVICE_MODE_ENTERPRISE, |
92 kTestDeviceId)); | 118 kTestDeviceId)); |
93 EXPECT_EQ(kTestDeviceId, install_attributes_.GetDeviceId()); | 119 EXPECT_EQ(kTestDeviceId, install_attributes_.GetDeviceId()); |
94 } | 120 } |
95 | 121 |
96 TEST_F(EnterpriseInstallAttributesTest, GetMode) { | 122 TEST_F(EnterpriseInstallAttributesTest, GetMode) { |
97 EXPECT_EQ(DEVICE_MODE_NOT_SET, | 123 install_attributes_.ReadCacheFile(GetTempPath()); |
98 install_attributes_.GetMode()); | 124 EXPECT_EQ(DEVICE_MODE_PENDING, install_attributes_.GetMode()); |
99 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | 125 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, |
100 install_attributes_.LockDevice( | 126 install_attributes_.LockDevice( |
101 kTestUser, | 127 kTestUser, |
102 DEVICE_MODE_KIOSK, | 128 DEVICE_MODE_KIOSK, |
103 kTestDeviceId)); | 129 kTestDeviceId)); |
104 EXPECT_EQ(DEVICE_MODE_KIOSK, | 130 EXPECT_EQ(DEVICE_MODE_KIOSK, |
105 install_attributes_.GetMode()); | 131 install_attributes_.GetMode()); |
106 } | 132 } |
107 | 133 |
108 TEST_F(EnterpriseInstallAttributesTest, ConsumerDevice) { | 134 TEST_F(EnterpriseInstallAttributesTest, ConsumerDevice) { |
109 EXPECT_EQ(DEVICE_MODE_NOT_SET, | 135 install_attributes_.ReadCacheFile(GetTempPath()); |
110 install_attributes_.GetMode()); | 136 EXPECT_EQ(DEVICE_MODE_PENDING, install_attributes_.GetMode()); |
111 // Lock the attributes empty. | 137 // Lock the attributes empty. |
112 ASSERT_TRUE(cryptohome_->InstallAttributesFinalize()); | 138 ASSERT_TRUE(cryptohome_->InstallAttributesFinalize()); |
| 139 install_attributes_.ReadImmutableAttributes(); |
113 ASSERT_FALSE(cryptohome_->InstallAttributesIsFirstInstall()); | 140 ASSERT_FALSE(cryptohome_->InstallAttributesIsFirstInstall()); |
114 EXPECT_EQ(DEVICE_MODE_CONSUMER, | 141 EXPECT_EQ(DEVICE_MODE_CONSUMER, install_attributes_.GetMode()); |
115 install_attributes_.GetMode()); | |
116 } | 142 } |
117 | 143 |
118 TEST_F(EnterpriseInstallAttributesTest, DeviceLockedFromOlderVersion) { | 144 TEST_F(EnterpriseInstallAttributesTest, DeviceLockedFromOlderVersion) { |
119 EXPECT_EQ(DEVICE_MODE_NOT_SET, | 145 install_attributes_.ReadCacheFile(GetTempPath()); |
120 install_attributes_.GetMode()); | 146 EXPECT_EQ(DEVICE_MODE_PENDING, install_attributes_.GetMode()); |
121 // Lock the attributes as if it was done from older Chrome version. | 147 // Lock the attributes as if it was done from older Chrome version. |
122 ASSERT_TRUE(cryptohome_->InstallAttributesSet(kAttrEnterpriseOwned, "true")); | 148 ASSERT_TRUE(cryptohome_->InstallAttributesSet(kAttrEnterpriseOwned, "true")); |
123 ASSERT_TRUE(cryptohome_->InstallAttributesSet(kAttrEnterpriseUser, | 149 ASSERT_TRUE(cryptohome_->InstallAttributesSet(kAttrEnterpriseUser, |
124 kTestUser)); | 150 kTestUser)); |
125 ASSERT_TRUE(cryptohome_->InstallAttributesFinalize()); | 151 ASSERT_TRUE(cryptohome_->InstallAttributesFinalize()); |
| 152 install_attributes_.ReadImmutableAttributes(); |
126 ASSERT_FALSE(cryptohome_->InstallAttributesIsFirstInstall()); | 153 ASSERT_FALSE(cryptohome_->InstallAttributesIsFirstInstall()); |
127 EXPECT_EQ(DEVICE_MODE_ENTERPRISE, | 154 EXPECT_EQ(DEVICE_MODE_ENTERPRISE, install_attributes_.GetMode()); |
128 install_attributes_.GetMode()); | |
129 EXPECT_EQ(kTestDomain, install_attributes_.GetDomain()); | 155 EXPECT_EQ(kTestDomain, install_attributes_.GetDomain()); |
130 EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser()); | 156 EXPECT_EQ(kTestUser, install_attributes_.GetRegistrationUser()); |
131 EXPECT_EQ("", install_attributes_.GetDeviceId()); | 157 EXPECT_EQ("", install_attributes_.GetDeviceId()); |
| 158 } |
| 159 |
| 160 TEST_F(EnterpriseInstallAttributesTest, ReadCacheFile) { |
| 161 cryptohome::SerializedInstallAttributes install_attrs_proto; |
| 162 SetAttribute(&install_attrs_proto, kAttrEnterpriseOwned, "true"); |
| 163 SetAttribute(&install_attrs_proto, 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()); |
132 } | 172 } |
133 | 173 |
134 } // namespace policy | 174 } // namespace policy |
OLD | NEW |