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 sandbox.Constants.*;
  35 
  36 /**
  37  * Unit test for running JavaFX apps in a sandbox with a restrictive
  38  * security manager.
  39  */
  40 public class SandboxAppTest {
  41 
  42     private static final String className = SandboxAppTest.class.getName();
  43     private static final String pkgName = className.substring(0, className.lastIndexOf("."));
  44 
  45     private String getJfxrtDir(final String classpath) {
  46         final String jfxrt = "jfxrt.jar";
  47         String cp = classpath;
  48         int idx = cp.replace('\\', '/').indexOf("/" + jfxrt);
  49         assertTrue("No " + jfxrt + " in classpath", idx >= 0);
  50         cp = cp.substring(0, idx);
  51         idx = cp.lastIndexOf(File.pathSeparator);
  52         if (idx >= 0) {
  53             cp = cp.substring(idx, cp.length());
  54         }
  55         return cp;
  56     }
  57 
  58     private static String getTestPolicyFile() {
  59         return SandboxAppTest.class.getResource("test.policy").toExternalForm();
  60     }
  61 
  62     private void runSandboxedApp(String appName) throws Exception {
  63         runSandboxedApp(appName, ERROR_NONE);
  64     }
  65 
  66     private void runSandboxedApp(String appName, int exitCode) throws Exception {
  67         final String testAppName = pkgName + ".app." + appName;
  68         final String classpath = System.getProperty("java.class.path");
  69         final String jfxrtDir = getJfxrtDir(classpath);
  70         final String testPolicy = getTestPolicyFile();
  71         ProcessBuilder builder;
  72         builder = new ProcessBuilder("java",
  73                 "-Djava.ext.dirs=" + jfxrtDir,
  74                 "-Djava.security.manager",
  75                 "-Djava.security.policy=" + testPolicy,
  76                 "-cp", classpath,
  77                 testAppName);
  78         builder.redirectError(ProcessBuilder.Redirect.INHERIT);
  79         builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
  80         Process process = builder.start();
  81         int retVal = process.waitFor();
  82         switch (retVal) {
  83             case 0:
  84             case ERROR_NONE:
  85                 assertEquals(testAppName + ": Unexpected 'success' exit code;",
  86                         exitCode, retVal);
  87                 break;
  88 
  89             case 1:
  90                 throw new AssertionFailedError(testAppName
  91                         + ": unable to launch java application");
  92 
  93             case ERROR_TIMEOUT:
  94                 throw new AssertionFailedError(testAppName
  95                         + ": Application timeout");
  96             case ERROR_SECURITY_EXCEPTION:
  97                 throw new AssertionFailedError(testAppName
  98                         + ": Application failed with a security exception");
  99             case ERROR_NO_SECURITY_EXCEPTION:
 100                 throw new AssertionFailedError(testAppName
 101                         + ": Application did not get expected security exception");
 102             case ERROR_UNEXPECTED_EXCEPTION:
 103                 throw new AssertionFailedError(testAppName
 104                         + ": Application failed with unexpected exception");
 105 
 106            default:
 107                 throw new AssertionFailedError(testAppName
 108                         + ": Unexpected error exit: " + retVal);
 109         }
 110     }
 111 
 112     // TEST CASES
 113 
 114     @Test (timeout=10000)
 115     public void testFXApp() throws Exception {
 116         runSandboxedApp("FXApp");
 117     }
 118 
 119     @Test (timeout=10000)
 120     public void testFXNonApp() throws Exception {
 121         runSandboxedApp("FXNonApp");
 122     }
 123 
 124     @Test (timeout=10000)
 125     public void testJFXPanelApp() throws Exception {
 126         runSandboxedApp("JFXPanelApp");
 127     }
 128 
 129     @Test (timeout=10000)
 130     public void testJFXPanelImplicitExitApp() throws Exception {
 131         // Test skipped on Mac OS X due to 8037776
 132         if (PlatformUtil.isMac()) {
 133             System.err.println("*** Skipping test on Mac OS X");
 134             return;
 135         }
 136         runSandboxedApp("JFXPanelImplicitExitApp", 0);
 137     }
 138 
 139 }