1 // Copyright (c) 2012 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.
5 #include "chrome/browser/ssl/ssl_client_certificate_selector_test.h"
8 #include "base/files/file_path.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/test/base/ui_test_utils.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/test/browser_test_utils.h"
15 #include "net/base/test_data_directory.h"
16 #include "net/cert/x509_certificate.h"
17 #include "net/http/http_transaction_factory.h"
18 #include "net/ssl/ssl_cert_request_info.h"
19 #include "net/test/cert_test_util.h"
20 #include "net/url_request/url_request.h"
21 #include "net/url_request/url_request_context.h"
22 #include "net/url_request/url_request_context_getter.h"
24 using ::testing::Mock
;
25 using ::testing::StrictMock
;
26 using content::BrowserThread
;
28 SSLClientCertificateSelectorTestBase::SSLClientCertificateSelectorTestBase()
29 : io_loop_finished_event_(false, false) {
32 SSLClientCertificateSelectorTestBase::~SSLClientCertificateSelectorTestBase() {
35 void SSLClientCertificateSelectorTestBase::SetUpInProcessBrowserTestFixture() {
36 base::FilePath certs_dir
= net::GetTestCertsDirectory();
38 mit_davidben_cert_
= net::ImportCertFromFile(certs_dir
, "mit.davidben.der");
39 ASSERT_TRUE(mit_davidben_cert_
.get());
41 foaf_me_chromium_test_cert_
= net::ImportCertFromFile(
42 certs_dir
, "foaf.me.chromium-test-cert.der");
43 ASSERT_TRUE(foaf_me_chromium_test_cert_
.get());
45 cert_request_info_
= new net::SSLCertRequestInfo
;
46 cert_request_info_
->host_and_port
= "foo:123";
47 cert_request_info_
->client_certs
.push_back(mit_davidben_cert_
);
48 cert_request_info_
->client_certs
.push_back(foaf_me_chromium_test_cert_
);
51 void SSLClientCertificateSelectorTestBase::SetUpOnMainThread() {
52 url_request_context_getter_
= browser()->profile()->GetRequestContext();
54 BrowserThread::PostTask(
57 base::Bind(&SSLClientCertificateSelectorTestBase::SetUpOnIOThread
,
60 io_loop_finished_event_
.Wait();
62 content::WaitForLoadStop(
63 browser()->tab_strip_model()->GetActiveWebContents());
66 // Have to release our reference to the auth handler during the test to allow
67 // it to be destroyed while the Browser and its IO thread still exist.
68 void SSLClientCertificateSelectorTestBase::CleanUpOnMainThread() {
69 BrowserThread::PostTask(
72 base::Bind(&SSLClientCertificateSelectorTestBase::CleanUpOnIOThread
,
75 io_loop_finished_event_
.Wait();
77 auth_requestor_
= NULL
;
80 void SSLClientCertificateSelectorTestBase::SetUpOnIOThread() {
81 url_request_
= MakeURLRequest(url_request_context_getter_
.get());
83 auth_requestor_
= new StrictMock
<SSLClientAuthRequestorMock
>(
84 url_request_
, cert_request_info_
.get());
86 io_loop_finished_event_
.Signal();
89 void SSLClientCertificateSelectorTestBase::CleanUpOnIOThread() {
92 io_loop_finished_event_
.Signal();
95 net::URLRequest
* SSLClientCertificateSelectorTestBase::MakeURLRequest(
96 net::URLRequestContextGetter
* context_getter
) {
97 net::URLRequest
* request
=
98 context_getter
->GetURLRequestContext()->CreateRequest(
99 GURL("https://example"), NULL
);