| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/certificate_selector.h" | 5 #include "chrome/browser/ui/views/certificate_selector.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 14 #include "chrome/grit/generated_resources.h" | 14 #include "chrome/grit/generated_resources.h" |
| 15 #include "chrome/test/base/in_process_browser_test.h" | 15 #include "chrome/test/base/in_process_browser_test.h" |
| 16 #include "chrome/test/base/interactive_test_utils.h" | 16 #include "chrome/test/base/interactive_test_utils.h" |
| 17 #include "content/public/test/browser_test_utils.h" | 17 #include "content/public/test/browser_test_utils.h" |
| 18 #include "net/cert/x509_certificate.h" | 18 #include "net/cert/x509_certificate.h" |
| 19 #include "net/ssl/client_cert_identity_test_util.h" |
| 19 #include "net/test/cert_test_util.h" | 20 #include "net/test/cert_test_util.h" |
| 20 #include "net/test/test_data_directory.h" | 21 #include "net/test/test_data_directory.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "ui/base/models/table_model.h" | 23 #include "ui/base/models/table_model.h" |
| 23 #include "ui/views/controls/label.h" | 24 #include "ui/views/controls/label.h" |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 class TestCertificateSelector : public chrome::CertificateSelector { | 28 class TestCertificateSelector : public chrome::CertificateSelector { |
| 28 public: | 29 public: |
| 29 TestCertificateSelector(const net::CertificateList& certificates, | 30 TestCertificateSelector(net::ClientCertIdentityList certificates, |
| 30 content::WebContents* web_contents) | 31 content::WebContents* web_contents) |
| 31 : CertificateSelector(certificates, web_contents) {} | 32 : CertificateSelector(std::move(certificates), web_contents) {} |
| 32 | 33 |
| 33 ~TestCertificateSelector() override { | 34 ~TestCertificateSelector() override { |
| 34 if (!on_destroy_.is_null()) | 35 if (!on_destroy_.is_null()) |
| 35 on_destroy_.Run(); | 36 on_destroy_.Run(); |
| 36 } | 37 } |
| 37 | 38 |
| 38 void Init() { | 39 void Init() { |
| 39 InitWithText(base::MakeUnique<views::Label>( | 40 InitWithText(base::MakeUnique<views::Label>( |
| 40 base::ASCIIToUTF16("some arbitrary text"))); | 41 base::ASCIIToUTF16("some arbitrary text"))); |
| 41 } | 42 } |
| 42 | 43 |
| 43 bool Accept() override { | 44 bool AcceptCertificate( |
| 45 std::unique_ptr<net::ClientCertIdentity> identity) override { |
| 44 if (accepted_) | 46 if (accepted_) |
| 45 *accepted_ = true; | 47 *accepted_ = true; |
| 46 return CertificateSelector::Accept(); | 48 return true; |
| 47 } | 49 } |
| 48 | 50 |
| 49 bool Cancel() override { | 51 bool Cancel() override { |
| 50 if (canceled_) | 52 if (canceled_) |
| 51 *canceled_ = true; | 53 *canceled_ = true; |
| 52 return CertificateSelector::Cancel(); | 54 return CertificateSelector::Cancel(); |
| 53 } | 55 } |
| 54 | 56 |
| 55 void TrackState(bool* accepted, bool* canceled) { | 57 void TrackState(bool* accepted, bool* canceled) { |
| 56 accepted_ = accepted; | 58 accepted_ = accepted; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 78 | 80 |
| 79 client_2_ = | 81 client_2_ = |
| 80 net::ImportCertFromFile(net::GetTestCertsDirectory(), "client_2.pem"); | 82 net::ImportCertFromFile(net::GetTestCertsDirectory(), "client_2.pem"); |
| 81 ASSERT_TRUE(client_2_); | 83 ASSERT_TRUE(client_2_); |
| 82 } | 84 } |
| 83 | 85 |
| 84 void SetUpOnMainThread() override { | 86 void SetUpOnMainThread() override { |
| 85 ASSERT_TRUE(content::WaitForLoadStop( | 87 ASSERT_TRUE(content::WaitForLoadStop( |
| 86 browser()->tab_strip_model()->GetActiveWebContents())); | 88 browser()->tab_strip_model()->GetActiveWebContents())); |
| 87 | 89 |
| 88 net::CertificateList certificates; | |
| 89 certificates.push_back(client_1_); | |
| 90 certificates.push_back(client_2_); | |
| 91 | |
| 92 selector_ = new TestCertificateSelector( | 90 selector_ = new TestCertificateSelector( |
| 93 certificates, browser()->tab_strip_model()->GetActiveWebContents()); | 91 net::FakeClientCertIdentityListFromCertificateList( |
| 92 {client_1_, client_2_}), |
| 93 browser()->tab_strip_model()->GetActiveWebContents()); |
| 94 selector_->Init(); | 94 selector_->Init(); |
| 95 selector_->Show(); | 95 selector_->Show(); |
| 96 } | 96 } |
| 97 | 97 |
| 98 protected: | 98 protected: |
| 99 scoped_refptr<net::X509Certificate> client_1_; | 99 scoped_refptr<net::X509Certificate> client_1_; |
| 100 scoped_refptr<net::X509Certificate> client_2_; | 100 scoped_refptr<net::X509Certificate> client_2_; |
| 101 | 101 |
| 102 // The selector will be owned by the Views hierarchy and will at latest be | 102 // The selector will be owned by the Views hierarchy and will at latest be |
| 103 // deleted during the browser shutdown. | 103 // deleted during the browser shutdown. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 121 model->GetText(1, IDS_CERT_SELECTOR_SUBJECT_COLUMN)); | 121 model->GetText(1, IDS_CERT_SELECTOR_SUBJECT_COLUMN)); |
| 122 EXPECT_EQ(base::UTF8ToUTF16("E CA"), | 122 EXPECT_EQ(base::UTF8ToUTF16("E CA"), |
| 123 model->GetText(1, IDS_CERT_SELECTOR_ISSUER_COLUMN)); | 123 model->GetText(1, IDS_CERT_SELECTOR_ISSUER_COLUMN)); |
| 124 EXPECT_EQ(base::string16(), | 124 EXPECT_EQ(base::string16(), |
| 125 model->GetText(1, IDS_CERT_SELECTOR_PROVIDER_COLUMN)); | 125 model->GetText(1, IDS_CERT_SELECTOR_PROVIDER_COLUMN)); |
| 126 EXPECT_EQ(base::UTF8ToUTF16("1002"), | 126 EXPECT_EQ(base::UTF8ToUTF16("1002"), |
| 127 model->GetText(1, IDS_CERT_SELECTOR_SERIAL_COLUMN)); | 127 model->GetText(1, IDS_CERT_SELECTOR_SERIAL_COLUMN)); |
| 128 } | 128 } |
| 129 | 129 |
| 130 IN_PROC_BROWSER_TEST_F(CertificateSelectorTest, GetSelectedCert) { | 130 IN_PROC_BROWSER_TEST_F(CertificateSelectorTest, GetSelectedCert) { |
| 131 EXPECT_EQ(client_1_.get(), selector_->GetSelectedCert()); | 131 ASSERT_TRUE(selector_->GetSelectedCert()); |
| 132 EXPECT_EQ(client_1_.get(), selector_->GetSelectedCert()->certificate()); |
| 132 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_DOWN, false, | 133 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_DOWN, false, |
| 133 false, false, false)); | 134 false, false, false)); |
| 134 EXPECT_EQ(client_2_.get(), selector_->GetSelectedCert()); | 135 ASSERT_TRUE(selector_->GetSelectedCert()); |
| 136 EXPECT_EQ(client_2_.get(), selector_->GetSelectedCert()->certificate()); |
| 135 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_UP, false, | 137 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_UP, false, |
| 136 false, false, false)); | 138 false, false, false)); |
| 137 EXPECT_EQ(client_1_.get(), selector_->GetSelectedCert()); | 139 ASSERT_TRUE(selector_->GetSelectedCert()); |
| 140 EXPECT_EQ(client_1_.get(), selector_->GetSelectedCert()->certificate()); |
| 138 } | 141 } |
| 139 | 142 |
| 140 IN_PROC_BROWSER_TEST_F(CertificateSelectorTest, DoubleClick) { | 143 IN_PROC_BROWSER_TEST_F(CertificateSelectorTest, DoubleClick) { |
| 141 bool accepted = false; | 144 bool accepted = false; |
| 142 bool canceled = false; | 145 bool canceled = false; |
| 143 selector_->TrackState(&accepted, &canceled); | 146 selector_->TrackState(&accepted, &canceled); |
| 144 | 147 |
| 145 base::RunLoop loop; | 148 base::RunLoop loop; |
| 146 selector_->set_on_destroy(loop.QuitClosure()); | 149 selector_->set_on_destroy(loop.QuitClosure()); |
| 147 | 150 |
| 148 // Simulate double clicking on an entry in the certificate list. | 151 // Simulate double clicking on an entry in the certificate list. |
| 149 selector_->OnDoubleClick(); | 152 selector_->OnDoubleClick(); |
| 150 | 153 |
| 151 // Wait for the dialog to be closed and destroyed. | 154 // Wait for the dialog to be closed and destroyed. |
| 152 loop.Run(); | 155 loop.Run(); |
| 153 | 156 |
| 154 // Closing the dialog through a double click must call only the Accept() | 157 // Closing the dialog through a double click must call only the Accept() |
| 155 // function and not Cancel(). | 158 // function and not Cancel(). |
| 156 EXPECT_TRUE(accepted); | 159 EXPECT_TRUE(accepted); |
| 157 EXPECT_FALSE(canceled); | 160 EXPECT_FALSE(canceled); |
| 158 } | 161 } |
| OLD | NEW |