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