key_storage_linux_unittest.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2017 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 "components/os_crypt/key_storage_linux.h"
  5. #include "base/bind.h"
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/task/sequenced_task_runner.h"
  8. #include "base/test/test_simple_task_runner.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. // We use a fake to avoid calling a real backend. We'll make calls to it and
  11. // test that the wrapping methods post accordingly.
  12. class FakeKeyStorageLinux : public KeyStorageLinux {
  13. public:
  14. explicit FakeKeyStorageLinux(base::SequencedTaskRunner* task_runner)
  15. : task_runner_(task_runner) {}
  16. FakeKeyStorageLinux(const FakeKeyStorageLinux&) = delete;
  17. FakeKeyStorageLinux& operator=(const FakeKeyStorageLinux&) = delete;
  18. ~FakeKeyStorageLinux() override = default;
  19. protected:
  20. bool Init() override { return true; }
  21. absl::optional<std::string> GetKeyImpl() override {
  22. return std::string("1234");
  23. }
  24. base::SequencedTaskRunner* GetTaskRunner() override { return task_runner_; }
  25. private:
  26. raw_ptr<base::SequencedTaskRunner> task_runner_;
  27. };
  28. class KeyStorageLinuxTest : public testing::Test {
  29. public:
  30. KeyStorageLinuxTest() = default;
  31. KeyStorageLinuxTest(const KeyStorageLinuxTest&) = delete;
  32. KeyStorageLinuxTest& operator=(const KeyStorageLinuxTest&) = delete;
  33. ~KeyStorageLinuxTest() override = default;
  34. };
  35. TEST_F(KeyStorageLinuxTest, SkipPostingToSameTaskRunner) {
  36. scoped_refptr<base::TestSimpleTaskRunner> task_runner(
  37. new base::TestSimpleTaskRunner());
  38. FakeKeyStorageLinux key_storage(task_runner.get());
  39. task_runner->PostTask(
  40. FROM_HERE, base::BindOnce(base::IgnoreResult(&KeyStorageLinux::GetKey),
  41. base::Unretained(&key_storage)));
  42. // This should not deadlock.
  43. task_runner->RunUntilIdle();
  44. }
  45. TEST_F(KeyStorageLinuxTest, IgnoreTaskRunnerIfNull) {
  46. FakeKeyStorageLinux key_storage(nullptr);
  47. // This should not deadlock or crash.
  48. ASSERT_EQ(std::string("1234"), key_storage.GetKey());
  49. }