Chromium Code Reviews| Index: net/cert/sth_distributor_unittest.cc |
| diff --git a/net/cert/sth_distributor_unittest.cc b/net/cert/sth_distributor_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bf231fb0ae683686401529dc6302c01a5e941ba1 |
| --- /dev/null |
| +++ b/net/cert/sth_distributor_unittest.cc |
| @@ -0,0 +1,98 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/cert/sth_distributor.h" |
| + |
| +#include <map> |
| +#include <string> |
| + |
| +#include "base/test/histogram_tester.h" |
| +#include "crypto/sha2.h" |
| +#include "net/cert/signed_tree_head.h" |
| +#include "net/cert/sth_observer.h" |
| +#include "net/test/ct_test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace net { |
| + |
| +namespace ct { |
| + |
| +namespace { |
| + |
| +// An STHObserver implementation that simply stores all |
| +// observed STHs, keyed by log ID. |
| +class StoringSTHObserver : public STHObserver { |
| + public: |
| + void NewSTHObserved(const SignedTreeHead& sth) override { |
| + sths[sth.log_id] = sth; |
| + } |
| + |
| + std::map<std::string, SignedTreeHead> sths; |
| +}; |
| + |
| +class STHDistributorTest : public ::testing::Test { |
| + public: |
| + STHDistributorTest() {} |
| + |
| + void SetUp() override { |
| + ASSERT_TRUE(GetSampleSignedTreeHead(&sample_sth_)); |
| + sample_sth_.log_id = GetTestPublicKeyId(); |
| + } |
| + |
| + protected: |
| + STHDistributor distributor_; |
| + SignedTreeHead sample_sth_; |
| +}; |
| + |
| +TEST_F(STHDistributorTest, NotifiesOfExistingSTHs) { |
|
Ryan Sleevi
2016/05/17 17:57:17
Test style - I haven't pushed as hard on this with
Eran Messeri
2016/05/17 21:35:07
Done.
|
| + const std::string other_log = "another log"; |
| + SignedTreeHead second_sth(sample_sth_); |
| + second_sth.log_id = other_log; |
| + |
| + distributor_.NewSTHObserved(sample_sth_); |
| + distributor_.NewSTHObserved(second_sth); |
| + |
| + StoringSTHObserver observer; |
| + distributor_.RegisterObserver(&observer); |
| + |
| + // Check that two STHs from different logs received prior to observer |
| + // registration were reported to the observer once registered. |
| + EXPECT_EQ(2u, observer.sths.size()); |
| + EXPECT_EQ(1u, observer.sths.count(other_log)); |
| +} |
| + |
| +TEST_F(STHDistributorTest, LogsUMAForPilotSTH) { |
|
Ryan Sleevi
2016/05/17 17:57:17
// Test that histograms are properly recorded for
Eran Messeri
2016/05/17 21:35:07
Done.
|
| + const char kPilotSTHAgeHistogram[] = |
| + "Net.CertificateTransparency.PilotSTHAge"; |
| + base::HistogramTester histograms; |
| + histograms.ExpectTotalCount(kPilotSTHAgeHistogram, 0); |
| + |
| + const uint8_t kPilotLogID[] = { |
| + 0xa4, 0xb9, 0x09, 0x90, 0xb4, 0x18, 0x58, 0x14, 0x87, 0xbb, 0x13, |
| + 0xa2, 0xcc, 0x67, 0x70, 0x0a, 0x3c, 0x35, 0x98, 0x04, 0xf9, 0x1b, |
| + 0xdf, 0xb8, 0xe3, 0x77, 0xcd, 0x0e, 0xc8, 0x0d, 0xdc, 0x10}; |
| + sample_sth_.log_id = std::string(reinterpret_cast<const char*>(kPilotLogID), |
| + crypto::kSHA256Length); |
| + |
| + distributor_.NewSTHObserved(sample_sth_); |
| + histograms.ExpectTotalCount(kPilotSTHAgeHistogram, 1); |
| +} |
| + |
| +TEST_F(STHDistributorTest, UpdatesObservedSTHData) { |
|
Ryan Sleevi
2016/05/17 17:57:17
Test that... blah
Eran Messeri
2016/05/17 21:35:07
Done.
|
| + uint64_t orig_tree_size = sample_sth_.tree_size; |
| + distributor_.NewSTHObserved(sample_sth_); |
| + sample_sth_.tree_size += 1; |
| + distributor_.NewSTHObserved(sample_sth_); |
| + |
| + StoringSTHObserver observer; |
| + distributor_.RegisterObserver(&observer); |
| + EXPECT_EQ(1u, observer.sths.size()); |
| + EXPECT_EQ(orig_tree_size + 1, observer.sths[GetTestPublicKeyId()].tree_size); |
|
Ryan Sleevi
2016/05/17 17:57:17
This is even less clear than what you originally h
Eran Messeri
2016/05/17 21:35:07
Done - adapted your suggestion, thanks for providi
|
| +} |
| + |
| +} // namespace |
| + |
| +} // namespace ct |
| + |
| +} // namespace net |