1 /* 2 * Copyright (c) 2007, 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 /** 25 * @test 26 * @bug 4151151 27 * @summary Confirm that low-level print code does doPrivilege. 28 * @author Graham Hamilton 29 */ 30 31 import java.awt.print.*; 32 33 public class CheckPrivilege implements Printable { 34 35 static boolean verbose; 36 37 private static void println(String mess) { 38 if (verbose) { 39 System.err.println(mess); 40 } 41 } 42 43 /** 44 * SecurityManager that allows print requests, but 45 * causes things like "exec" to get checked. 46 */ 47 static class PrintLover extends SecurityManager { 48 public void checkPrintJobAccess() { 49 } 50 public void checkPackageAccess(String pkg) { 51 } 52 public void checkPropertyAccess(String key) { 53 } 54 } 55 56 /** 57 * Internal exception to boucne us out of the print code 58 */ 59 class Printing extends RuntimeException { 60 } 61 62 public static void main(String argv[]) { 63 64 System.out.println( "-----------------------------------------------------------------------"); 65 System.out.println( "INSTRUCTIONS: You should have a printer configured in your system to do this test. Test fails if you get this error message:"); 66 System.out.println(" \"Regression: printing causes a NullPointerException\""); 67 System.out.println( "-----------------------------------------------------------------------"); 68 69 if (argv.length > 0 && argv[0].equals("-v")) { 70 verbose = true; 71 } 72 73 // We need to make sure AWT is initialized. This is bug #4162674 74 java.awt.Toolkit.getDefaultToolkit(); 75 76 // Try to install our own security manager. 77 try { 78 SecurityManager sm = new PrintLover(); 79 println("Installing PrintLover security manager"); 80 System.setSecurityManager(sm); 81 println("Installed security manager OK"); 82 83 } catch (Throwable th) { 84 System.err.println("Failed to install SecurityManager"); 85 th.printStackTrace(); 86 throw new RuntimeException("Failed to install SecurityManager"); 87 } 88 89 try { 90 println("calling getPrinterJob"); 91 PrinterJob pj = PrinterJob.getPrinterJob(); 92 if ((pj == null) || (pj.getPrintService() == null)){ 93 return; 94 } 95 96 println("PrinterJob class is " + pj.getClass()); 97 println("calling pj.setPrintable"); 98 pj.setPrintable(new CheckPrivilege()); 99 println("calling pj.print"); 100 pj.print(); 101 println("done pj.print"); 102 103 } catch (Printing ex) { 104 // We get here if the print request started OK. 105 println("Caught \"Printing\" exception OK"); 106 107 } catch (PrinterException ex) { 108 System.err.println("Caught " + ex); 109 throw new RuntimeException("" + ex); 110 111 } catch (NullPointerException ex) { 112 // This is the bug: 113 System.err.println("Caught " + ex); 114 System.err.println("Regression: printing causes a NullPointerException"); 115 throw ex; 116 } 117 118 //System.exit(0); 119 120 } 121 122 // Back-call from the new print APIs. 123 // We always say we have bothing to print. 124 public int print(java.awt.Graphics g, PageFormat pf, int index) { 125 println("Started printing " + index); 126 return Printable.NO_SUCH_PAGE; 127 } 128 129 130 }