1 /* 2 * Copyright (c) 2014, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sandbox; 27 28 import com.sun.javafx.PlatformUtil; 29 import java.io.File; 30 import junit.framework.AssertionFailedError; 31 import org.junit.Test; 32 33 import static org.junit.Assert.*; 34 import static org.junit.Assume.*; 35 import static sandbox.Constants.*; 36 37 /** 38 * Unit test for running JavaFX apps in a sandbox with a restrictive 39 * security manager. 40 */ 41 public class SandboxAppTest { 42 43 private static final String className = SandboxAppTest.class.getName(); 44 private static final String pkgName = className.substring(0, className.lastIndexOf(".")); 45 46 private String getJfxrtDir(final String classpath) { 47 final String jfxrt = "jfxrt.jar"; 48 String cp = classpath; 49 int idx = cp.replace('\\', '/').indexOf("/" + jfxrt); 50 assertTrue("No " + jfxrt + " in classpath", idx >= 0); 51 cp = cp.substring(0, idx); 52 idx = cp.lastIndexOf(File.pathSeparator); 53 if (idx >= 0) { 54 cp = cp.substring(idx, cp.length()); 55 } 56 return cp; 57 } 58 59 private static String getTestPolicyFile() { 60 return SandboxAppTest.class.getResource("test.policy").toExternalForm(); 61 } 62 63 private void runSandboxedApp(String appName) throws Exception { 64 runSandboxedApp(appName, ERROR_NONE); 65 } 66 67 private void runSandboxedApp(String appName, int exitCode) throws Exception { 68 final String testAppName = pkgName + ".app." + appName; 69 final String classpath = System.getProperty("java.class.path"); 70 final String jfxrtDir = getJfxrtDir(classpath); 71 final String testPolicy = getTestPolicyFile(); 72 ProcessBuilder builder; 73 builder = new ProcessBuilder("java", 74 "-Djava.ext.dirs=" + jfxrtDir, 75 "-Djava.security.manager", 76 "-Djava.security.policy=" + testPolicy, 77 "-cp", classpath, 78 testAppName); 79 builder.redirectError(ProcessBuilder.Redirect.INHERIT); 80 builder.redirectOutput(ProcessBuilder.Redirect.INHERIT); 81 Process process = builder.start(); 82 int retVal = process.waitFor(); 83 switch (retVal) { 84 case 0: 85 case ERROR_NONE: 86 assertEquals(testAppName + ": Unexpected 'success' exit code;", 87 exitCode, retVal); 88 break; 89 90 case 1: 91 throw new AssertionFailedError(testAppName 92 + ": unable to launch java application"); 93 94 case ERROR_TIMEOUT: 95 throw new AssertionFailedError(testAppName 96 + ": Application timeout"); 97 case ERROR_SECURITY_EXCEPTION: 98 throw new AssertionFailedError(testAppName 99 + ": Application failed with a security exception"); 100 case ERROR_NO_SECURITY_EXCEPTION: 101 throw new AssertionFailedError(testAppName 102 + ": Application did not get expected security exception"); 103 case ERROR_UNEXPECTED_EXCEPTION: 104 throw new AssertionFailedError(testAppName 105 + ": Application failed with unexpected exception"); 106 107 default: 108 throw new AssertionFailedError(testAppName 109 + ": Unexpected error exit: " + retVal); 110 } 111 } 112 113 // TEST CASES 114 115 @Test (timeout=10000) 116 public void testFXApp() throws Exception { 117 runSandboxedApp("FXApp"); 118 } 119 120 @Test (timeout=10000) 121 public void testFXNonApp() throws Exception { 122 runSandboxedApp("FXNonApp"); 123 } 124 125 @Test (timeout=10000) 126 public void testJFXPanelApp() throws Exception { 127 // TODO: re-enable this when 8139317 is fixed 128 assumeTrue(!PlatformUtil.isMac()); 129 runSandboxedApp("JFXPanelApp"); 130 } 131 132 @Test (timeout=10000) 133 public void testJFXPanelImplicitExitApp() throws Exception { 134 // Test skipped on Mac OS X due to JDK bug 8037776 135 assumeTrue(!PlatformUtil.isMac()); 136 runSandboxedApp("JFXPanelImplicitExitApp", 0); 137 } 138 139 }