Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/x509/equalNames/AltNamesEqualsTest.java
38853 views
/*1* Copyright (c) 1999, 2007, 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 427355927* @author Yassir Elley28*/2930import sun.security.x509.*;31import sun.security.util.ObjectIdentifier;3233public class AltNamesEqualsTest{3435private final static String baseDNSName = "bob.example.com";36private final static String baseDNSNameSame = "BOB.EXAMPLE.COM";37private final static String baseDNSNameDiff = "fred.example.com";3839private final static String baseRFCName = "[email protected]";40private final static String baseRFCNameSame = "[email protected]";41private final static String baseRFCNameDiff = "[email protected]";4243private final static String baseURIName = "http://bob.example.com/bob.html";44private final static String baseURINameSame ="HTTP://BOB.EXAMPLE.COM/bob.html";45private final static String baseURINameDiff ="http://bob.example.com/BOB.html";4647private final static String baseOIDName = "1.2.3.4";48private final static String baseOIDNameDiff = "1.2.3.5";4950private final static String baseIPName = "1.2.3.4";51private final static String baseIPNameDiff = "1.2.3.5";5253private DNSName dnsName, dnsNameSame, dnsNameDiff;54private RFC822Name rfcName, rfcNameSame, rfcNameDiff;55private URIName uriName, uriNameSame, uriNameDiff;56private OIDName oidName, oidNameSame, oidNameDiff;57private IPAddressName ipName, ipNameSame, ipNameDiff;5859public static void main(String [] args) throws Exception {60AltNamesEqualsTest test = new AltNamesEqualsTest();61test.run();62}6364private void run() throws Exception {65initializeNames();66testNames("DNSNames", dnsName, dnsNameSame, dnsNameDiff);67testNames("RFC822Names", rfcName, rfcNameSame, rfcNameDiff);68testNames("URINames", uriName, uriNameSame, uriNameDiff);69testNames("OIDNames", oidName, oidNameSame, oidNameDiff);70testNames("IPAddressNames", ipName, ipNameSame, ipNameDiff);71}7273private void initializeNames() throws Exception {74dnsName = new DNSName(baseDNSName);75dnsNameSame = new DNSName(baseDNSNameSame);76dnsNameDiff = new DNSName(baseDNSNameDiff);7778rfcName = new RFC822Name(baseRFCName);79rfcNameSame = new RFC822Name(baseRFCNameSame);80rfcNameDiff = new RFC822Name(baseRFCNameDiff);8182uriName = new URIName(baseURIName);83uriNameSame = new URIName(baseURINameSame);84uriNameDiff = new URIName(baseURINameDiff);8586oidName = stringToOIDName(baseOIDName);87oidNameSame = stringToOIDName(baseOIDName);88oidNameDiff = stringToOIDName(baseOIDNameDiff);8990ipName = stringToIPName(baseIPName);91ipNameSame = stringToIPName(baseIPName);92ipNameDiff = stringToIPName(baseIPNameDiff);93}9495private void testNames(String nameType, GeneralNameInterface name,96GeneralNameInterface sameName,97GeneralNameInterface diffName)98throws Exception99{100if (!name.equals(sameName)){101throw new Exception("Equal " + nameType + " are being " +102"considered unequal");103}104if (name.equals(diffName)){105throw new Exception("Unequal " + nameType + " are being " +106"considered equal");107}108}109110private OIDName stringToOIDName(String name)111throws Exception112{113OIDName oidName = null;114ObjectIdentifier oid = new ObjectIdentifier(name);115oidName = new OIDName(oid);116return oidName;117}118119private IPAddressName stringToIPName(String name)120throws Exception121{122IPAddressName ipAddr = null;123124//Convert to byte array125int ch = '.';126int start = 0;127int end = 0;128byte components [];129int componentLen;130131// Calculate length of IP address132componentLen = 0;133while ((end = name.indexOf(ch,start)) != -1) {134start = end + 1;135componentLen += 1;136}137componentLen += 1;138if (componentLen != 4)139throw new Exception("Invalid IP address: need four components");140components = new byte[componentLen];141142start = 0;143int i = 0;144String comp = null;145Integer compVal = new Integer(0);146while ((end = name.indexOf(ch,start)) != -1) {147comp = name.substring(start,end);148compVal = Integer.valueOf(comp);149if (compVal.intValue() < 0 || compVal.intValue() > 255)150throw new Exception("Invalid IP address: component value " +151"not between 0-255");152components[i++] = compVal.byteValue();153start = end + 1;154}155comp = name.substring(start);156components[i] = Integer.valueOf(comp).byteValue();157ipAddr = new IPAddressName(components);158return ipAddr;159}160}161162163