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