| 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 cryptohome::Identification& cryptohome_id, |
| 162 const Pkcs11GetTpmTokenInfoCallback& callback) override { | 163 const Pkcs11GetTpmTokenInfoCallback& callback) override { |
| 163 ASSERT_FALSE(user_id_.empty()); | 164 ASSERT_FALSE(cryptohome_id.id().empty()); |
| 164 ASSERT_EQ(user_id_, user_id); | 165 ASSERT_EQ(account_id_, cryptohome::GetAccountId(cryptohome_id)); |
| 165 | 166 |
| 166 HandleGetTpmTokenInfo(callback); | 167 HandleGetTpmTokenInfo(callback); |
| 167 } | 168 } |
| 168 | 169 |
| 169 // Handles Pkcs11GetTpmTokenInfo calls (both for system and user token). The | 170 // Handles Pkcs11GetTpmTokenInfo calls (both for system and user token). The |
| 170 // CryptohomeClient method overrides should make sure that |user_id_| is | 171 // CryptohomeClient method overrides should make sure that |account_id_| is |
| 171 // properly set before calling this. | 172 // properly set before calling this. |
| 172 void HandleGetTpmTokenInfo(const Pkcs11GetTpmTokenInfoCallback& callback) { | 173 void HandleGetTpmTokenInfo(const Pkcs11GetTpmTokenInfoCallback& callback) { |
| 173 ASSERT_TRUE(tpm_is_enabled_succeeded_); | 174 ASSERT_TRUE(tpm_is_enabled_succeeded_); |
| 174 ASSERT_FALSE(get_tpm_token_info_succeeded_); | 175 ASSERT_FALSE(get_tpm_token_info_succeeded_); |
| 175 ASSERT_TRUE(pending_get_tpm_token_info_callback_.is_null()); | 176 ASSERT_TRUE(pending_get_tpm_token_info_callback_.is_null()); |
| 176 | 177 |
| 177 if (get_tpm_token_info_failure_count_ > 0) { | 178 if (get_tpm_token_info_failure_count_ > 0) { |
| 178 --get_tpm_token_info_failure_count_; | 179 --get_tpm_token_info_failure_count_; |
| 179 base::ThreadTaskRunnerHandle::Get()->PostTask( | 180 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 180 FROM_HERE, | 181 FROM_HERE, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 211 // Called synchronously for convenience (to avoid using extra RunLoop in | 212 // Called synchronously for convenience (to avoid using extra RunLoop in |
| 212 // tests). Unlike with other Cryptohome callbacks, TPMTokenInfoGetter does | 213 // tests). Unlike with other Cryptohome callbacks, TPMTokenInfoGetter does |
| 213 // not rely on this callback being called asynchronously. | 214 // not rely on this callback being called asynchronously. |
| 214 pending_get_tpm_token_info_callback_.Run( | 215 pending_get_tpm_token_info_callback_.Run( |
| 215 chromeos::DBUS_METHOD_CALL_SUCCESS, | 216 chromeos::DBUS_METHOD_CALL_SUCCESS, |
| 216 tpm_token_info_.name, | 217 tpm_token_info_.name, |
| 217 tpm_token_info_.pin, | 218 tpm_token_info_.pin, |
| 218 tpm_token_info_.slot_id); | 219 tpm_token_info_.slot_id); |
| 219 } | 220 } |
| 220 | 221 |
| 221 std::string user_id_; | 222 AccountId account_id_; |
| 222 bool tpm_is_enabled_; | 223 bool tpm_is_enabled_; |
| 223 int tpm_is_enabled_failure_count_; | 224 int tpm_is_enabled_failure_count_; |
| 224 bool tpm_is_enabled_succeeded_; | 225 bool tpm_is_enabled_succeeded_; |
| 225 int get_tpm_token_info_failure_count_; | 226 int get_tpm_token_info_failure_count_; |
| 226 int get_tpm_token_info_not_set_count_; | 227 int get_tpm_token_info_not_set_count_; |
| 227 bool get_tpm_token_info_succeeded_; | 228 bool get_tpm_token_info_succeeded_; |
| 228 Pkcs11GetTpmTokenInfoCallback pending_get_tpm_token_info_callback_; | 229 Pkcs11GetTpmTokenInfoCallback pending_get_tpm_token_info_callback_; |
| 229 TestTPMTokenInfo tpm_token_info_; | 230 TestTPMTokenInfo tpm_token_info_; |
| 230 | 231 |
| 231 DISALLOW_COPY_AND_ASSIGN(TestCryptohomeClient); | 232 DISALLOW_COPY_AND_ASSIGN(TestCryptohomeClient); |
| 232 }; | 233 }; |
| 233 | 234 |
| 234 class SystemTPMTokenInfoGetterTest : public testing::Test { | 235 class SystemTPMTokenInfoGetterTest : public testing::Test { |
| 235 public: | 236 public: |
| 236 SystemTPMTokenInfoGetterTest() {} | 237 SystemTPMTokenInfoGetterTest() {} |
| 237 ~SystemTPMTokenInfoGetterTest() override {} | 238 ~SystemTPMTokenInfoGetterTest() override {} |
| 238 | 239 |
| 239 void SetUp() override { | 240 void SetUp() override { |
| 240 cryptohome_client_.reset(new TestCryptohomeClient(std::string())); | 241 cryptohome_client_.reset(new TestCryptohomeClient(EmptyAccountId())); |
| 241 tpm_token_info_getter_ = | 242 tpm_token_info_getter_ = |
| 242 chromeos::TPMTokenInfoGetter::CreateForSystemToken( | 243 chromeos::TPMTokenInfoGetter::CreateForSystemToken( |
| 243 cryptohome_client_.get(), | 244 cryptohome_client_.get(), |
| 244 scoped_refptr<base::TaskRunner>(new FakeTaskRunner(&delays_))); | 245 scoped_refptr<base::TaskRunner>(new FakeTaskRunner(&delays_))); |
| 245 } | 246 } |
| 246 | 247 |
| 247 protected: | 248 protected: |
| 248 scoped_ptr<TestCryptohomeClient> cryptohome_client_; | 249 scoped_ptr<TestCryptohomeClient> cryptohome_client_; |
| 249 scoped_ptr<chromeos::TPMTokenInfoGetter> tpm_token_info_getter_; | 250 scoped_ptr<chromeos::TPMTokenInfoGetter> tpm_token_info_getter_; |
| 250 | 251 |
| 251 std::vector<int64_t> delays_; | 252 std::vector<int64_t> delays_; |
| 252 | 253 |
| 253 private: | 254 private: |
| 254 base::MessageLoop message_loop_; | 255 base::MessageLoop message_loop_; |
| 255 | 256 |
| 256 DISALLOW_COPY_AND_ASSIGN(SystemTPMTokenInfoGetterTest); | 257 DISALLOW_COPY_AND_ASSIGN(SystemTPMTokenInfoGetterTest); |
| 257 }; | 258 }; |
| 258 | 259 |
| 259 class UserTPMTokenInfoGetterTest : public testing::Test { | 260 class UserTPMTokenInfoGetterTest : public testing::Test { |
| 260 public: | 261 public: |
| 261 UserTPMTokenInfoGetterTest() : user_id_("user") {} | 262 UserTPMTokenInfoGetterTest() |
| 263 : account_id_(AccountId::FromUserEmail("user")) {} |
| 262 ~UserTPMTokenInfoGetterTest() override {} | 264 ~UserTPMTokenInfoGetterTest() override {} |
| 263 | 265 |
| 264 void SetUp() override { | 266 void SetUp() override { |
| 265 cryptohome_client_.reset(new TestCryptohomeClient(user_id_)); | 267 cryptohome_client_.reset(new TestCryptohomeClient(account_id_)); |
| 266 tpm_token_info_getter_ = | 268 tpm_token_info_getter_ = chromeos::TPMTokenInfoGetter::CreateForUserToken( |
| 267 chromeos::TPMTokenInfoGetter::CreateForUserToken( | 269 account_id_, cryptohome_client_.get(), |
| 268 user_id_, | 270 scoped_refptr<base::TaskRunner>(new FakeTaskRunner(&delays_))); |
| 269 cryptohome_client_.get(), | |
| 270 scoped_refptr<base::TaskRunner>(new FakeTaskRunner(&delays_))); | |
| 271 } | 271 } |
| 272 | 272 |
| 273 protected: | 273 protected: |
| 274 scoped_ptr<TestCryptohomeClient> cryptohome_client_; | 274 scoped_ptr<TestCryptohomeClient> cryptohome_client_; |
| 275 scoped_ptr<chromeos::TPMTokenInfoGetter> tpm_token_info_getter_; | 275 scoped_ptr<chromeos::TPMTokenInfoGetter> tpm_token_info_getter_; |
| 276 | 276 |
| 277 std::string user_id_; | 277 const AccountId account_id_; |
| 278 std::vector<int64_t> delays_; | 278 std::vector<int64_t> delays_; |
| 279 | 279 |
| 280 private: | 280 private: |
| 281 base::MessageLoop message_loop_; | 281 base::MessageLoop message_loop_; |
| 282 | 282 |
| 283 DISALLOW_COPY_AND_ASSIGN(UserTPMTokenInfoGetterTest); | 283 DISALLOW_COPY_AND_ASSIGN(UserTPMTokenInfoGetterTest); |
| 284 }; | 284 }; |
| 285 | 285 |
| 286 TEST_F(SystemTPMTokenInfoGetterTest, BasicFlow) { | 286 TEST_F(SystemTPMTokenInfoGetterTest, BasicFlow) { |
| 287 TestTPMTokenInfo reported_info; | 287 TestTPMTokenInfo reported_info; |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 EXPECT_EQ("2222", reported_info.pin); | 514 EXPECT_EQ("2222", reported_info.pin); |
| 515 EXPECT_EQ(1, reported_info.slot_id); | 515 EXPECT_EQ(1, reported_info.slot_id); |
| 516 | 516 |
| 517 const int64_t kExpectedDelays[] = {100}; | 517 const int64_t kExpectedDelays[] = {100}; |
| 518 EXPECT_EQ(std::vector<int64_t>(kExpectedDelays, | 518 EXPECT_EQ(std::vector<int64_t>(kExpectedDelays, |
| 519 kExpectedDelays + arraysize(kExpectedDelays)), | 519 kExpectedDelays + arraysize(kExpectedDelays)), |
| 520 delays_); | 520 delays_); |
| 521 } | 521 } |
| 522 | 522 |
| 523 } // namespace | 523 } // namespace |
| OLD | NEW |