blob: d42664833b27e59710f608f2f6c4b8890ed40cc9 [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001package tests.api.javax.net.ssl;
2
Elliott Hughesf33eae72010-05-13 12:36:25 -07003import dalvik.annotation.TestTargetClass;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08004import dalvik.annotation.TestTargets;
5import dalvik.annotation.TestLevel;
6import dalvik.annotation.TestTargetNew;
7
8import java.io.ByteArrayInputStream;
9import java.security.cert.CertificateFactory;
10import java.security.cert.CertificateException;
11import java.security.cert.X509Certificate;
12import javax.net.ssl.X509TrustManager;
13
14import junit.framework.TestCase;
15
16import org.apache.harmony.security.tests.support.cert.TestUtils;
17import org.apache.harmony.xnet.tests.support.X509TrustManagerImpl;
18
19/**
20 * Tests for <code>X509TrustManager</code> class constructors and methods.
21 */
Elliott Hughesf33eae72010-05-13 12:36:25 -070022@TestTargetClass(X509TrustManager.class)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080023public class X509TrustManagerTest extends TestCase {
Elliott Hughesf33eae72010-05-13 12:36:25 -070024
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080025 private X509Certificate[] setX509Certificate() {
26 try {
27 CertificateFactory certFact = CertificateFactory.getInstance("X.509");
28 X509Certificate pemCert = (X509Certificate) certFact
29 .generateCertificate(new ByteArrayInputStream(TestUtils
30 .getX509Certificate_v3()));
31 X509Certificate[] xcert = {pemCert};
32 return xcert;
33 } catch (Exception ex) {
34 fail("Unexpected exception " + ex);
35 }
36 return null;
37 }
38
39 private X509Certificate[] setInvalid() {
40 try {
41 CertificateFactory certFact = CertificateFactory.getInstance("X.509");
42 X509Certificate pemCert = (X509Certificate) certFact
43 .generateCertificate(new ByteArrayInputStream(TestUtils
44 .getX509Certificate_v1()));
45 X509Certificate[] xcert = {pemCert};
46 return xcert;
47 } catch (Exception ex) {
48 fail("Unexpected exception " + ex);
49 }
50 return null;
51 }
Elliott Hughesf33eae72010-05-13 12:36:25 -070052
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080053 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -070054 * @tests javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[] chain, String authType)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080055 */
56 @TestTargetNew(
57 level = TestLevel.PARTIAL_COMPLETE,
58 notes = "",
59 method = "checkClientTrusted",
60 args = {java.security.cert.X509Certificate[].class, java.lang.String.class}
61 )
62 public void test_checkClientTrusted_01() {
63 X509TrustManagerImpl xtm = new X509TrustManagerImpl();
64 X509Certificate[] xcert = null;
65
66 try {
67 xtm.checkClientTrusted(xcert, "SSL");
68 fail("IllegalArgumentException wasn't thrown");
69 } catch (IllegalArgumentException iae) {
70 //expected
71 } catch (Exception e) {
Elliott Hughesf33eae72010-05-13 12:36:25 -070072 fail(e + " was thrown instead of IllegalArgumentException");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080073 }
Elliott Hughesf33eae72010-05-13 12:36:25 -070074
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080075 xcert = new X509Certificate[0];
76 try {
77 xtm.checkClientTrusted(xcert, "SSL");
78 fail("IllegalArgumentException wasn't thrown");
79 } catch (IllegalArgumentException iae) {
80 //expected
81 } catch (Exception e) {
Elliott Hughesf33eae72010-05-13 12:36:25 -070082 fail(e + " was thrown instead of IllegalArgumentException");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080083 }
Elliott Hughesf33eae72010-05-13 12:36:25 -070084
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080085 xcert = setX509Certificate();
86 try {
87 xtm.checkClientTrusted(xcert, null);
88 fail("IllegalArgumentException wasn't thrown");
89 } catch (IllegalArgumentException iae) {
90 //expected
91 } catch (Exception e) {
Elliott Hughesf33eae72010-05-13 12:36:25 -070092 fail(e + " was thrown instead of IllegalArgumentException");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080093 }
Elliott Hughesf33eae72010-05-13 12:36:25 -070094
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080095 try {
96 xtm.checkClientTrusted(xcert, "");
97 fail("IllegalArgumentException wasn't thrown");
98 } catch (IllegalArgumentException iae) {
99 //expected
100 } catch (Exception e) {
Elliott Hughesf33eae72010-05-13 12:36:25 -0700101 fail(e + " was thrown instead of IllegalArgumentException");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800102 }
103 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700104
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800105 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700106 * @tests javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[] chain, String authType)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800107 */
108 @TestTargetNew(
109 level = TestLevel.PARTIAL_COMPLETE,
110 notes = "",
111 method = "checkClientTrusted",
112 args = {java.security.cert.X509Certificate[].class, java.lang.String.class}
113 )
114 public void test_checkClientTrusted_02() {
115 X509TrustManagerImpl xtm = new X509TrustManagerImpl();
116 X509Certificate[] xcert = setInvalid();
117
118 try {
119 xtm.checkClientTrusted(xcert, "SSL");
120 fail("CertificateException wasn't thrown");
121 } catch (CertificateException ce) {
122 //expected
123 }
124 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700125
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800126 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700127 * @tests javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[] chain, String authType)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800128 */
129 @TestTargetNew(
130 level = TestLevel.PARTIAL_COMPLETE,
131 notes = "",
132 method = "checkClientTrusted",
133 args = {java.security.cert.X509Certificate[].class, java.lang.String.class}
134 )
135 public void test_checkClientTrusted_03() {
136 X509TrustManagerImpl xtm = new X509TrustManagerImpl();
137 X509Certificate[] xcert = setX509Certificate();
138
139 try {
140 xtm.checkClientTrusted(xcert, "SSL");
141 } catch (Exception ex) {
142 fail("Unexpected exception " + ex);
143 }
144 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700145
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800146 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700147 * @tests javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[] chain, String authType)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800148 */
149 @TestTargetNew(
150 level = TestLevel.PARTIAL_COMPLETE,
151 notes = "",
152 method = "checkServerTrusted",
153 args = {java.security.cert.X509Certificate[].class, java.lang.String.class}
154 )
155 public void test_checkServerTrusted_01() {
156 X509TrustManagerImpl xtm = new X509TrustManagerImpl();
157 X509Certificate[] xcert = null;
158
159 try {
160 xtm.checkServerTrusted(xcert, "SSL");
161 fail("IllegalArgumentException wasn't thrown");
162 } catch (IllegalArgumentException iae) {
163 //expected
164 } catch (Exception e) {
Elliott Hughesf33eae72010-05-13 12:36:25 -0700165 fail(e + " was thrown instead of IllegalArgumentException");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800166 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700167
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800168 xcert = new X509Certificate[0];
169 try {
170 xtm.checkServerTrusted(xcert, "SSL");
171 fail("IllegalArgumentException wasn't thrown");
172 } catch (IllegalArgumentException iae) {
173 //expected
174 } catch (Exception e) {
Elliott Hughesf33eae72010-05-13 12:36:25 -0700175 fail(e + " was thrown instead of IllegalArgumentException");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800176 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700177
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800178 xcert = setX509Certificate();
179 try {
180 xtm.checkServerTrusted(xcert, null);
181 fail("IllegalArgumentException wasn't thrown");
182 } catch (IllegalArgumentException iae) {
183 //expected
184 } catch (Exception e) {
Elliott Hughesf33eae72010-05-13 12:36:25 -0700185 fail(e + " was thrown instead of IllegalArgumentException");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800186 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700187
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800188 try {
189 xtm.checkServerTrusted(xcert, "");
190 fail("IllegalArgumentException wasn't thrown");
191 } catch (IllegalArgumentException iae) {
192 //expected
193 } catch (Exception e) {
Elliott Hughesf33eae72010-05-13 12:36:25 -0700194 fail(e + " was thrown instead of IllegalArgumentException");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800195 }
196 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700197
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800198 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700199 * @tests javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[] chain, String authType)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800200 */
201 @TestTargetNew(
202 level = TestLevel.PARTIAL_COMPLETE,
203 notes = "",
204 method = "checkServerTrusted",
205 args = {java.security.cert.X509Certificate[].class, java.lang.String.class}
206 )
207 public void test_checkServerTrusted_02() {
208 X509TrustManagerImpl xtm = new X509TrustManagerImpl();
209 X509Certificate[] xcert = setInvalid();
210
211 try {
212 xtm.checkServerTrusted(xcert, "SSL");
213 fail("CertificateException wasn't thrown");
214 } catch (CertificateException ce) {
215 //expected
216 }
217 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700218
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800219 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700220 * @tests javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[] chain, String authType)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800221 */
222 @TestTargetNew(
223 level = TestLevel.PARTIAL_COMPLETE,
224 notes = "",
225 method = "checkServerTrusted",
226 args = {java.security.cert.X509Certificate[].class, java.lang.String.class}
227 )
228 public void test_checkServerTrusted_03() {
229 X509TrustManagerImpl xtm = new X509TrustManagerImpl();
230 X509Certificate[] xcert = setX509Certificate();
231
232 try {
233 xtm.checkServerTrusted(xcert, "SSL");
234 } catch (Exception ex) {
235 fail("Unexpected exception " + ex);
236 }
237 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700238
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800239 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700240 * @tests javax.net.ssl.X509TrustManager#getAcceptedIssuers()
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800241 */
242 @TestTargetNew(
243 level = TestLevel.COMPLETE,
244 notes = "",
245 method = "getAcceptedIssuers",
246 args = {}
247 )
248 public void test_getAcceptedIssuers() {
249 X509TrustManagerImpl xtm = new X509TrustManagerImpl();
250
251 try {
252 assertNotNull(xtm.getAcceptedIssuers());
253 } catch (Exception ex) {
254 fail("Unexpected exception " + ex);
255 }
256 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700257
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800258}