Path: blob/master/test/jdk/java/nio/file/etc/MacVolumesTest.java
41153 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 823125425* @requires os.family == "mac"26* @summary Check access and basic NIO APIs on APFS for macOS version >= 10.1527*/28import java.io.BufferedReader;29import java.io.FileInputStream;30import java.io.InputStream;31import java.io.InputStreamReader;32import java.io.IOException;33import java.io.Reader;34import java.nio.ByteBuffer;35import java.nio.channels.SeekableByteChannel;36import java.nio.file.DirectoryStream;37import java.nio.file.Files;38import java.nio.file.Path;39import java.nio.file.StandardOpenOption;40import java.nio.file.attribute.FileTime;41import java.util.Arrays;42import java.util.Iterator;43import java.util.Random;4445public class MacVolumesTest {46private static final String SYSTEM_VOLUME = "/";47private static final String DATA_VOLUME = "/System/Volumes/Data";48private static final String FIRMLINKS = "/usr/share/firmlinks";4950private static final void checkSystemVolume() throws IOException {51System.out.format("--- Checking system volume %s ---%n", SYSTEM_VOLUME);52Path root = Path.of(SYSTEM_VOLUME);53if (!Files.getFileStore(root).isReadOnly()) {54throw new RuntimeException("Root volume is not read-only");55}5657Path tempDir;58try {59tempDir = Files.createTempDirectory(root, "tempDir");60throw new RuntimeException("Created temporary directory in root");61} catch (IOException ignore) {62}6364Path tempFile;65try {66tempFile = Files.createTempFile(root, "tempFile", null);67throw new RuntimeException("Created temporary file in root");68} catch (IOException ignore) {69}7071Path path = null;72Path etc = Path.of(SYSTEM_VOLUME, "etc");73if (Files.isWritable(etc)) {74throw new RuntimeException("System path " + etc + " is writable");75}76try (DirectoryStream<Path> ds = Files.newDirectoryStream(etc)) {77Iterator<Path> paths = ds.iterator();78while (paths.hasNext()) {79Path p = paths.next();80if (Files.isReadable(p) && Files.isRegularFile(p)) {81path = p;82break;83}84}85}86if (path == null) {87System.err.println("No root test file found: skipping file test");88return;89}90System.out.format("Using root test file %s%n", path);9192if (Files.isWritable(path)) {93throw new RuntimeException("Test file " + path + " is writable");94}9596FileTime creationTime =97(FileTime)Files.getAttribute(path, "basic:creationTime");98System.out.format("%s creation time: %s%n", path, creationTime);99100long size = Files.size(path);101int capacity = (int)Math.min(1024, size);102ByteBuffer buf = ByteBuffer.allocate(capacity);103try (SeekableByteChannel sbc = Files.newByteChannel(path)) {104int n = sbc.read(buf);105System.out.format("Read %d bytes from %s%n", n, path);106}107}108109private static final void checkDataVolume() throws IOException {110System.out.format("--- Checking data volume %s ---%n", DATA_VOLUME);111Path data = Path.of(DATA_VOLUME, "private", "tmp");112if (Files.getFileStore(data).isReadOnly()) {113throw new RuntimeException("Data volume is read-only");114}115116Path tempDir = Files.createTempDirectory(data, "tempDir");117tempDir.toFile().deleteOnExit();118System.out.format("Temporary directory: %s%n", tempDir);119if (!Files.isWritable(tempDir)) {120throw new RuntimeException("Temporary directory is not writable");121}122123Path tempFile = Files.createTempFile(tempDir, "tempFile", null);124tempFile.toFile().deleteOnExit();125System.out.format("Temporary file: %s%n", tempFile);126if (!Files.isWritable(tempFile)) {127throw new RuntimeException("Temporary file is not writable");128}129130byte[] bytes = new byte[42];131new Random().nextBytes(bytes);132try (SeekableByteChannel sbc = Files.newByteChannel(tempFile,133StandardOpenOption.WRITE)) {134ByteBuffer src = ByteBuffer.wrap(bytes);135if (sbc.write(src) != bytes.length) {136throw new RuntimeException("Incorrect number of bytes written");137}138}139140try (SeekableByteChannel sbc = Files.newByteChannel(tempFile)) {141ByteBuffer dst = ByteBuffer.allocate(bytes.length);142if (sbc.read(dst) != bytes.length) {143throw new RuntimeException("Incorrect number of bytes read");144}145if (!Arrays.equals(dst.array(), bytes)) {146throw new RuntimeException("Bytes read != bytes written");147}148}149}150151static void checkFirmlinks() throws IOException {152System.out.format("--- Checking firmlinks %s ---%n", FIRMLINKS);153Path firmlinks = Path.of(FIRMLINKS);154if (!Files.exists(firmlinks)) {155System.err.format("%s does not exist: skipping firmlinks test%n",156firmlinks);157return;158} else if (!Files.isReadable(firmlinks)) {159throw new RuntimeException(String.format("%s is not readable",160firmlinks));161}162163try (BufferedReader br = Files.newBufferedReader(firmlinks)) {164String line;165while ((line = br.readLine()) != null) {166String file = line.split("\\s")[0];167Path path = Path.of(file);168if (!Files.exists(path)) {169System.err.format("Firmlink %s does not exist: skipping%n",170file);171continue;172}173if (Files.getFileStore(path).isReadOnly()) {174String msg = String.format("%s is read-only%n", file);175throw new RuntimeException(msg);176} else {177System.out.format("Firmlink %s OK%n", file);178}179}180}181}182183public static void main(String[] args) throws Exception {184String[] osv = System.getProperty("os.version").split("\\.");185int major = Integer.valueOf(osv[0]);186int minor = Integer.valueOf(osv[1]);187if (major < 10 || (major == 10 && minor < 15)) {188System.out.format("macOS version %d.%d too old: skipping test%n",189major, minor);190return;191}192193// Check system volume for read-only.194checkSystemVolume();195196// Check data volume for read-write.197checkDataVolume();198199// Check firmlinks for read-write.200checkFirmlinks();201}202}203204205