Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: android_webview/browser/aw_contents_client_bridge_unittest.cc

Issue 2898573002: Refactor client cert private key handling. (Closed)
Patch Set: removed no longer needed forward declaration Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/browser/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"
16 #include "content/public/browser/client_certificate_delegate.h" 16 #include "content/public/browser/client_certificate_delegate.h"
17 #include "content/public/test/test_browser_thread_bundle.h" 17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "jni/MockAwContentsClientBridge_jni.h" 18 #include "jni/MockAwContentsClientBridge_jni.h"
19 #include "net/android/net_jni_registrar.h" 19 #include "net/android/net_jni_registrar.h"
20 #include "net/cert/x509_certificate.h"
20 #include "net/ssl/ssl_cert_request_info.h" 21 #include "net/ssl/ssl_cert_request_info.h"
22 #include "net/ssl/ssl_private_key.h"
21 #include "testing/gmock/include/gmock/gmock.h" 23 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
23 25
24 using base::android::AttachCurrentThread; 26 using base::android::AttachCurrentThread;
25 using base::android::ScopedJavaLocalRef; 27 using base::android::ScopedJavaLocalRef;
26 using net::SSLCertRequestInfo; 28 using net::SSLCertRequestInfo;
27 using net::SSLClientCertType; 29 using net::SSLClientCertType;
30 using net::SSLPrivateKey;
28 using net::X509Certificate; 31 using net::X509Certificate;
29 using testing::NotNull; 32 using testing::NotNull;
30 using testing::Test; 33 using testing::Test;
31 34
32 namespace android_webview { 35 namespace android_webview {
33 36
34 namespace { 37 namespace {
35 38
36 // Tests the android_webview contents client bridge. 39 // Tests the android_webview contents client bridge.
37 class AwContentsClientBridgeTest : public Test { 40 class AwContentsClientBridgeTest : public Test {
38 public: 41 public:
39 AwContentsClientBridgeTest() {} 42 AwContentsClientBridgeTest() {}
40 43
41 // Callback method called when a cert is selected. 44 // Callback method called when a cert is selected.
42 void CertSelected(X509Certificate* cert); 45 void CertSelected(scoped_refptr<X509Certificate> cert,
46 scoped_refptr<SSLPrivateKey> key);
43 47
44 protected: 48 protected:
45 void SetUp() override; 49 void SetUp() override;
46 void TestCertType(SSLClientCertType type, const std::string& expected_name); 50 void TestCertType(SSLClientCertType type, const std::string& expected_name);
47 // Create the TestBrowserThreads. Just instantiate the member variable. 51 // Create the TestBrowserThreads. Just instantiate the member variable.
48 content::TestBrowserThreadBundle thread_bundle_; 52 content::TestBrowserThreadBundle thread_bundle_;
49 base::android::ScopedJavaGlobalRef<jobject> jbridge_; 53 base::android::ScopedJavaGlobalRef<jobject> jbridge_;
50 std::unique_ptr<AwContentsClientBridge> bridge_; 54 std::unique_ptr<AwContentsClientBridge> bridge_;
51 scoped_refptr<SSLCertRequestInfo> cert_request_info_; 55 scoped_refptr<SSLCertRequestInfo> cert_request_info_;
52 X509Certificate* selected_cert_; 56 scoped_refptr<X509Certificate> selected_cert_;
57 scoped_refptr<SSLPrivateKey> selected_key_;
53 int cert_selected_callbacks_; 58 int cert_selected_callbacks_;
54 JNIEnv* env_; 59 JNIEnv* env_;
55 }; 60 };
56 61
57 class TestClientCertificateDelegate 62 class TestClientCertificateDelegate
58 : public content::ClientCertificateDelegate { 63 : public content::ClientCertificateDelegate {
59 public: 64 public:
60 explicit TestClientCertificateDelegate(AwContentsClientBridgeTest* test) 65 explicit TestClientCertificateDelegate(AwContentsClientBridgeTest* test)
61 : test_(test) {} 66 : test_(test) {}
62 67
63 // content::ClientCertificateDelegate. 68 // content::ClientCertificateDelegate.
64 void ContinueWithCertificate(net::X509Certificate* cert) override { 69 void ContinueWithCertificate(scoped_refptr<net::X509Certificate> cert,
65 test_->CertSelected(cert); 70 scoped_refptr<net::SSLPrivateKey> key) override {
71 test_->CertSelected(std::move(cert), std::move(key));
66 test_ = nullptr; 72 test_ = nullptr;
67 } 73 }
68 74
69 private: 75 private:
70 AwContentsClientBridgeTest* test_; 76 AwContentsClientBridgeTest* test_;
71 77
72 DISALLOW_COPY_AND_ASSIGN(TestClientCertificateDelegate); 78 DISALLOW_COPY_AND_ASSIGN(TestClientCertificateDelegate);
73 }; 79 };
74 80
75 } // namespace 81 } // namespace
76 82
77 void AwContentsClientBridgeTest::SetUp() { 83 void AwContentsClientBridgeTest::SetUp() {
78 env_ = AttachCurrentThread(); 84 env_ = AttachCurrentThread();
79 ASSERT_THAT(env_, NotNull()); 85 ASSERT_THAT(env_, NotNull());
80 ASSERT_TRUE(android_webview::RegisterAwContentsClientBridge(env_)); 86 ASSERT_TRUE(android_webview::RegisterAwContentsClientBridge(env_));
81 ASSERT_TRUE(net::android::RegisterJni(env_)); 87 ASSERT_TRUE(net::android::RegisterJni(env_));
82 jbridge_.Reset( 88 jbridge_.Reset(
83 env_, 89 env_,
84 Java_MockAwContentsClientBridge_getAwContentsClientBridge(env_).obj()); 90 Java_MockAwContentsClientBridge_getAwContentsClientBridge(env_).obj());
85 bridge_.reset(new AwContentsClientBridge(env_, jbridge_)); 91 bridge_.reset(new AwContentsClientBridge(env_, jbridge_));
86 selected_cert_ = nullptr; 92 selected_cert_ = nullptr;
87 cert_selected_callbacks_ = 0; 93 cert_selected_callbacks_ = 0;
88 cert_request_info_ = new net::SSLCertRequestInfo; 94 cert_request_info_ = new net::SSLCertRequestInfo;
89 } 95 }
90 96
91 void AwContentsClientBridgeTest::CertSelected(X509Certificate* cert) { 97 void AwContentsClientBridgeTest::CertSelected(
92 selected_cert_ = cert; 98 scoped_refptr<X509Certificate> cert,
99 scoped_refptr<SSLPrivateKey> key) {
100 selected_cert_ = std::move(cert);
101 selected_key_ = std::move(key);
93 cert_selected_callbacks_++; 102 cert_selected_callbacks_++;
94 } 103 }
95 104
96 TEST_F(AwContentsClientBridgeTest, TestClientCertKeyTypesCorrectlyEncoded) { 105 TEST_F(AwContentsClientBridgeTest, TestClientCertKeyTypesCorrectlyEncoded) {
97 SSLClientCertType cert_types[2] = {net::CLIENT_CERT_RSA_SIGN, 106 SSLClientCertType cert_types[2] = {net::CLIENT_CERT_RSA_SIGN,
98 net::CLIENT_CERT_ECDSA_SIGN}; 107 net::CLIENT_CERT_ECDSA_SIGN};
99 std::string expected_names[2] = {"RSA", "ECDSA"}; 108 std::string expected_names[2] = {"RSA", "ECDSA"};
100 109
101 for (int i = 0; i < 2; i++) { 110 for (int i = 0; i < 2; i++) {
102 TestCertType(cert_types[i], expected_names[i]); 111 TestCertType(cert_types[i], expected_names[i]);
(...skipping 27 matching lines...) Expand all
130 // can call on. 139 // can call on.
131 bridge_->SelectClientCertificate( 140 bridge_->SelectClientCertificate(
132 cert_request_info_.get(), 141 cert_request_info_.get(),
133 base::WrapUnique(new TestClientCertificateDelegate(this))); 142 base::WrapUnique(new TestClientCertificateDelegate(this)));
134 bridge_->ProvideClientCertificateResponse( 143 bridge_->ProvideClientCertificateResponse(
135 env_, jbridge_, 144 env_, jbridge_,
136 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_), 145 Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_),
137 Java_MockAwContentsClientBridge_createTestCertChain(env_, jbridge_), 146 Java_MockAwContentsClientBridge_createTestCertChain(env_, jbridge_),
138 nullptr); 147 nullptr);
139 base::RunLoop().RunUntilIdle(); 148 base::RunLoop().RunUntilIdle();
140 EXPECT_EQ(nullptr, selected_cert_); 149 EXPECT_EQ(nullptr, selected_cert_.get());
150 EXPECT_EQ(nullptr, selected_key_.get());
141 EXPECT_EQ(1, cert_selected_callbacks_); 151 EXPECT_EQ(1, cert_selected_callbacks_);
142 } 152 }
143 153
144 // Verify that ProvideClientCertificateResponse calls the callback with 154 // Verify that ProvideClientCertificateResponse calls the callback with
145 // null parameters when private key is not provided. 155 // null parameters when private key is not provided.
146 TEST_F(AwContentsClientBridgeTest, 156 TEST_F(AwContentsClientBridgeTest,
147 TestProvideClientCertificateResponseCallsCallbackOnNullChain) { 157 TestProvideClientCertificateResponseCallsCallbackOnNullChain) {
148 // Call SelectClientCertificate to create a callback id that mock java object 158 // Call SelectClientCertificate to create a callback id that mock java object
149 // can call on. 159 // can call on.
150 bridge_->SelectClientCertificate( 160 bridge_->SelectClientCertificate(
151 cert_request_info_.get(), 161 cert_request_info_.get(),
152 base::WrapUnique(new TestClientCertificateDelegate(this))); 162 base::WrapUnique(new TestClientCertificateDelegate(this)));
153 int requestId = Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_); 163 int requestId = Java_MockAwContentsClientBridge_getRequestId(env_, jbridge_);
154 bridge_->ProvideClientCertificateResponse(env_, jbridge_, requestId, nullptr, 164 bridge_->ProvideClientCertificateResponse(env_, jbridge_, requestId, nullptr,
155 nullptr); 165 nullptr);
156 base::RunLoop().RunUntilIdle(); 166 base::RunLoop().RunUntilIdle();
157 EXPECT_EQ(nullptr, selected_cert_); 167 EXPECT_EQ(nullptr, selected_cert_.get());
168 EXPECT_EQ(nullptr, selected_key_.get());
158 EXPECT_EQ(1, cert_selected_callbacks_); 169 EXPECT_EQ(1, cert_selected_callbacks_);
159 } 170 }
160 171
161 } // namespace android_webview 172 } // namespace android_webview
OLDNEW
« no previous file with comments | « android_webview/browser/aw_contents_client_bridge.cc ('k') | chrome/browser/chrome_content_browser_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698