The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | package tests.api.javax.net.ssl; |
| 2 | |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 3 | import dalvik.annotation.TestTargetClass; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 4 | import dalvik.annotation.TestTargets; |
| 5 | import dalvik.annotation.TestLevel; |
| 6 | import dalvik.annotation.TestTargetNew; |
| 7 | |
| 8 | import java.io.ByteArrayInputStream; |
| 9 | import java.security.cert.CertificateFactory; |
| 10 | import java.security.cert.CertificateException; |
| 11 | import java.security.cert.X509Certificate; |
| 12 | import javax.net.ssl.X509TrustManager; |
| 13 | |
| 14 | import junit.framework.TestCase; |
| 15 | |
| 16 | import org.apache.harmony.security.tests.support.cert.TestUtils; |
| 17 | import org.apache.harmony.xnet.tests.support.X509TrustManagerImpl; |
| 18 | |
| 19 | /** |
| 20 | * Tests for <code>X509TrustManager</code> class constructors and methods. |
| 21 | */ |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 22 | @TestTargetClass(X509TrustManager.class) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 23 | public class X509TrustManagerTest extends TestCase { |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 24 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 25 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 52 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 53 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 54 | * @tests javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[] chain, String authType) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 55 | */ |
| 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 72 | fail(e + " was thrown instead of IllegalArgumentException"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 73 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 74 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 75 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 82 | fail(e + " was thrown instead of IllegalArgumentException"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 83 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 84 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 85 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 92 | fail(e + " was thrown instead of IllegalArgumentException"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 93 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 94 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 95 | try { |
| 96 | xtm.checkClientTrusted(xcert, ""); |
| 97 | fail("IllegalArgumentException wasn't thrown"); |
| 98 | } catch (IllegalArgumentException iae) { |
| 99 | //expected |
| 100 | } catch (Exception e) { |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 101 | fail(e + " was thrown instead of IllegalArgumentException"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 104 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 105 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 106 | * @tests javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[] chain, String authType) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 107 | */ |
| 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 125 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 126 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 127 | * @tests javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[] chain, String authType) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 128 | */ |
| 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 145 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 146 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 147 | * @tests javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[] chain, String authType) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 148 | */ |
| 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 165 | fail(e + " was thrown instead of IllegalArgumentException"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 166 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 167 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 168 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 175 | fail(e + " was thrown instead of IllegalArgumentException"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 176 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 177 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 178 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 185 | fail(e + " was thrown instead of IllegalArgumentException"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 186 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 187 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 188 | try { |
| 189 | xtm.checkServerTrusted(xcert, ""); |
| 190 | fail("IllegalArgumentException wasn't thrown"); |
| 191 | } catch (IllegalArgumentException iae) { |
| 192 | //expected |
| 193 | } catch (Exception e) { |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 194 | fail(e + " was thrown instead of IllegalArgumentException"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 197 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 198 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 199 | * @tests javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[] chain, String authType) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 200 | */ |
| 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 218 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 219 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 220 | * @tests javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[] chain, String authType) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 221 | */ |
| 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 238 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 239 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 240 | * @tests javax.net.ssl.X509TrustManager#getAcceptedIssuers() |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 241 | */ |
| 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 257 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 258 | } |