1 /*
2  * Copyright (c) 2001, 2019, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4391898 8230407
27  * @summary SocketPermission(":",...) throws ArrayIndexOutOfBoundsException
28  *          SocketPermission constructor argument checks
29  * @run testng Ctor
30  */
31 
32 import java.net.SocketPermission;
33 import org.testng.annotations.Test;
34 import static java.lang.System.out;
35 import static org.testng.Assert.*;
36 
37 public class Ctor {
38 
39     static final Class<NullPointerException> NPE = NullPointerException.class;
40     static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
41 
42     @Test
positive()43     public void positive() {
44         // ArrayIndexOutOfBoundsException is the bug, 4391898, exists
45         SocketPermission sp1 =  new SocketPermission(":", "connect");
46     }
47 
48     @Test
npe()49     public void npe() {
50         NullPointerException e;
51         e = expectThrows(NPE, () -> new SocketPermission(null, null));
52         out.println("caught expected NPE: " + e);
53         e = expectThrows(NPE, () -> new SocketPermission("foo", null));
54         out.println("caught expected NPE: " + e);
55         e = expectThrows(NPE, () -> new SocketPermission(null, "connect"));
56         out.println("caught expected NPE: " + e);
57     }
58 
59     @Test
iae()60     public void iae() {
61         IllegalArgumentException e;
62         // host
63         e = expectThrows(IAE, () -> new SocketPermission("1:2:3:4", "connect"));
64         out.println("caught expected IAE: " + e);
65         e = expectThrows(IAE, () -> new SocketPermission("foo:5-4", "connect"));
66         out.println("caught expected IAE: " + e);
67 
68         // actions
69         e = expectThrows(IAE, () -> new SocketPermission("foo", ""));
70         out.println("caught expected IAE: " + e);
71         e = expectThrows(IAE, () -> new SocketPermission("foo", "badAction"));
72         out.println("caught expected IAE: " + e);
73         e = expectThrows(IAE, () -> new SocketPermission("foo", "badAction,connect"));
74         out.println("caught expected IAE: " + e);
75         e = expectThrows(IAE, () -> new SocketPermission("foo", "badAction,,connect"));
76         out.println("caught expected IAE: " + e);
77         e = expectThrows(IAE, () -> new SocketPermission("foo", ",connect"));
78         out.println("caught expected IAE: " + e);
79         e = expectThrows(IAE, () -> new SocketPermission("foo", ",,connect"));
80         out.println("caught expected IAE: " + e);
81         e = expectThrows(IAE, () -> new SocketPermission("foo", "connect,"));
82         out.println("caught expected IAE: " + e);
83         e = expectThrows(IAE, () -> new SocketPermission("foo", "connect,,"));
84         out.println("caught expected IAE: " + e);
85     }
86 }
87