1121N/A/*
4628N/A * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
1121N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1121N/A *
1121N/A * This code is free software; you can redistribute it and/or modify it
1121N/A * under the terms of the GNU General Public License version 2 only, as
1121N/A * published by the Free Software Foundation.
1121N/A *
1121N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1121N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1121N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1121N/A * version 2 for more details (a copy is included in the LICENSE file that
1121N/A * accompanied this code).
1121N/A *
1121N/A * You should have received a copy of the GNU General Public License version
1121N/A * 2 along with this work; if not, write to the Free Software Foundation,
1121N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1121N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1121N/A */
1121N/A
1121N/A/*
1121N/A * @test
1121N/A * @bug 6652929
1121N/A * @summary verify handling of File.getPath()
4628N/A * @compile FontFile.java
4628N/A * @run shell TestFontFile.sh
4628N/A */
4628N/A
4628N/A/*
4628N/A * When using jtreg this test needs to be run by shell script,
4628N/A * since otherwise jtreg reflectively invokes the main method
4628N/A * and the codebase for the purposes of the security manager
4628N/A * is that of the jtreg harness, not the codebase (class file location)
4628N/A * of this program, thus access to read to that location is not available.
1121N/A */
1121N/A
1121N/Aimport java.awt.*;
1121N/Aimport java.io.*;
1121N/A
1121N/Apublic class FontFile {
1121N/A public static void main(String[] args) throws Exception {
1121N/A String sep = System.getProperty("file.separator");
1121N/A String fname = ".." + sep + "A.ttf";
4628N/A //String dir = System.getProperty("test.src");
4628N/A String dir = System.getenv("TESTSRC");
1121N/A if (dir != null) {
1121N/A fname = dir + sep + fname;
1121N/A }
4628N/A //String classesDir = System.getProperty("test.classes");
4628N/A String classesDir = System.getenv("TESTCLASSES");
4628N/A System.out.println("classesDir="+classesDir);
4628N/A String testfile = "somefile";
4628N/A if (classesDir != null) {
4628N/A testfile = classesDir + sep + testfile;
4628N/A }
4628N/A final String somefile = testfile;
4628N/A System.out.println("somefile="+somefile);
4628N/A System.out.println("userdir="+System.getProperty("user.dir"));
1121N/A final String name = fname;
1121N/A System.out.println("Will try to access " + name);
1121N/A if (!(new File(name)).canRead()) {
1121N/A System.out.println("File not available : can't run test");
1121N/A return;
1121N/A }
1121N/A System.out.println("File is available. Verify no access under SM");
1121N/A
1121N/A System.setSecurityManager(new SecurityManager());
1121N/A
1121N/A
1121N/A // Check cannot read file.
1121N/A try {
1121N/A new FileInputStream(name);
1121N/A throw new Error("Something wrong with test environment");
1121N/A } catch (SecurityException exc) {
1121N/A // Good.
1121N/A }
1121N/A
1121N/A try {
1121N/A Font font = Font.createFont(Font.TRUETYPE_FONT,
1121N/A new File("nosuchfile") {
1121N/A private boolean read;
1121N/A @Override public String getPath() {
1121N/A if (read) {
1121N/A return name;
1121N/A } else {
1121N/A read = true;
4628N/A return somefile;
1121N/A }
1121N/A }
1121N/A @Override public boolean canRead() {
1121N/A return true;
1121N/A }
1121N/A }
1121N/A );
1121N/A System.err.println(font.getFontName());
1121N/A throw new RuntimeException("No expected exception");
1121N/A } catch (IOException e) {
1121N/A System.err.println("Test passed.");
1121N/A }
1121N/A }
1121N/A}