1 /*
   2  * Copyright (c) 2008, 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  */
  29 
  30 import java.awt.*;
  31 import java.io.*;
  32 
  33 public class FontFile {
  34     public static void main(String[] args) throws Exception {
  35         String sep = System.getProperty("file.separator");
  36         String fname = ".." + sep + "A.ttf";
  37         String dir = System.getProperty("test.src");
  38         if (dir != null) {
  39             fname = dir + sep + fname;
  40         }
  41         final String name = fname;
  42         System.out.println("Will try to access " + name);
  43         if (!(new File(name)).canRead()) {
  44            System.out.println("File not available : can't run test");
  45            return;
  46         }
  47         System.out.println("File is available. Verify no access under SM");
  48 
  49         System.setSecurityManager(new SecurityManager());
  50 
  51 
  52         // Check cannot read file.
  53         try {
  54             new FileInputStream(name);
  55             throw new Error("Something wrong with test environment");
  56         } catch (SecurityException exc) {
  57             // Good.
  58         }
  59 
  60         try {
  61             Font font = Font.createFont(Font.TRUETYPE_FONT,
  62             new File("nosuchfile") {
  63                     private boolean read;
  64                     @Override public String getPath() {
  65                         if (read) {
  66                             return name;
  67                         } else {
  68                             read = true;
  69                             return "somefile";
  70                         }
  71                     }
  72                     @Override public boolean canRead() {
  73                         return true;
  74                     }
  75                }
  76             );
  77           System.err.println(font.getFontName());
  78           throw new RuntimeException("No expected exception");
  79         }  catch (IOException e) {
  80           System.err.println("Test passed.");
  81         }
  82     }
  83 }