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