1 /*
   2  * Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4313887 6873621 6979526 7006126 7020517
  26  * @summary Unit test for java.nio.file.FileStore
  27  * @key intermittent
  28  * @library .. /test/lib
  29  * @build jdk.test.lib.util.FileUtils
  30  *        jdk.test.lib.Utils
  31  *        jdk.test.lib.Asserts
  32  *        jdk.test.lib.JDKToolFinder
  33  *        jdk.test.lib.JDKToolLauncher
  34  *        jdk.test.lib.Platform
  35  *        jdk.test.lib.process.*
  36  * @run main Basic
  37  */
  38 
  39 import java.nio.file.*;
  40 import java.nio.file.attribute.*;
  41 import java.io.File;
  42 import java.io.IOException;
  43 
  44 import jdk.test.lib.util.FileUtils;
  45 
  46 
  47 public class Basic {
  48 
  49     static final long G = 1024L * 1024L * 1024L;
  50 
  51     public static void main(String[] args) throws IOException {
  52         Path dir = TestUtil.createTemporaryDirectory();
  53         try {
  54             doTests(dir);
  55         } finally {
  56             TestUtil.removeAll(dir);
  57         }
  58     }
  59 
  60     static void assertTrue(boolean okay) {
  61         if (!okay)
  62             throw new RuntimeException("Assertion failed");
  63     }
  64 
  65     static void checkWithin1GB(long value1, long value2) {
  66         long diff = Math.abs(value1 - value2);
  67         if (diff > G)
  68             throw new RuntimeException("values differ by more than 1GB");
  69     }
  70 
  71     static void doTests(Path dir) throws IOException {
  72         /**
  73          * Test: Directory should be on FileStore that is writable
  74          */
  75         assertTrue(!Files.getFileStore(dir).isReadOnly());
  76 
  77         /**
  78          * Test: Two files should have the same FileStore
  79          */
  80         Path file1 = Files.createFile(dir.resolve("foo"));
  81         Path file2 = Files.createFile(dir.resolve("bar"));
  82         FileStore store1 = Files.getFileStore(file1);
  83         FileStore store2 = Files.getFileStore(file2);
  84         assertTrue(store1.equals(store2));
  85         assertTrue(store2.equals(store1));
  86         assertTrue(store1.hashCode() == store2.hashCode());
  87 
  88         /**
  89          * Test: File and FileStore attributes
  90          */
  91         assertTrue(store1.supportsFileAttributeView("basic"));
  92         assertTrue(store1.supportsFileAttributeView(BasicFileAttributeView.class));
  93         assertTrue(store1.supportsFileAttributeView("posix") ==
  94             store1.supportsFileAttributeView(PosixFileAttributeView.class));
  95         assertTrue(store1.supportsFileAttributeView("dos") ==
  96             store1.supportsFileAttributeView(DosFileAttributeView.class));
  97         assertTrue(store1.supportsFileAttributeView("acl") ==
  98             store1.supportsFileAttributeView(AclFileAttributeView.class));
  99         assertTrue(store1.supportsFileAttributeView("user") ==
 100             store1.supportsFileAttributeView(UserDefinedFileAttributeView.class));
 101 
 102         /**
 103          * Test: Space atributes
 104          */
 105         File f = file1.toFile();
 106         long total = f.getTotalSpace();
 107         long free = f.getFreeSpace();
 108         long usable = f.getUsableSpace();
 109 
 110         // check values are "close"
 111         checkWithin1GB(total,  store1.getTotalSpace());
 112         checkWithin1GB(free,   store1.getUnallocatedSpace());
 113         checkWithin1GB(usable, store1.getUsableSpace());
 114 
 115         // get values by name
 116         checkWithin1GB(total,  (Long)store1.getAttribute("totalSpace"));
 117         checkWithin1GB(free,   (Long)store1.getAttribute("unallocatedSpace"));
 118         checkWithin1GB(usable, (Long)store1.getAttribute("usableSpace"));
 119 
 120         /**
 121          * Test: Enumerate all FileStores
 122          */
 123         if (FileUtils.areFileSystemsAccessible()) {
 124             FileStore prev = null;
 125             for (FileStore store: FileSystems.getDefault().getFileStores()) {
 126                 System.out.format("%s (name=%s type=%s)\n", store, store.name(),
 127                     store.type());
 128 
 129                 // check space attributes are accessible
 130                 try {
 131                     store.getTotalSpace();
 132                     store.getUnallocatedSpace();
 133                     store.getUsableSpace();
 134                 } catch (NoSuchFileException nsfe) {
 135                     // ignore exception as the store could have been
 136                     // deleted since the iterator was instantiated
 137                     System.err.format("%s was not found\n", store);
 138                 } catch (AccessDeniedException ade) {
 139                     // ignore exception as the lack of ability to access the
 140                     // store due to lack of file permission or similar does not
 141                     // reflect whether the space attributes would be accessible
 142                     // were access to be permitted
 143                     System.err.format("%s is inaccessible\n", store);
 144                 }
 145 
 146                 // two distinct FileStores should not be equal
 147                 assertTrue(!store.equals(prev));
 148                 prev = store;
 149             }
 150         } else {
 151             System.err.println
 152                 ("Skipping FileStore check due to file system access failure");
 153         }
 154     }
 155 }