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