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