Path: blob/master/test/jdk/javax/security/auth/x500/X500Principal/Equals.java
41155 views
/*1* Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 438503126* @bug 445992527* @summary X500Principal.equals can be optimized,28* equals and hashcode are underspecified29*/3031import javax.security.auth.x500.X500Principal;3233public class Equals {3435private static final String p1String =36"O=sun, Ou=eng,cn=Test 1, [email protected], UID=1 ";3738private static final String p2String =39" o=SUN,OU=eng, cn=test 1,emailaddress = [email protected],UID=1";4041private static final String p3String =42" o = SUN, cn=test 1+ emailaddress = [email protected] + uId =5";4344private static final String p4String =45"o=SUN,uid=5 + emailaddress = [email protected] +cn=test 1";4647private static final String p5String =48"emailaddress = [email protected] + SURNAME=blah";4950private static final String p6String =51"surname=blah+ emailAddress = [email protected] ";5253private static final String p7String =54"o=sun, ou=esc\\\"quote, cn=duke";5556private static final String p8String =57"o=sun, ou= esc\\\"quote,cn=duke";5859public static void main(String[] args) {6061// test regular equals62X500Principal p1 = new X500Principal(p1String);63X500Principal p2 = new X500Principal(p2String);6465printName("Principal 1:", p1String, p1);66printName("Principal 2:", p2String, p2);6768if (!p1.equals(p2))69throw new SecurityException("Equals test failed: #1");7071X500Principal notEqual = new X500Principal("cn=test2");72if (p1.equals(notEqual))73throw new SecurityException("Equals test failed: #2");7475if (p1.equals(null))76throw new SecurityException("Equals test failed: #3");7778if (p1.hashCode() != p2.hashCode())79throw new SecurityException("Equals test failed: #4");8081// test multiple AVA's in an RDN82X500Principal p3 = new X500Principal(p3String);83X500Principal p4 = new X500Principal(p4String);8485printName("Principal 3:", p3String, p3);86printName("Principal 4:", p4String, p4);8788if (!p3.equals(p4))89throw new SecurityException("Equals test failed: #5");9091if (p1.equals(p3) || p2.equals(p3))92throw new SecurityException("Equals test failed: #6");9394if (p3.hashCode() != p4.hashCode())95throw new SecurityException("Equals test failed: #7");9697X500Principal p5 = new X500Principal(p5String);98X500Principal p6 = new X500Principal(p6String);99100printName("Principal 5:", p5String, p5);101printName("Principal 6:", p6String, p6);102103if (!p5.equals(p6))104throw new SecurityException("Equals test failed: #8");105106if (p5.hashCode() != p6.hashCode())107throw new SecurityException("Equals test failed: #9");108109X500Principal p7 = new X500Principal(p7String);110X500Principal p8 = new X500Principal(p8String);111112printName("Principal 7:", p7String, p7);113printName("Principal 8:", p8String, p8);114115if (!p7.equals(p8))116throw new SecurityException("Equals test failed: #10");117118if (p7.hashCode() != p8.hashCode())119throw new SecurityException("Equals test failed: #11");120121System.out.println("Equals test passed");122}123124static void printName(String heading, String input, X500Principal p) {125System.out.println(heading);126System.out.println(" input:\t\t" + input);127System.out.println();128System.out.println(" toString:\t" + p.toString());129System.out.println();130System.out.println(" getName:\t" + p.getName());131System.out.println();132System.out.println(" getName 1779:\t" + p.getName("rfc1779"));133System.out.println();134System.out.println(" getName 2253:\t" + p.getName("rfc2253"));135System.out.println();136System.out.println(" getName canon:\t" + p.getName("canonical"));137System.out.println();138}139}140141142