1 /* 2 * Copyright (c) 2008, 2012 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 6522586 6868690 27 @summary Enforce limits on font creation 28 @run main BigFont 29 */ 30 import java.applet.*; 31 import java.awt.*; 32 import java.io.*; 33 import java.net.*; 34 35 public class BigFont extends Applet { 36 37 public static void main(String args[]) { 38 System.setSecurityManager(new SecurityManager()); 39 (new BigFont()).runTest1(); 40 } 41 42 static private class SizedInputStream extends InputStream { 43 44 int size; 45 int cnt = 0; 46 47 SizedInputStream(int size) { 48 this.size = size; 49 } 50 51 public int read() { 52 if (cnt < size) { 53 cnt++; 54 return 0; 55 } else { 56 return -1; 57 } 58 } 59 60 public int getCurrentSize() { 61 return cnt; 62 } 63 } 64 65 String id; 66 String fileName; 67 68 public void init() { 69 id = getParameter("number"); 70 fileName = getParameter("font"); 71 72 System.out.println("Applet " + id + " "+ 73 Thread.currentThread().getThreadGroup()); 74 runTest1(); 75 runTest2(); 76 } 77 void runTest1() { 78 // Larger than size for a single font. 79 int fontSize = 64 * 1000 * 1000; 80 SizedInputStream sis = new SizedInputStream(fontSize); 81 try { 82 Font font = Font.createFont(Font.TRUETYPE_FONT, sis); 83 } catch (Throwable t) { 84 if (t instanceof FontFormatException || 85 fontSize <= sis.getCurrentSize()) 86 { 87 System.out.println(sis.getCurrentSize()); 88 System.out.println(t); 89 throw new RuntimeException("Allowed file to be too large."); 90 } 91 } 92 } 93 void runTest2() { 94 // The following part of the test was verified manually but 95 // is impractical to enable because it requires a fairly large 96 // valid font to be part of the test, and we can't easily include 97 // that, nor dependably reference one from the applet environment. 98 /* 99 if (fileName == null) { 100 return; 101 } 102 int size = getFileSize(fileName); 103 if (size == 0) { 104 return; 105 } 106 int fontCnt = 1000 * 1000 * 1000 / size; 107 loadMany(size, fontCnt, fileName); 108 System.gc(); System.gc(); 109 fontCnt = fontCnt / 2; 110 System.out.println("Applet " + id + " load more."); 111 loadMany(size, fontCnt, fileName); 112 */ 113 System.out.println("Applet " + id + " finished."); 114 } 115 116 int getFileSize(String fileName) { 117 try { 118 URL url = new URL(getCodeBase(), fileName); 119 InputStream inStream = url.openStream(); 120 BufferedInputStream fontStream = new BufferedInputStream(inStream); 121 int size = 0; 122 while (fontStream.read() != -1) { 123 size++; 124 } 125 fontStream.close(); 126 return size; 127 } catch (IOException e) { 128 return 0; 129 } 130 131 } 132 void loadMany(int oneFont, int fontCnt, String fileName) { 133 System.out.println("fontcnt= " + fontCnt); 134 Font[] fonts = new Font[fontCnt]; 135 int totalSize = 0; 136 boolean gotException = false; 137 for (int i=0; i<fontCnt; i++) { 138 try { 139 URL url = new URL(getCodeBase(), fileName); 140 InputStream inStream = url.openStream(); 141 BufferedInputStream fontStream = 142 new BufferedInputStream(inStream); 143 fonts[i] = Font.createFont(Font.TRUETYPE_FONT, fontStream); 144 totalSize += oneFont; 145 fontStream.close(); 146 } catch (Throwable t) { 147 gotException = true; 148 System.out.println("Applet " + id + " " + t); 149 } 150 } 151 if (!gotException) { 152 throw new RuntimeException("No expected exception"); 153 } 154 } 155 } 156