blob: f17badd0441789502f8a2d367f7eec306669802f [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4426033
27 * @summary Should add an X500Principal(InputStream) constructor
28 */
29
30import java.io.*;
31import javax.security.auth.x500.X500Principal;
32
33public class DerIsConstructor {
34 public static void main(String[] args) {
35
36 try {
37
38 // create 2 different X500Principals
39 X500Principal p = new X500Principal("o=sun, cn=duke");
40 X500Principal p2 = new X500Principal("o=sun, cn=dukette");
41
42 // get the encoded bytes for the 2 principals
43 byte[] encoded = p.getEncoded();
44 byte[] encoded2 = p2.getEncoded();
45
46 // create a ByteArrayInputStream with the
47 // encodings from the 2 principals
48 byte[] all = new byte[encoded.length + encoded2.length];
49 System.arraycopy(encoded, 0, all, 0, encoded.length);
50 System.arraycopy(encoded2, 0, all, encoded.length, encoded2.length);
51 ByteArrayInputStream bais = new ByteArrayInputStream(all);
52
53 // create 2 new X500Principals from the ByteArrayInputStream
54 X500Principal pp = new X500Principal(bais);
55 X500Principal pp2 = new X500Principal(bais);
56
57 // sanity check the 2 new principals
58 if (p.equals(pp) && p2.equals(pp2) && !pp.equals(pp2)) {
59 System.out.println("Test 1 passed");
60 } else {
61 throw new SecurityException("Test 1 failed");
62 }
63
64 // corrupt the ByteArrayInputStream and see if the
65 // mark/reset worked
66 byte[] all2 = new byte[all.length];
67 System.arraycopy(all, 0, all2, 0, all.length);
68 all2[encoded.length + 2] = (byte)-1;
69 bais = new ByteArrayInputStream(all2);
70
71 // this should work
72 X500Principal ppp = new X500Principal(bais);
73
74 // this should throw an IOException due to stream corruption
75 int origAvailable = bais.available();
76 try {
77 X500Principal ppp2 = new X500Principal(bais);
78 throw new SecurityException("Test 2 (part a) failed");
79 } catch (IllegalArgumentException iae) {
80 if (bais.available() == origAvailable) {
81 System.out.println("Test 2 passed");
82 } else {
83 throw new SecurityException("Test 2 (part b) failed");
84 }
85 }
86 } catch (Exception e) {
87 e.printStackTrace();
88 throw new SecurityException(e.getMessage());
89 }
90 }
91}