| 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/native/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" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 29 using testing::NotNull; | 29 using testing::NotNull; |
| 30 using testing::Test; | 30 using testing::Test; |
| 31 | 31 |
| 32 namespace android_webview { | 32 namespace android_webview { |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 // Tests the android_webview contents client bridge. | 36 // Tests the android_webview contents client bridge. |
| 37 class AwContentsClientBridgeTest : public Test { | 37 class AwContentsClientBridgeTest : public Test { |
| 38 public: | 38 public: |
| 39 AwContentsClientBridgeTest() { } | 39 AwContentsClientBridgeTest() {} |
| 40 | 40 |
| 41 // Callback method called when a cert is selected. | 41 // Callback method called when a cert is selected. |
| 42 void CertSelected(X509Certificate* cert); | 42 void CertSelected(X509Certificate* cert); |
| 43 |
| 43 protected: | 44 protected: |
| 44 void SetUp() override; | 45 void SetUp() override; |
| 45 void TestCertType(SSLClientCertType type, const std::string& expected_name); | 46 void TestCertType(SSLClientCertType type, const std::string& expected_name); |
| 46 // Create the TestBrowserThreads. Just instantiate the member variable. | 47 // Create the TestBrowserThreads. Just instantiate the member variable. |
| 47 content::TestBrowserThreadBundle thread_bundle_; | 48 content::TestBrowserThreadBundle thread_bundle_; |
| 48 base::android::ScopedJavaGlobalRef<jobject> jbridge_; | 49 base::android::ScopedJavaGlobalRef<jobject> jbridge_; |
| 49 std::unique_ptr<AwContentsClientBridge> bridge_; | 50 std::unique_ptr<AwContentsClientBridge> bridge_; |
| 50 scoped_refptr<SSLCertRequestInfo> cert_request_info_; | 51 scoped_refptr<SSLCertRequestInfo> cert_request_info_; |
| 51 X509Certificate* selected_cert_; | 52 X509Certificate* selected_cert_; |
| 52 int cert_selected_callbacks_; | 53 int cert_selected_callbacks_; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 64 test_->CertSelected(cert); | 65 test_->CertSelected(cert); |
| 65 test_ = nullptr; | 66 test_ = nullptr; |
| 66 } | 67 } |
| 67 | 68 |
| 68 private: | 69 private: |
| 69 AwContentsClientBridgeTest* test_; | 70 AwContentsClientBridgeTest* test_; |
| 70 | 71 |
| 71 DISALLOW_COPY_AND_ASSIGN(TestClientCertificateDelegate); | 72 DISALLOW_COPY_AND_ASSIGN(TestClientCertificateDelegate); |
| 72 }; | 73 }; |
| 73 | 74 |
| 74 } // namespace | 75 } // namespace |
| 75 | 76 |
| 76 void AwContentsClientBridgeTest::SetUp() { | 77 void AwContentsClientBridgeTest::SetUp() { |
| 77 env_ = AttachCurrentThread(); | 78 env_ = AttachCurrentThread(); |
| 78 ASSERT_THAT(env_, NotNull()); | 79 ASSERT_THAT(env_, NotNull()); |
| 79 ASSERT_TRUE(android_webview::RegisterAwContentsClientBridge(env_)); | 80 ASSERT_TRUE(android_webview::RegisterAwContentsClientBridge(env_)); |
| 80 ASSERT_TRUE(net::android::RegisterJni(env_)); | 81 ASSERT_TRUE(net::android::RegisterJni(env_)); |
| 81 jbridge_.Reset(env_, | 82 jbridge_.Reset( |
| 83 env_, |
| 82 Java_MockAwContentsClientBridge_getAwContentsClientBridge(env_).obj()); | 84 Java_MockAwContentsClientBridge_getAwContentsClientBridge(env_).obj()); |
| 83 bridge_.reset(new AwContentsClientBridge(env_, jbridge_)); | 85 bridge_.reset(new AwContentsClientBridge(env_, jbridge_)); |
| 84 selected_cert_ = nullptr; | 86 selected_cert_ = nullptr; |
| 85 cert_selected_callbacks_ = 0; | 87 cert_selected_callbacks_ = 0; |
| 86 cert_request_info_ = new net::SSLCertRequestInfo; | 88 cert_request_info_ = new net::SSLCertRequestInfo; |
| 87 } | 89 } |
| 88 | 90 |
| 89 void AwContentsClientBridgeTest::CertSelected(X509Certificate* cert) { | 91 void AwContentsClientBridgeTest::CertSelected(X509Certificate* cert) { |
| 90 selected_cert_ = cert; | 92 selected_cert_ = cert; |
| 91 cert_selected_callbacks_++; | 93 cert_selected_callbacks_++; |
| 92 } | 94 } |
| 93 | 95 |
| 94 TEST_F(AwContentsClientBridgeTest, TestClientCertKeyTypesCorrectlyEncoded) { | 96 TEST_F(AwContentsClientBridgeTest, TestClientCertKeyTypesCorrectlyEncoded) { |
| 95 SSLClientCertType cert_types[2] = {net::CLIENT_CERT_RSA_SIGN, | 97 SSLClientCertType cert_types[2] = {net::CLIENT_CERT_RSA_SIGN, |
| 96 net::CLIENT_CERT_ECDSA_SIGN}; | 98 net::CLIENT_CERT_ECDSA_SIGN}; |
| 97 std::string expected_names[2] = {"RSA", "ECDSA"}; | 99 std::string expected_names[2] = {"RSA", "ECDSA"}; |
| 98 | 100 |
| 99 for (int i = 0; i < 2; i++) { | 101 for (int i = 0; i < 2; i++) { |
| 100 TestCertType(cert_types[i], expected_names[i]); | 102 TestCertType(cert_types[i], expected_names[i]); |
| 101 } | 103 } |
| 102 } | 104 } |
| 103 | 105 |
| 104 void AwContentsClientBridgeTest::TestCertType(SSLClientCertType type, | 106 void AwContentsClientBridgeTest::TestCertType( |
| 105 const std::string& expected_name) { | 107 SSLClientCertType type, |
| 108 const std::string& expected_name) { |
| 106 cert_request_info_->cert_key_types.clear(); | 109 cert_request_info_->cert_key_types.clear(); |
| 107 cert_request_info_->cert_key_types.push_back(type); | 110 cert_request_info_->cert_key_types.push_back(type); |
| 108 bridge_->SelectClientCertificate( | 111 bridge_->SelectClientCertificate( |
| 109 cert_request_info_.get(), | 112 cert_request_info_.get(), |
| 110 base::MakeUnique<TestClientCertificateDelegate>(this)); | 113 base::MakeUnique<TestClientCertificateDelegate>(this)); |
| 111 base::RunLoop().RunUntilIdle(); | 114 base::RunLoop().RunUntilIdle(); |
| 112 EXPECT_EQ(0, cert_selected_callbacks_); | 115 EXPECT_EQ(0, cert_selected_callbacks_); |
| 113 ScopedJavaLocalRef<jobjectArray> key_types = | 116 ScopedJavaLocalRef<jobjectArray> key_types = |
| 114 Java_MockAwContentsClientBridge_getKeyTypes(env_, jbridge_); | 117 Java_MockAwContentsClientBridge_getKeyTypes(env_, jbridge_); |
| 115 std::vector<std::string> vec; | 118 std::vector<std::string> vec; |
| 116 base::android::AppendJavaStringArrayToStringVector(env_, | 119 base::android::AppendJavaStringArrayToStringVector(env_, key_types.obj(), |
| 117 key_types.obj(), | |
| 118 &vec); | 120 &vec); |
| 119 EXPECT_EQ(1u, vec.size()); | 121 EXPECT_EQ(1u, vec.size()); |
| 120 EXPECT_EQ(expected_name, vec[0]); | 122 EXPECT_EQ(expected_name, vec[0]); |
| 121 } | 123 } |
| 122 | 124 |
| 123 // Verify that ProvideClientCertificateResponse works properly when the client | 125 // Verify that ProvideClientCertificateResponse works properly when the client |
| 124 // responds with a null key. | 126 // responds with a null key. |
| 125 TEST_F(AwContentsClientBridgeTest, | 127 TEST_F(AwContentsClientBridgeTest, |
| 126 TestProvideClientCertificateResponseCallsCallbackOnNullKey) { | 128 TestProvideClientCertificateResponseCallsCallbackOnNullKey) { |
| 127 // Call SelectClientCertificate to create a callback id that mock java object | 129 // Call SelectClientCertificate to create a callback id that mock java object |
| 128 // can call on. | 130 // can call on. |
| 129 bridge_->SelectClientCertificate( | 131 bridge_->SelectClientCertificate( |
| 130 cert_request_info_.get(), | 132 cert_request_info_.get(), |
| 131 base::WrapUnique(new TestClientCertificateDelegate(this))); | 133 base::WrapUnique(new TestClientCertificateDelegate(this))); |
| 132 bridge_->ProvideClientCertificateResponse( | 134 bridge_->ProvideClientCertificateResponse( |
| 133 env_, jbridge_, | 135 env_, jbridge_, |
| 134 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_), | 136 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_), |
| 135 Java_MockAwContentsClientBridge_createTestCertChain(env_, jbridge_), | 137 Java_MockAwContentsClientBridge_createTestCertChain(env_, jbridge_), |
| 136 nullptr); | 138 nullptr); |
| 137 base::RunLoop().RunUntilIdle(); | 139 base::RunLoop().RunUntilIdle(); |
| 138 EXPECT_EQ(nullptr, selected_cert_); | 140 EXPECT_EQ(nullptr, selected_cert_); |
| 139 EXPECT_EQ(1, cert_selected_callbacks_); | 141 EXPECT_EQ(1, cert_selected_callbacks_); |
| 140 } | 142 } |
| 141 | 143 |
| 142 // Verify that ProvideClientCertificateResponse calls the callback with | 144 // Verify that ProvideClientCertificateResponse calls the callback with |
| 143 // null parameters when private key is not provided. | 145 // null parameters when private key is not provided. |
| 144 TEST_F(AwContentsClientBridgeTest, | 146 TEST_F(AwContentsClientBridgeTest, |
| 145 TestProvideClientCertificateResponseCallsCallbackOnNullChain) { | 147 TestProvideClientCertificateResponseCallsCallbackOnNullChain) { |
| 146 // Call SelectClientCertificate to create a callback id that mock java object | 148 // Call SelectClientCertificate to create a callback id that mock java object |
| 147 // can call on. | 149 // can call on. |
| 148 bridge_->SelectClientCertificate( | 150 bridge_->SelectClientCertificate( |
| 149 cert_request_info_.get(), | 151 cert_request_info_.get(), |
| 150 base::WrapUnique(new TestClientCertificateDelegate(this))); | 152 base::WrapUnique(new TestClientCertificateDelegate(this))); |
| 151 int requestId = Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_); | 153 int requestId = Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_); |
| 152 bridge_->ProvideClientCertificateResponse(env_, jbridge_, requestId, nullptr, | 154 bridge_->ProvideClientCertificateResponse(env_, jbridge_, requestId, nullptr, |
| 153 nullptr); | 155 nullptr); |
| 154 base::RunLoop().RunUntilIdle(); | 156 base::RunLoop().RunUntilIdle(); |
| 155 EXPECT_EQ(nullptr, selected_cert_); | 157 EXPECT_EQ(nullptr, selected_cert_); |
| 156 EXPECT_EQ(1, cert_selected_callbacks_); | 158 EXPECT_EQ(1, cert_selected_callbacks_); |
| 157 } | 159 } |
| 158 | 160 |
| 159 } // android_webview | 161 } // android_webview |
| OLD | NEW |