1 package tests.api.javax.net.ssl; 2 3 import java.io.ByteArrayInputStream; 4 import java.security.cert.CertificateFactory; 5 import java.security.cert.CertificateException; 6 import java.security.cert.X509Certificate; 7 import javax.net.ssl.X509TrustManager; 8 9 import junit.framework.TestCase; 10 11 import org.apache.harmony.security.tests.support.cert.TestUtils; 12 import org.apache.harmony.xnet.tests.support.X509TrustManagerImpl; 13 14 /** 15 * Tests for <code>X509TrustManager</code> class constructors and methods. 16 */ 17 public class X509TrustManagerTest extends TestCase { 18 19 private X509Certificate[] setX509Certificate() { 20 try { 21 CertificateFactory certFact = CertificateFactory.getInstance("X.509"); 22 X509Certificate pemCert = (X509Certificate) certFact 23 .generateCertificate(new ByteArrayInputStream(TestUtils 24 .getX509Certificate_v3())); 25 X509Certificate[] xcert = {pemCert}; 26 return xcert; 27 } catch (Exception ex) { 28 fail("Unexpected exception " + ex); 29 } 30 return null; 31 } 32 33 private X509Certificate[] setInvalid() { 34 try { 35 CertificateFactory certFact = CertificateFactory.getInstance("X.509"); 36 X509Certificate pemCert = (X509Certificate) certFact 37 .generateCertificate(new ByteArrayInputStream(TestUtils 38 .getX509Certificate_v1())); 39 X509Certificate[] xcert = {pemCert}; 40 return xcert; 41 } catch (Exception ex) { 42 fail("Unexpected exception " + ex); 43 } 44 return null; 45 } 46 47 /** 48 * javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[] chain, String authType) 49 */ 50 public void test_checkClientTrusted_01() { 51 X509TrustManagerImpl xtm = new X509TrustManagerImpl(); 52 X509Certificate[] xcert = null; 53 54 try { 55 xtm.checkClientTrusted(xcert, "SSL"); 56 fail("IllegalArgumentException wasn't thrown"); 57 } catch (IllegalArgumentException iae) { 58 //expected 59 } catch (Exception e) { 60 fail(e + " was thrown instead of IllegalArgumentException"); 61 } 62 63 xcert = new X509Certificate[0]; 64 try { 65 xtm.checkClientTrusted(xcert, "SSL"); 66 fail("IllegalArgumentException wasn't thrown"); 67 } catch (IllegalArgumentException iae) { 68 //expected 69 } catch (Exception e) { 70 fail(e + " was thrown instead of IllegalArgumentException"); 71 } 72 73 xcert = setX509Certificate(); 74 try { 75 xtm.checkClientTrusted(xcert, null); 76 fail("IllegalArgumentException wasn't thrown"); 77 } catch (IllegalArgumentException iae) { 78 //expected 79 } catch (Exception e) { 80 fail(e + " was thrown instead of IllegalArgumentException"); 81 } 82 83 try { 84 xtm.checkClientTrusted(xcert, ""); 85 fail("IllegalArgumentException wasn't thrown"); 86 } catch (IllegalArgumentException iae) { 87 //expected 88 } catch (Exception e) { 89 fail(e + " was thrown instead of IllegalArgumentException"); 90 } 91 } 92 93 /** 94 * javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[] chain, String authType) 95 */ 96 public void test_checkClientTrusted_02() { 97 X509TrustManagerImpl xtm = new X509TrustManagerImpl(); 98 X509Certificate[] xcert = setInvalid(); 99 100 try { 101 xtm.checkClientTrusted(xcert, "SSL"); 102 fail("CertificateException wasn't thrown"); 103 } catch (CertificateException ce) { 104 //expected 105 } 106 } 107 108 /** 109 * javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[] chain, String authType) 110 */ 111 public void test_checkClientTrusted_03() { 112 X509TrustManagerImpl xtm = new X509TrustManagerImpl(); 113 X509Certificate[] xcert = setX509Certificate(); 114 115 try { 116 xtm.checkClientTrusted(xcert, "SSL"); 117 } catch (Exception ex) { 118 fail("Unexpected exception " + ex); 119 } 120 } 121 122 /** 123 * javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[] chain, String authType) 124 */ 125 public void test_checkServerTrusted_01() { 126 X509TrustManagerImpl xtm = new X509TrustManagerImpl(); 127 X509Certificate[] xcert = null; 128 129 try { 130 xtm.checkServerTrusted(xcert, "SSL"); 131 fail("IllegalArgumentException wasn't thrown"); 132 } catch (IllegalArgumentException iae) { 133 //expected 134 } catch (Exception e) { 135 fail(e + " was thrown instead of IllegalArgumentException"); 136 } 137 138 xcert = new X509Certificate[0]; 139 try { 140 xtm.checkServerTrusted(xcert, "SSL"); 141 fail("IllegalArgumentException wasn't thrown"); 142 } catch (IllegalArgumentException iae) { 143 //expected 144 } catch (Exception e) { 145 fail(e + " was thrown instead of IllegalArgumentException"); 146 } 147 148 xcert = setX509Certificate(); 149 try { 150 xtm.checkServerTrusted(xcert, null); 151 fail("IllegalArgumentException wasn't thrown"); 152 } catch (IllegalArgumentException iae) { 153 //expected 154 } catch (Exception e) { 155 fail(e + " was thrown instead of IllegalArgumentException"); 156 } 157 158 try { 159 xtm.checkServerTrusted(xcert, ""); 160 fail("IllegalArgumentException wasn't thrown"); 161 } catch (IllegalArgumentException iae) { 162 //expected 163 } catch (Exception e) { 164 fail(e + " was thrown instead of IllegalArgumentException"); 165 } 166 } 167 168 /** 169 * javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[] chain, String authType) 170 */ 171 public void test_checkServerTrusted_02() { 172 X509TrustManagerImpl xtm = new X509TrustManagerImpl(); 173 X509Certificate[] xcert = setInvalid(); 174 175 try { 176 xtm.checkServerTrusted(xcert, "SSL"); 177 fail("CertificateException wasn't thrown"); 178 } catch (CertificateException ce) { 179 //expected 180 } 181 } 182 183 /** 184 * javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[] chain, String authType) 185 */ 186 public void test_checkServerTrusted_03() { 187 X509TrustManagerImpl xtm = new X509TrustManagerImpl(); 188 X509Certificate[] xcert = setX509Certificate(); 189 190 try { 191 xtm.checkServerTrusted(xcert, "SSL"); 192 } catch (Exception ex) { 193 fail("Unexpected exception " + ex); 194 } 195 } 196 197 /** 198 * javax.net.ssl.X509TrustManager#getAcceptedIssuers() 199 */ 200 public void test_getAcceptedIssuers() { 201 X509TrustManagerImpl xtm = new X509TrustManagerImpl(); 202 203 try { 204 assertNotNull(xtm.getAcceptedIssuers()); 205 } catch (Exception ex) { 206 fail("Unexpected exception " + ex); 207 } 208 } 209 210 } 211