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