From d5280868a3ee35da56e67b502062a7e9846040fa Mon Sep 17 00:00:00 2001 From: Roman Kennke Date: Tue, 2 Feb 2010 16:38:26 +0100 Subject: [PATCH 1/3] 6888734: PIT: regression test fails when java.security.manager is enabled Load FontManager instance in privileged block to avoid AccessControlException Reviewed-by: igor, tdv --- .../classes/sun/font/FontManagerFactory.java | 54 +++++---- .../PrintJob/Security/SecurityDialogTest.java | 105 ++++++++++++++++++ jdk/test/java/awt/PrintJob/Security/policy | 32 ++++++ 3 files changed, 162 insertions(+), 29 deletions(-) create mode 100644 jdk/test/java/awt/PrintJob/Security/SecurityDialogTest.java create mode 100644 jdk/test/java/awt/PrintJob/Security/policy diff --git a/jdk/src/share/classes/sun/font/FontManagerFactory.java b/jdk/src/share/classes/sun/font/FontManagerFactory.java index 44bfe68c541..a85fe3c007c 100644 --- a/jdk/src/share/classes/sun/font/FontManagerFactory.java +++ b/jdk/src/share/classes/sun/font/FontManagerFactory.java @@ -68,38 +68,34 @@ public final class FontManagerFactory { return instance; } - String fmClassName = AccessController.doPrivileged( - new GetPropertyAction("sun.font.fontmanager", - DEFAULT_CLASS)); + AccessController.doPrivileged(new PrivilegedAction() { - try { - @SuppressWarnings("unchecked") - ClassLoader cl = (ClassLoader) - AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - return ClassLoader.getSystemClassLoader(); - } - }); + public Object run() { + try { + String fmClassName = + System.getProperty("sun.font.fontmanager", + DEFAULT_CLASS); + ClassLoader cl = ClassLoader.getSystemClassLoader(); + Class fmClass = Class.forName(fmClassName, true, cl); + instance = (FontManager) fmClass.newInstance(); + } catch (ClassNotFoundException ex) { + InternalError err = new InternalError(); + err.initCause(ex); + throw err; - @SuppressWarnings("unchecked") - Class fmClass = Class.forName(fmClassName, true, cl); - instance = (FontManager) fmClass.newInstance(); + } catch (InstantiationException ex) { + InternalError err = new InternalError(); + err.initCause(ex); + throw err; - } catch (ClassNotFoundException ex) { - InternalError err = new InternalError(); - err.initCause(ex); - throw err; - - } catch (InstantiationException ex) { - InternalError err = new InternalError(); - err.initCause(ex); - throw err; - - } catch (IllegalAccessException ex) { - InternalError err = new InternalError(); - err.initCause(ex); - throw err; - } + } catch (IllegalAccessException ex) { + InternalError err = new InternalError(); + err.initCause(ex); + throw err; + } + return null; + } + }); return instance; } diff --git a/jdk/test/java/awt/PrintJob/Security/SecurityDialogTest.java b/jdk/test/java/awt/PrintJob/Security/SecurityDialogTest.java new file mode 100644 index 00000000000..bf8c86f3106 --- /dev/null +++ b/jdk/test/java/awt/PrintJob/Security/SecurityDialogTest.java @@ -0,0 +1,105 @@ +/* + * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +/** + * @test + * @bug 6195901 6195923 6195928 6195933 6491273 6888734 + * @summary No SecurityException should be thrown when printing to a file + using the given policy. + Print to file option should be selected. + * @run main/othervm/policy=policy SecurityDialogTest + */ +import java.awt.*; +import java.awt.event.*; +import java.util.*; +import java.io.*; + + +public class SecurityDialogTest extends Frame implements ActionListener { + // Declare things used in the test, like buttons and labels here + + Button nativeDlg, setSecurity; + boolean isNative = true; + + public SecurityDialogTest() { + + nativeDlg = new Button("Print Dialog"); + nativeDlg.addActionListener(this); + setSecurity = new Button("Toggle Dialog"); + setSecurity.addActionListener(this); + add("South", nativeDlg); + add("North", setSecurity); + setSize(300, 300); + setVisible(true); + } + + public static void main(String args[]) { + System.out.println("Native dialog is the default"); + SecurityDialogTest test = new SecurityDialogTest(); + } + + public void actionPerformed(ActionEvent e) { + + if (e.getSource() == setSecurity) { + if (isNative) { + isNative = false; + System.out.println("Common dialog is the default"); + + } else { + isNative = true; + System.out.println("Native dialog is the default"); + } + return; + } + + JobAttributes ja = new JobAttributes(); + PageAttributes pa = new PageAttributes(); + + if (isNative) { + ja.setDialog(JobAttributes.DialogType.NATIVE); + } else { + ja.setDialog(JobAttributes.DialogType.COMMON); + } + ja.setDestination(JobAttributes.DestinationType.FILE); + ja.setFileName("mohan.ps"); + + + PrintJob pjob = getToolkit().getPrintJob(this, null, ja, pa); + + if (pjob != null) { + Graphics pg = pjob.getGraphics(); + System.out.println("PJOB: " + pjob); + if (pg != null) { + System.out.println("Printer Graphics: " + pg); + this.printAll(pg); + pg.dispose(); + } else { + System.out.println("Printer Graphics is null"); + } + pjob.end(); + System.out.println("DONE"); + } else { + System.out.println("PJOB is null"); + } + } +} diff --git a/jdk/test/java/awt/PrintJob/Security/policy b/jdk/test/java/awt/PrintJob/Security/policy new file mode 100644 index 00000000000..da2ab38ed31 --- /dev/null +++ b/jdk/test/java/awt/PrintJob/Security/policy @@ -0,0 +1,32 @@ +/* + * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +/* AUTOMATICALLY GENERATED ON Thu Jan 03 15:48:39 PST 2002*/ +/* DO NOT EDIT */ + +grant { + permission java.io.FilePermission "<>", "read,write"; + permission java.lang.RuntimePermission "queuePrintJob"; + permission java.lang.RuntimePermission "setSecurityManager"; +}; + From aafe7d739828aec046812d78778d8689f30983a6 Mon Sep 17 00:00:00 2001 From: Roman Kennke Date: Wed, 3 Feb 2010 10:02:33 +0100 Subject: [PATCH 2/3] 6896335: GraphicsEnvironment.getDefaultScreenDevice() throws UnsatisfiedLinkError in headless mode Use local ge variable instead of localEnv field in GE. Reviewed-by: igor, prr --- .../classes/java/awt/GraphicsEnvironment.java | 2 +- .../TestGetDefScreenDevice.java | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 jdk/test/java/awt/GraphicsEnvironment/TestGetDefScreenDevice.java diff --git a/jdk/src/share/classes/java/awt/GraphicsEnvironment.java b/jdk/src/share/classes/java/awt/GraphicsEnvironment.java index 392a4478140..860be245ec3 100644 --- a/jdk/src/share/classes/java/awt/GraphicsEnvironment.java +++ b/jdk/src/share/classes/java/awt/GraphicsEnvironment.java @@ -110,7 +110,7 @@ public abstract class GraphicsEnvironment { // long t1 = System.currentTimeMillis(); // System.out.println("GE creation took " + (t1-t0)+ "ms."); if (isHeadless()) { - localEnv = new HeadlessGraphicsEnvironment(localEnv); + ge = new HeadlessGraphicsEnvironment(ge); } } catch (ClassNotFoundException e) { throw new Error("Could not find class: "+nm); diff --git a/jdk/test/java/awt/GraphicsEnvironment/TestGetDefScreenDevice.java b/jdk/test/java/awt/GraphicsEnvironment/TestGetDefScreenDevice.java new file mode 100644 index 00000000000..33c7eb92ce7 --- /dev/null +++ b/jdk/test/java/awt/GraphicsEnvironment/TestGetDefScreenDevice.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2010 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +/* + * @test + * @bug 6896335 + * @summary Test GraphicsEnvironment.getDefaultScreenDevice() in headless mode + * @run main/othervm -Djava.awt.headless=true TestGetDefScreenDevice + */ + +import java.awt.*; +public class TestGetDefScreenDevice { + + public static void main(String[] args) throws Exception { + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); + try { + ge.getDefaultScreenDevice(); + throw new Exception("Failed. HeadlessException not thrown"); + } catch (HeadlessException he) { + // OK, test passed. + } + } +} From c73000ad8f2b505d493ef080d9a06eb1ae3e23af Mon Sep 17 00:00:00 2001 From: Roman Kennke Date: Sun, 7 Feb 2010 11:07:50 +0100 Subject: [PATCH 3/3] 6904882: java.awt.Font.createFont() causes AccessControlException if executed with "-Djava.security.manager" Perform FontUtilities initialization in privileged block Reviewed-by: igor, prr --- .../share/classes/sun/font/FontUtilities.java | 113 +++++++++--------- .../java/awt/FontClass/FontPrivilege.java | 39 ++++++ 2 files changed, 97 insertions(+), 55 deletions(-) create mode 100644 jdk/test/java/awt/FontClass/FontPrivilege.java diff --git a/jdk/src/share/classes/sun/font/FontUtilities.java b/jdk/src/share/classes/sun/font/FontUtilities.java index 0c492b4c732..6d10874deef 100644 --- a/jdk/src/share/classes/sun/font/FontUtilities.java +++ b/jdk/src/share/classes/sun/font/FontUtilities.java @@ -32,9 +32,9 @@ import java.io.FileInputStream; import java.io.InputStreamReader; import java.security.AccessController; +import java.security.PrivilegedAction; import javax.swing.plaf.FontUIResource; -import sun.security.action.GetPropertyAction; import sun.util.logging.PlatformLogger; /** @@ -42,79 +42,82 @@ import sun.util.logging.PlatformLogger; */ public final class FontUtilities { - public static final boolean isSolaris; + public static boolean isSolaris; - public static final boolean isLinux; + public static boolean isLinux; - public static final boolean isSolaris8; + public static boolean isSolaris8; - public static final boolean isSolaris9; + public static boolean isSolaris9; - public static final boolean isOpenSolaris; + public static boolean isOpenSolaris; - public static final boolean useT2K; + public static boolean useT2K; - public static final boolean isWindows; + public static boolean isWindows; - public static final boolean isOpenJDK; + public static boolean isOpenJDK; static final String LUCIDA_FILE_NAME = "LucidaSansRegular.ttf"; // This static initializer block figures out the OS constants. static { - String osName = AccessController.doPrivileged( - new GetPropertyAction("os.name", "unknownOS")); - isSolaris = osName.startsWith("SunOS"); + AccessController.doPrivileged(new PrivilegedAction () { + public Object run() { + String osName = System.getProperty("os.name", "unknownOS"); + isSolaris = osName.startsWith("SunOS"); - isLinux = osName.startsWith("Linux"); + isLinux = osName.startsWith("Linux"); - String t2kStr = AccessController.doPrivileged( - new GetPropertyAction("sun.java2d.font.scaler")); - if (t2kStr != null) { - useT2K = "t2k".equals(t2kStr); - } else { - useT2K = false; - } - if (isSolaris) { - String version = AccessController.doPrivileged( - new GetPropertyAction("os.version", "0.0")); - isSolaris8 = version.startsWith("5.8"); - isSolaris9 = version.startsWith("5.9"); - float ver = Float.parseFloat(version); - if (ver > 5.10f) { - File f = new File("/etc/release"); - String line = null; - try { - FileInputStream fis = new FileInputStream(f); - InputStreamReader isr = new InputStreamReader( - fis, "ISO-8859-1"); - BufferedReader br = new BufferedReader(isr); - line = br.readLine(); - fis.close(); - } catch (Exception ex) { - // Nothing to do here. - } - if (line != null && line.indexOf("OpenSolaris") >= 0) { - isOpenSolaris = true; + String t2kStr = System.getProperty("sun.java2d.font.scaler"); + if (t2kStr != null) { + useT2K = "t2k".equals(t2kStr); } else { + useT2K = false; + } + if (isSolaris) { + String version = System.getProperty("os.version", "0.0"); + isSolaris8 = version.startsWith("5.8"); + isSolaris9 = version.startsWith("5.9"); + float ver = Float.parseFloat(version); + if (ver > 5.10f) { + File f = new File("/etc/release"); + String line = null; + try { + FileInputStream fis = new FileInputStream(f); + InputStreamReader isr = new InputStreamReader( + fis, "ISO-8859-1"); + BufferedReader br = new BufferedReader(isr); + line = br.readLine(); + fis.close(); + } catch (Exception ex) { + // Nothing to do here. + } + if (line != null && line.indexOf("OpenSolaris") >= 0) { + isOpenSolaris = true; + } else { + isOpenSolaris = false; + } + } else { + isOpenSolaris = false; + } + } else { + isSolaris8 = false; + isSolaris9 = false; isOpenSolaris = false; } - } else { - isOpenSolaris= false; + isWindows = osName.startsWith("Windows"); + String jreLibDirName = System.getProperty("java.home", "") + + File.separator + "lib"; + String jreFontDirName = + jreLibDirName + File.separator + "fonts"; + File lucidaFile = new File(jreFontDirName + File.separator + + LUCIDA_FILE_NAME); + isOpenJDK = !lucidaFile.exists(); + return null; } - } else { - isSolaris8 = false; - isSolaris9 = false; - isOpenSolaris = false; - } - isWindows = osName.startsWith("Windows"); - String jreLibDirName = AccessController.doPrivileged( - new GetPropertyAction("java.home","")) + File.separator + "lib"; - String jreFontDirName = jreLibDirName + File.separator + "fonts"; - File lucidaFile = - new File(jreFontDirName + File.separator + LUCIDA_FILE_NAME); - isOpenJDK = !lucidaFile.exists(); + }); } /** diff --git a/jdk/test/java/awt/FontClass/FontPrivilege.java b/jdk/test/java/awt/FontClass/FontPrivilege.java new file mode 100644 index 00000000000..c0442874509 --- /dev/null +++ b/jdk/test/java/awt/FontClass/FontPrivilege.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2010 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +/* + * @test + * @bug 5010310 6319835 6904882 + * @summary test fonts can be created in the presence of a security manager + * @run main/othervm/secure=java.lang.SecurityManager FontPrivilege + */ + +import java.awt.Font; + +public class FontPrivilege { + + public static void main(String[] args) throws Exception { + new Font("Helvetica", Font.PLAIN, 12).getFamily(); + new Font("foo bar", Font.PLAIN, 12).getFamily(); + } +}