1 /*
   2  * Copyright (c) 2014, 2016, 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 test.sandbox;
  27 
  28 import com.sun.javafx.PlatformUtil;
  29 import java.util.ArrayList;
  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 test.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 static String getTestPolicyFile() {
  47         return SandboxAppTest.class.getResource("test.policy").toExternalForm();
  48     }
  49 
  50     private void runSandboxedApp(String appName) throws Exception {
  51         runSandboxedApp(appName, ERROR_NONE);
  52     }
  53 
  54     private void runSandboxedApp(String appName, int exitCode) throws Exception {
  55         final String testAppName = pkgName + ".app." + appName;
  56         final String testPolicy = getTestPolicyFile();
  57 
  58         final ArrayList<String> cmd =
  59                 test.util.Util.createApplicationLaunchCommand(
  60                         testAppName,
  61                         null,
  62                         testPolicy
  63                 );
  64 
  65         final ProcessBuilder builder = new ProcessBuilder(cmd);
  66         builder.redirectError(ProcessBuilder.Redirect.INHERIT);
  67         builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
  68         Process process = builder.start();
  69         int retVal = process.waitFor();
  70         switch (retVal) {
  71             case 0:
  72             case ERROR_NONE:
  73                 assertEquals(testAppName + ": Unexpected 'success' exit code;",
  74                         exitCode, retVal);
  75                 break;
  76 
  77             case 1:
  78                 throw new AssertionFailedError(testAppName
  79                         + ": unable to launch java application");
  80 
  81             case ERROR_TIMEOUT:
  82                 throw new AssertionFailedError(testAppName
  83                         + ": Application timeout");
  84             case ERROR_SECURITY_EXCEPTION:
  85                 throw new AssertionFailedError(testAppName
  86                         + ": Application failed with a security exception");
  87             case ERROR_NO_SECURITY_EXCEPTION:
  88                 throw new AssertionFailedError(testAppName
  89                         + ": Application did not get expected security exception");
  90             case ERROR_UNEXPECTED_EXCEPTION:
  91                 throw new AssertionFailedError(testAppName
  92                         + ": Application failed with unexpected exception");
  93 
  94            default:
  95                 throw new AssertionFailedError(testAppName
  96                         + ": Unexpected error exit: " + retVal);
  97         }
  98     }
  99 
 100     // TEST CASES
 101 
 102     @Test (timeout=15000)
 103     public void testFXApp() throws Exception {
 104         runSandboxedApp("FXApp");
 105     }
 106 
 107     @Test (timeout=15000)
 108     public void testFXNonApp() throws Exception {
 109         runSandboxedApp("FXNonApp");
 110     }
 111 
 112     @Test (timeout=15000)
 113     public void testJFXPanelApp() throws Exception {
 114         runSandboxedApp("JFXPanelApp");
 115     }
 116 
 117     @Test (timeout=15000)
 118     public void testJFXPanelImplicitExitApp() throws Exception {
 119         // Test skipped on Mac OS X due to JDK bug 8037776
 120         assumeTrue(!PlatformUtil.isMac());
 121         runSandboxedApp("JFXPanelImplicitExitApp", 0);
 122     }
 123 
 124 }