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