| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "android_webview/browser/aw_contents_client_bridge.h" | 5 #include "android_webview/browser/aw_contents_client_bridge.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| 11 #include "base/android/scoped_java_ref.h" | 11 #include "base/android/scoped_java_ref.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "content/public/browser/client_certificate_delegate.h" | 16 #include "content/public/browser/client_certificate_delegate.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "jni/MockAwContentsClientBridge_jni.h" | 18 #include "jni/MockAwContentsClientBridge_jni.h" |
| 19 #include "net/android/net_jni_registrar.h" | 19 #include "net/android/net_jni_registrar.h" |
| 20 #include "net/ssl/ssl_cert_request_info.h" | 20 #include "net/ssl/ssl_cert_request_info.h" |
| 21 #include "net/ssl/ssl_private_key.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 24 |
| 24 using base::android::AttachCurrentThread; | 25 using base::android::AttachCurrentThread; |
| 25 using base::android::ScopedJavaLocalRef; | 26 using base::android::ScopedJavaLocalRef; |
| 26 using net::SSLCertRequestInfo; | 27 using net::SSLCertRequestInfo; |
| 27 using net::SSLClientCertType; | 28 using net::SSLClientCertType; |
| 29 using net::SSLPrivateKey; |
| 28 using net::X509Certificate; | 30 using net::X509Certificate; |
| 29 using testing::NotNull; | 31 using testing::NotNull; |
| 30 using testing::Test; | 32 using testing::Test; |
| 31 | 33 |
| 32 namespace android_webview { | 34 namespace android_webview { |
| 33 | 35 |
| 34 namespace { | 36 namespace { |
| 35 | 37 |
| 36 // Tests the android_webview contents client bridge. | 38 // Tests the android_webview contents client bridge. |
| 37 class AwContentsClientBridgeTest : public Test { | 39 class AwContentsClientBridgeTest : public Test { |
| 38 public: | 40 public: |
| 39 AwContentsClientBridgeTest() {} | 41 AwContentsClientBridgeTest() {} |
| 40 | 42 |
| 41 // Callback method called when a cert is selected. | 43 // Callback method called when a cert is selected. |
| 42 void CertSelected(X509Certificate* cert); | 44 void CertSelected(scoped_refptr<X509Certificate> cert, |
| 45 scoped_refptr<SSLPrivateKey> key); |
| 43 | 46 |
| 44 protected: | 47 protected: |
| 45 void SetUp() override; | 48 void SetUp() override; |
| 46 void TestCertType(SSLClientCertType type, const std::string& expected_name); | 49 void TestCertType(SSLClientCertType type, const std::string& expected_name); |
| 47 // Create the TestBrowserThreads. Just instantiate the member variable. | 50 // Create the TestBrowserThreads. Just instantiate the member variable. |
| 48 content::TestBrowserThreadBundle thread_bundle_; | 51 content::TestBrowserThreadBundle thread_bundle_; |
| 49 base::android::ScopedJavaGlobalRef<jobject> jbridge_; | 52 base::android::ScopedJavaGlobalRef<jobject> jbridge_; |
| 50 std::unique_ptr<AwContentsClientBridge> bridge_; | 53 std::unique_ptr<AwContentsClientBridge> bridge_; |
| 51 scoped_refptr<SSLCertRequestInfo> cert_request_info_; | 54 scoped_refptr<SSLCertRequestInfo> cert_request_info_; |
| 52 X509Certificate* selected_cert_; | 55 scoped_refptr<X509Certificate> selected_cert_; |
| 56 scoped_refptr<SSLPrivateKey> selected_key_; |
| 53 int cert_selected_callbacks_; | 57 int cert_selected_callbacks_; |
| 54 JNIEnv* env_; | 58 JNIEnv* env_; |
| 55 }; | 59 }; |
| 56 | 60 |
| 57 class TestClientCertificateDelegate | 61 class TestClientCertificateDelegate |
| 58 : public content::ClientCertificateDelegate { | 62 : public content::ClientCertificateDelegate { |
| 59 public: | 63 public: |
| 60 explicit TestClientCertificateDelegate(AwContentsClientBridgeTest* test) | 64 explicit TestClientCertificateDelegate(AwContentsClientBridgeTest* test) |
| 61 : test_(test) {} | 65 : test_(test) {} |
| 62 | 66 |
| 63 // content::ClientCertificateDelegate. | 67 // content::ClientCertificateDelegate. |
| 64 void ContinueWithCertificate(net::X509Certificate* cert) override { | 68 void ContinueWithCertificate(scoped_refptr<net::X509Certificate> cert, |
| 65 test_->CertSelected(cert); | 69 scoped_refptr<net::SSLPrivateKey> key) override { |
| 70 test_->CertSelected(std::move(cert), std::move(key)); |
| 66 test_ = nullptr; | 71 test_ = nullptr; |
| 67 } | 72 } |
| 68 | 73 |
| 69 private: | 74 private: |
| 70 AwContentsClientBridgeTest* test_; | 75 AwContentsClientBridgeTest* test_; |
| 71 | 76 |
| 72 DISALLOW_COPY_AND_ASSIGN(TestClientCertificateDelegate); | 77 DISALLOW_COPY_AND_ASSIGN(TestClientCertificateDelegate); |
| 73 }; | 78 }; |
| 74 | 79 |
| 75 } // namespace | 80 } // namespace |
| 76 | 81 |
| 77 void AwContentsClientBridgeTest::SetUp() { | 82 void AwContentsClientBridgeTest::SetUp() { |
| 78 env_ = AttachCurrentThread(); | 83 env_ = AttachCurrentThread(); |
| 79 ASSERT_THAT(env_, NotNull()); | 84 ASSERT_THAT(env_, NotNull()); |
| 80 ASSERT_TRUE(android_webview::RegisterAwContentsClientBridge(env_)); | 85 ASSERT_TRUE(android_webview::RegisterAwContentsClientBridge(env_)); |
| 81 ASSERT_TRUE(net::android::RegisterJni(env_)); | 86 ASSERT_TRUE(net::android::RegisterJni(env_)); |
| 82 jbridge_.Reset( | 87 jbridge_.Reset( |
| 83 env_, | 88 env_, |
| 84 Java_MockAwContentsClientBridge_getAwContentsClientBridge(env_).obj()); | 89 Java_MockAwContentsClientBridge_getAwContentsClientBridge(env_).obj()); |
| 85 bridge_.reset(new AwContentsClientBridge(env_, jbridge_)); | 90 bridge_.reset(new AwContentsClientBridge(env_, jbridge_)); |
| 86 selected_cert_ = nullptr; | 91 selected_cert_ = nullptr; |
| 87 cert_selected_callbacks_ = 0; | 92 cert_selected_callbacks_ = 0; |
| 88 cert_request_info_ = new net::SSLCertRequestInfo; | 93 cert_request_info_ = new net::SSLCertRequestInfo; |
| 89 } | 94 } |
| 90 | 95 |
| 91 void AwContentsClientBridgeTest::CertSelected(X509Certificate* cert) { | 96 void AwContentsClientBridgeTest::CertSelected( |
| 92 selected_cert_ = cert; | 97 scoped_refptr<X509Certificate> cert, |
| 98 scoped_refptr<SSLPrivateKey> key) { |
| 99 selected_cert_ = std::move(cert); |
| 100 selected_key_ = std::move(key); |
| 93 cert_selected_callbacks_++; | 101 cert_selected_callbacks_++; |
| 94 } | 102 } |
| 95 | 103 |
| 96 TEST_F(AwContentsClientBridgeTest, TestClientCertKeyTypesCorrectlyEncoded) { | 104 TEST_F(AwContentsClientBridgeTest, TestClientCertKeyTypesCorrectlyEncoded) { |
| 97 SSLClientCertType cert_types[2] = {net::CLIENT_CERT_RSA_SIGN, | 105 SSLClientCertType cert_types[2] = {net::CLIENT_CERT_RSA_SIGN, |
| 98 net::CLIENT_CERT_ECDSA_SIGN}; | 106 net::CLIENT_CERT_ECDSA_SIGN}; |
| 99 std::string expected_names[2] = {"RSA", "ECDSA"}; | 107 std::string expected_names[2] = {"RSA", "ECDSA"}; |
| 100 | 108 |
| 101 for (int i = 0; i < 2; i++) { | 109 for (int i = 0; i < 2; i++) { |
| 102 TestCertType(cert_types[i], expected_names[i]); | 110 TestCertType(cert_types[i], expected_names[i]); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 130 // can call on. | 138 // can call on. |
| 131 bridge_->SelectClientCertificate( | 139 bridge_->SelectClientCertificate( |
| 132 cert_request_info_.get(), | 140 cert_request_info_.get(), |
| 133 base::WrapUnique(new TestClientCertificateDelegate(this))); | 141 base::WrapUnique(new TestClientCertificateDelegate(this))); |
| 134 bridge_->ProvideClientCertificateResponse( | 142 bridge_->ProvideClientCertificateResponse( |
| 135 env_, jbridge_, | 143 env_, jbridge_, |
| 136 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_), | 144 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_), |
| 137 Java_MockAwContentsClientBridge_createTestCertChain(env_, jbridge_), | 145 Java_MockAwContentsClientBridge_createTestCertChain(env_, jbridge_), |
| 138 nullptr); | 146 nullptr); |
| 139 base::RunLoop().RunUntilIdle(); | 147 base::RunLoop().RunUntilIdle(); |
| 140 EXPECT_EQ(nullptr, selected_cert_); | 148 EXPECT_EQ(nullptr, selected_cert_.get()); |
| 149 EXPECT_EQ(nullptr, selected_key_.get()); |
| 141 EXPECT_EQ(1, cert_selected_callbacks_); | 150 EXPECT_EQ(1, cert_selected_callbacks_); |
| 142 } | 151 } |
| 143 | 152 |
| 144 // Verify that ProvideClientCertificateResponse calls the callback with | 153 // Verify that ProvideClientCertificateResponse calls the callback with |
| 145 // null parameters when private key is not provided. | 154 // null parameters when private key is not provided. |
| 146 TEST_F(AwContentsClientBridgeTest, | 155 TEST_F(AwContentsClientBridgeTest, |
| 147 TestProvideClientCertificateResponseCallsCallbackOnNullChain) { | 156 TestProvideClientCertificateResponseCallsCallbackOnNullChain) { |
| 148 // Call SelectClientCertificate to create a callback id that mock java object | 157 // Call SelectClientCertificate to create a callback id that mock java object |
| 149 // can call on. | 158 // can call on. |
| 150 bridge_->SelectClientCertificate( | 159 bridge_->SelectClientCertificate( |
| 151 cert_request_info_.get(), | 160 cert_request_info_.get(), |
| 152 base::WrapUnique(new TestClientCertificateDelegate(this))); | 161 base::WrapUnique(new TestClientCertificateDelegate(this))); |
| 153 int requestId = Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_); | 162 int requestId = Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_); |
| 154 bridge_->ProvideClientCertificateResponse(env_, jbridge_, requestId, nullptr, | 163 bridge_->ProvideClientCertificateResponse(env_, jbridge_, requestId, nullptr, |
| 155 nullptr); | 164 nullptr); |
| 156 base::RunLoop().RunUntilIdle(); | 165 base::RunLoop().RunUntilIdle(); |
| 157 EXPECT_EQ(nullptr, selected_cert_); | 166 EXPECT_EQ(nullptr, selected_cert_.get()); |
| 167 EXPECT_EQ(nullptr, selected_key_.get()); |
| 158 EXPECT_EQ(1, cert_selected_callbacks_); | 168 EXPECT_EQ(1, cert_selected_callbacks_); |
| 159 } | 169 } |
| 160 | 170 |
| 161 } // android_webview | 171 } // android_webview |
| OLD | NEW |