0N/A/*
2362N/A * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4426033
0N/A * @summary Should add an X500Principal(InputStream) constructor
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport javax.security.auth.x500.X500Principal;
0N/A
0N/Apublic class DerIsConstructor {
0N/A public static void main(String[] args) {
0N/A
0N/A try {
0N/A
0N/A // create 2 different X500Principals
0N/A X500Principal p = new X500Principal("o=sun, cn=duke");
0N/A X500Principal p2 = new X500Principal("o=sun, cn=dukette");
0N/A
0N/A // get the encoded bytes for the 2 principals
0N/A byte[] encoded = p.getEncoded();
0N/A byte[] encoded2 = p2.getEncoded();
0N/A
0N/A // create a ByteArrayInputStream with the
0N/A // encodings from the 2 principals
0N/A byte[] all = new byte[encoded.length + encoded2.length];
0N/A System.arraycopy(encoded, 0, all, 0, encoded.length);
0N/A System.arraycopy(encoded2, 0, all, encoded.length, encoded2.length);
0N/A ByteArrayInputStream bais = new ByteArrayInputStream(all);
0N/A
0N/A // create 2 new X500Principals from the ByteArrayInputStream
0N/A X500Principal pp = new X500Principal(bais);
0N/A X500Principal pp2 = new X500Principal(bais);
0N/A
0N/A // sanity check the 2 new principals
0N/A if (p.equals(pp) && p2.equals(pp2) && !pp.equals(pp2)) {
0N/A System.out.println("Test 1 passed");
0N/A } else {
0N/A throw new SecurityException("Test 1 failed");
0N/A }
0N/A
0N/A // corrupt the ByteArrayInputStream and see if the
0N/A // mark/reset worked
0N/A byte[] all2 = new byte[all.length];
0N/A System.arraycopy(all, 0, all2, 0, all.length);
0N/A all2[encoded.length + 2] = (byte)-1;
0N/A bais = new ByteArrayInputStream(all2);
0N/A
0N/A // this should work
0N/A X500Principal ppp = new X500Principal(bais);
0N/A
0N/A // this should throw an IOException due to stream corruption
0N/A int origAvailable = bais.available();
0N/A try {
0N/A X500Principal ppp2 = new X500Principal(bais);
0N/A throw new SecurityException("Test 2 (part a) failed");
0N/A } catch (IllegalArgumentException iae) {
0N/A if (bais.available() == origAvailable) {
0N/A System.out.println("Test 2 passed");
0N/A } else {
0N/A throw new SecurityException("Test 2 (part b) failed");
0N/A }
0N/A }
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A throw new SecurityException(e.getMessage());
0N/A }
0N/A }
0N/A}