test/java/awt/FontClass/CreateFont/fileaccess/FontFile.java

Print this page


   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 }
   1 /*
   2  * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
   4  *



   5  *





   6  *



   7  *
   8  *
   9  *
  10  *
  11  *
  12  *
  13  *
  14  *
  15  *
  16  *
  17  *
  18  *
  19  *
  20  *
  21  *
  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 }