Merge "Fix bad isinstance check in X509TrustManagerExtensions and add test." into jb-mr1-dev
diff --git a/core/java/android/net/X509TrustManagerExtensions.java b/core/java/android/net/X509TrustManagerExtensions.java
index 4026a1d..115e972 100644
--- a/core/java/android/net/X509TrustManagerExtensions.java
+++ b/core/java/android/net/X509TrustManagerExtensions.java
@@ -43,7 +43,7 @@
* @throws IllegalArgumentException If tm is an unsupported TrustManager type.
*/
public X509TrustManagerExtensions(X509TrustManager tm) throws IllegalArgumentException {
- if (mDelegate instanceof TrustManagerImpl) {
+ if (tm instanceof TrustManagerImpl) {
mDelegate = (TrustManagerImpl) tm;
} else {
throw new IllegalArgumentException("tm is not a supported type of X509TrustManager");
diff --git a/core/tests/coretests/src/android/net/http/X509TrustManagerExtensionsTest.java b/core/tests/coretests/src/android/net/http/X509TrustManagerExtensionsTest.java
new file mode 100644
index 0000000..b2e1895
--- /dev/null
+++ b/core/tests/coretests/src/android/net/http/X509TrustManagerExtensionsTest.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net.http;
+
+import java.security.KeyStore;
+import java.security.cert.X509Certificate;
+
+import javax.net.ssl.X509TrustManager;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.xnet.provider.jsse.TrustManagerImpl;
+
+public class X509TrustManagerExtensionsTest extends TestCase {
+
+ private class NotATrustManagerImpl implements X509TrustManager {
+
+ public void checkClientTrusted(X509Certificate[] chain, String authType) {}
+
+ public void checkServerTrusted(X509Certificate[] chain, String authType) {}
+
+ public X509Certificate[] getAcceptedIssuers() {
+ return new X509Certificate[0];
+ }
+ }
+
+ public void testBadCast() throws Exception {
+ NotATrustManagerImpl ntmi = new NotATrustManagerImpl();
+ try {
+ X509TrustManagerExtensions tme = new X509TrustManagerExtensions(ntmi);
+ } catch (IllegalArgumentException ignored) {
+ return;
+ }
+ fail();
+ }
+
+ public void testGoodCast() throws Exception {
+ String defaultType = KeyStore.getDefaultType();
+ TrustManagerImpl tmi = new TrustManagerImpl(KeyStore.getInstance(defaultType));
+ X509TrustManagerExtensions tme = new X509TrustManagerExtensions(tmi);
+ }
+}