prepare_tpm_unittest.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2019 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. #include "ash/components/tpm/prepare_tpm.h"
  5. #include "base/run_loop.h"
  6. #include "base/test/task_environment.h"
  7. #include "chromeos/dbus/tpm_manager/tpm_manager.pb.h"
  8. #include "chromeos/dbus/tpm_manager/tpm_manager_client.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace ash {
  11. namespace {
  12. class PrepareTpmTest : public ::testing::Test {
  13. public:
  14. PrepareTpmTest() { TpmManagerClient::InitializeFake(); }
  15. ~PrepareTpmTest() override { TpmManagerClient::Shutdown(); }
  16. private:
  17. base::test::SingleThreadTaskEnvironment task_environment;
  18. };
  19. } // namespace
  20. // Tests if the password is getting cleared when TPM is owned.
  21. TEST_F(PrepareTpmTest, PrepareTpmOwned) {
  22. TpmManagerClient::Get()
  23. ->GetTestInterface()
  24. ->mutable_nonsensitive_status_reply()
  25. ->set_is_owned(true);
  26. base::RunLoop run_loop;
  27. auto on_finished = base::BindOnce(
  28. [](base::RunLoop* run_loop) { run_loop->Quit(); }, &run_loop);
  29. PrepareTpm(std::move(on_finished));
  30. run_loop.Run();
  31. EXPECT_EQ(TpmManagerClient::Get()
  32. ->GetTestInterface()
  33. ->clear_stored_owner_password_count(),
  34. 1);
  35. }
  36. // Tests if the ownership process is triggered if TPM is not owned yet.
  37. TEST_F(PrepareTpmTest, PrepareTpmNotOwned) {
  38. TpmManagerClient::Get()
  39. ->GetTestInterface()
  40. ->mutable_nonsensitive_status_reply()
  41. ->set_is_owned(false);
  42. base::RunLoop run_loop;
  43. auto on_finished = base::BindOnce(
  44. [](base::RunLoop* run_loop) { run_loop->Quit(); }, &run_loop);
  45. PrepareTpm(std::move(on_finished));
  46. run_loop.Run();
  47. EXPECT_EQ(TpmManagerClient::Get()->GetTestInterface()->take_ownership_count(),
  48. 1);
  49. }
  50. // Tests the program flow doesn't fall through and execute any unexpected
  51. // follow-up action if tpm manager reports error.
  52. TEST_F(PrepareTpmTest, PrepareTpmFailedToGetStatus) {
  53. TpmManagerClient::Get()
  54. ->GetTestInterface()
  55. ->mutable_nonsensitive_status_reply()
  56. ->set_status(::tpm_manager::STATUS_DBUS_ERROR);
  57. base::RunLoop run_loop;
  58. auto on_finished = base::BindOnce(
  59. [](base::RunLoop* run_loop) { run_loop->Quit(); }, &run_loop);
  60. PrepareTpm(std::move(on_finished));
  61. run_loop.Run();
  62. EXPECT_EQ(TpmManagerClient::Get()
  63. ->GetTestInterface()
  64. ->clear_stored_owner_password_count(),
  65. 0);
  66. EXPECT_EQ(TpmManagerClient::Get()->GetTestInterface()->take_ownership_count(),
  67. 0);
  68. }
  69. } // namespace ash