| 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/native/aw_contents_client_bridge.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_array.h" | 8 #include "base/android/jni_array.h" |
| 9 #include "base/android/scoped_java_ref.h" | 9 #include "base/android/scoped_java_ref.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/macros.h" |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "content/public/browser/client_certificate_delegate.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "jni/MockAwContentsClientBridge_jni.h" | 16 #include "jni/MockAwContentsClientBridge_jni.h" |
| 15 #include "net/android/net_jni_registrar.h" | 17 #include "net/android/net_jni_registrar.h" |
| 16 #include "net/ssl/ssl_cert_request_info.h" | 18 #include "net/ssl/ssl_cert_request_info.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 21 |
| 20 | 22 |
| 21 using base::android::AttachCurrentThread; | 23 using base::android::AttachCurrentThread; |
| 22 using base::android::ScopedJavaLocalRef; | 24 using base::android::ScopedJavaLocalRef; |
| 23 using net::SSLCertRequestInfo; | 25 using net::SSLCertRequestInfo; |
| 24 using net::SSLClientCertType; | 26 using net::SSLClientCertType; |
| 25 using net::X509Certificate; | 27 using net::X509Certificate; |
| 26 using testing::NotNull; | 28 using testing::NotNull; |
| 27 using testing::Test; | 29 using testing::Test; |
| 28 | 30 |
| 29 namespace android_webview { | 31 namespace android_webview { |
| 30 | 32 |
| 31 namespace { | 33 namespace { |
| 32 | 34 |
| 33 // Tests the android_webview contents client bridge. | 35 // Tests the android_webview contents client bridge. |
| 34 class AwContentsClientBridgeTest : public Test { | 36 class AwContentsClientBridgeTest : public Test { |
| 35 public: | 37 public: |
| 36 typedef AwContentsClientBridge::SelectCertificateCallback | |
| 37 SelectCertificateCallback; | |
| 38 | |
| 39 AwContentsClientBridgeTest() { } | 38 AwContentsClientBridgeTest() { } |
| 40 | 39 |
| 41 // Callback method called when a cert is selected. | 40 // Callback method called when a cert is selected. |
| 42 void CertSelected(X509Certificate* cert); | 41 void CertSelected(X509Certificate* cert); |
| 43 protected: | 42 protected: |
| 44 void SetUp() override; | 43 void SetUp() override; |
| 45 void TestCertType(SSLClientCertType type, const std::string& expected_name); | 44 void TestCertType(SSLClientCertType type, const std::string& expected_name); |
| 46 // Create the TestBrowserThreads. Just instantiate the member variable. | 45 // Create the TestBrowserThreads. Just instantiate the member variable. |
| 47 content::TestBrowserThreadBundle thread_bundle_; | 46 content::TestBrowserThreadBundle thread_bundle_; |
| 48 base::android::ScopedJavaGlobalRef<jobject> jbridge_; | 47 base::android::ScopedJavaGlobalRef<jobject> jbridge_; |
| 49 scoped_ptr<AwContentsClientBridge> bridge_; | 48 scoped_ptr<AwContentsClientBridge> bridge_; |
| 50 scoped_refptr<SSLCertRequestInfo> cert_request_info_; | 49 scoped_refptr<SSLCertRequestInfo> cert_request_info_; |
| 51 X509Certificate* selected_cert_; | 50 X509Certificate* selected_cert_; |
| 52 int cert_selected_callbacks_; | 51 int cert_selected_callbacks_; |
| 53 JNIEnv* env_; | 52 JNIEnv* env_; |
| 54 }; | 53 }; |
| 55 | 54 |
| 55 class TestClientCertificateDelegate |
| 56 : public content::ClientCertificateDelegate { |
| 57 public: |
| 58 explicit TestClientCertificateDelegate(AwContentsClientBridgeTest* test) |
| 59 : test_(test) {} |
| 60 |
| 61 // content::ClientCertificateDelegate. |
| 62 void ContinueWithCertificate(net::X509Certificate* cert) override { |
| 63 test_->CertSelected(cert); |
| 64 } |
| 65 |
| 66 void CancelCertificateSelection() override { |
| 67 ADD_FAILURE() << "Selection canceled"; |
| 68 } |
| 69 |
| 70 private: |
| 71 AwContentsClientBridgeTest* test_; |
| 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(TestClientCertificateDelegate); |
| 74 }; |
| 75 |
| 56 } // namespace | 76 } // namespace |
| 57 | 77 |
| 58 void AwContentsClientBridgeTest::SetUp() { | 78 void AwContentsClientBridgeTest::SetUp() { |
| 59 env_ = AttachCurrentThread(); | 79 env_ = AttachCurrentThread(); |
| 60 ASSERT_THAT(env_, NotNull()); | 80 ASSERT_THAT(env_, NotNull()); |
| 61 ASSERT_TRUE(android_webview::RegisterAwContentsClientBridge(env_)); | 81 ASSERT_TRUE(android_webview::RegisterAwContentsClientBridge(env_)); |
| 62 ASSERT_TRUE(RegisterNativesImpl(env_)); | 82 ASSERT_TRUE(RegisterNativesImpl(env_)); |
| 63 ASSERT_TRUE(net::android::RegisterJni(env_)); | 83 ASSERT_TRUE(net::android::RegisterJni(env_)); |
| 64 jbridge_.Reset(env_, | 84 jbridge_.Reset(env_, |
| 65 Java_MockAwContentsClientBridge_getAwContentsClientBridge(env_).obj()); | 85 Java_MockAwContentsClientBridge_getAwContentsClientBridge(env_).obj()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 83 TestCertType(cert_types[i], expected_names[i]); | 103 TestCertType(cert_types[i], expected_names[i]); |
| 84 } | 104 } |
| 85 } | 105 } |
| 86 | 106 |
| 87 void AwContentsClientBridgeTest::TestCertType(SSLClientCertType type, | 107 void AwContentsClientBridgeTest::TestCertType(SSLClientCertType type, |
| 88 const std::string& expected_name) { | 108 const std::string& expected_name) { |
| 89 cert_request_info_->cert_key_types.clear(); | 109 cert_request_info_->cert_key_types.clear(); |
| 90 cert_request_info_->cert_key_types.push_back(type); | 110 cert_request_info_->cert_key_types.push_back(type); |
| 91 bridge_->SelectClientCertificate( | 111 bridge_->SelectClientCertificate( |
| 92 cert_request_info_.get(), | 112 cert_request_info_.get(), |
| 93 base::Bind( | 113 make_scoped_ptr(new TestClientCertificateDelegate(this))); |
| 94 &AwContentsClientBridgeTest::CertSelected, | |
| 95 base::Unretained(static_cast<AwContentsClientBridgeTest*>(this)))); | |
| 96 base::RunLoop().RunUntilIdle(); | 114 base::RunLoop().RunUntilIdle(); |
| 97 EXPECT_EQ(0, cert_selected_callbacks_); | 115 EXPECT_EQ(0, cert_selected_callbacks_); |
| 98 ScopedJavaLocalRef<jobjectArray> key_types = | 116 ScopedJavaLocalRef<jobjectArray> key_types = |
| 99 Java_MockAwContentsClientBridge_getKeyTypes(env_, jbridge_.obj()); | 117 Java_MockAwContentsClientBridge_getKeyTypes(env_, jbridge_.obj()); |
| 100 std::vector<std::string> vec; | 118 std::vector<std::string> vec; |
| 101 base::android::AppendJavaStringArrayToStringVector(env_, | 119 base::android::AppendJavaStringArrayToStringVector(env_, |
| 102 key_types.obj(), | 120 key_types.obj(), |
| 103 &vec); | 121 &vec); |
| 104 EXPECT_EQ(1u, vec.size()); | 122 EXPECT_EQ(1u, vec.size()); |
| 105 EXPECT_EQ(expected_name, vec[0]); | 123 EXPECT_EQ(expected_name, vec[0]); |
| 106 } | 124 } |
| 107 | 125 |
| 108 // Verify that ProvideClientCertificateResponse works properly when the client | 126 // Verify that ProvideClientCertificateResponse works properly when the client |
| 109 // responds with a null key. | 127 // responds with a null key. |
| 110 TEST_F(AwContentsClientBridgeTest, | 128 TEST_F(AwContentsClientBridgeTest, |
| 111 TestProvideClientCertificateResponseCallsCallbackOnNullKey) { | 129 TestProvideClientCertificateResponseCallsCallbackOnNullKey) { |
| 112 // Call SelectClientCertificate to create a callback id that mock java object | 130 // Call SelectClientCertificate to create a callback id that mock java object |
| 113 // can call on. | 131 // can call on. |
| 114 bridge_->SelectClientCertificate( | 132 bridge_->SelectClientCertificate( |
| 115 cert_request_info_.get(), | 133 cert_request_info_.get(), |
| 116 base::Bind( | 134 make_scoped_ptr(new TestClientCertificateDelegate(this))); |
| 117 &AwContentsClientBridgeTest::CertSelected, | |
| 118 base::Unretained(static_cast<AwContentsClientBridgeTest*>(this)))); | |
| 119 bridge_->ProvideClientCertificateResponse(env_, jbridge_.obj(), | 135 bridge_->ProvideClientCertificateResponse(env_, jbridge_.obj(), |
| 120 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_.obj()), | 136 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_.obj()), |
| 121 Java_MockAwContentsClientBridge_createTestCertChain( | 137 Java_MockAwContentsClientBridge_createTestCertChain( |
| 122 env_, jbridge_.obj()).obj(), | 138 env_, jbridge_.obj()).obj(), |
| 123 NULL); | 139 NULL); |
| 124 base::RunLoop().RunUntilIdle(); | 140 base::RunLoop().RunUntilIdle(); |
| 125 EXPECT_EQ(NULL, selected_cert_); | 141 EXPECT_EQ(NULL, selected_cert_); |
| 126 EXPECT_EQ(1, cert_selected_callbacks_); | 142 EXPECT_EQ(1, cert_selected_callbacks_); |
| 127 } | 143 } |
| 128 | 144 |
| 129 // Verify that ProvideClientCertificateResponse calls the callback with | 145 // Verify that ProvideClientCertificateResponse calls the callback with |
| 130 // NULL parameters when private key is not provided. | 146 // NULL parameters when private key is not provided. |
| 131 TEST_F(AwContentsClientBridgeTest, | 147 TEST_F(AwContentsClientBridgeTest, |
| 132 TestProvideClientCertificateResponseCallsCallbackOnNullChain) { | 148 TestProvideClientCertificateResponseCallsCallbackOnNullChain) { |
| 133 // Call SelectClientCertificate to create a callback id that mock java object | 149 // Call SelectClientCertificate to create a callback id that mock java object |
| 134 // can call on. | 150 // can call on. |
| 135 bridge_->SelectClientCertificate( | 151 bridge_->SelectClientCertificate( |
| 136 cert_request_info_.get(), | 152 cert_request_info_.get(), |
| 137 base::Bind( | 153 make_scoped_ptr(new TestClientCertificateDelegate(this))); |
| 138 &AwContentsClientBridgeTest::CertSelected, | |
| 139 base::Unretained(static_cast<AwContentsClientBridgeTest*>(this)))); | |
| 140 int requestId = | 154 int requestId = |
| 141 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_.obj()); | 155 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_.obj()); |
| 142 bridge_->ProvideClientCertificateResponse(env_, jbridge_.obj(), | 156 bridge_->ProvideClientCertificateResponse(env_, jbridge_.obj(), |
| 143 requestId, | 157 requestId, |
| 144 NULL, | 158 NULL, |
| 145 Java_MockAwContentsClientBridge_createTestPrivateKey( | 159 Java_MockAwContentsClientBridge_createTestPrivateKey( |
| 146 env_, jbridge_.obj()).obj()); | 160 env_, jbridge_.obj()).obj()); |
| 147 base::RunLoop().RunUntilIdle(); | 161 base::RunLoop().RunUntilIdle(); |
| 148 EXPECT_EQ(NULL, selected_cert_); | 162 EXPECT_EQ(NULL, selected_cert_); |
| 149 EXPECT_EQ(1, cert_selected_callbacks_); | 163 EXPECT_EQ(1, cert_selected_callbacks_); |
| 150 } | 164 } |
| 151 | 165 |
| 152 } // android_webview | 166 } // android_webview |
| OLD | NEW |