Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/x509/X500Name/DerValueConstructor.java
38853 views
/*1* Copyright (c) 1999, 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/* @test24* @bug 422883325* @summary Make sure constructor that takes DerValue argument works26*/2728import sun.security.util.*;29import sun.security.x509.*;3031public class DerValueConstructor {3233public static void main(String[] args) throws Exception {34String name = "CN=anne test";3536DerOutputStream debugDER;37byte[] ba;3839/*40* X500Name41*/4243// encode44X500Name dn = new X500Name(name);45System.err.println("DEBUG: dn: " + dn.toString());46debugDER = new DerOutputStream();47dn.encode(debugDER);48ba = debugDER.toByteArray();49System.err.print("DEBUG: encoded X500Name bytes: ");50System.out.println(toHexString(ba));51System.err.println();5253// decode54System.out.println("DEBUG: decoding into X500Name ...");55X500Name dn1 = new X500Name(new DerValue(ba));56System.err.println("DEBUG: dn1: " + dn1.toString());57System.err.println();58dn1 = new X500Name(ba);59System.err.println("DEBUG: dn1: " + dn1.toString());60System.err.println();61dn1 = new X500Name(new DerInputStream(ba));62System.err.println("DEBUG: dn1: " + dn1.toString());63System.err.println();6465/*66* GeneralName67*/6869// encode70GeneralName gn = new GeneralName(dn);71System.err.println("DEBUG: gn: " + gn.toString());72debugDER = new DerOutputStream();73gn.encode(debugDER);74ba = debugDER.toByteArray();75System.err.print("DEBUG: encoded GeneralName bytes: ");76System.out.println(toHexString(ba));77System.err.println();7879// decode80System.out.println("DEBUG: decoding into GeneralName ...");81GeneralName gn1 = new GeneralName(new DerValue(ba));82System.err.println("DEBUG: gn1: " + gn1.toString());83System.err.println();8485/*86* GeneralSubtree87*/8889// encode90GeneralSubtree subTree = new GeneralSubtree(gn, 0, -1);91System.err.println("DEBUG: subTree: " + subTree.toString());92debugDER = new DerOutputStream();93subTree.encode(debugDER);94ba = debugDER.toByteArray();95System.err.print("DEBUG: encoded GeneralSubtree bytes: ");96System.out.println(toHexString(ba));97System.err.println();9899// decode100GeneralSubtree debugSubtree = new GeneralSubtree(new DerValue(ba));101}102103/*104* Converts a byte to hex digit and writes to the supplied buffer105*/106private static void byte2hex(byte b, StringBuffer buf) {107char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',108'9', 'A', 'B', 'C', 'D', 'E', 'F' };109int high = ((b & 0xf0) >> 4);110int low = (b & 0x0f);111buf.append(hexChars[high]);112buf.append(hexChars[low]);113}114115/*116* Converts a byte array to hex string117*/118private static String toHexString(byte[] block) {119StringBuffer buf = new StringBuffer();120121int len = block.length;122123for (int i = 0; i < len; i++) {124byte2hex(block[i], buf);125if (i < len-1) {126buf.append(":");127}128}129return buf.toString();130}131}132133134