Path: blob/master/test/jdk/sun/security/x509/equalNames/AltNamesEqualsTest.java
41152 views
/*1* Copyright (c) 1999, 2020, 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* @summary Make sure names that are equal are treated as such.26* @bug 4273559 824215127* @author Yassir Elley28* @modules java.base/sun.security.util29* java.base/sun.security.x50930*/3132import sun.security.x509.*;33import sun.security.util.ObjectIdentifier;3435public class AltNamesEqualsTest{3637private final static String baseDNSName = "bob.example.com";38private final static String baseDNSNameSame = "BOB.EXAMPLE.COM";39private final static String baseDNSNameDiff = "fred.example.com";4041private final static String baseRFCName = "[email protected]";42private final static String baseRFCNameSame = "[email protected]";43private final static String baseRFCNameDiff = "[email protected]";4445private final static String baseURIName = "http://bob.example.com/bob.html";46private final static String baseURINameSame ="HTTP://BOB.EXAMPLE.COM/bob.html";47private final static String baseURINameDiff ="http://bob.example.com/BOB.html";4849private final static String baseOIDName = "1.2.3.4";50private final static String baseOIDNameDiff = "1.2.3.5";5152private final static String baseIPName = "1.2.3.4";53private final static String baseIPNameDiff = "1.2.3.5";5455private DNSName dnsName, dnsNameSame, dnsNameDiff;56private RFC822Name rfcName, rfcNameSame, rfcNameDiff;57private URIName uriName, uriNameSame, uriNameDiff;58private OIDName oidName, oidNameSame, oidNameDiff;59private IPAddressName ipName, ipNameSame, ipNameDiff;6061public static void main(String [] args) throws Exception {62AltNamesEqualsTest test = new AltNamesEqualsTest();63test.run();64}6566private void run() throws Exception {67initializeNames();68testNames("DNSNames", dnsName, dnsNameSame, dnsNameDiff);69testNames("RFC822Names", rfcName, rfcNameSame, rfcNameDiff);70testNames("URINames", uriName, uriNameSame, uriNameDiff);71testNames("OIDNames", oidName, oidNameSame, oidNameDiff);72testNames("IPAddressNames", ipName, ipNameSame, ipNameDiff);73}7475private void initializeNames() throws Exception {76dnsName = new DNSName(baseDNSName);77dnsNameSame = new DNSName(baseDNSNameSame);78dnsNameDiff = new DNSName(baseDNSNameDiff);7980rfcName = new RFC822Name(baseRFCName);81rfcNameSame = new RFC822Name(baseRFCNameSame);82rfcNameDiff = new RFC822Name(baseRFCNameDiff);8384uriName = new URIName(baseURIName);85uriNameSame = new URIName(baseURINameSame);86uriNameDiff = new URIName(baseURINameDiff);8788oidName = stringToOIDName(baseOIDName);89oidNameSame = stringToOIDName(baseOIDName);90oidNameDiff = stringToOIDName(baseOIDNameDiff);9192ipName = stringToIPName(baseIPName);93ipNameSame = stringToIPName(baseIPName);94ipNameDiff = stringToIPName(baseIPNameDiff);95}9697private void testNames(String nameType, GeneralNameInterface name,98GeneralNameInterface sameName,99GeneralNameInterface diffName)100throws Exception101{102if (!name.equals(sameName)){103throw new Exception("Equal " + nameType + " are being " +104"considered unequal");105}106if (name.equals(diffName)){107throw new Exception("Unequal " + nameType + " are being " +108"considered equal");109}110}111112private OIDName stringToOIDName(String name)113throws Exception114{115OIDName oidName = null;116ObjectIdentifier oid = ObjectIdentifier.of(name);117oidName = new OIDName(oid);118return oidName;119}120121private IPAddressName stringToIPName(String name)122throws Exception123{124IPAddressName ipAddr = null;125126//Convert to byte array127int ch = '.';128int start = 0;129int end = 0;130byte components [];131int componentLen;132133// Calculate length of IP address134componentLen = 0;135while ((end = name.indexOf(ch,start)) != -1) {136start = end + 1;137componentLen += 1;138}139componentLen += 1;140if (componentLen != 4)141throw new Exception("Invalid IP address: need four components");142components = new byte[componentLen];143144start = 0;145int i = 0;146String comp = null;147Integer compVal = new Integer(0);148while ((end = name.indexOf(ch,start)) != -1) {149comp = name.substring(start,end);150compVal = Integer.valueOf(comp);151if (compVal.intValue() < 0 || compVal.intValue() > 255)152throw new Exception("Invalid IP address: component value " +153"not between 0-255");154components[i++] = compVal.byteValue();155start = end + 1;156}157comp = name.substring(start);158components[i] = Integer.valueOf(comp).byteValue();159ipAddr = new IPAddressName(components);160return ipAddr;161}162}163164165