OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/task_runner.h" | 16 #include "base/task_runner.h" |
17 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| 18 #include "chromeos/cryptohome/cryptohome_parameters.h" |
18 #include "chromeos/dbus/fake_cryptohome_client.h" | 19 #include "chromeos/dbus/fake_cryptohome_client.h" |
| 20 #include "chromeos/login/auth/utils.h" |
19 #include "chromeos/tpm/tpm_token_info_getter.h" | 21 #include "chromeos/tpm/tpm_token_info_getter.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
21 | 23 |
22 namespace { | 24 namespace { |
23 | 25 |
24 // The struct holding information returned by TPMTokenInfoGetter::Start | 26 // The struct holding information returned by TPMTokenInfoGetter::Start |
25 // callback. | 27 // callback. |
26 struct TestTPMTokenInfo { | 28 struct TestTPMTokenInfo { |
27 TestTPMTokenInfo() : enabled(false), slot_id(-2) {} | 29 TestTPMTokenInfo() : enabled(false), slot_id(-2) {} |
28 | 30 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 DISALLOW_COPY_AND_ASSIGN(FakeTaskRunner); | 79 DISALLOW_COPY_AND_ASSIGN(FakeTaskRunner); |
78 }; | 80 }; |
79 | 81 |
80 // Implementation of CryptohomeClient used in these tests. Note that | 82 // Implementation of CryptohomeClient used in these tests. Note that |
81 // TestCryptohomeClient implements FakeCryptohomeClient purely for convenience | 83 // TestCryptohomeClient implements FakeCryptohomeClient purely for convenience |
82 // of not having to implement whole CryptohomeClient interface. | 84 // of not having to implement whole CryptohomeClient interface. |
83 // TestCryptohomeClient overrides all CryptohomeClient methods used in | 85 // TestCryptohomeClient overrides all CryptohomeClient methods used in |
84 // TPMTokenInfoGetter tests. | 86 // TPMTokenInfoGetter tests. |
85 class TestCryptohomeClient : public chromeos::FakeCryptohomeClient { | 87 class TestCryptohomeClient : public chromeos::FakeCryptohomeClient { |
86 public: | 88 public: |
87 // |user_id|: The user associated with the TPMTokenInfoGetter that will be | 89 // |account_id|: The user associated with the TPMTokenInfoGetter that will be |
88 // using the TestCryptohomeClient. Should be empty for system token. | 90 // using the TestCryptohomeClient. Should be empty for system token. |
89 explicit TestCryptohomeClient(const std::string& user_id) | 91 explicit TestCryptohomeClient(const AccountId& account_id) |
90 : user_id_(user_id), | 92 : account_id_(account_id), |
91 tpm_is_enabled_(true), | 93 tpm_is_enabled_(true), |
92 tpm_is_enabled_failure_count_(0), | 94 tpm_is_enabled_failure_count_(0), |
93 tpm_is_enabled_succeeded_(false), | 95 tpm_is_enabled_succeeded_(false), |
94 get_tpm_token_info_failure_count_(0), | 96 get_tpm_token_info_failure_count_(0), |
95 get_tpm_token_info_not_set_count_(0), | 97 get_tpm_token_info_not_set_count_(0), |
96 get_tpm_token_info_succeeded_(false) { | 98 get_tpm_token_info_succeeded_(false) {} |
97 } | |
98 | 99 |
99 ~TestCryptohomeClient() override {} | 100 ~TestCryptohomeClient() override {} |
100 | 101 |
101 void set_tpm_is_enabled(bool value) { | 102 void set_tpm_is_enabled(bool value) { |
102 tpm_is_enabled_ = value; | 103 tpm_is_enabled_ = value; |
103 } | 104 } |
104 | 105 |
105 void set_tpm_is_enabled_failure_count(int value) { | 106 void set_tpm_is_enabled_failure_count(int value) { |
106 ASSERT_GT(value, 0); | 107 ASSERT_GT(value, 0); |
107 tpm_is_enabled_failure_count_ = value; | 108 tpm_is_enabled_failure_count_ = value; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 tpm_is_enabled_succeeded_ = true; | 146 tpm_is_enabled_succeeded_ = true; |
146 base::ThreadTaskRunnerHandle::Get()->PostTask( | 147 base::ThreadTaskRunnerHandle::Get()->PostTask( |
147 FROM_HERE, | 148 FROM_HERE, |
148 base::Bind(callback, | 149 base::Bind(callback, |
149 chromeos::DBUS_METHOD_CALL_SUCCESS, tpm_is_enabled_)); | 150 chromeos::DBUS_METHOD_CALL_SUCCESS, tpm_is_enabled_)); |
150 } | 151 } |
151 } | 152 } |
152 | 153 |
153 void Pkcs11GetTpmTokenInfo( | 154 void Pkcs11GetTpmTokenInfo( |
154 const Pkcs11GetTpmTokenInfoCallback& callback) override { | 155 const Pkcs11GetTpmTokenInfoCallback& callback) override { |
155 ASSERT_TRUE(user_id_.empty()); | 156 ASSERT_TRUE(account_id_.empty()); |
156 | 157 |
157 HandleGetTpmTokenInfo(callback); | 158 HandleGetTpmTokenInfo(callback); |
158 } | 159 } |
159 | 160 |
160 void Pkcs11GetTpmTokenInfoForUser( | 161 void Pkcs11GetTpmTokenInfoForUser( |
161 const std::string& user_id, | 162 const std::string& user_id, |
162 const Pkcs11GetTpmTokenInfoCallback& callback) override { | 163 const Pkcs11GetTpmTokenInfoCallback& callback) override { |
163 ASSERT_FALSE(user_id_.empty()); | 164 ASSERT_FALSE(user_id.empty()); |
164 ASSERT_EQ(user_id_, user_id); | 165 ASSERT_EQ(account_id_, |
| 166 cryptohome::GetAccountId( |
| 167 cryptohome::Identification::FromString(user_id))); |
165 | 168 |
166 HandleGetTpmTokenInfo(callback); | 169 HandleGetTpmTokenInfo(callback); |
167 } | 170 } |
168 | 171 |
169 // Handles Pkcs11GetTpmTokenInfo calls (both for system and user token). The | 172 // Handles Pkcs11GetTpmTokenInfo calls (both for system and user token). The |
170 // CryptohomeClient method overrides should make sure that |user_id_| is | 173 // CryptohomeClient method overrides should make sure that |account_id_| is |
171 // properly set before calling this. | 174 // properly set before calling this. |
172 void HandleGetTpmTokenInfo(const Pkcs11GetTpmTokenInfoCallback& callback) { | 175 void HandleGetTpmTokenInfo(const Pkcs11GetTpmTokenInfoCallback& callback) { |
173 ASSERT_TRUE(tpm_is_enabled_succeeded_); | 176 ASSERT_TRUE(tpm_is_enabled_succeeded_); |
174 ASSERT_FALSE(get_tpm_token_info_succeeded_); | 177 ASSERT_FALSE(get_tpm_token_info_succeeded_); |
175 ASSERT_TRUE(pending_get_tpm_token_info_callback_.is_null()); | 178 ASSERT_TRUE(pending_get_tpm_token_info_callback_.is_null()); |
176 | 179 |
177 if (get_tpm_token_info_failure_count_ > 0) { | 180 if (get_tpm_token_info_failure_count_ > 0) { |
178 --get_tpm_token_info_failure_count_; | 181 --get_tpm_token_info_failure_count_; |
179 base::ThreadTaskRunnerHandle::Get()->PostTask( | 182 base::ThreadTaskRunnerHandle::Get()->PostTask( |
180 FROM_HERE, | 183 FROM_HERE, |
(...skipping 30 matching lines...) Expand all Loading... |
211 // Called synchronously for convenience (to avoid using extra RunLoop in | 214 // Called synchronously for convenience (to avoid using extra RunLoop in |
212 // tests). Unlike with other Cryptohome callbacks, TPMTokenInfoGetter does | 215 // tests). Unlike with other Cryptohome callbacks, TPMTokenInfoGetter does |
213 // not rely on this callback being called asynchronously. | 216 // not rely on this callback being called asynchronously. |
214 pending_get_tpm_token_info_callback_.Run( | 217 pending_get_tpm_token_info_callback_.Run( |
215 chromeos::DBUS_METHOD_CALL_SUCCESS, | 218 chromeos::DBUS_METHOD_CALL_SUCCESS, |
216 tpm_token_info_.name, | 219 tpm_token_info_.name, |
217 tpm_token_info_.pin, | 220 tpm_token_info_.pin, |
218 tpm_token_info_.slot_id); | 221 tpm_token_info_.slot_id); |
219 } | 222 } |
220 | 223 |
221 std::string user_id_; | 224 AccountId account_id_; |
222 bool tpm_is_enabled_; | 225 bool tpm_is_enabled_; |
223 int tpm_is_enabled_failure_count_; | 226 int tpm_is_enabled_failure_count_; |
224 bool tpm_is_enabled_succeeded_; | 227 bool tpm_is_enabled_succeeded_; |
225 int get_tpm_token_info_failure_count_; | 228 int get_tpm_token_info_failure_count_; |
226 int get_tpm_token_info_not_set_count_; | 229 int get_tpm_token_info_not_set_count_; |
227 bool get_tpm_token_info_succeeded_; | 230 bool get_tpm_token_info_succeeded_; |
228 Pkcs11GetTpmTokenInfoCallback pending_get_tpm_token_info_callback_; | 231 Pkcs11GetTpmTokenInfoCallback pending_get_tpm_token_info_callback_; |
229 TestTPMTokenInfo tpm_token_info_; | 232 TestTPMTokenInfo tpm_token_info_; |
230 | 233 |
231 DISALLOW_COPY_AND_ASSIGN(TestCryptohomeClient); | 234 DISALLOW_COPY_AND_ASSIGN(TestCryptohomeClient); |
232 }; | 235 }; |
233 | 236 |
234 class SystemTPMTokenInfoGetterTest : public testing::Test { | 237 class SystemTPMTokenInfoGetterTest : public testing::Test { |
235 public: | 238 public: |
236 SystemTPMTokenInfoGetterTest() {} | 239 SystemTPMTokenInfoGetterTest() {} |
237 ~SystemTPMTokenInfoGetterTest() override {} | 240 ~SystemTPMTokenInfoGetterTest() override {} |
238 | 241 |
239 void SetUp() override { | 242 void SetUp() override { |
240 cryptohome_client_.reset(new TestCryptohomeClient(std::string())); | 243 cryptohome_client_.reset(new TestCryptohomeClient(EmptyAccountId())); |
241 tpm_token_info_getter_ = | 244 tpm_token_info_getter_ = |
242 chromeos::TPMTokenInfoGetter::CreateForSystemToken( | 245 chromeos::TPMTokenInfoGetter::CreateForSystemToken( |
243 cryptohome_client_.get(), | 246 cryptohome_client_.get(), |
244 scoped_refptr<base::TaskRunner>(new FakeTaskRunner(&delays_))); | 247 scoped_refptr<base::TaskRunner>(new FakeTaskRunner(&delays_))); |
245 } | 248 } |
246 | 249 |
247 protected: | 250 protected: |
248 scoped_ptr<TestCryptohomeClient> cryptohome_client_; | 251 scoped_ptr<TestCryptohomeClient> cryptohome_client_; |
249 scoped_ptr<chromeos::TPMTokenInfoGetter> tpm_token_info_getter_; | 252 scoped_ptr<chromeos::TPMTokenInfoGetter> tpm_token_info_getter_; |
250 | 253 |
251 std::vector<int64_t> delays_; | 254 std::vector<int64_t> delays_; |
252 | 255 |
253 private: | 256 private: |
254 base::MessageLoop message_loop_; | 257 base::MessageLoop message_loop_; |
255 | 258 |
256 DISALLOW_COPY_AND_ASSIGN(SystemTPMTokenInfoGetterTest); | 259 DISALLOW_COPY_AND_ASSIGN(SystemTPMTokenInfoGetterTest); |
257 }; | 260 }; |
258 | 261 |
259 class UserTPMTokenInfoGetterTest : public testing::Test { | 262 class UserTPMTokenInfoGetterTest : public testing::Test { |
260 public: | 263 public: |
261 UserTPMTokenInfoGetterTest() : user_id_("user") {} | 264 UserTPMTokenInfoGetterTest() |
| 265 : account_id_(AccountId::FromUserEmail("user")) {} |
262 ~UserTPMTokenInfoGetterTest() override {} | 266 ~UserTPMTokenInfoGetterTest() override {} |
263 | 267 |
264 void SetUp() override { | 268 void SetUp() override { |
265 cryptohome_client_.reset(new TestCryptohomeClient(user_id_)); | 269 cryptohome_client_.reset(new TestCryptohomeClient(account_id_)); |
266 tpm_token_info_getter_ = | 270 tpm_token_info_getter_ = chromeos::TPMTokenInfoGetter::CreateForUserToken( |
267 chromeos::TPMTokenInfoGetter::CreateForUserToken( | 271 account_id_, cryptohome_client_.get(), |
268 user_id_, | 272 scoped_refptr<base::TaskRunner>(new FakeTaskRunner(&delays_))); |
269 cryptohome_client_.get(), | |
270 scoped_refptr<base::TaskRunner>(new FakeTaskRunner(&delays_))); | |
271 } | 273 } |
272 | 274 |
273 protected: | 275 protected: |
274 scoped_ptr<TestCryptohomeClient> cryptohome_client_; | 276 scoped_ptr<TestCryptohomeClient> cryptohome_client_; |
275 scoped_ptr<chromeos::TPMTokenInfoGetter> tpm_token_info_getter_; | 277 scoped_ptr<chromeos::TPMTokenInfoGetter> tpm_token_info_getter_; |
276 | 278 |
277 std::string user_id_; | 279 const AccountId account_id_; |
278 std::vector<int64_t> delays_; | 280 std::vector<int64_t> delays_; |
279 | 281 |
280 private: | 282 private: |
281 base::MessageLoop message_loop_; | 283 base::MessageLoop message_loop_; |
282 | 284 |
283 DISALLOW_COPY_AND_ASSIGN(UserTPMTokenInfoGetterTest); | 285 DISALLOW_COPY_AND_ASSIGN(UserTPMTokenInfoGetterTest); |
284 }; | 286 }; |
285 | 287 |
286 TEST_F(SystemTPMTokenInfoGetterTest, BasicFlow) { | 288 TEST_F(SystemTPMTokenInfoGetterTest, BasicFlow) { |
287 TestTPMTokenInfo reported_info; | 289 TestTPMTokenInfo reported_info; |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 EXPECT_EQ("2222", reported_info.pin); | 516 EXPECT_EQ("2222", reported_info.pin); |
515 EXPECT_EQ(1, reported_info.slot_id); | 517 EXPECT_EQ(1, reported_info.slot_id); |
516 | 518 |
517 const int64_t kExpectedDelays[] = {100}; | 519 const int64_t kExpectedDelays[] = {100}; |
518 EXPECT_EQ(std::vector<int64_t>(kExpectedDelays, | 520 EXPECT_EQ(std::vector<int64_t>(kExpectedDelays, |
519 kExpectedDelays + arraysize(kExpectedDelays)), | 521 kExpectedDelays + arraysize(kExpectedDelays)), |
520 delays_); | 522 delays_); |
521 } | 523 } |
522 | 524 |
523 } // namespace | 525 } // namespace |
OLD | NEW |