1 /* 2 * Copyright (c) 2014, 2015, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import jdk.testlibrary.OSInfo; 25 import java.awt.Robot; 26 import java.awt.event.KeyEvent; 27 import java.io.File; 28 import java.io.IOException; 29 import java.io.InputStream; 30 import java.io.PrintWriter; 31 import java.util.concurrent.TimeUnit; 32 import javax.swing.JFileChooser; 33 import javax.swing.SwingUtilities; 34 import javax.swing.filechooser.FileSystemView; 35 36 /** 37 * @test 38 * @bug 8062561 39 * @key headful 40 * @requires (os.family == "windows") 41 * @summary File system view returns null default directory 42 * @library ../../../../lib/testlibrary 43 * @modules java.desktop/sun.awt 44 * @build jdk.testlibrary.OSInfo 45 * @run main/othervm bug8062561 GENERATE_POLICY 46 * @run main/othervm/policy=security.policy bug8062561 CHECK_DEFAULT_DIR run 47 */ 48 public class bug8062561 { 49 50 private static final String POLICY_FILE = "security2.policy"; 51 private static volatile boolean fileChooserIsShown = false; 52 53 public static void main(String[] args) throws Exception { 54 55 String test = args[0]; 56 57 switch (test) { 58 case "GENERATE_POLICY": 59 generatePolicyFile(); 60 break; 61 case "CHECK_DEFAULT_DIR": 62 checkDefaultDirectory(); 63 break; 64 case "CHECK_FILE_CHOOSER": 65 checkFileChooser(); 66 break; 67 default: 68 throw new RuntimeException("Wrong argument!"); 69 } 70 } 71 72 private static void checkDefaultDirectory() { 73 if (System.getSecurityManager() == null) { 74 throw new RuntimeException("Security manager is not set!"); 75 } 76 77 File defaultDirectory = FileSystemView.getFileSystemView(). 78 getDefaultDirectory(); 79 if (defaultDirectory != null) { 80 throw new RuntimeException("File system default directory must be null! (FilePermission has not been granted in our policy file)."); 81 } 82 } 83 private static volatile JFileChooser fileChooser; 84 85 private static void checkFileChooser() throws Exception { 86 if (System.getSecurityManager() == null) { 87 throw new RuntimeException("Security manager is not set!"); 88 } 89 90 Robot robot = new Robot(); 91 robot.setAutoDelay(50); 92 93 SwingUtilities.invokeLater(new Runnable() { 94 95 public void run() { 96 fileChooser = new JFileChooser(); 97 fileChooser.showOpenDialog(null); 98 fileChooserIsShown = true; 99 System.out.println("Start file chooser: " + fileChooserIsShown); 100 } 101 }); 102 103 long time = System.currentTimeMillis(); 104 while (fileChooser == null) { 105 if (System.currentTimeMillis() - time >= 10000) { 106 throw new RuntimeException("FileChoser is not shown!"); 107 } 108 Thread.sleep(500); 109 } 110 111 Thread.sleep(500); 112 robot.keyPress(KeyEvent.VK_ESCAPE); 113 robot.keyRelease(KeyEvent.VK_ESCAPE); 114 System.exit(0); 115 } 116 117 private static void generatePolicyFile() throws Exception { 118 if (System.getSecurityManager() != null) { 119 throw new RuntimeException("Security manager should be null!"); 120 } 121 122 if (!OSInfo.getOSType().equals(OSInfo.OSType.WINDOWS)) { 123 return; 124 } 125 126 File defaultDirectory = FileSystemView.getFileSystemView(). 127 getDefaultDirectory(); 128 129 if (defaultDirectory == null) { 130 throw new RuntimeException("Default directory is null!"); 131 } 132 133 File policyFile = new File(POLICY_FILE); 134 if (!policyFile.exists()) { 135 policyFile.createNewFile(); 136 } 137 138 try (PrintWriter writer = new PrintWriter(policyFile, "UTF-8")) { 139 writer.println("grant {"); 140 String documents = defaultDirectory.getCanonicalPath(); 141 documents = documents.replace('\\', '/'); 142 // Documents permission 143 writer.print(" permission java.io.FilePermission"); 144 writer.print(" \"" + documents + "\","); 145 writer.println(" \"read\";"); 146 // Desktop permission 147 writer.print(" permission java.io.FilePermission"); 148 writer.print(" \"" + documents.replace("Documents", "Desktop") + "\","); 149 writer.println(" \"read\";"); 150 // robot permission // "java.awt.AWTPermission" "createRobot" 151 writer.print(" permission java.awt.AWTPermission"); 152 writer.println(" \"createRobot\";"); 153 writer.println("};"); 154 } 155 156 performTest(); 157 } 158 159 private static void performTest() throws Exception { 160 String javaPath = System.getProperty("java.home", ""); 161 String command = javaPath + File.separator + "bin" + File.separator + "java" 162 + " -Djava.security.manager -Djava.security.policy=" + POLICY_FILE 163 + " bug8062561 CHECK_FILE_CHOOSER"; 164 System.out.println(command); 165 boolean processExit = false; 166 167 Process process = Runtime.getRuntime().exec(command); 168 169 try { 170 processExit = process.waitFor(20, TimeUnit.SECONDS); 171 } catch (IllegalThreadStateException e) { 172 throw new RuntimeException(e); 173 } 174 System.out.println("[RESULT] : " 175 + "The sub process has cleanly exited : PASS"); 176 177 InputStream errorStream = process.getErrorStream(); 178 System.out.println("========= Child process stderr ========"); 179 boolean exception = dumpStream(errorStream); 180 if (exception) { 181 throw new RuntimeException("[RESULT] :" 182 + " Exception in child process : FAIL"); 183 } 184 System.out.println("======================================="); 185 186 InputStream processInputStream = process.getInputStream(); 187 System.out.println("========= Child process output ========"); 188 dumpStream(processInputStream); 189 System.out.println("======================================="); 190 191 if (!processExit) { 192 process.destroy(); 193 throw new RuntimeException("[RESULT] : " 194 + "The sub process has not exited : FAIL"); 195 } 196 } 197 198 public static boolean dumpStream(InputStream in) throws IOException { 199 String tempString; 200 int count = in.available(); 201 boolean exception = false; 202 while (count > 0) { 203 byte[] b = new byte[count]; 204 in.read(b); 205 tempString = new String(b); 206 if (!exception) { 207 exception = tempString.indexOf("Exception") != -1; 208 } 209 System.out.println(tempString); 210 count = in.available(); 211 } 212 213 return exception; 214 } 215 }