Path: blob/master/test/jdk/java/security/Policy/Dynamic/TestDynamicPolicy.java
41155 views
/*1* Copyright (c) 2000, 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* @author Danny Hendler26* @author Gary Ellison27* @bug 424427128* @summary New policy sometimes has no effect with no indication given29* @run main/othervm/policy=setpolicy.jp TestDynamicPolicy30*/3132/*33The test should be given the following permissions:3435grant codeBase "file:testPath" {36permission java.security.SecurityPermission "setPolicy";37permission java.security.SecurityPermission "getPolicy";38};3940*/414243import java.io.PrintStream;44import java.io.IOException;4546import java.lang.System;47import java.security.Policy;484950public class TestDynamicPolicy {5152public static void main(String args[]) throws Exception {5354try {55//56TestDynamicPolicy jstest = new TestDynamicPolicy();57jstest.doit();58} catch(Exception e) {59System.out.println("Failed. Unexpected exception:" + e);60throw e;61}62System.out.println("Passed. OKAY");63}6465private void doit() throws Exception {66// A security manager must be installed67SecurityManager sm=System.getSecurityManager();68if (sm==null)69throw new70Exception("Test must be run with a security manager installed");7172// Instantiate and set the new policy73DynamicPolicy dp = new DynamicPolicy();74Policy.setPolicy(dp);7576// Verify that policy has been set77if (dp != Policy.getPolicy())78throw new Exception("Policy was not set!!");7980// now see this class can access user.name81String usr = getUserName();8283if (usr != null) {84System.out.println("Test was able to read user.name prior to refresh!");85throw new86Exception("Test was able to read user.name prior to refresh!");87}8889// Now, make policy allow reading user.name90dp.refresh();9192// now I should be able to read it93usr = getUserName();9495if (usr == null) {96System.out.println("Test was unable to read user.name after refresh!");97throw new98Exception("Test was unable to read user.name after refresh!");99}100// Now, take away permission to read user.name101dp.refresh();102103// now I should not be able to read it104usr = getUserName();105106if (usr != null) {107System.out.println("Test was able to read user.name following 2nd refresh!");108throw new109Exception("Test was able to read user.name following 2nd refresh!");110}111112}113114private String getUserName() {115String usr = null;116117try {118usr = System.getProperty("user.name");119} catch (Exception e) {120}121return usr;122}123}124125126