Debian Codegraph @

tests/reproducers/simple/CustomPolicies/testcases/CustomPoliciesTest.java

raw
1 /* CustomPoliciesTest.java
2 Copyright (C) 2014 Red Hat, Inc.
3
4 This file is part of IcedTea.
5
6 IcedTea is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 2.
9
10 IcedTea is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with IcedTea; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301 USA.
19
20 Linking this library statically or dynamically with other modules is
21 making a combined work based on this library. Thus, the terms and
22 conditions of the GNU General Public License cover the whole
23 combination.
24
25 As a special exception, the copyright holders of this library give you
26 permission to link this library with independent modules to produce an
27 executable, regardless of the license terms of these independent
28 modules, and to copy and distribute the resulting executable under
29 terms of your choice, provided that you also meet, for each linked
30 independent module, the terms and conditions of the license of that
31 module. An independent module is a module which is not derived from
32 or based on this library. If you modify this library, you may extend
33 this exception to your version of the library, but you are not
34 obligated to do so. If you do not wish to do so, delete this
35 exception statement from your version.
36 */
37
38 import static org.junit.Assert.assertFalse;
39 import static org.junit.Assert.assertTrue;
40
41 import java.io.File;
42 import java.io.FileWriter;
43 import java.io.FilenameFilter;
44 import java.io.IOException;
45 import java.net.URL;
46
47 import net.sourceforge.jnlp.ProcessResult;
48 import net.sourceforge.jnlp.ServerAccess;
49 import net.sourceforge.jnlp.annotations.NeedsDisplay;
50 import net.sourceforge.jnlp.annotations.TestInBrowsers;
51 import net.sourceforge.jnlp.browsertesting.BrowserTest;
52 import net.sourceforge.jnlp.browsertesting.Browsers;
53 import net.sourceforge.jnlp.closinglisteners.RulesFolowingClosingListener;
54 import net.sourceforge.jnlp.config.DeploymentConfiguration;
55 import net.sourceforge.jnlp.runtime.JNLPRuntime;
56
57 import org.junit.After;
58 import org.junit.Before;
59 import org.junit.BeforeClass;
60 import org.junit.Test;
61
62 /* Test that adding permission for all codesources to read the user.home property
63 * results in an unsigned applet being able to perform this action
64 */
65 public class CustomPoliciesTest extends BrowserTest {
66
67 private static DeploymentConfiguration config = JNLPRuntime.getConfiguration();
68 private static File policy, policyBackup;
69
70 @BeforeClass
71 public static void setPolicyLocation() throws Exception {
72 policy = new File(new URL(config.getProperty(DeploymentConfiguration.KEY_USER_SECURITY_POLICY)).getPath());
73 File securityDir = policy.getParentFile();
74 File[] previousBackups = securityDir.listFiles(new FilenameFilter() {
75 @Override
76 public boolean accept(File dir, String name) {
77 return name.startsWith("java.policy.bak");
78 }
79 });
80 for (File backup : previousBackups) {
81 ServerAccess.logErrorReprint("Warning: found previous policy file backup at " + backup);
82 }
83 }
84
85 @Before
86 public void backupPolicy() throws Exception {
87 if (policy.isFile()) {
88 policyBackup = File.createTempFile("java.policy.bak", null, policy.getParentFile());
89 if (!policy.renameTo(policyBackup)) {
90 ServerAccess.logErrorReprint("Could not back up existing policy file");
91 throw new RuntimeException("Could not back up existing policy file");
92 }
93 }
94
95 }
96
97 @After
98 public void restorePolicy() {
99 policy.delete();
100 if (policyBackup != null && policyBackup.isFile()) {
101 policyBackup.renameTo(policy);
102 }
103 }
104
105 private void writePolicy() throws IOException {
106 FileWriter out = new FileWriter(policy);
107 try {
108 String policyText="grant {\n permission java.util.PropertyPermission \"user.home\", \"read\";\n};\n";
109 out.write(policyText, 0, policyText.length());
110 } finally {
111 out.close();
112 }
113 }
114
115 @NeedsDisplay
116 @Test
117 @TestInBrowsers(testIn={Browsers.one})
118 public void testHtmlLaunchWithPolicy() throws Exception {
119 writePolicy();
120 assertPolicyExists();
121 RulesFolowingClosingListener listener = new RulesFolowingClosingListener();
122 listener.addContainsRule("CustomPolicies applet read:");
123 ProcessResult pr = server.executeBrowser("CustomPolicies.html", listener, null);
124 assertInit(pr);
125 assertReadProps(pr);
126 assertNoAccessControlException(pr);
127 }
128
129 @NeedsDisplay
130 @Test
131 @TestInBrowsers(testIn={Browsers.one})
132 public void testHtmlJnlpHrefLaunchWithPolicy() throws Exception {
133 writePolicy();
134 assertPolicyExists();
135 RulesFolowingClosingListener listener = new RulesFolowingClosingListener();
136 listener.addContainsRule("CustomPolicies applet read:");
137 ProcessResult pr = server.executeBrowser("CustomPoliciesJnlpHref.html", listener, null);
138 assertInit(pr);
139 assertReadProps(pr);
140 assertNoAccessControlException(pr);
141 }
142
143 @Test
144 public void testJnlpAppletLaunchWithPolicy() throws Exception {
145 writePolicy();
146 assertPolicyExists();
147 ProcessResult pr = server.executeJavawsHeadless("CustomPoliciesApplet.jnlp");
148 assertInit(pr);
149 assertReadProps(pr);
150 assertNoAccessControlException(pr);
151 }
152
153 @Test
154 public void testJnlpApplicationLaunchWithPolicy() throws Exception {
155 writePolicy();
156 assertPolicyExists();
157 ProcessResult pr = server.executeJavawsHeadless("CustomPoliciesApplication.jnlp");
158 assertInit(pr);
159 assertReadProps(pr);
160 assertNoAccessControlException(pr);
161 }
162
163 @NeedsDisplay
164 @Test
165 @TestInBrowsers(testIn = { Browsers.one })
166 public void testHtmlLaunch() throws Exception {
167 assertNoPolicyExists();
168 RulesFolowingClosingListener listener = new RulesFolowingClosingListener();
169 listener.addContainsRule("CustomPolicies applet read:");
170 ProcessResult pr = server.executeBrowser("CustomPolicies.html", listener, null);
171 assertInit(pr);
172 assertAccessControlException(pr);
173 }
174
175 @NeedsDisplay
176 @Test
177 @TestInBrowsers(testIn = { Browsers.one })
178 public void testHtmlJnlpHrefLaunch() throws Exception {
179 assertNoPolicyExists();
180 RulesFolowingClosingListener listener = new RulesFolowingClosingListener();
181 listener.addContainsRule("CustomPolicies applet read:");
182 ProcessResult pr = server.executeBrowser("CustomPoliciesJnlpHref.html", listener, null);
183 assertInit(pr);
184 assertAccessControlException(pr);
185 }
186
187 @Test
188 public void testJnlpAppletLaunch() throws Exception {
189 assertNoPolicyExists();
190 ProcessResult pr = server.executeJavawsHeadless("CustomPoliciesApplet.jnlp");
191 assertInit(pr);
192 assertAccessControlException(pr);
193 }
194
195 @Test
196 public void testJnlpApplicationLaunch() throws Exception {
197 assertNoPolicyExists();
198 ProcessResult pr = server.executeJavawsHeadless("CustomPoliciesApplication.jnlp");
199 assertInit(pr);
200 assertAccessControlException(pr);
201 }
202
203 private void assertAccessControlException(ProcessResult pr) {
204 assertTrue("Applet should not have been able to read user.home", pr.stdout.contains("AccessControlException: access denied"));
205 }
206
207 private void assertPolicyExists() {
208 assertTrue("A user policy file should be installed", policy.isFile());
209 }
210
211 private void assertNoPolicyExists() {
212 assertFalse("A user policy file should not be installed", policy.isFile());
213 }
214
215 private void assertInit(ProcessResult pr) {
216 assertTrue("Applet should have initialized", pr.stdout.contains("CustomPolicies applet read:"));
217 }
218
219 private void assertReadProps(ProcessResult pr) {
220 assertTrue("stdout should contain user.home", pr.stdout.contains(System.getProperty("user.home")));
221 }
222
223 private void assertNoAccessControlException(ProcessResult pr) {
224 assertFalse("Applet should have been able to read user.home", pr.stdout.contains("AccessControlException: access denied"));
225 }
226
227 }