Path: blob/master/test/jdk/java/awt/FontClass/CreateFont/BigFont.java
41153 views
/*1* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Font;24import java.awt.FontFormatException;25import java.io.BufferedInputStream;26import java.io.IOException;27import java.io.InputStream;28import java.net.URL;29import java.nio.file.Paths;3031/**32* @test33* @key headful34* @bug 652258635* @summary Enforce limits on font creation36* @run main/othervm -Djava.security.manager=allow BigFont 1 A.ttf37* @run main/othervm -Djava.security.manager=allow BigFont 2 A.ttf38*/39public class BigFont {4041static private class SizedInputStream extends InputStream {4243int size;44int cnt = 0;4546SizedInputStream(int size) {47this.size = size;48}4950public int read() {51if (cnt < size) {52cnt++;53return 0;54} else {55return -1;56}57}5859public int getCurrentSize() {60return cnt;61}62}6364static String id;65static String fileName;6667public static void main(final String[] args) {68id = args[0];69fileName = args[1];7071System.out.println("Applet " + id + " "+72Thread.currentThread().getThreadGroup());73if (System.getSecurityManager() == null) {74System.setSecurityManager(new SecurityManager());75}76// Larger than size for a single font.77int fontSize = 64 * 1000 * 1000;78SizedInputStream sis = new SizedInputStream(fontSize);79try {80Font font = Font.createFont(Font.TRUETYPE_FONT, sis);81} catch (Throwable t) {82t.printStackTrace();83if (t instanceof FontFormatException ||84fontSize <= sis.getCurrentSize())85{86System.out.println(sis.getCurrentSize());87System.out.println(t);88throw new RuntimeException("Allowed file to be too large.");89}90}91// The following part of the test was verified manually but92// is impractical to enable because it requires a fairly large93// valid font to be part of the test, and we can't easily include94// that, nor dependably reference one from the applet environment.95/*96if (fileName == null) {97return;98}99int size = getFileSize(fileName);100if (size == 0) {101return;102}103int fontCnt = 1000 * 1000 * 1000 / size;104loadMany(size, fontCnt, fileName);105System.gc(); System.gc();106fontCnt = fontCnt / 2;107System.out.println("Applet " + id + " load more.");108loadMany(size, fontCnt, fileName);109*/110System.out.println("Applet " + id + " finished.");111}112113int getFileSize(String fileName) {114try {115String path = Paths.get(System.getProperty("test.src", "."),116fileName).toAbsolutePath().normalize()117.toString();118URL url = new URL(path);119InputStream inStream = url.openStream();120BufferedInputStream fontStream = new BufferedInputStream(inStream);121int size = 0;122while (fontStream.read() != -1) {123size++;124}125fontStream.close();126return size;127} catch (IOException e) {128return 0;129}130131}132void loadMany(int oneFont, int fontCnt, String fileName) {133System.out.println("fontcnt= " + fontCnt);134Font[] fonts = new Font[fontCnt];135int totalSize = 0;136boolean gotException = false;137for (int i=0; i<fontCnt; i++) {138try {139String path = Paths.get(System.getProperty("test.src", "."),140fileName).toAbsolutePath().normalize()141.toString();142URL url = new URL(path);143InputStream inStream = url.openStream();144BufferedInputStream fontStream =145new BufferedInputStream(inStream);146fonts[i] = Font.createFont(Font.TRUETYPE_FONT, fontStream);147totalSize += oneFont;148fontStream.close();149} catch (Throwable t) {150gotException = true;151System.out.println("Applet " + id + " " + t);152}153}154if (!gotException) {155throw new RuntimeException("No expected exception");156}157}158}159160161162