jdk/test/java/io/File/Basic.java
author robm
Wed, 15 Aug 2012 22:46:35 +0100
changeset 13568 ce5ab758aeb5
parent 7668 d4a77089c587
child 45574 04189244d1b2
permissions -rw-r--r--
6931128: (spec) File attribute tests fail when run as root. Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
13568
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 7668
diff changeset
     2
 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4655
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4655
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4655
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
   @bug 4165666 4203706 4288670 4290024
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
   @summary Basic heartbeat test for File methods that access the filesystem
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
13568
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 7668
diff changeset
    28
   @build Basic Util
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
   @run shell basic.sh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.RandomAccessFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class Basic {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static PrintStream out = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static File nonExistantFile = new File("x.Basic.non");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static File rwFile = new File("x.Basic.rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static File bigFile = new File("x.Basic.big");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static File roFile = new File("x.Basic.ro");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static File thisDir = new File(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static File dir = new File("x.Basic.dir");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static File nonDir = new File("x.Basic.nonDir");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static void showBoolean(String what, boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        out.println("  " + what + ": " + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static void showLong(String what, long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        out.println("  " + what + ": " + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static void show(File f) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        out.println(f + ": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        showBoolean("exists", f.exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        showBoolean("isFile", f.isFile());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        showBoolean("isDirectory", f.isDirectory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        showBoolean("canRead", f.canRead());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        showBoolean("canWrite", f.canWrite());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        showLong("lastModified", f.lastModified());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        showLong("length", f.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static void testFile(File f, boolean writeable, long length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (!f.exists()) fail(f, "does not exist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (!f.isFile()) fail(f, "is not a file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (f.isDirectory()) fail(f, "is a directory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (!f.canRead()) fail(f, "is not readable");
13568
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 7668
diff changeset
    76
        if (!Util.isPrivileged() && f.canWrite() != writeable)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            fail(f, writeable ? "is not writeable" : "is writeable");
4655
5dc740d03407 6910834: TEST: java/io/File/Basic.java fails on Windows CYGWIN environment
ohair
parents: 2
diff changeset
    78
        int rwLen = 6;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (f.length() != length) fail(f, "has wrong length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static void fail(File f, String why) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        throw new Exception(f + " " + why);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        show(nonExistantFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (nonExistantFile.exists()) fail(nonExistantFile, "exists");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        show(rwFile);
4655
5dc740d03407 6910834: TEST: java/io/File/Basic.java fails on Windows CYGWIN environment
ohair
parents: 2
diff changeset
    92
        testFile(rwFile, true, 6);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        rwFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (rwFile.exists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            fail(rwFile, "could not delete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        show(roFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        testFile(roFile, false, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        show(thisDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (!thisDir.exists()) fail(thisDir, "does not exist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (thisDir.isFile()) fail(thisDir, "is a file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (!thisDir.isDirectory()) fail(thisDir, "is not a directory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (!thisDir.canRead()) fail(thisDir, "is readable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (!thisDir.canWrite()) fail(thisDir, "is writeable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        String[] fs = thisDir.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (fs == null) fail(thisDir, "list() returned null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        out.print("  [" + fs.length + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        for (int i = 0; i < fs.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            out.print(" " + fs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (fs.length == 0) fail(thisDir, "is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (!nonExistantFile.createNewFile())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            fail(nonExistantFile, "could not create");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        nonExistantFile.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (!nonDir.mkdir())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            fail(nonDir, "could not create");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (!dir.renameTo(new File("x.Basic.dir2")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            fail(dir, "failed to rename");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (System.getProperty("os.name").equals("SunOS")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            && System.getProperty("os.version").compareTo("5.6") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (bigFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                bigFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                if (bigFile.exists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    fail(bigFile, "could not delete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            RandomAccessFile raf = new RandomAccessFile(bigFile, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            long big = ((long)Integer.MAX_VALUE) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                raf.seek(big);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                raf.write('x');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                show(bigFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                testFile(bigFile, true, big + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                raf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            bigFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (bigFile.exists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                fail(bigFile, "could not delete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            System.err.println("NOTE: Large files not supported on this system");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
}