Path: blob/master/test/jdk/javax/security/auth/kerberos/ServicePermissionCollection.java
41152 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 805617926* @summary Unit test for ServicePermissionCollection subclass27*/2829import java.security.Permission;30import java.security.PermissionCollection;31import java.security.SecurityPermission;32import java.util.Enumeration;33import javax.security.auth.kerberos.ServicePermission;3435public class ServicePermissionCollection {3637private static final String FOO = "host/[email protected]";38private static final String BAR = "host/[email protected]";39private static final String BAZ = "host/[email protected]";4041public static void main(String[] args) throws Exception {4243int testFail = 0;4445ServicePermission perm = new ServicePermission(FOO, "accept");46PermissionCollection perms = perm.newPermissionCollection();4748// test 149System.out.println50("test 1: add throws IllegalArgExc for wrong permission type");51try {52perms.add(new SecurityPermission("createAccessControlContext"));53System.err.println("Expected IllegalArgumentException");54testFail++;55} catch (IllegalArgumentException iae) {}5657// test 258System.out.println("test 2: implies returns false for wrong perm type");59if (perms.implies(new SecurityPermission("getPolicy"))) {60System.err.println("Expected false, returned true");61testFail++;62}6364// test 365System.out.println66("test 3: implies returns true for match on name and action");67perms.add(new ServicePermission(FOO, "accept"));68if (!perms.implies(new ServicePermission(FOO, "accept"))) {69System.err.println("Expected true, returned false");70testFail++;71}7273// test 474System.out.println75("test 4: implies returns false for match on name but not action");76if (perms.implies(new ServicePermission(FOO, "initiate"))) {77System.err.println("Expected false, returned true");78testFail++;79}8081// test 582System.out.println("test 5: implies returns true for match on " +83"name and subset of actions");84perms.add(new ServicePermission(BAR, "accept, initiate"));85if (!perms.implies(new ServicePermission(BAR, "accept"))) {86System.err.println("Expected true, returned false");87testFail++;88}8990// test 691System.out.println("test 6: implies returns false for aggregate " +92"match on name and action");93perms.add(new ServicePermission(BAZ, "accept"));94perms.add(new ServicePermission(BAZ, "initiate"));95if (!perms.implies(new ServicePermission(BAZ, "initiate"))) {96System.err.println("Expected true, returned false");97testFail++;98}99if (!perms.implies(new ServicePermission(BAZ, "initiate, accept"))) {100System.err.println("Expected true, returned false");101testFail++;102}103104// test 7105System.out.println("test 7: implies returns true for wildcard " +106"match on name and action");107perms.add(new ServicePermission("*", "initiate"));108if (!perms.implies(new ServicePermission("Duke", "initiate"))) {109System.err.println("Expected true, returned false");110testFail++;111}112113// test 8114System.out.println("test 8: elements returns correct number of perms");115int numPerms = 0;116Enumeration<Permission> e = perms.elements();117while (e.hasMoreElements()) {118numPerms++;119System.out.println(e.nextElement());120}121// the 2 FOO permissions and the 2 BAZ permisssions122// are combined into one123if (numPerms != 4) {124System.err.println("Expected 4, got " + numPerms);125testFail++;126}127128if (testFail > 0) {129throw new Exception(testFail + " test(s) failed");130}131}132}133134135