1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.javax.security.auth; 19 20import dalvik.annotation.TestLevel; 21import dalvik.annotation.TestTargetClass; 22import dalvik.annotation.TestTargetNew; 23 24import junit.framework.TestCase; 25 26import javax.security.auth.x500.X500Principal; 27 28import java.io.ByteArrayInputStream; 29import java.io.InputStream; 30import java.security.cert.CertificateFactory; 31import java.security.cert.X509Certificate; 32import org.apache.harmony.security.tests.support.cert.TestUtils; 33 34/** 35 * Tests for <code>X500Principal</code> class constructors and methods. 36 * 37 */ 38@TestTargetClass(X500Principal.class) 39public class X500PrincipalTest extends TestCase { 40 41 /** 42 * @tests javax.security.auth.x500.X500Principal#X500Principal(String name) 43 */ 44 @TestTargetNew( 45 level = TestLevel.COMPLETE, 46 notes = "", 47 method = "X500Principal", 48 args = {String.class} 49 ) 50 public void test_X500Principal_01() { 51 String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US"; 52 53 try { 54 X500Principal xpr = new X500Principal(name); 55 assertNotNull("Null object returned", xpr); 56 String resName = xpr.getName(); 57 assertEquals(name, resName); 58 } catch (Exception e) { 59 fail("Unexpected exception: " + e); 60 } 61 62 try { 63 X500Principal xpr = new X500Principal((String)null); 64 fail("NullPointerException wasn't thrown"); 65 } catch (NullPointerException npe) { 66 } catch (Exception e) { 67 fail(e + " was thrown instead of NullPointerException"); 68 } 69 70 try { 71 X500Principal xpr = new X500Principal("X500PrincipalName"); 72 fail("IllegalArgumentException wasn't thrown"); 73 } catch (IllegalArgumentException npe) { 74 } catch (Exception e) { 75 fail(e + " was thrown instead of IllegalArgumentException"); 76 } 77 } 78 79 /** 80 * @tests javax.security.auth.x500.X500Principal#X500Principal(InputStream is) 81 */ 82 @TestTargetNew( 83 level = TestLevel.COMPLETE, 84 notes = "", 85 method = "X500Principal", 86 args = {InputStream.class} 87 ) 88 public void test_X500Principal_02() { 89 String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US"; 90 byte[] ba = getByteArray(TestUtils.getX509Certificate_v1()); 91 ByteArrayInputStream is = new ByteArrayInputStream(ba); 92 InputStream isNull = null; 93 94 try { 95 X500Principal xpr = new X500Principal(is); 96 assertNotNull("Null object returned", xpr); 97 byte[] resArray = xpr.getEncoded(); 98 assertEquals(ba.length, resArray.length); 99 } catch (Exception e) { 100 fail("Unexpected exception: " + e); 101 } 102 103 try { 104 X500Principal xpr = new X500Principal(isNull); 105 fail("NullPointerException wasn't thrown"); 106 } catch (NullPointerException npe) { 107 } catch (Exception e) { 108 fail(e + " was thrown instead of NullPointerException"); 109 } 110 111 is = new ByteArrayInputStream(name.getBytes()); 112 try { 113 X500Principal xpr = new X500Principal(is); 114 fail("IllegalArgumentException wasn't thrown"); 115 } catch (IllegalArgumentException npe) { 116 } catch (Exception e) { 117 fail(e + " was thrown instead of IllegalArgumentException"); 118 } 119 } 120 121 /** 122 * @tests javax.security.auth.x500.X500Principal#X500Principal(byte[] name) 123 */ 124 @TestTargetNew( 125 level = TestLevel.COMPLETE, 126 notes = "", 127 method = "X500Principal", 128 args = {byte[].class} 129 ) 130 public void test_X500Principal_03() { 131 String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US"; 132 byte[] ba = getByteArray(TestUtils.getX509Certificate_v1()); 133 byte[] baNull = null; 134 135 try { 136 X500Principal xpr = new X500Principal(ba); 137 assertNotNull("Null object returned", xpr); 138 byte[] resArray = xpr.getEncoded(); 139 assertEquals(ba.length, resArray.length); 140 } catch (Exception e) { 141 fail("Unexpected exception: " + e); 142 } 143 144 try { 145 X500Principal xpr = new X500Principal(baNull); 146 fail("IllegalArgumentException wasn't thrown"); 147 } catch (IllegalArgumentException npe) { 148 } catch (Exception e) { 149 fail(e + " was thrown instead of IllegalArgumentException"); 150 } 151 152 ba = name.getBytes(); 153 try { 154 X500Principal xpr = new X500Principal(ba); 155 fail("IllegalArgumentException wasn't thrown"); 156 } catch (IllegalArgumentException npe) { 157 } catch (Exception e) { 158 fail(e + " was thrown instead of IllegalArgumentException"); 159 } 160 } 161 162 /** 163 * @tests javax.security.auth.x500.X500Principal#getName() 164 */ 165 @TestTargetNew( 166 level = TestLevel.COMPLETE, 167 notes = "", 168 method = "getName", 169 args = {} 170 ) 171 public void test_getName() { 172 String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US"; 173 X500Principal xpr = new X500Principal(name); 174 try { 175 String resName = xpr.getName(); 176 assertEquals(name, resName); 177 } catch (Exception e) { 178 fail("Unexpected exception: " + e); 179 } 180 } 181 182 /** 183 * @tests javax.security.auth.x500.X500Principal#getName(String format) 184 */ 185 @TestTargetNew( 186 level = TestLevel.COMPLETE, 187 notes = "", 188 method = "getName", 189 args = {String.class} 190 ) 191 public void test_getName_Format() { 192 String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US"; 193 String expectedName = "cn=duke,ou=javasoft,o=sun microsystems,c=us"; 194 X500Principal xpr = new X500Principal(name); 195 try { 196 String resName = xpr.getName(X500Principal.CANONICAL); 197 assertEquals(expectedName, resName); 198 } catch (Exception e) { 199 fail("Unexpected exception: " + e); 200 } 201 202 expectedName = "CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US"; 203 try { 204 String resName = xpr.getName(X500Principal.RFC1779); 205 assertEquals(expectedName, resName); 206 } catch (Exception e) { 207 fail("Unexpected exception: " + e); 208 } 209 210 try { 211 String resName = xpr.getName(X500Principal.RFC2253); 212 assertEquals(name, resName); 213 } catch (Exception e) { 214 fail("Unexpected exception: " + e); 215 } 216 217 try { 218 String resName = xpr.getName(null); 219 fail("IllegalArgumentException wasn't thrown"); 220 } catch (IllegalArgumentException iae) { 221 } 222 try { 223 String resName = xpr.getName("RFC2254"); 224 fail("IllegalArgumentException wasn't thrown"); 225 } catch (IllegalArgumentException iae) { 226 } 227 } 228 229 /** 230 * @tests javax.security.auth.x500.X500Principal#hashCode() 231 */ 232 @TestTargetNew( 233 level = TestLevel.COMPLETE, 234 notes = "", 235 method = "hashCode", 236 args = {} 237 ) 238 public void test_hashCode() { 239 String name = "CN=Duke,OU=JavaSoft,O=Sun Microsystems,C=US"; 240 X500Principal xpr = new X500Principal(name); 241 try { 242 int res = xpr.hashCode(); 243 assertNotNull(res); 244 } catch (Exception e) { 245 fail("Unexpected exception: " + e); 246 } 247 } 248 249 /** 250 * @tests javax.security.auth.x500.X500Principal#toString() 251 */ 252 @TestTargetNew( 253 level = TestLevel.COMPLETE, 254 notes = "", 255 method = "toString", 256 args = {} 257 ) 258 public void test_toString() { 259 String name = "CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US"; 260 X500Principal xpr = new X500Principal(name); 261 try { 262 String res = xpr.toString(); 263 assertNotNull(res); 264 assertEquals(name, res); 265 } catch (Exception e) { 266 fail("Unexpected exception: " + e); 267 } 268 } 269 270 /** 271 * @tests javax.security.auth.x500.X500Principal#getEncoded() 272 */ 273 @TestTargetNew( 274 level = TestLevel.COMPLETE, 275 notes = "", 276 method = "getEncoded", 277 args = {} 278 ) 279 public void test_getEncoded() { 280 byte[] ba = getByteArray(TestUtils.getX509Certificate_v1()); 281 X500Principal xpr = new X500Principal(ba); 282 try { 283 byte[] res = xpr.getEncoded(); 284 assertNotNull(res); 285 assertEquals(ba.length, res.length); 286 } catch (Exception e) { 287 fail("Unexpected exception: " + e); 288 } 289 } 290 291 /** 292 * @tests javax.security.auth.x500.X500Principal#equals(Object o) 293 */ 294 @TestTargetNew( 295 level = TestLevel.COMPLETE, 296 notes = "", 297 method = "equals", 298 args = {Object.class} 299 ) 300 public void test_equals() { 301 String name1 = "CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US"; 302 String name2 = "cn=duke,ou=javasoft,o=sun microsystems,c=us"; 303 String name3 = "CN=Alex Astapchuk, OU=SSG, O=Intel ZAO, C=RU"; 304 X500Principal xpr1 = new X500Principal(name1); 305 X500Principal xpr2 = new X500Principal(name2); 306 X500Principal xpr3 = new X500Principal(name3); 307 try { 308 assertTrue("False returned", xpr1.equals(xpr2)); 309 assertFalse("True returned", xpr1.equals(xpr3)); 310 } catch (Exception e) { 311 fail("Unexpected exception: " + e); 312 } 313 } 314 315 private byte[] getByteArray(byte[] array) { 316 byte[] x = null; 317 try { 318 ByteArrayInputStream is = new ByteArrayInputStream(array); 319 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 320 X509Certificate cert = (X509Certificate)cf.generateCertificate(is); 321 X500Principal xx = cert.getIssuerX500Principal(); 322 x = xx.getEncoded(); 323 } catch (Exception e) { 324 return null; 325 } 326 return x; 327 } 328} 329 330