Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 | |
| 5 #include "net/cert/sth_distributor.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/test/histogram_tester.h" | |
| 11 #include "crypto/sha2.h" | |
| 12 #include "net/cert/signed_tree_head.h" | |
| 13 #include "net/cert/sth_observer.h" | |
| 14 #include "net/test/ct_test_util.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace net { | |
| 18 | |
| 19 namespace ct { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // An STHObserver implementation that simply stores all | |
| 24 // observed STHs, keyed by log ID. | |
| 25 class StoringSTHObserver : public STHObserver { | |
| 26 public: | |
| 27 void NewSTHObserved(const SignedTreeHead& sth) override { | |
| 28 sths[sth.log_id] = sth; | |
| 29 } | |
| 30 | |
| 31 std::map<std::string, SignedTreeHead> sths; | |
| 32 }; | |
| 33 | |
| 34 class STHDistributorTest : public ::testing::Test { | |
| 35 public: | |
| 36 STHDistributorTest() {} | |
| 37 | |
| 38 void SetUp() override { | |
| 39 ASSERT_TRUE(GetSampleSignedTreeHead(&sample_sth_)); | |
| 40 sample_sth_.log_id = GetTestPublicKeyId(); | |
| 41 } | |
| 42 | |
| 43 protected: | |
| 44 STHDistributor distributor_; | |
| 45 SignedTreeHead sample_sth_; | |
| 46 }; | |
| 47 | |
| 48 TEST_F(STHDistributorTest, NotifiesOfExistingSTHs) { | |
| 49 const std::string other_log = "another log"; | |
| 50 SignedTreeHead second_sth(sample_sth_); | |
| 51 second_sth.log_id = other_log; | |
| 52 | |
| 53 distributor_.NewSTHObserved(sample_sth_); | |
| 54 distributor_.NewSTHObserved(second_sth); | |
| 55 | |
| 56 StoringSTHObserver observer; | |
| 57 distributor_.RegisterObserver(&observer); | |
| 58 | |
| 59 // Check that two STHs from different logs received prior to observer | |
| 60 // registration were reported to the observer once registered. | |
| 61 EXPECT_EQ(2u, observer.sths.size()); | |
| 62 EXPECT_EQ(1u, observer.sths.count(other_log)); | |
| 63 } | |
| 64 | |
| 65 TEST_F(STHDistributorTest, LogsUMAForPilotSTH) { | |
| 66 const char kPilotSTHAgeHistogram[] = | |
| 67 "Net.CertificateTransparency.PilotSTHAge"; | |
| 68 base::HistogramTester histograms; | |
| 69 histograms.ExpectTotalCount(kPilotSTHAgeHistogram, 0); | |
| 70 | |
| 71 const uint8_t kPilotLogID[] = { | |
| 72 0xa4, 0xb9, 0x09, 0x90, 0xb4, 0x18, 0x58, 0x14, 0x87, 0xbb, 0x13, | |
| 73 0xa2, 0xcc, 0x67, 0x70, 0x0a, 0x3c, 0x35, 0x98, 0x04, 0xf9, 0x1b, | |
| 74 0xdf, 0xb8, 0xe3, 0x77, 0xcd, 0x0e, 0xc8, 0x0d, 0xdc, 0x10}; | |
| 75 sample_sth_.log_id = std::string(reinterpret_cast<const char*>(kPilotLogID), | |
| 76 crypto::kSHA256Length); | |
| 77 | |
| 78 distributor_.NewSTHObserved(sample_sth_); | |
| 79 histograms.ExpectTotalCount(kPilotSTHAgeHistogram, 1); | |
| 80 } | |
| 81 | |
| 82 TEST_F(STHDistributorTest, UpdatesObservedSTHData) { | |
| 83 distributor_.NewSTHObserved(sample_sth_); | |
| 84 sample_sth_.tree_size = 23u; | |
|
Ryan Sleevi
2016/05/17 16:52:51
DESIGN: You should set a value for .tree_size befo
Eran Messeri
2016/05/17 17:04:26
What I've done is stored the value of sample_sth_.
| |
| 85 distributor_.NewSTHObserved(sample_sth_); | |
| 86 | |
| 87 StoringSTHObserver observer; | |
| 88 distributor_.RegisterObserver(&observer); | |
| 89 EXPECT_EQ(1u, observer.sths.size()); | |
| 90 EXPECT_EQ(23u, observer.sths[GetTestPublicKeyId()].tree_size); | |
| 91 } | |
| 92 | |
| 93 } // namespace | |
| 94 | |
| 95 } // namespace ct | |
| 96 | |
| 97 } // namespace net | |
| OLD | NEW |