1 /*
   2  * Copyright (c) 1998, 2017, 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 /* @test
  25  * @bug 4165666 4203706 4288670 4290024
  26  * @summary Basic heartbeat test for File methods that access the filesystem
  27  * @build Basic Util
  28  * @run main/othervm Basic
  29  */
  30 
  31 import java.io.FileOutputStream;
  32 import java.io.IOException;
  33 import java.io.File;
  34 import java.io.PrintStream;
  35 import java.io.RandomAccessFile;
  36 
  37 
  38 public class Basic {
  39 
  40     static PrintStream out = System.err;
  41 
  42     static File rwFile = new File("x.Basic.rw");
  43     static File bigFile = new File("x.Basic.big");
  44     static File roFile = new File("x.Basic.ro");
  45     static File thisDir = new File(".");
  46     static File dir = new File("x.Basic.dir");
  47     static File dir2 = new File("x.Basic.dir2");
  48     static File nonDir = new File("x.Basic.nonDir");
  49     static byte bytes[] = new byte[] {1, 2, 3, 4, 5, 6};
  50 
  51     static void showBoolean(String what, boolean value) {
  52         out.println("  " + what + ": " + value);
  53     }
  54 
  55     static void showLong(String what, long value) {
  56         out.println("  " + what + ": " + value);
  57     }
  58 
  59     static void show(File f) throws Exception {
  60         out.println(f + ": ");
  61         showBoolean("exists", f.exists());
  62         showBoolean("isFile", f.isFile());
  63         showBoolean("isDirectory", f.isDirectory());
  64         showBoolean("canRead", f.canRead());
  65         showBoolean("canWrite", f.canWrite());
  66         showLong("lastModified", f.lastModified());
  67         showLong("length", f.length());
  68     }
  69 
  70     static void testFile(File f, boolean writeable, long length)
  71         throws Exception
  72     {
  73         if (!f.exists()) fail(f, "does not exist");
  74         if (!f.isFile()) fail(f, "is not a file");
  75         if (f.isDirectory()) fail(f, "is a directory");
  76         if (!f.canRead()) fail(f, "is not readable");
  77         if (!Util.isPrivileged() && f.canWrite() != writeable)
  78             fail(f, writeable ? "is not writeable" : "is writeable");
  79         if (f.length() != length) fail(f, "has wrong length");
  80     }
  81 
  82     static void fail(File f, String why) throws Exception {
  83         throw new Exception(f + " " + why);
  84     }
  85 
  86     static void setup() throws Exception {
  87         rwFile.delete();
  88         bigFile.delete();
  89         roFile.delete();
  90         thisDir.delete();
  91         dir.delete();
  92         dir2.delete();
  93         nonDir.delete();
  94 
  95         try (FileOutputStream fos = new FileOutputStream(rwFile)) {
  96             fos.write(bytes);
  97         }
  98 
  99         roFile.createNewFile();
 100         roFile.setReadOnly();
 101 
 102         dir.mkdir();
 103     }
 104 
 105     public static void main(String[] args) throws Exception {
 106         setup();
 107 
 108         show(rwFile);
 109         testFile(rwFile, true, bytes.length);
 110         rwFile.delete();
 111         if (rwFile.exists()) {
 112             fail(rwFile, "could not delete");
 113         }
 114 
 115         show(roFile);
 116         testFile(roFile, false, 0);
 117 
 118         show(thisDir);
 119         if (!thisDir.exists()) fail(thisDir, "does not exist");
 120         if (thisDir.isFile()) fail(thisDir, "is a file");
 121         if (!thisDir.isDirectory()) fail(thisDir, "is not a directory");
 122         if (!thisDir.canRead()) fail(thisDir, "is readable");
 123         if (!thisDir.canWrite()) fail(thisDir, "is writeable");
 124         String[] fs = thisDir.list();
 125         if (fs == null) fail(thisDir, "list() returned null");
 126         out.print("  [" + fs.length + "]");
 127         for (int i = 0; i < fs.length; i++) {
 128             out.print(" " + fs[i]);
 129         }
 130         out.println();
 131         if (fs.length == 0) fail(thisDir, "is empty");
 132 
 133         if (!nonDir.mkdir()) {
 134             fail(nonDir, "could not create");
 135         }
 136         if (!nonDir.exists() || !nonDir.isDirectory()) {
 137             fail(nonDir, "not created");
 138         }
 139 
 140         if (!dir.renameTo(dir2)) {
 141             fail(dir, "failed to rename");
 142         }
 143         if (dir.exists() || !dir2.exists() || !dir2.isDirectory()) {
 144             fail(dir, "not renamed");
 145         }
 146 
 147         if (System.getProperty("os.name").equals("SunOS")
 148             && System.getProperty("os.version").compareTo("5.6") >= 0) {
 149             if (bigFile.exists()) {
 150                 bigFile.delete();
 151                 if (bigFile.exists())
 152                     fail(bigFile, "could not delete");
 153             }
 154             RandomAccessFile raf = new RandomAccessFile(bigFile, "rw");
 155             long big = ((long)Integer.MAX_VALUE) * 2;
 156             try {
 157                 raf.seek(big);
 158                 raf.write('x');
 159                 show(bigFile);
 160                 testFile(bigFile, true, big + 1);
 161             } finally {
 162                 raf.close();
 163             }
 164             bigFile.delete();
 165             if (bigFile.exists())
 166                 fail(bigFile, "could not delete");
 167         } else {
 168             System.err.println("NOTE: Large files not supported on this system");
 169         }
 170 
 171     }
 172 
 173 }