ct_log_list_distributor_unittest.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2021 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 "base/logging.h"
  5. #include "services/network/ct_log_list_distributor.h"
  6. #include "base/bind.h"
  7. #include "base/callback.h"
  8. #include "base/callback_list.h"
  9. #include "base/test/task_environment.h"
  10. #include "crypto/sha2.h"
  11. #include "net/cert/ct_log_verifier.h"
  12. #include "net/cert/multi_log_ct_verifier.h"
  13. #include "net/test/ct_test_util.h"
  14. #include "services/network/public/mojom/ct_log_info.mojom.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. namespace network {
  17. class MockLogVerifier {
  18. public:
  19. MockLogVerifier() = default;
  20. ~MockLogVerifier() = default;
  21. void SetLogs(const std::vector<scoped_refptr<const net::CTLogVerifier>>&
  22. log_verifiers) {
  23. logs_ = log_verifiers;
  24. }
  25. std::vector<scoped_refptr<const net::CTLogVerifier>> GetLogs() {
  26. return logs_;
  27. }
  28. private:
  29. std::vector<scoped_refptr<const net::CTLogVerifier>> logs_;
  30. };
  31. class CtLogListDistributorTest : public ::testing::Test {
  32. public:
  33. void SetUp() override {
  34. subscription_ = distributor_.RegisterLogsListCallback(base::BindRepeating(
  35. &MockLogVerifier::SetLogs, base::Unretained(&verifier_)));
  36. }
  37. void Wait() { task_environment_.RunUntilIdle(); }
  38. protected:
  39. base::test::TaskEnvironment task_environment_{
  40. base::test::TaskEnvironment::MainThreadType::DEFAULT,
  41. base::test::TaskEnvironment::ThreadPoolExecutionMode::QUEUED};
  42. CtLogListDistributor distributor_;
  43. MockLogVerifier verifier_;
  44. base::CallbackListSubscription subscription_;
  45. };
  46. TEST_F(CtLogListDistributorTest, TestOnNewCtConfig) {
  47. const char kLogDescription[] = "somelog";
  48. // Create log list with a single log.
  49. std::vector<network::mojom::CTLogInfoPtr> log_list_mojo;
  50. network::mojom::CTLogInfoPtr log_ptr = network::mojom::CTLogInfo::New();
  51. log_ptr->name = kLogDescription;
  52. log_ptr->public_key = net::ct::GetTestPublicKey();
  53. log_list_mojo.push_back(std::move(log_ptr));
  54. // Pass the log list to the distributor.
  55. distributor_.OnNewCtConfig(log_list_mojo);
  56. // Wait for parsing to finish.
  57. Wait();
  58. // Verifier should have been notified and have the log list.
  59. std::vector<scoped_refptr<const net::CTLogVerifier>> logs =
  60. verifier_.GetLogs();
  61. EXPECT_EQ(logs.size(), 1u);
  62. EXPECT_EQ(logs[0]->description(), kLogDescription);
  63. EXPECT_EQ(logs[0]->key_id(),
  64. crypto::SHA256HashString(net::ct::GetTestPublicKey()));
  65. }
  66. } // namespace network