jdk/test/sun/security/x509/equalNames/AltNamesEqualsTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Make sure names that are equal are treated as such.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @bug 4273559
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.ObjectIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class AltNamesEqualsTest{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private final static String baseDNSName = "bob.example.com";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private final static String baseDNSNameSame = "BOB.EXAMPLE.COM";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private final static String baseDNSNameDiff = "fred.example.com";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private final static String baseRFCName = "bob@example.com";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private final static String baseRFCNameSame = "BOB@EXAMPLE.COM";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private final static String baseRFCNameDiff = "fred@example.com";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private final static String baseURIName = "http://bob.example.com/bob.html";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private final static String baseURINameSame ="HTTP://BOB.EXAMPLE.COM/bob.html";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private final static String baseURINameDiff ="http://bob.example.com/BOB.html";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private final static String baseOIDName = "1.2.3.4";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final static String baseOIDNameDiff = "1.2.3.5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private final static String baseIPName = "1.2.3.4";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final static String baseIPNameDiff = "1.2.3.5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private DNSName dnsName, dnsNameSame, dnsNameDiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private RFC822Name rfcName, rfcNameSame, rfcNameDiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private URIName uriName, uriNameSame, uriNameDiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private OIDName oidName, oidNameSame, oidNameDiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private IPAddressName ipName, ipNameSame, ipNameDiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static void main(String [] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        AltNamesEqualsTest test = new AltNamesEqualsTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        test.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private void run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        initializeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        testNames("DNSNames", dnsName, dnsNameSame, dnsNameDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        testNames("RFC822Names", rfcName, rfcNameSame, rfcNameDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        testNames("URINames", uriName, uriNameSame, uriNameDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        testNames("OIDNames", oidName, oidNameSame, oidNameDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        testNames("IPAddressNames", ipName, ipNameSame, ipNameDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private void initializeNames() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        dnsName = new DNSName(baseDNSName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        dnsNameSame = new DNSName(baseDNSNameSame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        dnsNameDiff = new DNSName(baseDNSNameDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        rfcName = new RFC822Name(baseRFCName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        rfcNameSame = new RFC822Name(baseRFCNameSame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        rfcNameDiff = new RFC822Name(baseRFCNameDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        uriName = new URIName(baseURIName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        uriNameSame = new URIName(baseURINameSame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        uriNameDiff = new URIName(baseURINameDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        oidName = stringToOIDName(baseOIDName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        oidNameSame = stringToOIDName(baseOIDName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        oidNameDiff = stringToOIDName(baseOIDNameDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        ipName = stringToIPName(baseIPName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        ipNameSame = stringToIPName(baseIPName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        ipNameDiff = stringToIPName(baseIPNameDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private void testNames(String nameType, GeneralNameInterface name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                           GeneralNameInterface sameName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                           GeneralNameInterface diffName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (!name.equals(sameName)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new Exception("Equal " + nameType + " are being " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                "considered unequal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (name.equals(diffName)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new Exception("Unequal " + nameType + " are being " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                "considered equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private OIDName stringToOIDName(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        OIDName oidName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        ObjectIdentifier oid = new ObjectIdentifier(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        oidName = new OIDName(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return oidName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private IPAddressName stringToIPName(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        IPAddressName ipAddr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        //Convert to byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        int ch = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        int start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int end = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        byte components [];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        int  componentLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // Calculate length of IP address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        componentLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        while ((end = name.indexOf(ch,start)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            start = end + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            componentLen += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        componentLen += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (componentLen != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw new Exception("Invalid IP address: need four components");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        components = new byte[componentLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        String comp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        Integer compVal = new Integer(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        while ((end = name.indexOf(ch,start)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            comp = name.substring(start,end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            compVal = Integer.valueOf(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (compVal.intValue() < 0 || compVal.intValue() > 255)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                throw new Exception("Invalid IP address: component value " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                    "not between 0-255");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            components[i++] = compVal.byteValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            start = end + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        comp = name.substring(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        components[i] = Integer.valueOf(comp).byteValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        ipAddr = new IPAddressName(components);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return ipAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}