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 <memory> |
| 8 |
7 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
8 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
9 #include "base/android/scoped_java_ref.h" | 11 #include "base/android/scoped_java_ref.h" |
10 #include "base/bind.h" | 12 #include "base/bind.h" |
11 #include "base/macros.h" | 13 #include "base/macros.h" |
12 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/ptr_util.h" |
13 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
14 #include "content/public/browser/client_certificate_delegate.h" | 16 #include "content/public/browser/client_certificate_delegate.h" |
15 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
16 #include "jni/MockAwContentsClientBridge_jni.h" | 18 #include "jni/MockAwContentsClientBridge_jni.h" |
17 #include "net/android/net_jni_registrar.h" | 19 #include "net/android/net_jni_registrar.h" |
18 #include "net/ssl/ssl_cert_request_info.h" | 20 #include "net/ssl/ssl_cert_request_info.h" |
19 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
21 | 23 |
22 using base::android::AttachCurrentThread; | 24 using base::android::AttachCurrentThread; |
(...skipping 14 matching lines...) Expand all Loading... |
37 AwContentsClientBridgeTest() { } | 39 AwContentsClientBridgeTest() { } |
38 | 40 |
39 // Callback method called when a cert is selected. | 41 // Callback method called when a cert is selected. |
40 void CertSelected(X509Certificate* cert); | 42 void CertSelected(X509Certificate* cert); |
41 protected: | 43 protected: |
42 void SetUp() override; | 44 void SetUp() override; |
43 void TestCertType(SSLClientCertType type, const std::string& expected_name); | 45 void TestCertType(SSLClientCertType type, const std::string& expected_name); |
44 // Create the TestBrowserThreads. Just instantiate the member variable. | 46 // Create the TestBrowserThreads. Just instantiate the member variable. |
45 content::TestBrowserThreadBundle thread_bundle_; | 47 content::TestBrowserThreadBundle thread_bundle_; |
46 base::android::ScopedJavaGlobalRef<jobject> jbridge_; | 48 base::android::ScopedJavaGlobalRef<jobject> jbridge_; |
47 scoped_ptr<AwContentsClientBridge> bridge_; | 49 std::unique_ptr<AwContentsClientBridge> bridge_; |
48 scoped_refptr<SSLCertRequestInfo> cert_request_info_; | 50 scoped_refptr<SSLCertRequestInfo> cert_request_info_; |
49 X509Certificate* selected_cert_; | 51 X509Certificate* selected_cert_; |
50 int cert_selected_callbacks_; | 52 int cert_selected_callbacks_; |
51 JNIEnv* env_; | 53 JNIEnv* env_; |
52 }; | 54 }; |
53 | 55 |
54 class TestClientCertificateDelegate | 56 class TestClientCertificateDelegate |
55 : public content::ClientCertificateDelegate { | 57 : public content::ClientCertificateDelegate { |
56 public: | 58 public: |
57 explicit TestClientCertificateDelegate(AwContentsClientBridgeTest* test) | 59 explicit TestClientCertificateDelegate(AwContentsClientBridgeTest* test) |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 TestCertType(cert_types[i], expected_names[i]); | 101 TestCertType(cert_types[i], expected_names[i]); |
100 } | 102 } |
101 } | 103 } |
102 | 104 |
103 void AwContentsClientBridgeTest::TestCertType(SSLClientCertType type, | 105 void AwContentsClientBridgeTest::TestCertType(SSLClientCertType type, |
104 const std::string& expected_name) { | 106 const std::string& expected_name) { |
105 cert_request_info_->cert_key_types.clear(); | 107 cert_request_info_->cert_key_types.clear(); |
106 cert_request_info_->cert_key_types.push_back(type); | 108 cert_request_info_->cert_key_types.push_back(type); |
107 bridge_->SelectClientCertificate( | 109 bridge_->SelectClientCertificate( |
108 cert_request_info_.get(), | 110 cert_request_info_.get(), |
109 make_scoped_ptr(new TestClientCertificateDelegate(this))); | 111 base::WrapUnique(new TestClientCertificateDelegate(this))); |
110 base::RunLoop().RunUntilIdle(); | 112 base::RunLoop().RunUntilIdle(); |
111 EXPECT_EQ(0, cert_selected_callbacks_); | 113 EXPECT_EQ(0, cert_selected_callbacks_); |
112 ScopedJavaLocalRef<jobjectArray> key_types = | 114 ScopedJavaLocalRef<jobjectArray> key_types = |
113 Java_MockAwContentsClientBridge_getKeyTypes(env_, jbridge_.obj()); | 115 Java_MockAwContentsClientBridge_getKeyTypes(env_, jbridge_.obj()); |
114 std::vector<std::string> vec; | 116 std::vector<std::string> vec; |
115 base::android::AppendJavaStringArrayToStringVector(env_, | 117 base::android::AppendJavaStringArrayToStringVector(env_, |
116 key_types.obj(), | 118 key_types.obj(), |
117 &vec); | 119 &vec); |
118 EXPECT_EQ(1u, vec.size()); | 120 EXPECT_EQ(1u, vec.size()); |
119 EXPECT_EQ(expected_name, vec[0]); | 121 EXPECT_EQ(expected_name, vec[0]); |
120 } | 122 } |
121 | 123 |
122 // Verify that ProvideClientCertificateResponse works properly when the client | 124 // Verify that ProvideClientCertificateResponse works properly when the client |
123 // responds with a null key. | 125 // responds with a null key. |
124 TEST_F(AwContentsClientBridgeTest, | 126 TEST_F(AwContentsClientBridgeTest, |
125 TestProvideClientCertificateResponseCallsCallbackOnNullKey) { | 127 TestProvideClientCertificateResponseCallsCallbackOnNullKey) { |
126 // Call SelectClientCertificate to create a callback id that mock java object | 128 // Call SelectClientCertificate to create a callback id that mock java object |
127 // can call on. | 129 // can call on. |
128 bridge_->SelectClientCertificate( | 130 bridge_->SelectClientCertificate( |
129 cert_request_info_.get(), | 131 cert_request_info_.get(), |
130 make_scoped_ptr(new TestClientCertificateDelegate(this))); | 132 base::WrapUnique(new TestClientCertificateDelegate(this))); |
131 bridge_->ProvideClientCertificateResponse( | 133 bridge_->ProvideClientCertificateResponse( |
132 env_, jbridge_, | 134 env_, jbridge_, |
133 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_.obj()), | 135 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_.obj()), |
134 Java_MockAwContentsClientBridge_createTestCertChain(env_, jbridge_.obj()), | 136 Java_MockAwContentsClientBridge_createTestCertChain(env_, jbridge_.obj()), |
135 nullptr); | 137 nullptr); |
136 base::RunLoop().RunUntilIdle(); | 138 base::RunLoop().RunUntilIdle(); |
137 EXPECT_EQ(nullptr, selected_cert_); | 139 EXPECT_EQ(nullptr, selected_cert_); |
138 EXPECT_EQ(1, cert_selected_callbacks_); | 140 EXPECT_EQ(1, cert_selected_callbacks_); |
139 } | 141 } |
140 | 142 |
141 // Verify that ProvideClientCertificateResponse calls the callback with | 143 // Verify that ProvideClientCertificateResponse calls the callback with |
142 // null parameters when private key is not provided. | 144 // null parameters when private key is not provided. |
143 TEST_F(AwContentsClientBridgeTest, | 145 TEST_F(AwContentsClientBridgeTest, |
144 TestProvideClientCertificateResponseCallsCallbackOnNullChain) { | 146 TestProvideClientCertificateResponseCallsCallbackOnNullChain) { |
145 // Call SelectClientCertificate to create a callback id that mock java object | 147 // Call SelectClientCertificate to create a callback id that mock java object |
146 // can call on. | 148 // can call on. |
147 bridge_->SelectClientCertificate( | 149 bridge_->SelectClientCertificate( |
148 cert_request_info_.get(), | 150 cert_request_info_.get(), |
149 make_scoped_ptr(new TestClientCertificateDelegate(this))); | 151 base::WrapUnique(new TestClientCertificateDelegate(this))); |
150 int requestId = | 152 int requestId = |
151 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_.obj()); | 153 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_.obj()); |
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 |