Path: blob/master/test/jdk/javax/security/auth/x500/X500Principal/DerIsConstructor.java
41155 views
/*1* Copyright (c) 2001, 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 442603326* @summary Should add an X500Principal(InputStream) constructor27*/2829import java.io.*;30import javax.security.auth.x500.X500Principal;3132public class DerIsConstructor {33public static void main(String[] args) {3435try {3637// create 2 different X500Principals38X500Principal p = new X500Principal("o=sun, cn=duke");39X500Principal p2 = new X500Principal("o=sun, cn=dukette");4041// get the encoded bytes for the 2 principals42byte[] encoded = p.getEncoded();43byte[] encoded2 = p2.getEncoded();4445// create a ByteArrayInputStream with the46// encodings from the 2 principals47byte[] all = new byte[encoded.length + encoded2.length];48System.arraycopy(encoded, 0, all, 0, encoded.length);49System.arraycopy(encoded2, 0, all, encoded.length, encoded2.length);50ByteArrayInputStream bais = new ByteArrayInputStream(all);5152// create 2 new X500Principals from the ByteArrayInputStream53X500Principal pp = new X500Principal(bais);54X500Principal pp2 = new X500Principal(bais);5556// sanity check the 2 new principals57if (p.equals(pp) && p2.equals(pp2) && !pp.equals(pp2)) {58System.out.println("Test 1 passed");59} else {60throw new SecurityException("Test 1 failed");61}6263// corrupt the ByteArrayInputStream and see if the64// mark/reset worked65byte[] all2 = new byte[all.length];66System.arraycopy(all, 0, all2, 0, all.length);67all2[encoded.length + 2] = (byte)-1;68bais = new ByteArrayInputStream(all2);6970// this should work71X500Principal ppp = new X500Principal(bais);7273// this should throw an IOException due to stream corruption74int origAvailable = bais.available();75try {76X500Principal ppp2 = new X500Principal(bais);77throw new SecurityException("Test 2 (part a) failed");78} catch (IllegalArgumentException iae) {79if (bais.available() == origAvailable) {80System.out.println("Test 2 passed");81} else {82throw new SecurityException("Test 2 (part b) failed");83}84}85} catch (Exception e) {86e.printStackTrace();87throw new SecurityException(e.getMessage());88}89}90}919293