0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4797850
0N/A * @summary Security policy file does not grok hash mark in pathnames
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport sun.security.provider.*;
0N/A
0N/Apublic class EncodeURL {
0N/A
0N/A // java.ext.dirs input and encoding
0N/A private static final String extInput = "foo bar";
0N/A private static final String extAnswer = "foo%20bar";
0N/A private static final String policy0 =
0N/A "grant codebase \"${java.ext.dirs}\" { permission java.security.AllPermission; };";
0N/A
0N/A // keystore inputs and encodings
0N/A private static final String prop1 = "http://foobar";
0N/A private static final String answer1 = "http://foobar/foo";
0N/A private static final String policy1 =
0N/A "keystore \"${prop1}/foo\"; grant { permission java.security.AllPermission; };";
0N/A
0N/A private static final String prop2 = "foo#bar";
0N/A private static final String answer2 = "http://foo%23bar/foo";
0N/A private static final String policy2 =
0N/A "keystore \"http://${prop2}/foo\"; grant { permission java.security.AllPermission; };";
0N/A
0N/A private static final String prop3 = "goofy:foo#bar";
0N/A private static final String answer3 = "http://goofy:foo%23bar/foo";
0N/A private static final String policy3 =
0N/A "keystore \"http://${prop3}/foo\"; grant { permission java.security.AllPermission; };";
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A // make sure 'file' URLs created from java.ext.dirs
0N/A // are encoded
0N/A
0N/A System.setProperty("java.ext.dirs", extInput);
0N/A PolicyParser pp = new PolicyParser(true);
0N/A pp.read(new StringReader(policy0));
0N/A Enumeration e = pp.grantElements();
0N/A while (e.hasMoreElements()) {
0N/A PolicyParser.GrantEntry ge =
0N/A (PolicyParser.GrantEntry)e.nextElement();
0N/A if (ge.codeBase.indexOf("foo") >= 0 &&
0N/A ge.codeBase.indexOf(extAnswer) < 0) {
0N/A throw new SecurityException("test 0 failed: " +
0N/A "expected " + extAnswer +
0N/A " inside " + ge.codeBase);
0N/A }
0N/A }
0N/A
0N/A // make sure keystore URL is properly encoded (or not)
0N/A
0N/A System.setProperty("prop1", prop1);
0N/A pp = new PolicyParser(true);
0N/A pp.read(new StringReader(policy1));
0N/A if (!pp.getKeyStoreUrl().equals(answer1)) {
0N/A throw new SecurityException("test 1 failed: " +
0N/A "expected " + answer1 +
0N/A ", and got " + pp.getKeyStoreUrl());
0N/A }
0N/A
0N/A System.setProperty("prop2", prop2);
0N/A pp = new PolicyParser(true);
0N/A pp.read(new StringReader(policy2));
0N/A if (!pp.getKeyStoreUrl().equals(answer2)) {
0N/A throw new SecurityException("test 2 failed: " +
0N/A "expected " + answer2 +
0N/A ", and got " + pp.getKeyStoreUrl());
0N/A }
0N/A
0N/A System.setProperty("prop3", prop3);
0N/A pp = new PolicyParser(true);
0N/A pp.read(new StringReader(policy3));
0N/A if (!pp.getKeyStoreUrl().equals(answer3)) {
0N/A throw new SecurityException("test 3 failed: " +
0N/A "expected " + answer3 +
0N/A ", and got " + pp.getKeyStoreUrl());
0N/A }
0N/A
0N/A System.out.println("test passed");
0N/A }
0N/A}