< prev index next >

test/jdk/java/net/SocketPermission/SocketPermissionCollection.java

Print this page
rev 52898 : 8231785: Improved socket permissions
Reviewed-by: ahgross, chegar, mullan, rhalade
   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 
  30 import java.net.SocketPermission;
  31 import java.security.Permission;
  32 import java.security.PermissionCollection;
  33 import java.security.SecurityPermission;
  34 import java.util.Enumeration;
  35 
  36 public 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


 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 }
   1 /*
   2  * Copyright (c) 2015, 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 8056179 8231785
  27  * @summary Unit test for PermissionCollection subclasses
  28  */
  29 
  30 import java.net.SocketPermission;
  31 import java.security.Permission;
  32 import java.security.PermissionCollection;
  33 import java.security.SecurityPermission;
  34 import java.util.Enumeration;
  35 
  36 public 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


 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 
 130         // test 10
 131         System.out.println("test 10: elements returns correct number of perms");
 132         perms.add(new SocketPermission("www.example.us", "resolve"));
 133         int numPerms = 0;
 134         Enumeration<Permission> e = perms.elements();
 135         while (e.hasMoreElements()) {
 136             numPerms++;
 137             System.out.println(e.nextElement());
 138         }
 139         // the two "/tmp/baz" entries were combined into one
 140         if (numPerms != 5) {
 141             System.err.println("Expected 5, got " + numPerms);
 142             testFail++;
 143         }
 144 
 145         if (testFail > 0) {
 146             throw new Exception(testFail + " test(s) failed");
 147         }
 148     }
 149 }
< prev index next >