1 /*
   2  * Copyright (c) 2008, 2015, 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 6652929
  27  * @summary verify handling of File.getPath()
  28  * @compile FontFile.java
  29  * @run shell TestFontFile.sh
  30  */
  31 
  32 /*
  33  * When using jtreg this test needs to be run by shell script,
  34  * since otherwise jtreg reflectively invokes the main method
  35  * and the codebase for the purposes of the security manager
  36  * is that of the jtreg harness, not the codebase (class file location)
  37  * of this program, thus access to read to that location is not available.
  38  */
  39 
  40 import java.awt.*;
  41 import java.io.*;
  42 
  43 public class FontFile {
  44     public static void main(String[] args) throws Exception {
  45         String sep = System.getProperty("file.separator");
  46         String fname = ".." + sep + "A.ttf";
  47         //String dir = System.getProperty("test.src");
  48         String dir = System.getenv("TESTSRC");
  49         if (dir != null) {
  50             fname = dir + sep + fname;
  51         }
  52         //String classesDir = System.getProperty("test.classes");
  53         String classesDir = System.getenv("TESTCLASSES");
  54         System.out.println("classesDir="+classesDir);
  55         String testfile = "somefile";
  56         if (classesDir != null) {
  57             testfile = classesDir + sep + testfile;
  58         }
  59         final String somefile = testfile;
  60         System.out.println("somefile="+somefile);
  61         System.out.println("userdir="+System.getProperty("user.dir"));
  62         final String name = fname;
  63         System.out.println("Will try to access " + name);
  64         if (!(new File(name)).canRead()) {
  65            System.out.println("File not available : can't run test");
  66            return;
  67         }
  68         System.out.println("File is available. Verify no access under SM");
  69 
  70         System.setSecurityManager(new SecurityManager());
  71 
  72 
  73         // Check cannot read file.
  74         try {
  75             new FileInputStream(name);
  76             throw new Error("Something wrong with test environment");
  77         } catch (SecurityException exc) {
  78             // Good.
  79         }
  80 
  81         try {
  82             Font font = Font.createFont(Font.TRUETYPE_FONT,
  83             new File("nosuchfile") {
  84                     private boolean read;
  85                     @Override public String getPath() {
  86                         if (read) {
  87                             return name;
  88                         } else {
  89                             read = true;
  90                             return somefile;
  91                         }
  92                     }
  93                     @Override public boolean canRead() {
  94                         return true;
  95                     }
  96                }
  97             );
  98           System.err.println(font.getFontName());
  99           throw new RuntimeException("No expected exception");
 100         }  catch (IOException e) {
 101           System.err.println("Test passed.");
 102         }
 103     }
 104 }