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.
4
5#include "chrome/browser/ssl/ssl_client_certificate_selector_test.h"
6
7#include "base/bind.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/request_priority.h"
16#include "net/base/test_data_directory.h"
17#include "net/cert/x509_certificate.h"
18#include "net/http/http_transaction_factory.h"
19#include "net/ssl/ssl_cert_request_info.h"
20#include "net/test/cert_test_util.h"
21#include "net/url_request/url_request.h"
22#include "net/url_request/url_request_context.h"
23#include "net/url_request/url_request_context_getter.h"
24
25using ::testing::Mock;
26using ::testing::StrictMock;
27using content::BrowserThread;
28
29SSLClientCertificateSelectorTestBase::SSLClientCertificateSelectorTestBase()
30    : io_loop_finished_event_(false, false) {
31}
32
33SSLClientCertificateSelectorTestBase::~SSLClientCertificateSelectorTestBase() {
34}
35
36void SSLClientCertificateSelectorTestBase::SetUpInProcessBrowserTestFixture() {
37  base::FilePath certs_dir = net::GetTestCertsDirectory();
38
39  mit_davidben_cert_ = net::ImportCertFromFile(certs_dir, "mit.davidben.der");
40  ASSERT_TRUE(mit_davidben_cert_.get());
41
42  foaf_me_chromium_test_cert_ = net::ImportCertFromFile(
43      certs_dir, "foaf.me.chromium-test-cert.der");
44  ASSERT_TRUE(foaf_me_chromium_test_cert_.get());
45
46  cert_request_info_ = new net::SSLCertRequestInfo;
47  cert_request_info_->host_and_port = net::HostPortPair("foo", 123);
48  cert_request_info_->client_certs.push_back(mit_davidben_cert_);
49  cert_request_info_->client_certs.push_back(foaf_me_chromium_test_cert_);
50}
51
52void SSLClientCertificateSelectorTestBase::SetUpOnMainThread() {
53  url_request_context_getter_ = browser()->profile()->GetRequestContext();
54
55  BrowserThread::PostTask(
56      BrowserThread::IO,
57      FROM_HERE,
58      base::Bind(&SSLClientCertificateSelectorTestBase::SetUpOnIOThread,
59                 this));
60
61  io_loop_finished_event_.Wait();
62
63  content::WaitForLoadStop(
64      browser()->tab_strip_model()->GetActiveWebContents());
65}
66
67// Have to release our reference to the auth handler during the test to allow
68// it to be destroyed while the Browser and its IO thread still exist.
69void SSLClientCertificateSelectorTestBase::TearDownOnMainThread() {
70  BrowserThread::PostTask(
71      BrowserThread::IO,
72      FROM_HERE,
73      base::Bind(&SSLClientCertificateSelectorTestBase::TearDownOnIOThread,
74                 this));
75
76  io_loop_finished_event_.Wait();
77
78  auth_requestor_ = NULL;
79}
80
81void SSLClientCertificateSelectorTestBase::SetUpOnIOThread() {
82  url_request_ = MakeURLRequest(url_request_context_getter_.get()).release();
83
84  auth_requestor_ = new StrictMock<SSLClientAuthRequestorMock>(
85      url_request_, cert_request_info_.get());
86
87  io_loop_finished_event_.Signal();
88}
89
90void SSLClientCertificateSelectorTestBase::TearDownOnIOThread() {
91  delete url_request_;
92
93  io_loop_finished_event_.Signal();
94}
95
96scoped_ptr<net::URLRequest>
97SSLClientCertificateSelectorTestBase::MakeURLRequest(
98    net::URLRequestContextGetter* context_getter) {
99  scoped_ptr<net::URLRequest> request =
100      context_getter->GetURLRequestContext()->CreateRequest(
101          GURL("https://example"), net::DEFAULT_PRIORITY, NULL, NULL);
102  return request.Pass();
103}
104