1 // Copyright 2018 The Chromium Authors 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/http/transport_security_state_test_util.h" 6 7 #include <iterator> 8 9 #include "base/stl_util.h" 10 #include "base/strings/string_number_conversions.h" 11 #include "net/http/transport_security_state.h" 12 #include "url/gurl.h" 13 14 namespace net { 15 16 namespace test_default { 17 #include "net/http/transport_security_state_static_unittest_default.h" 18 } // namespace test_default 19 ScopedTransportSecurityStateSource()20ScopedTransportSecurityStateSource::ScopedTransportSecurityStateSource() { 21 // TODO(mattm): allow using other source? 22 net::SetTransportSecurityStateSourceForTesting( 23 &net::test_default::kHSTSSource); 24 } 25 ScopedTransportSecurityStateSource(uint16_t reporting_port)26ScopedTransportSecurityStateSource::ScopedTransportSecurityStateSource( 27 uint16_t reporting_port) { 28 // TODO(mattm): allow using other source? 29 const TransportSecurityStateSource* base_source = 30 &net::test_default::kHSTSSource; 31 std::string reporting_port_string = base::NumberToString(reporting_port); 32 GURL::Replacements replace_port; 33 replace_port.SetPortStr(reporting_port_string); 34 35 const char* last_report_uri = nullptr; 36 for (size_t i = 0; i < base_source->pinsets_count; ++i) { 37 const auto* pinset = &base_source->pinsets[i]; 38 if (pinset->report_uri == kNoReportURI) 39 continue; 40 // Currently only one PKP report URI is supported. 41 if (last_report_uri) 42 DCHECK_EQ(base::StringPiece(last_report_uri), pinset->report_uri); 43 else 44 last_report_uri = pinset->report_uri; 45 pkp_report_uri_ = 46 GURL(pinset->report_uri).ReplaceComponents(replace_port).spec(); 47 } 48 for (size_t i = 0; i < base_source->pinsets_count; ++i) { 49 const auto* pinset = &base_source->pinsets[i]; 50 pinsets_.push_back({pinset->accepted_pins, pinset->rejected_pins, 51 pinset->report_uri == kNoReportURI 52 ? kNoReportURI 53 : pkp_report_uri_.c_str()}); 54 } 55 56 const net::TransportSecurityStateSource new_source = { 57 base_source->huffman_tree, base_source->huffman_tree_size, 58 base_source->preloaded_data, base_source->preloaded_bits, 59 base_source->root_position, pinsets_.data(), 60 base_source->pinsets_count}; 61 62 source_ = std::make_unique<TransportSecurityStateSource>(new_source); 63 64 net::SetTransportSecurityStateSourceForTesting(source_.get()); 65 } 66 ~ScopedTransportSecurityStateSource()67ScopedTransportSecurityStateSource::~ScopedTransportSecurityStateSource() { 68 net::SetTransportSecurityStateSourceForTesting(nullptr); 69 } 70 71 } // namespace net 72