1/*
2 * Copyright (c) 2015, 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 8056179
27 * @summary Unit test for PermissionCollection subclasses
28 */
29
30import java.net.SocketPermission;
31import java.security.Permission;
32import java.security.PermissionCollection;
33import java.security.SecurityPermission;
34import java.util.Enumeration;
35
36public class SocketPermissionCollection {
37
38    public static void main(String[] args) throws Exception {
39
40        int testFail = 0;
41
42        SocketPermission perm = new SocketPermission("www.example.com",
43                                                     "connect");
44        PermissionCollection perms = perm.newPermissionCollection();
45
46        // test 1
47        System.out.println
48            ("test 1: add throws IllegalArgExc for wrong perm type");
49        try {
50            perms.add(new SecurityPermission("createAccessControlContext"));
51            System.err.println("Expected IllegalArgumentException");
52            testFail++;
53        } catch (IllegalArgumentException iae) {}
54
55        // test 2
56        System.out.println("test 2: implies returns false for wrong perm type");
57        if (perms.implies(new SecurityPermission("getPolicy"))) {
58            System.err.println("Expected false, returned true");
59            testFail++;
60        }
61
62        // test 3
63        System.out.println
64            ("test 3: implies returns true for match on name and action");
65        perms.add(new SocketPermission("www.example.com", "connect"));
66        if (!perms.implies(new SocketPermission("www.example.com", "connect"))) {
67            System.err.println("Expected true, returned false");
68            testFail++;
69        }
70
71        // test 4
72        System.out.println
73            ("test 4: implies returns false for match on name but not action");
74        if (perms.implies(new SocketPermission("www.example.com", "accept"))) {
75            System.err.println("Expected false, returned true");
76            testFail++;
77        }
78
79        // test 5
80        System.out.println("test 5: implies returns true for match on " +
81                           "name and subset of actions");
82        perms.add(new SocketPermission("www.example.org", "accept, connect"));
83        if (!perms.implies(new SocketPermission("www.example.org", "connect"))) {
84            System.err.println("Expected true, returned false");
85            testFail++;
86        }
87
88        // test 6
89        System.out.println("test 6: implies returns true for aggregate " +
90                           "match on name and action");
91        perms.add(new SocketPermission("www.example.us", "accept"));
92        perms.add(new SocketPermission("www.example.us", "connect"));
93        if (!perms.implies(new SocketPermission("www.example.us", "accept"))) {
94            System.err.println("Expected true, returned false");
95            testFail++;
96        }
97        if (!perms.implies(new SocketPermission("www.example.us",
98                                                "connect,accept"))) {
99            System.err.println("Expected true, returned false");
100            testFail++;
101        }
102
103        // test 7
104        System.out.println("test 7: implies returns true for wildcard " +
105                           "and match on action");
106        perms.add(new SocketPermission("*.example.edu", "resolve"));
107        if (!perms.implies(new SocketPermission("foo.example.edu", "resolve"))) {
108            System.err.println("Expected true, returned false");
109            testFail++;
110        }
111
112        // test 8
113        System.out.println("test 8: implies returns false for non-match " +
114                           "on wildcard");
115        if (perms.implies(new SocketPermission("foo.example.edu", "connect"))) {
116            System.err.println("Expected false, returned true");
117            testFail++;
118        }
119
120        // test 9
121        System.out.println("test 9: implies returns true for matching " +
122                           "port range and action");
123        perms.add(new SocketPermission("204.160.241.0:1024-65535", "connect"));
124        if (!perms.implies(new SocketPermission("204.160.241.0:1025", "connect"))) {
125            System.err.println("Expected true, returned false");
126            testFail++;
127        }
128
129        // test 13
130        System.out.println("test 13: elements returns correct number of perms");
131        int numPerms = 0;
132        Enumeration<Permission> e = perms.elements();
133        while (e.hasMoreElements()) {
134            numPerms++;
135            System.out.println(e.nextElement());
136        }
137        // the two "/tmp/baz" entries were combined into one
138        if (numPerms != 5) {
139            System.err.println("Expected 5, got " + numPerms);
140            testFail++;
141        }
142
143        if (testFail > 0) {
144            throw new Exception(testFail + " test(s) failed");
145        }
146    }
147}
148