Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/provider/PolicyParser/EncodeURL.java
38853 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* @summary Security policy file does not grok hash mark in pathnames27*/2829import java.io.*;30import java.util.*;31import sun.security.provider.*;3233public class EncodeURL {3435// java.ext.dirs input and encoding36private static final String extInput = "foo bar";37private static final String extAnswer = "foo%20bar";38private static final String policy0 =39"grant codebase \"${java.ext.dirs}\" { permission java.security.AllPermission; };";4041// keystore inputs and encodings42private static final String prop1 = "http://foobar";43private static final String answer1 = "http://foobar/foo";44private static final String policy1 =45"keystore \"${prop1}/foo\"; grant { permission java.security.AllPermission; };";4647private static final String prop2 = "foo#bar";48private static final String answer2 = "http://foo%23bar/foo";49private static final String policy2 =50"keystore \"http://${prop2}/foo\"; grant { permission java.security.AllPermission; };";5152private static final String prop3 = "goofy:foo#bar";53private static final String answer3 = "http://goofy:foo%23bar/foo";54private static final String policy3 =55"keystore \"http://${prop3}/foo\"; grant { permission java.security.AllPermission; };";5657public static void main(String[] args) throws Exception {5859// make sure 'file' URLs created from java.ext.dirs60// are encoded6162System.setProperty("java.ext.dirs", extInput);63PolicyParser pp = new PolicyParser(true);64pp.read(new StringReader(policy0));65Enumeration e = pp.grantElements();66while (e.hasMoreElements()) {67PolicyParser.GrantEntry ge =68(PolicyParser.GrantEntry)e.nextElement();69if (ge.codeBase.indexOf("foo") >= 0 &&70ge.codeBase.indexOf(extAnswer) < 0) {71throw new SecurityException("test 0 failed: " +72"expected " + extAnswer +73" inside " + ge.codeBase);74}75}7677// make sure keystore URL is properly encoded (or not)7879System.setProperty("prop1", prop1);80pp = new PolicyParser(true);81pp.read(new StringReader(policy1));82if (!pp.getKeyStoreUrl().equals(answer1)) {83throw new SecurityException("test 1 failed: " +84"expected " + answer1 +85", and got " + pp.getKeyStoreUrl());86}8788System.setProperty("prop2", prop2);89pp = new PolicyParser(true);90pp.read(new StringReader(policy2));91if (!pp.getKeyStoreUrl().equals(answer2)) {92throw new SecurityException("test 2 failed: " +93"expected " + answer2 +94", and got " + pp.getKeyStoreUrl());95}9697System.setProperty("prop3", prop3);98pp = new PolicyParser(true);99pp.read(new StringReader(policy3));100if (!pp.getKeyStoreUrl().equals(answer3)) {101throw new SecurityException("test 3 failed: " +102"expected " + answer3 +103", and got " + pp.getKeyStoreUrl());104}105106System.out.println("test passed");107}108}109110111