Roll src/third_party/skia e784db4:fa5e68e
[chromium-blink-merge.git] / android_webview / native / aw_contents_client_bridge_unittest.cc
blobf7a1de8a792cb6ee476bf296149e0f1bd1287337
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.
5 #include "android_webview/native/aw_contents_client_bridge.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "base/bind.h"
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/run_loop.h"
14 #include "content/public/browser/client_certificate_delegate.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "jni/MockAwContentsClientBridge_jni.h"
17 #include "net/android/net_jni_registrar.h"
18 #include "net/ssl/ssl_cert_request_info.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
23 using base::android::AttachCurrentThread;
24 using base::android::ScopedJavaLocalRef;
25 using net::SSLCertRequestInfo;
26 using net::SSLClientCertType;
27 using net::X509Certificate;
28 using testing::NotNull;
29 using testing::Test;
31 namespace android_webview {
33 namespace {
35 // Tests the android_webview contents client bridge.
36 class AwContentsClientBridgeTest : public Test {
37 public:
38 AwContentsClientBridgeTest() { }
40 // Callback method called when a cert is selected.
41 void CertSelected(X509Certificate* cert);
42 protected:
43 void SetUp() override;
44 void TestCertType(SSLClientCertType type, const std::string& expected_name);
45 // Create the TestBrowserThreads. Just instantiate the member variable.
46 content::TestBrowserThreadBundle thread_bundle_;
47 base::android::ScopedJavaGlobalRef<jobject> jbridge_;
48 scoped_ptr<AwContentsClientBridge> bridge_;
49 scoped_refptr<SSLCertRequestInfo> cert_request_info_;
50 X509Certificate* selected_cert_;
51 int cert_selected_callbacks_;
52 JNIEnv* env_;
55 class TestClientCertificateDelegate
56 : public content::ClientCertificateDelegate {
57 public:
58 explicit TestClientCertificateDelegate(AwContentsClientBridgeTest* test)
59 : test_(test) {}
61 // content::ClientCertificateDelegate.
62 void ContinueWithCertificate(net::X509Certificate* cert) override {
63 test_->CertSelected(cert);
64 test_ = nullptr;
67 private:
68 AwContentsClientBridgeTest* test_;
70 DISALLOW_COPY_AND_ASSIGN(TestClientCertificateDelegate);
73 } // namespace
75 void AwContentsClientBridgeTest::SetUp() {
76 env_ = AttachCurrentThread();
77 ASSERT_THAT(env_, NotNull());
78 ASSERT_TRUE(android_webview::RegisterAwContentsClientBridge(env_));
79 ASSERT_TRUE(RegisterNativesImpl(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_.obj()));
84 selected_cert_ = NULL;
85 cert_selected_callbacks_ = 0;
86 cert_request_info_ = new net::SSLCertRequestInfo;
89 void AwContentsClientBridgeTest::CertSelected(X509Certificate* cert) {
90 selected_cert_ = cert;
91 cert_selected_callbacks_++;
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"};
99 for (int i = 0; i < 2; i++) {
100 TestCertType(cert_types[i], expected_names[i]);
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 make_scoped_ptr(new TestClientCertificateDelegate(this)));
111 base::RunLoop().RunUntilIdle();
112 EXPECT_EQ(0, cert_selected_callbacks_);
113 ScopedJavaLocalRef<jobjectArray> key_types =
114 Java_MockAwContentsClientBridge_getKeyTypes(env_, jbridge_.obj());
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]);
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 make_scoped_ptr(new TestClientCertificateDelegate(this)));
132 bridge_->ProvideClientCertificateResponse(env_, jbridge_.obj(),
133 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_.obj()),
134 Java_MockAwContentsClientBridge_createTestCertChain(
135 env_, jbridge_.obj()).obj(),
136 NULL);
137 base::RunLoop().RunUntilIdle();
138 EXPECT_EQ(NULL, selected_cert_);
139 EXPECT_EQ(1, cert_selected_callbacks_);
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 make_scoped_ptr(new TestClientCertificateDelegate(this)));
151 int requestId =
152 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_.obj());
153 bridge_->ProvideClientCertificateResponse(env_, jbridge_.obj(),
154 requestId,
155 NULL,
156 Java_MockAwContentsClientBridge_createTestPrivateKey(
157 env_, jbridge_.obj()).obj());
158 base::RunLoop().RunUntilIdle();
159 EXPECT_EQ(NULL, selected_cert_);
160 EXPECT_EQ(1, cert_selected_callbacks_);
163 } // android_webview