Chromium Code Reviews| 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 | |
|
boliu
2014/04/16 02:57:46
unittests are usually put into its own anonymous n
sgurun-gerrit only
2014/04/17 15:10:51
Done.
boliu
2014/04/18 17:10:12
Actually wrap the whole file in android_webview, t
sgurun-gerrit only
2014/04/19 01:28:49
That's something very loosely applied it seems -ju
| |
| 20 using android_webview::AwContentsClientBridge; | |
| 21 using base::android::AttachCurrentThread; | |
| 22 using base::android::ScopedJavaLocalRef; | |
| 23 using net::SSLCertRequestInfo; | |
| 24 using net::SSLClientCertType; | |
| 25 using net::X509Certificate; | |
| 26 using testing::NotNull; | |
| 27 using testing::Test; | |
| 28 | |
| 29 // Tests the android_webview contents client bridge. | |
| 30 class AwContentsClientBridgeTest : public Test { | |
| 31 public: | |
| 32 typedef AwContentsClientBridge::SelectCertificateCallback | |
| 33 SelectCertificateCallback; | |
| 34 | |
| 35 AwContentsClientBridgeTest() { } | |
| 36 | |
| 37 // Callback method called when a cert is selected. | |
| 38 void CertSelected(X509Certificate* cert); | |
| 39 protected: | |
| 40 virtual void SetUp(); | |
| 41 void TestCertType(SSLClientCertType type, const std::string& expected_name); | |
| 42 // Create the TestBrowserThreads. Just instantiate the member variable. | |
| 43 content::TestBrowserThreadBundle thread_bundle_; | |
| 44 base::android::ScopedJavaGlobalRef<jobject> jbridge_; | |
| 45 scoped_ptr<AwContentsClientBridge> bridge_; | |
| 46 scoped_refptr<SSLCertRequestInfo> cert_request_info_; | |
| 47 X509Certificate* selected_cert_; | |
| 48 int cert_selected_callbacks_; | |
| 49 JNIEnv* env_; | |
| 50 }; | |
| 51 | |
| 52 void AwContentsClientBridgeTest::SetUp() { | |
| 53 env_ = AttachCurrentThread(); | |
| 54 ASSERT_THAT(env_, NotNull()); | |
| 55 ASSERT_TRUE(android_webview::RegisterAwContentsClientBridge(env_)); | |
| 56 ASSERT_TRUE(RegisterNativesImpl(env_)); | |
| 57 ASSERT_TRUE(net::android::RegisterJni(env_)); | |
| 58 jbridge_.Reset(env_, | |
| 59 Java_MockAwContentsClientBridge_getAwContentsClientBridge(env_).obj()); | |
| 60 bridge_.reset(new AwContentsClientBridge(env_, jbridge_.obj())); | |
| 61 selected_cert_ = NULL; | |
| 62 cert_selected_callbacks_ = 0; | |
| 63 cert_request_info_ = new net::SSLCertRequestInfo; | |
| 64 } | |
| 65 | |
| 66 void AwContentsClientBridgeTest::CertSelected(X509Certificate* cert) { | |
| 67 selected_cert_ = cert; | |
| 68 cert_selected_callbacks_++; | |
| 69 } | |
| 70 | |
| 71 TEST_F(AwContentsClientBridgeTest, | |
| 72 TestClientCertCallbackCalledWhenJavaDoesNotTakeOwnership) { | |
| 73 Java_MockAwContentsClientBridge_setEarlyOutSelectClientCertificate( | |
| 74 env_, | |
| 75 jbridge_.obj(), | |
| 76 true); | |
| 77 bridge_->SelectClientCertificate( | |
| 78 cert_request_info_.get(), | |
| 79 base::Bind( | |
| 80 &AwContentsClientBridgeTest::CertSelected, | |
| 81 base::Unretained(static_cast<AwContentsClientBridgeTest*>(this)))); | |
| 82 base::RunLoop().RunUntilIdle(); | |
| 83 EXPECT_EQ(NULL, selected_cert_); | |
| 84 EXPECT_EQ(1, cert_selected_callbacks_); | |
| 85 } | |
| 86 | |
| 87 // Verify that if java takes ownership, SelectClientCertificate() does not | |
| 88 // call the callback. | |
| 89 TEST_F(AwContentsClientBridgeTest, | |
| 90 TestClientCertCallbackNotCalledWhenJavaTakesOwnership) { | |
| 91 bridge_->SelectClientCertificate( | |
| 92 cert_request_info_.get(), | |
| 93 base::Bind( | |
| 94 &AwContentsClientBridgeTest::CertSelected, | |
| 95 base::Unretained(static_cast<AwContentsClientBridgeTest*>(this)))); | |
| 96 base::RunLoop().RunUntilIdle(); | |
| 97 EXPECT_EQ(NULL, selected_cert_); | |
| 98 EXPECT_EQ(0, cert_selected_callbacks_); | |
|
boliu
2014/04/16 02:57:46
Does the native object leak here?
Maybe the mock
sgurun-gerrit only
2014/04/18 01:44:55
This does not apply anymore.
On 2014/04/16 02:57:4
| |
| 99 } | |
| 100 | |
| 101 TEST_F(AwContentsClientBridgeTest, TestClientCertKeyTypesCorrectlyEncoded) { | |
| 102 SSLClientCertType cert_types[3] = {net::CLIENT_CERT_RSA_SIGN, | |
| 103 net::CLIENT_CERT_DSS_SIGN, net::CLIENT_CERT_ECDSA_SIGN}; | |
| 104 std::string expected_names[3] = {"RSA", "DSA" ,"ECDSA"}; | |
| 105 | |
| 106 for(int i = 0; i<3; i++) { | |
|
boliu
2014/04/16 02:57:46
nit: space add space around "<"
sgurun-gerrit only
2014/04/17 15:10:51
Done.
| |
| 107 TestCertType(cert_types[i], expected_names[i]); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 void AwContentsClientBridgeTest::TestCertType(SSLClientCertType type, | |
| 112 const std::string& expected_name) { | |
| 113 cert_request_info_->cert_key_types.clear(); | |
| 114 cert_request_info_->cert_key_types.push_back(type); | |
| 115 bridge_->SelectClientCertificate( | |
| 116 cert_request_info_.get(), | |
| 117 base::Bind( | |
| 118 &AwContentsClientBridgeTest::CertSelected, | |
| 119 base::Unretained(static_cast<AwContentsClientBridgeTest*>(this)))); | |
| 120 base::RunLoop().RunUntilIdle(); | |
| 121 EXPECT_EQ(0, cert_selected_callbacks_); | |
| 122 ScopedJavaLocalRef<jobjectArray> key_types = | |
| 123 Java_MockAwContentsClientBridge_getKeyTypes(env_, jbridge_.obj()); | |
| 124 std::vector<std::string> vec; | |
| 125 base::android::AppendJavaStringArrayToStringVector(env_, | |
| 126 key_types.obj(), | |
| 127 &vec); | |
| 128 EXPECT_EQ(1u, vec.size()); | |
| 129 EXPECT_EQ(expected_name, vec[0]); | |
| 130 } | |
| 131 | |
| 132 // Verify that ProvideClientCertificateResponse calls the callback with NULL | |
| 133 // parameters when encodedchain is not provided. | |
| 134 TEST_F(AwContentsClientBridgeTest, | |
| 135 TestProvideClientCertificateResponseCallsCallbackOnNullKey) { | |
| 136 SelectCertificateCallback callback = base::Bind( | |
| 137 &AwContentsClientBridgeTest::CertSelected, | |
| 138 base::Unretained(static_cast<AwContentsClientBridgeTest*>(this))); | |
| 139 // Create a copy of the callback on the heap. We will test if | |
| 140 // ProvideClientCertificateResponse manages ownership properly. | |
| 141 SelectCertificateCallback* temp = new SelectCertificateCallback(callback); | |
| 142 jlong request_id = reinterpret_cast<intptr_t>(temp); | |
| 143 jclass byteClass = env_->FindClass("java/lang/Byte"); | |
| 144 jobjectArray array = env_->NewObjectArray(1, byteClass, 0); | |
|
boliu
2014/04/16 02:57:46
If mock uses ClientCertificateRequestCallback dire
sgurun-gerrit only
2014/04/18 01:44:55
not doing crazy jni anymore.
On 2014/04/16 02:57:4
| |
| 145 bridge_->ProvideClientCertificateResponse(env_, jbridge_.obj(), request_id, | |
| 146 array, NULL); | |
| 147 base::RunLoop().RunUntilIdle(); | |
| 148 EXPECT_EQ(NULL, selected_cert_); | |
| 149 EXPECT_EQ(1, cert_selected_callbacks_); | |
| 150 } | |
| 151 | |
| 152 // Verify that ProvideClientCertificateResponse calls the callback with | |
| 153 // NULL parameters when private key is not provided. | |
| 154 TEST_F(AwContentsClientBridgeTest, | |
| 155 TestProvideClientCertificateResponseCallsCallbackOnNullChain) { | |
| 156 SelectCertificateCallback callback = base::Bind( | |
| 157 &AwContentsClientBridgeTest::CertSelected, | |
| 158 base::Unretained(static_cast<AwContentsClientBridgeTest*>(this))); | |
| 159 // Create a copy of the callback on the heap. We will test if | |
| 160 // ProvideClientCertificateResponse manages ownership properly. | |
| 161 SelectCertificateCallback* temp = new SelectCertificateCallback(callback); | |
| 162 jlong request_id = reinterpret_cast<intptr_t>(temp); | |
| 163 ScopedJavaLocalRef<jobject> privateKey = | |
| 164 Java_MockAwContentsClientBridge_getTestPrivateKey(env_, jbridge_.obj()); | |
| 165 bridge_->ProvideClientCertificateResponse(env_, jbridge_.obj(), request_id, | |
| 166 NULL, privateKey.obj()); | |
| 167 base::RunLoop().RunUntilIdle(); | |
| 168 EXPECT_EQ(NULL, selected_cert_); | |
| 169 EXPECT_EQ(1, cert_selected_callbacks_); | |
| 170 } | |
| OLD | NEW |