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. 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 * @summary File system view returns null default directory 40 * @library ../../../../lib/testlibrary 41 * @build jdk.testlibrary.OSInfo 42 * @run main/othervm bug8062561 GENERATE_POLICY 43 * @run main/othervm/policy=security.policy bug8062561 CHECK_DEFAULT_DIR run 44 */ 45 public class bug8062561 { 46 47 private static final String POLICY_FILE = "security2.policy"; 48 private static volatile boolean fileChooserIsShown = false; 49 50 public static void main(String[] args) throws Exception { 51 52 String test = args[0]; 53 54 switch (test) { 55 case "GENERATE_POLICY": 56 generatePolicyFile(); 57 break; 58 case "CHECK_DEFAULT_DIR": 59 checkDefaultDirectory(); 60 break; 61 case "CHECK_FILE_CHOOSER": 62 checkFileChooser(); 63 break; 64 default: 65 throw new RuntimeException("Wrong argument!"); 66 } 67 } 68 69 private static void checkDefaultDirectory() { 70 if (System.getSecurityManager() == null) { 71 throw new RuntimeException("Security manager is not set!"); 72 } 73 74 File defaultDirectory = FileSystemView.getFileSystemView(). 75 getDefaultDirectory(); 76 if (defaultDirectory != null) { 77 throw new RuntimeException("File system default directory must be null! (FilePermission has not been granted in our policy file)."); 78 } 79 } 80 private static volatile JFileChooser fileChooser; 81 82 private static void checkFileChooser() throws Exception { 83 if (System.getSecurityManager() == null) { 84 throw new RuntimeException("Security manager is not set!"); 85 } 86 87 Robot robot = new Robot(); 88 robot.setAutoDelay(50); 89 90 SwingUtilities.invokeLater(new Runnable() { 91 92 public void run() { 93 fileChooser = new JFileChooser(); 94 fileChooser.showOpenDialog(null); 95 fileChooserIsShown = true; 96 System.out.println("Start file chooser: " + fileChooserIsShown); 97 } 98 }); 99 100 long time = System.currentTimeMillis(); 101 while (fileChooser == null) { 102 if (System.currentTimeMillis() - time >= 10000) { 103 throw new RuntimeException("FileChoser is not shown!"); 104 } 105 Thread.sleep(500); 106 } 107 108 Thread.sleep(500); 109 robot.keyPress(KeyEvent.VK_ESCAPE); 110 robot.keyRelease(KeyEvent.VK_ESCAPE); 111 System.exit(0); 112 } 113 114 private static void generatePolicyFile() throws Exception { 115 if (System.getSecurityManager() != null) { 116 throw new RuntimeException("Security manager should be null!"); 117 } 118 119 if (!OSInfo.getOSType().equals(OSInfo.OSType.WINDOWS)) { 120 return; 121 } 122 123 File defaultDirectory = FileSystemView.getFileSystemView(). 124 getDefaultDirectory(); 125 126 if (defaultDirectory == null) { 127 throw new RuntimeException("Default directory is null!"); 128 } 129 130 File policyFile = new File(POLICY_FILE); 131 if (!policyFile.exists()) { 132 policyFile.createNewFile(); 133 } 134 135 try (PrintWriter writer = new PrintWriter(policyFile, "UTF-8")) { 136 writer.println("grant {"); 137 String documents = defaultDirectory.getCanonicalPath(); 138 documents = documents.replace('\\', '/'); 139 // Documents permission 140 writer.print(" permission java.io.FilePermission"); 141 writer.print(" \"" + documents + "\","); 142 writer.println(" \"read\";"); 143 // Desktop permission 144 writer.print(" permission java.io.FilePermission"); 145 writer.print(" \"" + documents.replace("Documents", "Desktop") + "\","); 146 writer.println(" \"read\";"); 147 // robot permission // "java.awt.AWTPermission" "createRobot" 148 writer.print(" permission java.awt.AWTPermission"); 149 writer.println(" \"createRobot\";"); 150 writer.println("};"); 151 } 152 153 performTest(); 154 } 155 156 private static void performTest() throws Exception { 157 String javaPath = System.getProperty("java.home", ""); 158 String command = javaPath + File.separator + "bin" + File.separator + "java" 159 + " -Djava.security.manager -Djava.security.policy=" + POLICY_FILE 160 + " bug8062561 CHECK_FILE_CHOOSER"; 161 System.out.println(command); 162 boolean processExit = false; 163 164 Process process = Runtime.getRuntime().exec(command); 165 166 try { 167 processExit = process.waitFor(20, TimeUnit.SECONDS); 168 } catch (IllegalThreadStateException e) { 169 throw new RuntimeException(e); 170 } 171 System.out.println("[RESULT] : " 172 + "The sub process has cleanly exited : PASS"); 173 174 InputStream errorStream = process.getErrorStream(); 175 System.out.println("========= Child process stderr ========"); 176 boolean exception = dumpStream(errorStream); 177 if (exception) { 178 throw new RuntimeException("[RESULT] :" 179 + " Exception in child process : FAIL"); 180 } 181 System.out.println("======================================="); 182 183 InputStream processInputStream = process.getInputStream(); 184 System.out.println("========= Child process output ========"); 185 dumpStream(processInputStream); 186 System.out.println("======================================="); 187 188 if (!processExit) { 189 process.destroy(); 190 throw new RuntimeException("[RESULT] : " 191 + "The sub process has not exited : FAIL"); 192 } 193 } 194 195 public static boolean dumpStream(InputStream in) throws IOException { 196 String tempString; 197 int count = in.available(); 198 boolean exception = false; 199 while (count > 0) { 200 byte[] b = new byte[count]; 201 in.read(b); 202 tempString = new String(b); 203 if (!exception) { 204 exception = tempString.indexOf("Exception") != -1; 205 } 206 System.out.println(tempString); 207 count = in.available(); 208 } 209 210 return exception; 211 } 212 }