Path: blob/master/test/jdk/javax/security/auth/x500/X500Principal/NameFormat.java
41155 views
/*1* Copyright (c) 2012, 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 4505980 5109882 7049963 709056526* @summary X500Principal input name parsing issues and wrong exception thrown27* @modules java.base/sun.security.x50928* @run main/othervm -Djava.security.debug=x509,ava NameFormat29*30* The debug=ava above must be set in order to check for escaped hex chars.31*/32import javax.security.auth.x500.X500Principal;3334public class NameFormat {3536public static void main(String[] args) throws Exception {3738// tests for leading/trailing escaped/non-escaped spaces3940testName("cn=\\ duke ", "RFC1779", "CN=\" duke\"", 1);41testName("cn=\\ duke ", "RFC2253", "CN=\\ duke", 2);42testName("cn=\\ duke ", "CANONICAL", "cn=duke", 3);43testName("cn=\\ duke ", "toString", "CN=\" duke\"", 4);4445testName("cn= duke", "RFC1779", "CN=duke", 5);46testName("cn= duke", "RFC2253", "CN=duke", 6);47testName("cn= duke", "CANONICAL", "cn=duke", 7);48testName("cn= duke", "toString", "CN=duke", 8);4950testName("cn=duke\\ ", "RFC1779", "CN=\"duke \"", 9);51testName("cn=duke\\ ", "RFC2253", "CN=duke\\ ", 10);52testName("cn=duke\\ ", "CANONICAL", "cn=duke", 11);53testName("cn=duke\\ ", "toString", "CN=\"duke \"", 12);5455testName("cn=duke\\ , ou= sun\\ ", "RFC1779",56"CN=\"duke \", OU=\"sun \"", 13);57testName("cn=duke\\ , ou= sun\\ ", "RFC2253",58"CN=duke\\ ,OU=sun\\ ", 14);59testName("cn=duke\\ , ou= sun\\ ", "CANONICAL",60"cn=duke,ou=sun", 15);61testName("cn=duke\\ , ou= sun\\ ", "toString",62"CN=\"duke \", OU=\"sun \"", 16);6364// tests for trailing escaped backslash6566testName("cn=duke \\\\\\,test,O=java", "CANONICAL",67"cn=duke \\\\\\,test,o=java", 17);6869testName("cn=duke\\\\, o=java", "CANONICAL",70"cn=duke\\\\,o=java", 18);7172X500Principal p = new X500Principal("cn=duke \\\\\\,test,o=java");73X500Principal p2 = new X500Principal(p.getName("CANONICAL"));74if (p.getName("CANONICAL").equals(p2.getName("CANONICAL"))) {75System.out.println("test 19 succeeded");76} else {77throw new SecurityException("test 19 failed\n" +78p.getName("CANONICAL") + " not equal to " +79p2.getName("CANONICAL"));80}8182try {83p = new X500Principal("cn=duke \\\\,test,o=java");84throw new SecurityException("test 19.5 failed:\n" +85p.getName("CANONICAL"));86} catch (IllegalArgumentException iae) {87System.out.println("test 19.5 succeeded");88iae.printStackTrace();89}9091// tests for wrong exception thrown92try {93byte[] encoding = {94(byte)0x17, (byte)0x80, (byte)0x70, (byte)0x41,95(byte)0x6b, (byte)0x15, (byte)0xdc, (byte)0x84,96(byte)0xef, (byte)0x58, (byte)0xac, (byte)0x88,97(byte)0xae, (byte)0xb0, (byte)0x19, (byte)0x7c,98(byte)0x6f, (byte)0xea, (byte)0xf5, (byte)0x56,99};100p = new X500Principal(new java.io.DataInputStream101(new java.io.ByteArrayInputStream(encoding)));102} catch (IllegalArgumentException iae) {103System.out.println("test 20 succeeded");104iae.printStackTrace();105} catch (Exception e) {106System.out.println("test 20 failed");107throw e;108}109110// tests for escaping '+' in canonical form111112testName("cn=se\\+an, ou= sun\\ ", "CANONICAL",113"cn=se\\+an,ou=sun", 21);114115// tests for embedded hex pairs116117testName("CN=Before\\0dAfter,DC=example,DC=net", "toString",118"CN=Before\\0DAfter, DC=example, DC=net", 22);119testName("CN=Before\\0dAfter,DC=example,DC=net", "RFC1779",120"CN=Before\\0DAfter, " +121"OID.0.9.2342.19200300.100.1.25=example, " +122"OID.0.9.2342.19200300.100.1.25=net", 23);123testName("CN=Before\\0dAfter,DC=example,DC=net", "RFC2253",124"CN=Before\\0DAfter,DC=example,DC=net", 24);125testName("CN=Before\\0dAfter,DC=example,DC=net", "CANONICAL",126"cn=before\\0dafter,dc=#16076578616d706c65,dc=#16036e6574", 25);127128testName("CN=Lu\\C4\\8Di\\C4\\87", "toString",129"CN=Lu\\C4\\8Di\\C4\\87", 26);130testName("CN=Lu\\C4\\8Di\\C4\\87", "RFC1779",131"CN=Lu\\C4\\8Di\\C4\\87", 27);132testName("CN=Lu\\C4\\8Di\\C4\\87", "RFC2253",133"CN=Lu\\C4\\8Di\\C4\\87", 28);134testName("CN=Lu\\C4\\8Di\\C4\\87", "CANONICAL",135"cn=lu\\c4\\8di\\c4\\87", 29);136137try {138p = new X500Principal("cn=\\gg");139throw new SecurityException("test 30 failed");140} catch (IllegalArgumentException iae) {141System.out.println("test 30 succeeded");142}143144// tests for invalid escaped chars145146try {147p = new X500Principal("cn=duke \\test");148throw new SecurityException("test 31 failed");149} catch (IllegalArgumentException iae) {150System.out.println("test 31 succeeded");151}152153try {154p = new X500Principal("cn=duke \\?test");155throw new SecurityException("test 32 failed");156} catch (IllegalArgumentException iae) {157System.out.println("test 32 succeeded");158}159160// tests for X500Name using RFC2253 as format161162try {163// invalid non-escaped leading space164sun.security.x509.X500Name name =165new sun.security.x509.X500Name("cn= duke test", "RFC2253");166throw new SecurityException("test 33 failed");167} catch (java.io.IOException ioe) {168ioe.printStackTrace();169System.out.println("test 33 succeeded");170}171172try {173// invalid non-escaped trailing space174sun.security.x509.X500Name name =175new sun.security.x509.X500Name("cn=duke test ", "RFC2253");176throw new SecurityException("test 34 failed");177} catch (java.io.IOException ioe) {178System.out.println("test 34 succeeded");179}180181testName("CN=SPECIAL CHARS,OU=\\#\\\"\\,\\<\\>\\+\\;,O=foo, " +182"L=bar, ST=baz, C=JP", "RFC1779",183"CN=SPECIAL CHARS, OU=\"#\\\",<>+;\", O=foo, L=bar, " +184"ST=baz, C=JP", 35);185186// test that double-quoted string is not escaped in RFC 1779 format187testName("CN=\"\\\"Duke\\\"\"", "RFC1779", "CN=\"Duke\"", 36);188}189190public static void testName(String in, String outFormat,191String expect, int n)192throws Exception {193194X500Principal p = new X500Principal(in);195if (outFormat.equalsIgnoreCase("toString")) {196if (p.toString().equals(expect)) {197System.out.println("test " + n + " succeeded");198} else {199throw new SecurityException("test " + n + " failed:\n" +200"expected '" + expect + "'\n" +201"got '" + p.toString() + "'");202}203} else {204if (p.getName(outFormat).equals(expect)) {205System.out.println("test " + n + " succeeded");206} else {207throw new SecurityException("test " + n + " failed:\n" +208"expected '" + expect + "'\n" +209"got '" + p.getName(outFormat) + "'");210}211}212}213}214215216