Path: blob/master/test/jdk/javax/swing/JFileChooser/8062561/bug8062561.java
41154 views
/*1* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import jdk.test.lib.Platform;24import java.awt.Robot;25import java.awt.event.KeyEvent;26import java.io.File;27import java.io.IOException;28import java.io.InputStream;29import java.io.PrintWriter;30import java.util.concurrent.TimeUnit;31import javax.swing.JFileChooser;32import javax.swing.SwingUtilities;33import javax.swing.filechooser.FileSystemView;3435/**36* @test37* @bug 806256138* @key headful39* @requires (os.family == "windows")40* @summary File system view returns null default directory41* @library /test/lib42* @modules java.desktop/sun.awt43* @build jdk.test.lib.Platform44* @run main/othervm bug8062561 GENERATE_POLICY45* @run main/othervm/policy=security.policy bug8062561 CHECK_DEFAULT_DIR run46*/47public class bug8062561 {4849private static final String POLICY_FILE = "security2.policy";50private static volatile boolean fileChooserIsShown = false;5152public static void main(String[] args) throws Exception {5354String test = args[0];5556switch (test) {57case "GENERATE_POLICY":58generatePolicyFile();59break;60case "CHECK_DEFAULT_DIR":61checkDefaultDirectory();62break;63case "CHECK_FILE_CHOOSER":64checkFileChooser();65break;66default:67throw new RuntimeException("Wrong argument!");68}69}7071private static void checkDefaultDirectory() {72if (System.getSecurityManager() == null) {73throw new RuntimeException("Security manager is not set!");74}7576File defaultDirectory = FileSystemView.getFileSystemView().77getDefaultDirectory();78if (defaultDirectory != null) {79throw new RuntimeException("File system default directory must be null! (FilePermission has not been granted in our policy file).");80}81}82private static volatile JFileChooser fileChooser;8384private static void checkFileChooser() throws Exception {85if (System.getSecurityManager() == null) {86throw new RuntimeException("Security manager is not set!");87}8889Robot robot = new Robot();90robot.setAutoDelay(50);9192SwingUtilities.invokeLater(new Runnable() {9394public void run() {95fileChooser = new JFileChooser();96fileChooser.showOpenDialog(null);97fileChooserIsShown = true;98System.out.println("Start file chooser: " + fileChooserIsShown);99}100});101102long time = System.currentTimeMillis();103while (fileChooser == null) {104if (System.currentTimeMillis() - time >= 10000) {105throw new RuntimeException("FileChoser is not shown!");106}107Thread.sleep(500);108}109110Thread.sleep(500);111robot.keyPress(KeyEvent.VK_ESCAPE);112robot.keyRelease(KeyEvent.VK_ESCAPE);113System.exit(0);114}115116private static void generatePolicyFile() throws Exception {117if (System.getSecurityManager() != null) {118throw new RuntimeException("Security manager should be null!");119}120121if (!Platform.isWindows()) {122return;123}124125File defaultDirectory = FileSystemView.getFileSystemView().126getDefaultDirectory();127128if (defaultDirectory == null) {129throw new RuntimeException("Default directory is null!");130}131132File policyFile = new File(POLICY_FILE);133if (!policyFile.exists()) {134policyFile.createNewFile();135}136137try (PrintWriter writer = new PrintWriter(policyFile, "UTF-8")) {138writer.println("grant {");139String documents = defaultDirectory.getCanonicalPath();140documents = documents.replace('\\', '/');141// Documents permission142writer.print(" permission java.io.FilePermission");143writer.print(" \"" + documents + "\",");144writer.println(" \"read\";");145// Desktop permission146writer.print(" permission java.io.FilePermission");147writer.print(" \"" + documents.replace("Documents", "Desktop") + "\",");148writer.println(" \"read\";");149// robot permission // "java.awt.AWTPermission" "createRobot"150writer.print(" permission java.awt.AWTPermission");151writer.println(" \"createRobot\";");152writer.println("};");153}154155performTest();156}157158private static void performTest() throws Exception {159String javaPath = System.getProperty("java.home", "");160String command = javaPath + File.separator + "bin" + File.separator + "java"161+ " -Djava.security.manager -Djava.security.policy=" + POLICY_FILE162+ " bug8062561 CHECK_FILE_CHOOSER";163System.out.println(command);164boolean processExit = false;165166Process process = Runtime.getRuntime().exec(command);167168try {169processExit = process.waitFor(20, TimeUnit.SECONDS);170} catch (IllegalThreadStateException e) {171throw new RuntimeException(e);172}173System.out.println("[RESULT] : "174+ "The sub process has cleanly exited : PASS");175176InputStream errorStream = process.getErrorStream();177System.out.println("========= Child process stderr ========");178boolean exception = dumpStream(errorStream);179if (exception) {180throw new RuntimeException("[RESULT] :"181+ " Exception in child process : FAIL");182}183System.out.println("=======================================");184185InputStream processInputStream = process.getInputStream();186System.out.println("========= Child process output ========");187dumpStream(processInputStream);188System.out.println("=======================================");189190if (!processExit) {191process.destroy();192throw new RuntimeException("[RESULT] : "193+ "The sub process has not exited : FAIL");194}195}196197public static boolean dumpStream(InputStream in) throws IOException {198String tempString;199int count = in.available();200boolean exception = false;201while (count > 0) {202byte[] b = new byte[count];203in.read(b);204tempString = new String(b);205if (!exception) {206exception = tempString.indexOf("Exception") != -1;207}208System.out.println(tempString);209count = in.available();210}211212return exception;213}214}215216217