| 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 "base/lazy_instance.h" |
| 6 #include "base/macros.h" |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/threading/thread.h" |
| 12 #include "components/grpc_support/include/bidirectional_stream_c.h" |
| 13 #include "components/grpc_support/test/quic_test_server.h" |
| 14 #include "net/base/host_port_pair.h" |
| 15 #include "net/cert/mock_cert_verifier.h" |
| 16 #include "net/dns/mapped_host_resolver.h" |
| 17 #include "net/dns/mock_host_resolver.h" |
| 18 #include "net/http/http_server_properties_impl.h" |
| 19 #include "net/url_request/url_request_test_util.h" |
| 20 |
| 21 namespace grpc_support { |
| 22 namespace { |
| 23 |
| 24 // URLRequestContextGetter for BidirectionalStreamTest. This is used instead of |
| 25 // net::TestURLRequestContextGetter because the URLRequestContext needs to be |
| 26 // created on the test_io_thread_ for the test, and TestURLRequestContextGetter |
| 27 // does not allow for lazy instantiation of the URLRequestContext if additional |
| 28 // setup is required. |
| 29 class BidirectionalStreamTestURLRequestContextGetter |
| 30 : public net::URLRequestContextGetter { |
| 31 public: |
| 32 BidirectionalStreamTestURLRequestContextGetter( |
| 33 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
| 34 : task_runner_(task_runner) {} |
| 35 |
| 36 net::URLRequestContext* GetURLRequestContext() override { |
| 37 if (!request_context_.get()) { |
| 38 request_context_.reset( |
| 39 new net::TestURLRequestContext(true /* delay_initialization */)); |
| 40 auto mock_host_resolver = base::MakeUnique<net::MockHostResolver>(); |
| 41 host_resolver_.reset( |
| 42 new net::MappedHostResolver(std::move(mock_host_resolver))); |
| 43 UpdateHostResolverRules(); |
| 44 mock_cert_verifier_.reset(new net::MockCertVerifier()); |
| 45 mock_cert_verifier_->set_default_result(net::OK); |
| 46 server_properties_.reset(new net::HttpServerPropertiesImpl()); |
| 47 |
| 48 // Need to enable QUIC for the test server. |
| 49 auto params = base::MakeUnique<net::HttpNetworkSession::Params>(); |
| 50 params->enable_quic = true; |
| 51 params->enable_http2 = true; |
| 52 net::AlternativeService alternative_service(net::AlternateProtocol::QUIC, |
| 53 "", 443); |
| 54 url::SchemeHostPort quic_hint_server("https", kTestServerHost, 443); |
| 55 server_properties_->SetAlternativeService( |
| 56 quic_hint_server, alternative_service, base::Time::Max()); |
| 57 params->quic_host_whitelist.insert(kTestServerHost); |
| 58 |
| 59 request_context_->set_cert_verifier(mock_cert_verifier_.get()); |
| 60 request_context_->set_host_resolver(host_resolver_.get()); |
| 61 request_context_->set_http_server_properties(server_properties_.get()); |
| 62 request_context_->set_http_network_session_params(std::move(params)); |
| 63 |
| 64 request_context_->Init(); |
| 65 } |
| 66 return request_context_.get(); |
| 67 } |
| 68 |
| 69 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() |
| 70 const override { |
| 71 return task_runner_; |
| 72 } |
| 73 |
| 74 void SetTestServerPort(int port) { |
| 75 test_server_port_ = port; |
| 76 UpdateHostResolverRules(); |
| 77 } |
| 78 |
| 79 private: |
| 80 void UpdateHostResolverRules() { |
| 81 if (!host_resolver_) |
| 82 return; |
| 83 host_resolver_->SetRulesFromString( |
| 84 base::StringPrintf("MAP notfound.example.com ~NOTFOUND," |
| 85 "MAP test.example.com 127.0.0.1:%d", |
| 86 test_server_port_)); |
| 87 } |
| 88 ~BidirectionalStreamTestURLRequestContextGetter() override {} |
| 89 |
| 90 int test_server_port_; |
| 91 std::unique_ptr<net::HttpServerProperties> server_properties_; |
| 92 std::unique_ptr<net::MockCertVerifier> mock_cert_verifier_; |
| 93 std::unique_ptr<net::MappedHostResolver> host_resolver_; |
| 94 std::unique_ptr<net::TestURLRequestContext> request_context_; |
| 95 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(BidirectionalStreamTestURLRequestContextGetter); |
| 98 }; |
| 99 |
| 100 base::LazyInstance< |
| 101 scoped_refptr<BidirectionalStreamTestURLRequestContextGetter>> |
| 102 ::Leaky g_request_context_getter_ = LAZY_INSTANCE_INITIALIZER; |
| 103 bool g_initialized_ = false; |
| 104 |
| 105 } // namespace |
| 106 |
| 107 void CreateRequestContextGetterIfNecessary() { |
| 108 if (!g_initialized_) { |
| 109 g_initialized_ = true; |
| 110 static base::Thread* test_io_thread_ = |
| 111 new base::Thread("grpc_support_test_io_thread"); |
| 112 base::Thread::Options options; |
| 113 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 114 bool started = test_io_thread_->StartWithOptions(options); |
| 115 DCHECK(started); |
| 116 |
| 117 g_request_context_getter_.Get() = |
| 118 new BidirectionalStreamTestURLRequestContextGetter( |
| 119 test_io_thread_->task_runner()); |
| 120 } |
| 121 } |
| 122 |
| 123 stream_engine* GetTestStreamEngine(int port) { |
| 124 CreateRequestContextGetterIfNecessary(); |
| 125 g_request_context_getter_.Get()->SetTestServerPort(port); |
| 126 static stream_engine engine; |
| 127 engine.obj = g_request_context_getter_.Get().get(); |
| 128 return &engine; |
| 129 } |
| 130 |
| 131 } // namespace grpc_support |
| OLD | NEW |