diff options
author | xunjieli <xunjieli@chromium.org> | 2015-07-20 09:18:51 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-20 16:20:21 +0000 |
commit | 885de3126c2f1569f54134419ed24998f3c7fe91 (patch) | |
tree | 2680222271c3fba65d22ed14ad06243765a099e4 /net/test | |
parent | ed26a546f567abb17a2134a459b21e776e5be655 (diff) | |
download | chromium_src-885de3126c2f1569f54134419ed24998f3c7fe91.zip chromium_src-885de3126c2f1569f54134419ed24998f3c7fe91.tar.gz chromium_src-885de3126c2f1569f54134419ed24998f3c7fe91.tar.bz2 |
[Cronet] Surface SSL cert error through CronetURLRequestAdapter::OnSSLCertificateError
Review URL: https://codereview.chromium.org/1239543003
Cr-Commit-Position: refs/heads/master@{#339455}
Diffstat (limited to 'net/test')
-rw-r--r-- | net/test/url_request/ssl_certificate_error_job.cc | 73 | ||||
-rw-r--r-- | net/test/url_request/ssl_certificate_error_job.h | 44 |
2 files changed, 117 insertions, 0 deletions
diff --git a/net/test/url_request/ssl_certificate_error_job.cc b/net/test/url_request/ssl_certificate_error_job.cc new file mode 100644 index 0000000..8f2a12b --- /dev/null +++ b/net/test/url_request/ssl_certificate_error_job.cc @@ -0,0 +1,73 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/test/url_request/ssl_certificate_error_job.h" + +#include <string> + +#include "base/bind.h" +#include "base/strings/stringprintf.h" +#include "base/thread_task_runner_handle.h" +#include "net/ssl/ssl_info.h" +#include "net/url_request/url_request.h" +#include "net/url_request/url_request_filter.h" +#include "net/url_request/url_request_interceptor.h" + +namespace net { + +namespace { + +const char kMockHostname[] = "mock.ssl.cert.error.request"; + +class MockJobInterceptor : public URLRequestInterceptor { + public: + MockJobInterceptor() {} + ~MockJobInterceptor() override {} + + // URLRequestJobFactory::ProtocolHandler implementation: + URLRequestJob* MaybeInterceptRequest( + URLRequest* request, + NetworkDelegate* network_delegate) const override { + return new SSLCertificateErrorJob(request, network_delegate); + } + + private: + DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor); +}; + +} // namespace + +SSLCertificateErrorJob::SSLCertificateErrorJob( + URLRequest* request, + NetworkDelegate* network_delegate) + : URLRequestJob(request, network_delegate), weak_factory_(this) { +} + +void SSLCertificateErrorJob::Start() { + base::ThreadTaskRunnerHandle::Get()->PostTask( + FROM_HERE, base::Bind(&SSLCertificateErrorJob::NotifyError, + weak_factory_.GetWeakPtr())); +} + +void SSLCertificateErrorJob::AddUrlHandler() { + URLRequestFilter* filter = URLRequestFilter::GetInstance(); + filter->AddHostnameInterceptor( + "https", kMockHostname, + scoped_ptr<URLRequestInterceptor>(new MockJobInterceptor())); +} + +GURL SSLCertificateErrorJob::GetMockUrl() { + return GURL(base::StringPrintf("https://%s", kMockHostname)); +} + +SSLCertificateErrorJob::~SSLCertificateErrorJob() { +} + +void SSLCertificateErrorJob::NotifyError() { + SSLInfo info; + info.cert_status = CERT_STATUS_DATE_INVALID; + NotifySSLCertificateError(info, true); +} + +} // namespace net diff --git a/net/test/url_request/ssl_certificate_error_job.h b/net/test/url_request/ssl_certificate_error_job.h new file mode 100644 index 0000000..b3b372e --- /dev/null +++ b/net/test/url_request/ssl_certificate_error_job.h @@ -0,0 +1,44 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef NET_TEST_URL_REQUEST_SSL_CERTIFICATE_ERROR_JOB_H_ +#define NET_TEST_URL_REQUEST_SSL_CERTIFICATE_ERROR_JOB_H_ + +#include "base/macros.h" +#include "base/memory/weak_ptr.h" +#include "net/url_request/url_request_job.h" +#include "url/gurl.h" + +namespace net { + +class NetworkDelegate; +class URLRequest; + +// SSLCertificateErrorJob simulates a ERR_CERT_DATE_INVALID error. +class SSLCertificateErrorJob : public URLRequestJob { + public: + SSLCertificateErrorJob(URLRequest* request, + NetworkDelegate* network_delegate); + + // URLRequestJob implementation: + void Start() override; + + // Adds the testing URLs to the URLRequestFilter. + static void AddUrlHandler(); + + static GURL GetMockUrl(); + + private: + ~SSLCertificateErrorJob() override; + + void NotifyError(); + + base::WeakPtrFactory<SSLCertificateErrorJob> weak_factory_; + + DISALLOW_COPY_AND_ASSIGN(SSLCertificateErrorJob); +}; + +} // namespace net + +#endif // NET_TEST_URL_REQUEST_SSL_CERTIFICATE_ERROR_JOB_H_ |