Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: components/certificate_transparency/single_tree_tracker_unittest.cc

Issue 2153123002: Certificate Transparency: Collect metrics on age of SCT vs STH (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename histogram Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/certificate_transparency/single_tree_tracker.h" 5 #include "components/certificate_transparency/single_tree_tracker.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/strings/string_piece.h" 10 #include "base/strings/string_piece.h"
11 #include "base/test/histogram_tester.h"
11 #include "net/cert/ct_log_verifier.h" 12 #include "net/cert/ct_log_verifier.h"
12 #include "net/cert/ct_serialization.h" 13 #include "net/cert/ct_serialization.h"
13 #include "net/cert/signed_certificate_timestamp.h" 14 #include "net/cert/signed_certificate_timestamp.h"
14 #include "net/cert/signed_tree_head.h" 15 #include "net/cert/signed_tree_head.h"
15 #include "net/cert/x509_certificate.h" 16 #include "net/cert/x509_certificate.h"
16 #include "net/test/ct_test_util.h" 17 #include "net/test/ct_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 19
19 namespace certificate_transparency { 20 namespace certificate_transparency {
20 21
21 namespace { 22 namespace {
23 const char kHistogramName[] =
24 "Net.CertificateTransparency.CanInclusionCheckSCT";
22 25
23 bool GetOldSignedTreeHead(net::ct::SignedTreeHead* sth) { 26 bool GetOldSignedTreeHead(net::ct::SignedTreeHead* sth) {
24 sth->version = net::ct::SignedTreeHead::V1; 27 sth->version = net::ct::SignedTreeHead::V1;
25 sth->timestamp = base::Time::UnixEpoch() + 28 sth->timestamp = base::Time::UnixEpoch() +
26 base::TimeDelta::FromMilliseconds(INT64_C(1348589665525)); 29 base::TimeDelta::FromMilliseconds(INT64_C(1348589665525));
27 sth->tree_size = 12u; 30 sth->tree_size = 12u;
28 31
29 const uint8_t kOldSTHRootHash[] = { 32 const uint8_t kOldSTHRootHash[] = {
30 0x18, 0x04, 0x1b, 0xd4, 0x66, 0x50, 0x83, 0x00, 0x1f, 0xba, 0x8c, 33 0x18, 0x04, 0x1b, 0xd4, 0x66, 0x50, 0x83, 0x00, 0x1f, 0xba, 0x8c,
31 0x54, 0x11, 0xd2, 0xd7, 0x48, 0xe8, 0xab, 0xbf, 0xdc, 0xdf, 0xd9, 34 0x54, 0x11, 0xd2, 0xd7, 0x48, 0xe8, 0xab, 0xbf, 0xdc, 0xdf, 0xd9,
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 protected: 71 protected:
69 scoped_refptr<const net::CTLogVerifier> log_; 72 scoped_refptr<const net::CTLogVerifier> log_;
70 std::unique_ptr<SingleTreeTracker> tree_tracker_; 73 std::unique_ptr<SingleTreeTracker> tree_tracker_;
71 scoped_refptr<net::X509Certificate> chain_; 74 scoped_refptr<net::X509Certificate> chain_;
72 scoped_refptr<net::ct::SignedCertificateTimestamp> cert_sct_; 75 scoped_refptr<net::ct::SignedCertificateTimestamp> cert_sct_;
73 }; 76 };
74 77
75 // Test that an SCT is classified as pending for a newer STH if the 78 // Test that an SCT is classified as pending for a newer STH if the
76 // SingleTreeTracker has not seen any STHs so far. 79 // SingleTreeTracker has not seen any STHs so far.
77 TEST_F(SingleTreeTrackerTest, CorrectlyClassifiesUnobservedSCTNoSTH) { 80 TEST_F(SingleTreeTrackerTest, CorrectlyClassifiesUnobservedSCTNoSTH) {
81 base::HistogramTester histograms;
78 // First make sure the SCT has not been observed at all. 82 // First make sure the SCT has not been observed at all.
79 EXPECT_EQ( 83 EXPECT_EQ(
80 SingleTreeTracker::SCT_NOT_OBSERVED, 84 SingleTreeTracker::SCT_NOT_OBSERVED,
81 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get())); 85 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get()));
82 86
83 tree_tracker_->OnSCTVerified(chain_.get(), cert_sct_.get()); 87 tree_tracker_->OnSCTVerified(chain_.get(), cert_sct_.get());
84 88
85 // Since no STH was provided to the tree_tracker_ the status should be that 89 // Since no STH was provided to the tree_tracker_ the status should be that
86 // the SCT is pending a newer STH. 90 // the SCT is pending a newer STH.
87 EXPECT_EQ( 91 EXPECT_EQ(
88 SingleTreeTracker::SCT_PENDING_NEWER_STH, 92 SingleTreeTracker::SCT_PENDING_NEWER_STH,
89 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get())); 93 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get()));
94
95 // Since there's no valid STH expect nothing was logged to UMA.
96 histograms.ExpectTotalCount(kHistogramName, 0);
90 } 97 }
91 98
92 // Test that an SCT is classified as pending an inclusion check if the 99 // Test that an SCT is classified as pending an inclusion check if the
93 // SingleTreeTracker has a fresh-enough STH to check inclusion against. 100 // SingleTreeTracker has a fresh-enough STH to check inclusion against.
94 TEST_F(SingleTreeTrackerTest, CorrectlyClassifiesUnobservedSCTWithRecentSTH) { 101 TEST_F(SingleTreeTrackerTest, CorrectlyClassifiesUnobservedSCTWithRecentSTH) {
102 base::HistogramTester histograms;
95 // Provide an STH to the tree_tracker_. 103 // Provide an STH to the tree_tracker_.
96 net::ct::SignedTreeHead sth; 104 net::ct::SignedTreeHead sth;
97 net::ct::GetSampleSignedTreeHead(&sth); 105 net::ct::GetSampleSignedTreeHead(&sth);
98 tree_tracker_->NewSTHObserved(sth); 106 tree_tracker_->NewSTHObserved(sth);
99 107
100 // Make sure the SCT status is the same as if there's no STH for 108 // Make sure the SCT status is the same as if there's no STH for
101 // this log. 109 // this log.
102 EXPECT_EQ( 110 EXPECT_EQ(
103 SingleTreeTracker::SCT_NOT_OBSERVED, 111 SingleTreeTracker::SCT_NOT_OBSERVED,
104 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get())); 112 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get()));
105 113
106 tree_tracker_->OnSCTVerified(chain_.get(), cert_sct_.get()); 114 tree_tracker_->OnSCTVerified(chain_.get(), cert_sct_.get());
107 115
108 // The status for this SCT should be 'pending inclusion check' since the STH 116 // The status for this SCT should be 'pending inclusion check' since the STH
109 // provided at the beginning of the test is newer than the SCT. 117 // provided at the beginning of the test is newer than the SCT.
110 EXPECT_EQ( 118 EXPECT_EQ(
111 SingleTreeTracker::SCT_PENDING_INCLUSION_CHECK, 119 SingleTreeTracker::SCT_PENDING_INCLUSION_CHECK,
112 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get())); 120 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get()));
121
122 // Exactly one value should be logged, indicating the SCT
123 // can be checked for inclusion.
124 histograms.ExpectTotalCount(kHistogramName, 1);
125 histograms.ExpectBucketCount(kHistogramName, true, 1);
113 } 126 }
114 127
115 // Test that the SingleTreeTracker correctly queues verified SCTs for inclusion 128 // Test that the SingleTreeTracker correctly queues verified SCTs for inclusion
116 // checking such that, upon receiving a fresh STH, it changes the SCT's status 129 // checking such that, upon receiving a fresh STH, it changes the SCT's status
117 // from pending newer STH to pending inclusion check. 130 // from pending newer STH to pending inclusion check.
118 TEST_F(SingleTreeTrackerTest, CorrectlyUpdatesSCTStatusOnNewSTH) { 131 TEST_F(SingleTreeTrackerTest, CorrectlyUpdatesSCTStatusOnNewSTH) {
132 base::HistogramTester histograms;
119 // Report an observed SCT and make sure it's in the pending newer STH 133 // Report an observed SCT and make sure it's in the pending newer STH
120 // state. 134 // state.
121 tree_tracker_->OnSCTVerified(chain_.get(), cert_sct_.get()); 135 tree_tracker_->OnSCTVerified(chain_.get(), cert_sct_.get());
122 EXPECT_EQ( 136 EXPECT_EQ(
123 SingleTreeTracker::SCT_PENDING_NEWER_STH, 137 SingleTreeTracker::SCT_PENDING_NEWER_STH,
124 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get())); 138 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get()));
139 histograms.ExpectTotalCount(kHistogramName, 0);
125 140
126 // Provide with a fresh STH 141 // Provide with a fresh STH
127 net::ct::SignedTreeHead sth; 142 net::ct::SignedTreeHead sth;
128 net::ct::GetSampleSignedTreeHead(&sth); 143 net::ct::GetSampleSignedTreeHead(&sth);
129 tree_tracker_->NewSTHObserved(sth); 144 tree_tracker_->NewSTHObserved(sth);
130 145
131 // Test that its status has changed. 146 // Test that its status has changed.
132 EXPECT_EQ( 147 EXPECT_EQ(
133 SingleTreeTracker::SCT_PENDING_INCLUSION_CHECK, 148 SingleTreeTracker::SCT_PENDING_INCLUSION_CHECK,
134 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get())); 149 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get()));
150 // Check that no UMA was logged for this case as the histogram is only
151 // supposed to measure the state of newly-observed SCTs, not pending ones.
152 histograms.ExpectTotalCount(kHistogramName, 0);
135 } 153 }
136 154
137 // Test that the SingleTreeTracker does not change an SCT's status if an STH 155 // Test that the SingleTreeTracker does not change an SCT's status if an STH
138 // from the log it was issued by is observed, but that STH is too old to check 156 // from the log it was issued by is observed, but that STH is too old to check
139 // inclusion against. 157 // inclusion against.
140 TEST_F(SingleTreeTrackerTest, DoesNotUpdatesSCTStatusOnOldSTH) { 158 TEST_F(SingleTreeTrackerTest, DoesNotUpdatesSCTStatusOnOldSTH) {
141 // Notify of an SCT and make sure it's in the 'pending newer STH' state. 159 // Notify of an SCT and make sure it's in the 'pending newer STH' state.
142 tree_tracker_->OnSCTVerified(chain_.get(), cert_sct_.get()); 160 tree_tracker_->OnSCTVerified(chain_.get(), cert_sct_.get());
143 EXPECT_EQ( 161 EXPECT_EQ(
144 SingleTreeTracker::SCT_PENDING_NEWER_STH, 162 SingleTreeTracker::SCT_PENDING_NEWER_STH,
145 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get())); 163 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get()));
146 164
147 // Provide an old STH for the same log. 165 // Provide an old STH for the same log.
148 net::ct::SignedTreeHead sth; 166 net::ct::SignedTreeHead sth;
149 GetOldSignedTreeHead(&sth); 167 GetOldSignedTreeHead(&sth);
150 tree_tracker_->NewSTHObserved(sth); 168 tree_tracker_->NewSTHObserved(sth);
151 169
152 // Make sure the SCT's state hasn't changed. 170 // Make sure the SCT's state hasn't changed.
153 EXPECT_EQ( 171 EXPECT_EQ(
154 SingleTreeTracker::SCT_PENDING_NEWER_STH, 172 SingleTreeTracker::SCT_PENDING_NEWER_STH,
155 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get())); 173 tree_tracker_->GetLogEntryInclusionStatus(chain_.get(), cert_sct_.get()));
156 } 174 }
157 175
176 // Test that the SingleTreeTracker correctly logs that an SCT is pending a new
177 // STH when it has STH but the observed SCT is newer than that.
178 TEST_F(SingleTreeTrackerTest, LogsUMAForNewSCTAndOldSTH) {
179 base::HistogramTester histograms;
180 // Provide an old STH for the same log.
181 net::ct::SignedTreeHead sth;
182 GetOldSignedTreeHead(&sth);
183 tree_tracker_->NewSTHObserved(sth);
184
185 histograms.ExpectTotalCount(kHistogramName, 0);
186 // Notify of an SCT and make sure it's in the 'pending newer STH' state.
187 tree_tracker_->OnSCTVerified(chain_.get(), cert_sct_.get());
188 // Exactly one value should be logged, indicating the SCT cannot be checked
189 // for inclusion as the STH is too old.
190 histograms.ExpectTotalCount(kHistogramName, 1);
191 histograms.ExpectBucketCount(kHistogramName, false, 1);
192 }
193
158 } // namespace certificate_transparency 194 } // namespace certificate_transparency
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698