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

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

Issue 2331923003: Allow LogDnsClient queries to be rate-limited (Closed)
Patch Set: Created 4 years, 3 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/log_dns_client.h" 5 #include "components/certificate_transparency/log_dns_client.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/memory/ptr_util.h"
12 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h" 14 #include "base/run_loop.h"
14 #include "components/certificate_transparency/mock_log_dns_traffic.h" 15 #include "components/certificate_transparency/mock_log_dns_traffic.h"
15 #include "crypto/sha2.h" 16 #include "crypto/sha2.h"
16 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
17 #include "net/cert/merkle_audit_proof.h" 18 #include "net/cert/merkle_audit_proof.h"
18 #include "net/cert/signed_certificate_timestamp.h" 19 #include "net/cert/signed_certificate_timestamp.h"
19 #include "net/dns/dns_client.h" 20 #include "net/dns/dns_client.h"
20 #include "net/dns/dns_config_service.h" 21 #include "net/dns/dns_config_service.h"
21 #include "net/dns/dns_protocol.h" 22 #include "net/dns/dns_protocol.h"
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 }; 114 };
114 115
115 class LogDnsClientTest : public ::testing::TestWithParam<net::IoMode> { 116 class LogDnsClientTest : public ::testing::TestWithParam<net::IoMode> {
116 protected: 117 protected:
117 LogDnsClientTest() 118 LogDnsClientTest()
118 : network_change_notifier_(net::NetworkChangeNotifier::CreateMock()) { 119 : network_change_notifier_(net::NetworkChangeNotifier::CreateMock()) {
119 mock_dns_.SetSocketReadMode(GetParam()); 120 mock_dns_.SetSocketReadMode(GetParam());
120 mock_dns_.InitializeDnsConfig(); 121 mock_dns_.InitializeDnsConfig();
121 } 122 }
122 123
124 std::unique_ptr<LogDnsClient> CreateLogDnsClient() {
125 return base::MakeUnique<LogDnsClient>(mock_dns_.CreateDnsClient(),
126 net::BoundNetLog());
127 }
128
129 void QueryLeafIndexAsync(LogDnsClient* log_client,
130 base::StringPiece log_domain,
131 base::StringPiece leaf_hash,
132 const LogDnsClient::LeafIndexCallback& callback) {
133 log_client->QueryLeafIndex(log_domain, leaf_hash, callback);
134 }
135
123 void QueryLeafIndex(base::StringPiece log_domain, 136 void QueryLeafIndex(base::StringPiece log_domain,
124 base::StringPiece leaf_hash, 137 base::StringPiece leaf_hash,
125 MockLeafIndexCallback* callback) { 138 MockLeafIndexCallback* callback) {
126 LogDnsClient log_client(mock_dns_.CreateDnsClient(), net::BoundNetLog()); 139 auto log_client = CreateLogDnsClient();
127 log_client.QueryLeafIndex(log_domain, leaf_hash, callback->AsCallback()); 140 QueryLeafIndexAsync(log_client.get(), log_domain, leaf_hash,
141 callback->AsCallback());
128 callback->WaitUntilRun(); 142 callback->WaitUntilRun();
129 } 143 }
130 144
145 void QueryAuditProofAsync(LogDnsClient* log_client,
146 base::StringPiece log_domain,
147 uint64_t leaf_index,
148 uint64_t tree_size,
149 const LogDnsClient::AuditProofCallback& callback) {
150 log_client->QueryAuditProof(log_domain, leaf_index, tree_size, callback);
151 }
152
131 void QueryAuditProof(base::StringPiece log_domain, 153 void QueryAuditProof(base::StringPiece log_domain,
132 uint64_t leaf_index, 154 uint64_t leaf_index,
133 uint64_t tree_size, 155 uint64_t tree_size,
134 MockAuditProofCallback* callback) { 156 MockAuditProofCallback* callback) {
135 LogDnsClient log_client(mock_dns_.CreateDnsClient(), net::BoundNetLog()); 157 auto log_client = CreateLogDnsClient();
136 log_client.QueryAuditProof(log_domain, leaf_index, tree_size, 158 QueryAuditProofAsync(log_client.get(), log_domain, leaf_index, tree_size,
137 callback->AsCallback()); 159 callback->AsCallback());
138 callback->WaitUntilRun(); 160 callback->WaitUntilRun();
139 } 161 }
140 162
141 // This will be the NetworkChangeNotifier singleton for the duration of the 163 // This will be the NetworkChangeNotifier singleton for the duration of the
142 // test. It is accessed statically by LogDnsClient. 164 // test. It is accessed statically by LogDnsClient.
143 std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier_; 165 std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier_;
144 // Queues and handles asynchronous DNS tasks. Indirectly used by LogDnsClient, 166 // Queues and handles asynchronous DNS tasks. Indirectly used by LogDnsClient,
145 // the underlying net::DnsClient, and NetworkChangeNotifier. 167 // the underlying net::DnsClient, and NetworkChangeNotifier.
146 base::MessageLoopForIO message_loop_; 168 base::MessageLoopForIO message_loop_;
147 // Allows mock DNS sockets to be setup. 169 // Allows mock DNS sockets to be setup.
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 net::DnsConfig config(*dns_client->GetConfig()); 558 net::DnsConfig config(*dns_client->GetConfig());
537 ASSERT_THAT(config.nameservers, Not(IsEmpty())); 559 ASSERT_THAT(config.nameservers, Not(IsEmpty()));
538 config.nameservers.clear(); // Makes config invalid 560 config.nameservers.clear(); // Makes config invalid
539 mock_dns_.SetDnsConfig(config); 561 mock_dns_.SetDnsConfig(config);
540 562
541 // Let the DNS config change propogate. 563 // Let the DNS config change propogate.
542 base::RunLoop().RunUntilIdle(); 564 base::RunLoop().RunUntilIdle();
543 EXPECT_THAT(dns_client->GetConfig()->nameservers, Not(IsEmpty())); 565 EXPECT_THAT(dns_client->GetConfig()->nameservers, Not(IsEmpty()));
544 } 566 }
545 567
568 TEST_P(LogDnsClientTest, CanBeThrottledToOneQueryAtATime) {
569 const std::vector<std::string> audit_proof = GetSampleAuditProof(20);
570
571 // It should require 3 queries to collect the entire audit proof, as there is
572 // only space for 7 nodes per UDP packet.
573 mock_dns_.ExpectAuditProofRequestAndResponse("0.123456.999999.tree.ct.test.",
574 audit_proof.begin(),
575 audit_proof.begin() + 7);
576 mock_dns_.ExpectAuditProofRequestAndResponse("7.123456.999999.tree.ct.test.",
577 audit_proof.begin() + 7,
578 audit_proof.begin() + 14);
579 mock_dns_.ExpectAuditProofRequestAndResponse("14.123456.999999.tree.ct.test.",
580 audit_proof.begin() + 14,
581 audit_proof.end());
582
583 auto log_client = CreateLogDnsClient();
584 log_client->SetMaxConcurrentQueries(1);
585
586 MockAuditProofCallback callback1;
587 QueryAuditProofAsync(log_client.get(), "ct.test", 123456, 999999,
588 callback1.AsCallback());
589 MockAuditProofCallback callback2;
590 QueryAuditProofAsync(log_client.get(), "ct.test", 123456, 999999,
591 callback2.AsCallback());
592
593 callback1.WaitUntilRun();
594 callback2.WaitUntilRun();
595
596 ASSERT_TRUE(callback1.called());
597 EXPECT_THAT(callback1.net_error(), IsOk());
598 ASSERT_THAT(callback1.proof(), NotNull());
599 EXPECT_THAT(callback1.proof()->leaf_index, 123456);
600 // EXPECT_THAT(callback1.proof()->tree_size, 999999);
601 EXPECT_THAT(callback1.proof()->nodes, audit_proof);
602
603 ASSERT_TRUE(callback2.called());
604 EXPECT_THAT(callback2.net_error(), IsError(net::ERR_TEMPORARILY_THROTTLED));
605 EXPECT_THAT(callback2.proof(), IsNull());
606 }
607
546 INSTANTIATE_TEST_CASE_P(ReadMode, 608 INSTANTIATE_TEST_CASE_P(ReadMode,
547 LogDnsClientTest, 609 LogDnsClientTest,
548 ::testing::Values(net::IoMode::ASYNC, 610 ::testing::Values(net::IoMode::ASYNC,
549 net::IoMode::SYNCHRONOUS)); 611 net::IoMode::SYNCHRONOUS));
550 612
551 } // namespace 613 } // namespace
552 } // namespace certificate_transparency 614 } // namespace certificate_transparency
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698