Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/provider/PolicyParser/EncodeURL.java
41152 views
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
* @modules java.base/sun.security.provider
28
* @summary Security policy file does not grok hash mark in pathnames
29
*/
30
31
import java.io.*;
32
import java.util.*;
33
import sun.security.provider.*;
34
35
public class EncodeURL {
36
37
// java.ext.dirs input and encoding
38
private static final String extInput = "foo bar";
39
private static final String extAnswer = "foo%20bar";
40
private static final String policy0 =
41
"grant codebase \"${java.ext.dirs}\" { permission java.security.AllPermission; };";
42
43
// keystore inputs and encodings
44
private static final String prop1 = "http://foobar";
45
private static final String answer1 = "http://foobar/foo";
46
private static final String policy1 =
47
"keystore \"${prop1}/foo\"; grant { permission java.security.AllPermission; };";
48
49
private static final String prop2 = "foo#bar";
50
private static final String answer2 = "http://foo%23bar/foo";
51
private static final String policy2 =
52
"keystore \"http://${prop2}/foo\"; grant { permission java.security.AllPermission; };";
53
54
private static final String prop3 = "goofy:foo#bar";
55
private static final String answer3 = "http://goofy:foo%23bar/foo";
56
private static final String policy3 =
57
"keystore \"http://${prop3}/foo\"; grant { permission java.security.AllPermission; };";
58
59
public static void main(String[] args) throws Exception {
60
61
// make sure 'file' URLs created from java.ext.dirs
62
// are encoded
63
64
System.setProperty("java.ext.dirs", extInput);
65
PolicyParser pp = new PolicyParser(true);
66
pp.read(new StringReader(policy0));
67
Enumeration e = pp.grantElements();
68
while (e.hasMoreElements()) {
69
PolicyParser.GrantEntry ge =
70
(PolicyParser.GrantEntry)e.nextElement();
71
if (ge.codeBase.indexOf("foo") >= 0 &&
72
ge.codeBase.indexOf(extAnswer) < 0) {
73
throw new SecurityException("test 0 failed: " +
74
"expected " + extAnswer +
75
" inside " + ge.codeBase);
76
}
77
}
78
79
// make sure keystore URL is properly encoded (or not)
80
81
System.setProperty("prop1", prop1);
82
pp = new PolicyParser(true);
83
pp.read(new StringReader(policy1));
84
if (!pp.getKeyStoreUrl().equals(answer1)) {
85
throw new SecurityException("test 1 failed: " +
86
"expected " + answer1 +
87
", and got " + pp.getKeyStoreUrl());
88
}
89
90
System.setProperty("prop2", prop2);
91
pp = new PolicyParser(true);
92
pp.read(new StringReader(policy2));
93
if (!pp.getKeyStoreUrl().equals(answer2)) {
94
throw new SecurityException("test 2 failed: " +
95
"expected " + answer2 +
96
", and got " + pp.getKeyStoreUrl());
97
}
98
99
System.setProperty("prop3", prop3);
100
pp = new PolicyParser(true);
101
pp.read(new StringReader(policy3));
102
if (!pp.getKeyStoreUrl().equals(answer3)) {
103
throw new SecurityException("test 3 failed: " +
104
"expected " + answer3 +
105
", and got " + pp.getKeyStoreUrl());
106
}
107
108
System.out.println("test passed");
109
}
110
}
111
112