test/jdk/java/io/File/GetXSpace.java
author tschatzl
Wed, 24 Jul 2019 11:49:39 +0200
changeset 57508 28ab01c06755
parent 47216 71c04702a3d5
permissions -rw-r--r--
8228388: Add information about dirty/skipped card for Merge HCC in G1 log Summary: Collect and print informatio about the number of processed cards during the Merge HCC phase to improve log output. Reviewed-by: kbarrett, sangheki
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12538
211d6e82fe51 7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6
jmelvin
parents: 12047
diff changeset
     2
 * Copyright (c) 2005, 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: 2401
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2401
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2401
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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4057701 6286712 6364377
2401
96065ac71318 6824012: Add jdk regression tests to default jprt jobs
ohair
parents: 2
diff changeset
    27
 * @ignore until 6492634 and 6501010 is fixed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @run build GetXSpace
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run shell GetXSpace.sh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @summary Basic functionality of File.get-X-Space methods.
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 12538
diff changeset
    31
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.FilePermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.regex.Matcher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.regex.Pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import static java.lang.System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class GetXSpace {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static SecurityManager [] sma = { null, new Allow(), new DenyFSA(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                                              new DenyRead() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final String name = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final String dfFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    54
        if (name.equals("SunOS") || name.equals("Linux")
12538
211d6e82fe51 7130404: [macosx] "os.arch" value should be "x86_64" for compatibility with Apple JDK6
jmelvin
parents: 12047
diff changeset
    55
                || name.contains("OS X")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            // FileSystem Total Used Available Use% MountedOn
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            dfFormat = "([^\\s]+)\\s+(\\d+)\\s+\\d+\\s+(\\d+)\\s+\\d+%\\s+([^\\s]+)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        } else if (name.startsWith("Windows")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            // Drive (MountedOn) Available/Total
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            dfFormat = "([^\\s]+)\\s+\\(([^\\s]+)\\)\\s+(\\d+)\\/(\\d+)\\s+";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            throw new RuntimeException("unrecognized system:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                       + " os.name == " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static Pattern dfPattern = Pattern.compile(dfFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static int fail = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static int pass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static Throwable first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static void pass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        pass++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static void fail(String p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (first == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            setFirst(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        System.err.format("FAILED: %s%n", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        fail++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static void fail(String p, long exp, String cmp, long got) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        String s = String.format("'%s': %d %s %d", p, exp, cmp, got);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (first == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            setFirst(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        System.err.format("FAILED: %s%n", s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        fail++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static void fail(String p, Class ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        String s = String.format("'%s': expected %s - FAILED%n", p, ex.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (first == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            setFirst(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        System.err.format("FAILED: %s%n", s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        fail++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static void setFirst(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new RuntimeException(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } catch (RuntimeException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            first = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static class Space {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        private static final long KSIZE = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        private long total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        private long free;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        Space(String total, String free, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                this.total = Long.valueOf(total) * KSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                this.free = Long.valueOf(free) * KSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                // the regex should have caught this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        String name() { return name; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        long total() { return total; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        long free() { return free; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        boolean woomFree(long freeSpace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return ((freeSpace >= (free / 10)) && (freeSpace <= (free * 10)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return String.format("%s (%d/%d)", name, free, total);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static ArrayList space(String f) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        ArrayList al = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        Process p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        String cmd = "df -k" + (f == null ? "" : " " + f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        p = Runtime.getRuntime().exec(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        BufferedReader in = new BufferedReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            (new InputStreamReader(p.getInputStream()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        String s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        while ((s = in.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            // skip header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (i++ == 0 && !name.startsWith("Windows")) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            sb.append(s).append("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        Matcher m = dfPattern.matcher(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        while (j < sb.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (m.find(j)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (!name.startsWith("Windows")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    // swap can change while this test is running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    if (!m.group(1).equals("swap")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        String name = (f == null ? m.group(4): f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        al.add(new Space(m.group(2), m.group(3), name));;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    String name = (f == null ? m.group(2) : f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    al.add(new Space(m.group(4), m.group(3), name ));;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                j = m.end() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                throw new RuntimeException("unrecognized df output format: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                           + "charAt(" + j + ") = '"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                           + sb.charAt(j) + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (al.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // df did not produce output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            String name = (f == null ? "" : f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            al.add(new Space("0", "0", name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return al;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private static void tryCatch(Space s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        out.format("%s:%n", s.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        File f = new File(s.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (sm instanceof Deny) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            String fmt = "  %14s: \"%s\" thrown as expected%n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                f.getTotalSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                fail(s.name(), SecurityException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } catch (SecurityException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                out.format(fmt, "getTotalSpace", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                f.getFreeSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                fail(s.name(), SecurityException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            } catch (SecurityException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                out.format(fmt, "getFreeSpace", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                f.getUsableSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                fail(s.name(), SecurityException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            } catch (SecurityException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                out.format(fmt, "getUsableSpace", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private static void compare(Space s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        File f = new File(s.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        long ts = f.getTotalSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        long fs = f.getFreeSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        long us = f.getUsableSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        out.format("%s:%n", s.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        String fmt = "  %-4s total= %12d free = %12d usable = %12d%n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        out.format(fmt, "df", s.total(), 0, s.free());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        out.format(fmt, "getX", ts, fs, us);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // if the file system can dynamically change size, this check will fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (ts != s.total())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            fail(s.name(), s.total(), "!=", ts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        // unix df returns statvfs.f_bavail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        long tsp = (!name.startsWith("Windows") ? us : fs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (!s.woomFree(tsp))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            fail(s.name(), s.free(), "??", tsp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (fs > s.total())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            fail(s.name(), s.total(), ">", fs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (us > s.total())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            fail(s.name(), s.total(), ">", us);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private static String FILE_PREFIX = "/getSpace.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    private static void compareZeroNonExist() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        File f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            f = new File(FILE_PREFIX + Math.random());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (f.exists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        long [] s = { f.getTotalSpace(), f.getFreeSpace(), f.getUsableSpace() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        for (int i = 0; i < s.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (s[i] != 0L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                fail(f.getName(), s[i], "!=", 0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    private static void compareZeroExist() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            File f = File.createTempFile("tmp", null, new File("."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            long [] s = { f.getTotalSpace(), f.getFreeSpace(), f.getUsableSpace() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            for (int i = 0; i < s.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                if (s[i] == 0L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    fail(f.getName(), s[i], "==", 0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            fail("Couldn't create temp file for test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    private static class Allow extends SecurityManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        public void checkRead(String file) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        public void checkPermission(Permission p) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        public void checkPermission(Permission p, Object context) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    private static class Deny extends SecurityManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        public void checkPermission(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            if (p.implies(new RuntimePermission("setSecurityManager"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                || p.implies(new RuntimePermission("getProtectionDomain")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
              return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            super.checkPermission(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        public void checkPermission(Permission p, Object context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (p.implies(new RuntimePermission("setSecurityManager"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                || p.implies(new RuntimePermission("getProtectionDomain")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
              return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            super.checkPermission(p, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    private static class DenyFSA extends Deny {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        private String err = "sorry - getFileSystemAttributes";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        public void checkPermission(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (p.implies(new RuntimePermission("getFileSystemAttributes")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                throw new SecurityException(err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            super.checkPermission(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        public void checkPermission(Permission p, Object context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (p.implies(new RuntimePermission("getFileSystemAttributes")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                throw new SecurityException(err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            super.checkPermission(p, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private static class DenyRead extends Deny {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        private String err = "sorry - checkRead()";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        public void checkRead(String file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            throw new SecurityException(err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private static void testFile(String dirName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        out.format("--- Testing %s%n", dirName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        ArrayList l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            l = space(dirName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            throw new RuntimeException(dirName + " can't get file system information", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        compare((GetXSpace.Space) l.get(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private static void testDF() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        out.format("--- Testing df");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        // Find all of the partitions on the machine and verify that the size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        // returned by "df" is equivalent to File.getXSpace() values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        ArrayList l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            l = space(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            throw new RuntimeException("can't get file system information", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (l.size() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            throw new RuntimeException("no partitions?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        for (int i = 0; i < sma.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            System.setSecurityManager(sma[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            if (sma[i] != null && sm == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                throw new RuntimeException("Test configuration error "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                           + " - can't set security manager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            out.format("%nSecurityManager = %s%n" ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                       (sm == null ? "null" : sm.getClass().getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            for (int j = 0; j < l.size(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                Space s = (GetXSpace.Space) l.get(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                if (sm instanceof Deny) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    tryCatch(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    compare(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    compareZeroNonExist();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    compareZeroExist();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public static void main(String [] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            testFile(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            testDF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (fail != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            throw new RuntimeException((fail + pass) + " tests: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                       + fail + " failure(s), first", first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            out.format("all %d tests passed%n", fail + pass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
}