Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/x500/X500Principal/NameFormat.java
38862 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* @run main/othervm -Djava.security.debug=x509,ava NameFormat28*29* The debug=ava above must be set in order to check for escaped hex chars.30*/31import javax.security.auth.x500.X500Principal;3233public class NameFormat {3435public static void main(String[] args) throws Exception {3637// tests for leading/trailing escaped/non-escaped spaces3839testName("cn=\\ duke ", "RFC1779", "CN=\" duke\"", 1);40testName("cn=\\ duke ", "RFC2253", "CN=\\ duke", 2);41testName("cn=\\ duke ", "CANONICAL", "cn=duke", 3);42testName("cn=\\ duke ", "toString", "CN=\" duke\"", 4);4344testName("cn= duke", "RFC1779", "CN=duke", 5);45testName("cn= duke", "RFC2253", "CN=duke", 6);46testName("cn= duke", "CANONICAL", "cn=duke", 7);47testName("cn= duke", "toString", "CN=duke", 8);4849testName("cn=duke\\ ", "RFC1779", "CN=\"duke \"", 9);50testName("cn=duke\\ ", "RFC2253", "CN=duke\\ ", 10);51testName("cn=duke\\ ", "CANONICAL", "cn=duke", 11);52testName("cn=duke\\ ", "toString", "CN=\"duke \"", 12);5354testName("cn=duke\\ , ou= sun\\ ", "RFC1779",55"CN=\"duke \", OU=\"sun \"", 13);56testName("cn=duke\\ , ou= sun\\ ", "RFC2253",57"CN=duke\\ ,OU=sun\\ ", 14);58testName("cn=duke\\ , ou= sun\\ ", "CANONICAL",59"cn=duke,ou=sun", 15);60testName("cn=duke\\ , ou= sun\\ ", "toString",61"CN=\"duke \", OU=\"sun \"", 16);6263// tests for trailing escaped backslash6465testName("cn=duke \\\\\\,test,O=java", "CANONICAL",66"cn=duke \\\\\\,test,o=java", 17);6768testName("cn=duke\\\\, o=java", "CANONICAL",69"cn=duke\\\\,o=java", 18);7071X500Principal p = new X500Principal("cn=duke \\\\\\,test,o=java");72X500Principal p2 = new X500Principal(p.getName("CANONICAL"));73if (p.getName("CANONICAL").equals(p2.getName("CANONICAL"))) {74System.out.println("test 19 succeeded");75} else {76throw new SecurityException("test 19 failed\n" +77p.getName("CANONICAL") + " not equal to " +78p2.getName("CANONICAL"));79}8081try {82p = new X500Principal("cn=duke \\\\,test,o=java");83throw new SecurityException("test 19.5 failed:\n" +84p.getName("CANONICAL"));85} catch (IllegalArgumentException iae) {86System.out.println("test 19.5 succeeded");87iae.printStackTrace();88}8990// tests for wrong exception thrown91try {92byte[] encoding = {93(byte)0x17, (byte)0x80, (byte)0x70, (byte)0x41,94(byte)0x6b, (byte)0x15, (byte)0xdc, (byte)0x84,95(byte)0xef, (byte)0x58, (byte)0xac, (byte)0x88,96(byte)0xae, (byte)0xb0, (byte)0x19, (byte)0x7c,97(byte)0x6f, (byte)0xea, (byte)0xf5, (byte)0x56,98};99p = new X500Principal(new java.io.DataInputStream100(new java.io.ByteArrayInputStream(encoding)));101} catch (IllegalArgumentException iae) {102System.out.println("test 20 succeeded");103iae.printStackTrace();104} catch (Exception e) {105System.out.println("test 20 failed");106throw e;107}108109// tests for escaping '+' in canonical form110111testName("cn=se\\+an, ou= sun\\ ", "CANONICAL",112"cn=se\\+an,ou=sun", 21);113114// tests for embedded hex pairs115116testName("CN=Before\\0dAfter,DC=example,DC=net", "toString",117"CN=Before\\0DAfter, DC=example, DC=net", 22);118testName("CN=Before\\0dAfter,DC=example,DC=net", "RFC1779",119"CN=Before\\0DAfter, " +120"OID.0.9.2342.19200300.100.1.25=example, " +121"OID.0.9.2342.19200300.100.1.25=net", 23);122testName("CN=Before\\0dAfter,DC=example,DC=net", "RFC2253",123"CN=Before\\0DAfter,DC=example,DC=net", 24);124testName("CN=Before\\0dAfter,DC=example,DC=net", "CANONICAL",125"cn=before\\0dafter,dc=#16076578616d706c65,dc=#16036e6574", 25);126127testName("CN=Lu\\C4\\8Di\\C4\\87", "toString",128"CN=Lu\\C4\\8Di\\C4\\87", 26);129testName("CN=Lu\\C4\\8Di\\C4\\87", "RFC1779",130"CN=Lu\\C4\\8Di\\C4\\87", 27);131testName("CN=Lu\\C4\\8Di\\C4\\87", "RFC2253",132"CN=Lu\\C4\\8Di\\C4\\87", 28);133testName("CN=Lu\\C4\\8Di\\C4\\87", "CANONICAL",134"cn=lu\\c4\\8di\\c4\\87", 29);135136try {137p = new X500Principal("cn=\\gg");138throw new SecurityException("test 30 failed");139} catch (IllegalArgumentException iae) {140System.out.println("test 30 succeeded");141}142143// tests for invalid escaped chars144145try {146p = new X500Principal("cn=duke \\test");147throw new SecurityException("test 31 failed");148} catch (IllegalArgumentException iae) {149System.out.println("test 31 succeeded");150}151152try {153p = new X500Principal("cn=duke \\?test");154throw new SecurityException("test 32 failed");155} catch (IllegalArgumentException iae) {156System.out.println("test 32 succeeded");157}158159// tests for X500Name using RFC2253 as format160161try {162// invalid non-escaped leading space163sun.security.x509.X500Name name =164new sun.security.x509.X500Name("cn= duke test", "RFC2253");165throw new SecurityException("test 33 failed");166} catch (java.io.IOException ioe) {167ioe.printStackTrace();168System.out.println("test 33 succeeded");169}170171try {172// invalid non-escaped trailing space173sun.security.x509.X500Name name =174new sun.security.x509.X500Name("cn=duke test ", "RFC2253");175throw new SecurityException("test 34 failed");176} catch (java.io.IOException ioe) {177System.out.println("test 34 succeeded");178}179180testName("CN=SPECIAL CHARS,OU=\\#\\\"\\,\\<\\>\\+\\;,O=foo, " +181"L=bar, ST=baz, C=JP", "RFC1779",182"CN=SPECIAL CHARS, OU=\"#\\\",<>+;\", O=foo, L=bar, " +183"ST=baz, C=JP", 35);184185// test that double-quoted string is not escaped in RFC 1779 format186testName("CN=\"\\\"Duke\\\"\"", "RFC1779", "CN=\"Duke\"", 36);187}188189public static void testName(String in, String outFormat,190String expect, int n)191throws Exception {192193X500Principal p = new X500Principal(in);194if (outFormat.equalsIgnoreCase("toString")) {195if (p.toString().equals(expect)) {196System.out.println("test " + n + " succeeded");197} else {198throw new SecurityException("test " + n + " failed:\n" +199"expected '" + expect + "'\n" +200"got '" + p.toString() + "'");201}202} else {203if (p.getName(outFormat).equals(expect)) {204System.out.println("test " + n + " succeeded");205} else {206throw new SecurityException("test " + n + " failed:\n" +207"expected '" + expect + "'\n" +208"got '" + p.getName(outFormat) + "'");209}210}211}212}213214215