Path: blob/master/test/jdk/sun/security/provider/PolicyParser/EncodeURL.java
41152 views
/*1* Copyright (c) 2003, 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 479785026* @modules java.base/sun.security.provider27* @summary Security policy file does not grok hash mark in pathnames28*/2930import java.io.*;31import java.util.*;32import sun.security.provider.*;3334public class EncodeURL {3536// java.ext.dirs input and encoding37private static final String extInput = "foo bar";38private static final String extAnswer = "foo%20bar";39private static final String policy0 =40"grant codebase \"${java.ext.dirs}\" { permission java.security.AllPermission; };";4142// keystore inputs and encodings43private static final String prop1 = "http://foobar";44private static final String answer1 = "http://foobar/foo";45private static final String policy1 =46"keystore \"${prop1}/foo\"; grant { permission java.security.AllPermission; };";4748private static final String prop2 = "foo#bar";49private static final String answer2 = "http://foo%23bar/foo";50private static final String policy2 =51"keystore \"http://${prop2}/foo\"; grant { permission java.security.AllPermission; };";5253private static final String prop3 = "goofy:foo#bar";54private static final String answer3 = "http://goofy:foo%23bar/foo";55private static final String policy3 =56"keystore \"http://${prop3}/foo\"; grant { permission java.security.AllPermission; };";5758public static void main(String[] args) throws Exception {5960// make sure 'file' URLs created from java.ext.dirs61// are encoded6263System.setProperty("java.ext.dirs", extInput);64PolicyParser pp = new PolicyParser(true);65pp.read(new StringReader(policy0));66Enumeration e = pp.grantElements();67while (e.hasMoreElements()) {68PolicyParser.GrantEntry ge =69(PolicyParser.GrantEntry)e.nextElement();70if (ge.codeBase.indexOf("foo") >= 0 &&71ge.codeBase.indexOf(extAnswer) < 0) {72throw new SecurityException("test 0 failed: " +73"expected " + extAnswer +74" inside " + ge.codeBase);75}76}7778// make sure keystore URL is properly encoded (or not)7980System.setProperty("prop1", prop1);81pp = new PolicyParser(true);82pp.read(new StringReader(policy1));83if (!pp.getKeyStoreUrl().equals(answer1)) {84throw new SecurityException("test 1 failed: " +85"expected " + answer1 +86", and got " + pp.getKeyStoreUrl());87}8889System.setProperty("prop2", prop2);90pp = new PolicyParser(true);91pp.read(new StringReader(policy2));92if (!pp.getKeyStoreUrl().equals(answer2)) {93throw new SecurityException("test 2 failed: " +94"expected " + answer2 +95", and got " + pp.getKeyStoreUrl());96}9798System.setProperty("prop3", prop3);99pp = new PolicyParser(true);100pp.read(new StringReader(policy3));101if (!pp.getKeyStoreUrl().equals(answer3)) {102throw new SecurityException("test 3 failed: " +103"expected " + answer3 +104", and got " + pp.getKeyStoreUrl());105}106107System.out.println("test passed");108}109}110111112