0N/A/*
4944N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4057701 6286712 6364377
1009N/A * @ignore until 6492634 and 6501010 is fixed
0N/A * @run build GetXSpace
0N/A * @run shell GetXSpace.sh
0N/A * @summary Basic functionality of File.get-X-Space methods.
0N/A */
0N/A
0N/Aimport java.io.BufferedReader;
0N/Aimport java.io.File;
0N/Aimport java.io.FilePermission;
0N/Aimport java.io.InputStreamReader;
0N/Aimport java.io.IOException;
0N/Aimport java.security.Permission;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.regex.Matcher;
0N/Aimport java.util.regex.Pattern;
0N/A
0N/Aimport static java.lang.System.out;
0N/A
0N/Apublic class GetXSpace {
0N/A
0N/A private static SecurityManager [] sma = { null, new Allow(), new DenyFSA(),
0N/A new DenyRead() };
0N/A
0N/A private static final String name = System.getProperty("os.name");
0N/A private static final String dfFormat;
0N/A static {
4652N/A if (name.equals("SunOS") || name.equals("Linux")
4944N/A || name.contains("OS X")) {
0N/A // FileSystem Total Used Available Use% MountedOn
0N/A dfFormat = "([^\\s]+)\\s+(\\d+)\\s+\\d+\\s+(\\d+)\\s+\\d+%\\s+([^\\s]+)";
0N/A } else if (name.startsWith("Windows")) {
0N/A // Drive (MountedOn) Available/Total
0N/A dfFormat = "([^\\s]+)\\s+\\(([^\\s]+)\\)\\s+(\\d+)\\/(\\d+)\\s+";
0N/A } else {
0N/A throw new RuntimeException("unrecognized system:"
0N/A + " os.name == " + name);
0N/A }
0N/A }
0N/A private static Pattern dfPattern = Pattern.compile(dfFormat);
0N/A
0N/A private static int fail = 0;
0N/A private static int pass = 0;
0N/A private static Throwable first;
0N/A
0N/A static void pass() {
0N/A pass++;
0N/A }
0N/A
0N/A static void fail(String p) {
0N/A if (first == null)
0N/A setFirst(p);
0N/A System.err.format("FAILED: %s%n", p);
0N/A fail++;
0N/A }
0N/A
0N/A static void fail(String p, long exp, String cmp, long got) {
0N/A String s = String.format("'%s': %d %s %d", p, exp, cmp, got);
0N/A if (first == null)
0N/A setFirst(s);
0N/A System.err.format("FAILED: %s%n", s);
0N/A fail++;
0N/A }
0N/A
0N/A private static void fail(String p, Class ex) {
0N/A String s = String.format("'%s': expected %s - FAILED%n", p, ex.getName());
0N/A if (first == null)
0N/A setFirst(s);
0N/A System.err.format("FAILED: %s%n", s);
0N/A fail++;
0N/A }
0N/A
0N/A private static void setFirst(String s) {
0N/A try {
0N/A throw new RuntimeException(s);
0N/A } catch (RuntimeException x) {
0N/A first = x;
0N/A }
0N/A }
0N/A
0N/A private static class Space {
0N/A private static final long KSIZE = 1024;
0N/A private String name;
0N/A private long total;
0N/A private long free;
0N/A
0N/A Space(String total, String free, String name) {
0N/A try {
0N/A this.total = Long.valueOf(total) * KSIZE;
0N/A this.free = Long.valueOf(free) * KSIZE;
0N/A } catch (NumberFormatException x) {
0N/A // the regex should have caught this
0N/A assert false;
0N/A }
0N/A this.name = name;
0N/A }
0N/A
0N/A String name() { return name; }
0N/A long total() { return total; }
0N/A long free() { return free; }
0N/A boolean woomFree(long freeSpace) {
0N/A return ((freeSpace >= (free / 10)) && (freeSpace <= (free * 10)));
0N/A }
0N/A public String toString() {
0N/A return String.format("%s (%d/%d)", name, free, total);
0N/A }
0N/A }
0N/A
0N/A private static ArrayList space(String f) throws IOException {
0N/A ArrayList al = new ArrayList();
0N/A
0N/A Process p = null;
0N/A String cmd = "df -k" + (f == null ? "" : " " + f);
0N/A p = Runtime.getRuntime().exec(cmd);
0N/A BufferedReader in = new BufferedReader
0N/A (new InputStreamReader(p.getInputStream()));
0N/A String s;
0N/A int i = 0;
0N/A StringBuilder sb = new StringBuilder();
0N/A while ((s = in.readLine()) != null) {
0N/A // skip header
0N/A if (i++ == 0 && !name.startsWith("Windows")) continue;
0N/A sb.append(s).append("\n");
0N/A }
0N/A
0N/A Matcher m = dfPattern.matcher(sb);
0N/A int j = 0;
0N/A while (j < sb.length()) {
0N/A if (m.find(j)) {
0N/A if (!name.startsWith("Windows")) {
0N/A // swap can change while this test is running
0N/A if (!m.group(1).equals("swap")) {
0N/A String name = (f == null ? m.group(4): f);
0N/A al.add(new Space(m.group(2), m.group(3), name));;
0N/A }
0N/A } else {
0N/A String name = (f == null ? m.group(2) : f);
0N/A al.add(new Space(m.group(4), m.group(3), name ));;
0N/A }
0N/A j = m.end() + 1;
0N/A } else {
0N/A throw new RuntimeException("unrecognized df output format: "
0N/A + "charAt(" + j + ") = '"
0N/A + sb.charAt(j) + "'");
0N/A }
0N/A }
0N/A
0N/A if (al.size() == 0) {
0N/A // df did not produce output
0N/A String name = (f == null ? "" : f);
0N/A al.add(new Space("0", "0", name));
0N/A }
0N/A in.close();
0N/A return al;
0N/A }
0N/A
0N/A private static void tryCatch(Space s) {
0N/A out.format("%s:%n", s.name());
0N/A File f = new File(s.name());
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm instanceof Deny) {
0N/A String fmt = " %14s: \"%s\" thrown as expected%n";
0N/A try {
0N/A f.getTotalSpace();
0N/A fail(s.name(), SecurityException.class);
0N/A } catch (SecurityException x) {
0N/A out.format(fmt, "getTotalSpace", x);
0N/A pass();
0N/A }
0N/A try {
0N/A f.getFreeSpace();
0N/A fail(s.name(), SecurityException.class);
0N/A } catch (SecurityException x) {
0N/A out.format(fmt, "getFreeSpace", x);
0N/A pass();
0N/A }
0N/A try {
0N/A f.getUsableSpace();
0N/A fail(s.name(), SecurityException.class);
0N/A } catch (SecurityException x) {
0N/A out.format(fmt, "getUsableSpace", x);
0N/A pass();
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static void compare(Space s) {
0N/A File f = new File(s.name());
0N/A long ts = f.getTotalSpace();
0N/A long fs = f.getFreeSpace();
0N/A long us = f.getUsableSpace();
0N/A
0N/A out.format("%s:%n", s.name());
0N/A String fmt = " %-4s total= %12d free = %12d usable = %12d%n";
0N/A out.format(fmt, "df", s.total(), 0, s.free());
0N/A out.format(fmt, "getX", ts, fs, us);
0N/A
0N/A // if the file system can dynamically change size, this check will fail
0N/A if (ts != s.total())
0N/A fail(s.name(), s.total(), "!=", ts);
0N/A else
0N/A pass();
0N/A
0N/A // unix df returns statvfs.f_bavail
0N/A long tsp = (!name.startsWith("Windows") ? us : fs);
0N/A if (!s.woomFree(tsp))
0N/A fail(s.name(), s.free(), "??", tsp);
0N/A else
0N/A pass();
0N/A
0N/A if (fs > s.total())
0N/A fail(s.name(), s.total(), ">", fs);
0N/A else
0N/A pass();
0N/A
0N/A if (us > s.total())
0N/A fail(s.name(), s.total(), ">", us);
0N/A else
0N/A pass();
0N/A }
0N/A
0N/A private static String FILE_PREFIX = "/getSpace.";
0N/A private static void compareZeroNonExist() {
0N/A File f;
0N/A while (true) {
0N/A f = new File(FILE_PREFIX + Math.random());
0N/A if (f.exists())
0N/A continue;
0N/A break;
0N/A }
0N/A
0N/A long [] s = { f.getTotalSpace(), f.getFreeSpace(), f.getUsableSpace() };
0N/A
0N/A for (int i = 0; i < s.length; i++) {
0N/A if (s[i] != 0L)
0N/A fail(f.getName(), s[i], "!=", 0L);
0N/A else
0N/A pass();
0N/A }
0N/A }
0N/A
0N/A private static void compareZeroExist() {
0N/A try {
0N/A File f = File.createTempFile("tmp", null, new File("."));
0N/A
0N/A long [] s = { f.getTotalSpace(), f.getFreeSpace(), f.getUsableSpace() };
0N/A
0N/A for (int i = 0; i < s.length; i++) {
0N/A if (s[i] == 0L)
0N/A fail(f.getName(), s[i], "==", 0L);
0N/A else
0N/A pass();
0N/A }
0N/A } catch (IOException x) {
0N/A fail("Couldn't create temp file for test");
0N/A }
0N/A }
0N/A
0N/A private static class Allow extends SecurityManager {
0N/A public void checkRead(String file) {}
0N/A public void checkPermission(Permission p) {}
0N/A public void checkPermission(Permission p, Object context) {}
0N/A }
0N/A
0N/A private static class Deny extends SecurityManager {
0N/A public void checkPermission(Permission p) {
0N/A if (p.implies(new RuntimePermission("setSecurityManager"))
0N/A || p.implies(new RuntimePermission("getProtectionDomain")))
0N/A return;
0N/A super.checkPermission(p);
0N/A }
0N/A
0N/A public void checkPermission(Permission p, Object context) {
0N/A if (p.implies(new RuntimePermission("setSecurityManager"))
0N/A || p.implies(new RuntimePermission("getProtectionDomain")))
0N/A return;
0N/A super.checkPermission(p, context);
0N/A }
0N/A }
0N/A
0N/A private static class DenyFSA extends Deny {
0N/A private String err = "sorry - getFileSystemAttributes";
0N/A
0N/A public void checkPermission(Permission p) {
0N/A if (p.implies(new RuntimePermission("getFileSystemAttributes")))
0N/A throw new SecurityException(err);
0N/A super.checkPermission(p);
0N/A }
0N/A
0N/A public void checkPermission(Permission p, Object context) {
0N/A if (p.implies(new RuntimePermission("getFileSystemAttributes")))
0N/A throw new SecurityException(err);
0N/A super.checkPermission(p, context);
0N/A }
0N/A }
0N/A
0N/A private static class DenyRead extends Deny {
0N/A private String err = "sorry - checkRead()";
0N/A
0N/A public void checkRead(String file) {
0N/A throw new SecurityException(err);
0N/A }
0N/A }
0N/A
0N/A private static void testFile(String dirName) {
0N/A out.format("--- Testing %s%n", dirName);
0N/A ArrayList l;
0N/A try {
0N/A l = space(dirName);
0N/A } catch (IOException x) {
0N/A throw new RuntimeException(dirName + " can't get file system information", x);
0N/A }
0N/A compare((GetXSpace.Space) l.get(0));
0N/A }
0N/A
0N/A private static void testDF() {
0N/A out.format("--- Testing df");
0N/A // Find all of the partitions on the machine and verify that the size
0N/A // returned by "df" is equivalent to File.getXSpace() values.
0N/A ArrayList l;
0N/A try {
0N/A l = space(null);
0N/A } catch (IOException x) {
0N/A throw new RuntimeException("can't get file system information", x);
0N/A }
0N/A if (l.size() == 0)
0N/A throw new RuntimeException("no partitions?");
0N/A
0N/A for (int i = 0; i < sma.length; i++) {
0N/A System.setSecurityManager(sma[i]);
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sma[i] != null && sm == null)
0N/A throw new RuntimeException("Test configuration error "
0N/A + " - can't set security manager");
0N/A
0N/A out.format("%nSecurityManager = %s%n" ,
0N/A (sm == null ? "null" : sm.getClass().getName()));
0N/A for (int j = 0; j < l.size(); j++) {
0N/A Space s = (GetXSpace.Space) l.get(j);
0N/A if (sm instanceof Deny) {
0N/A tryCatch(s);
0N/A } else {
0N/A compare(s);
0N/A compareZeroNonExist();
0N/A compareZeroExist();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public static void main(String [] args) {
0N/A if (args.length > 0) {
0N/A testFile(args[0]);
0N/A } else {
0N/A testDF();
0N/A }
0N/A
0N/A if (fail != 0)
0N/A throw new RuntimeException((fail + pass) + " tests: "
0N/A + fail + " failure(s), first", first);
0N/A else
0N/A out.format("all %d tests passed%n", fail + pass);
0N/A }
0N/A}