1 /* 2 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4797850 27 * @summary Security policy file does not grok hash mark in pathnames 28 */ 29 30 import java.io.*; 31 import java.util.*; 32 import sun.security.provider.*; 33 34 public class EncodeURL { 35 36 // java.ext.dirs input and encoding 37 private static final String extInput = "foo bar"; 38 private static final String extAnswer = "foo%20bar"; 39 private static final String policy0 = 40 "grant codebase \"${java.ext.dirs}\" { permission java.security.AllPermission; };"; 41 42 // keystore inputs and encodings 43 private static final String prop1 = "http://foobar"; 44 private static final String answer1 = "http://foobar/foo"; 45 private static final String policy1 = 46 "keystore \"${prop1}/foo\"; grant { permission java.security.AllPermission; };"; 47 48 private static final String prop2 = "foo#bar"; 49 private static final String answer2 = "http://foo%23bar/foo"; 50 private static final String policy2 = 51 "keystore \"http://${prop2}/foo\"; grant { permission java.security.AllPermission; };"; 52 53 private static final String prop3 = "goofy:foo#bar"; 54 private static final String answer3 = "http://goofy:foo%23bar/foo"; 55 private static final String policy3 = 56 "keystore \"http://${prop3}/foo\"; grant { permission java.security.AllPermission; };"; 57 58 public static void main(String[] args) throws Exception { 59 60 // make sure 'file' URLs created from java.ext.dirs 61 // are encoded 62 63 System.setProperty("java.ext.dirs", extInput); 64 PolicyParser pp = new PolicyParser(true); 65 pp.read(new StringReader(policy0)); 66 Enumeration e = pp.grantElements(); 67 while (e.hasMoreElements()) { 68 PolicyParser.GrantEntry ge = 69 (PolicyParser.GrantEntry)e.nextElement(); 70 if (ge.codeBase.indexOf("foo") >= 0 && 71 ge.codeBase.indexOf(extAnswer) < 0) { 72 throw new SecurityException("test 0 failed: " + 73 "expected " + extAnswer + 74 " inside " + ge.codeBase); 75 } 76 } 77 78 // make sure keystore URL is properly encoded (or not) 79 80 System.setProperty("prop1", prop1); 81 pp = new PolicyParser(true); 82 pp.read(new StringReader(policy1)); 83 if (!pp.getKeyStoreUrl().equals(answer1)) { 84 throw new SecurityException("test 1 failed: " + 85 "expected " + answer1 + 86 ", and got " + pp.getKeyStoreUrl()); 87 } 88 89 System.setProperty("prop2", prop2); 90 pp = new PolicyParser(true); 91 pp.read(new StringReader(policy2)); 92 if (!pp.getKeyStoreUrl().equals(answer2)) { 93 throw new SecurityException("test 2 failed: " + 94 "expected " + answer2 + 95 ", and got " + pp.getKeyStoreUrl()); 96 } 97 98 System.setProperty("prop3", prop3); 99 pp = new PolicyParser(true); 100 pp.read(new StringReader(policy3)); 101 if (!pp.getKeyStoreUrl().equals(answer3)) { 102 throw new SecurityException("test 3 failed: " + 103 "expected " + answer3 + 104 ", and got " + pp.getKeyStoreUrl()); 105 } 106 107 System.out.println("test passed"); 108 } 109 }