test/jdk/java/nio/file/etc/MacVolumesTest.java
author bpb
Mon, 23 Sep 2019 13:59:41 -0700
changeset 58276 1e57d3774190
child 58975 19744a63c295
permissions -rw-r--r--
8231254: (fs) Add test for macOS Catalina changes to protect system software Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58276
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
     1
/*
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
     4
 *
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
     8
 *
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    13
 * accompanied this code).
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    14
 *
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    18
 *
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    21
 * questions.
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    22
 */
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    23
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    24
/* @test
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    25
 * @bug 8231254
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    26
 * @requires os.family == "mac"
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    27
 * @summary Check access and basic NIO APIs on APFS for macOS version >= 10.15
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    28
 */
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    29
import java.io.BufferedReader;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    30
import java.io.FileInputStream;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    31
import java.io.InputStream;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    32
import java.io.InputStreamReader;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    33
import java.io.IOException;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    34
import java.io.Reader;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    35
import java.nio.ByteBuffer;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    36
import java.nio.channels.SeekableByteChannel;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    37
import java.nio.file.DirectoryStream;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    38
import java.nio.file.Files;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    39
import java.nio.file.Path;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    40
import java.nio.file.StandardOpenOption;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    41
import java.nio.file.attribute.FileTime;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    42
import java.util.Arrays;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    43
import java.util.Iterator;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    44
import java.util.Random;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    45
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    46
public class MacVolumesTest {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    47
    private static final String SYSTEM_VOLUME = "/";
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    48
    private static final String DATA_VOLUME = "/System/Volumes/Data";
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    49
    private static final String FIRMLINKS = "/usr/share/firmlinks";
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    50
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    51
    private static final void checkSystemVolume() throws IOException {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    52
        System.out.format("--- Checking system volume %s ---%n", SYSTEM_VOLUME);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    53
        Path root = Path.of(SYSTEM_VOLUME);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    54
        if (!Files.getFileStore(root).isReadOnly()) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    55
            throw new RuntimeException("Root volume is not read-only");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    56
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    57
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    58
        Path tempDir;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    59
        try {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    60
            tempDir = Files.createTempDirectory(root, "tempDir");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    61
            throw new RuntimeException("Created temporary directory in root");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    62
        } catch (IOException ignore) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    63
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    64
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    65
        Path tempFile;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    66
        try {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    67
            tempFile = Files.createTempFile(root, "tempFile", null);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    68
            throw new RuntimeException("Created temporary file in root");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    69
        } catch (IOException ignore) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    70
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    71
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    72
        Path path = null;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    73
        Path etc = Path.of(SYSTEM_VOLUME, "etc");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    74
        if (Files.isWritable(etc)) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    75
            throw new RuntimeException("System path " + etc + " is writable");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    76
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    77
        try (DirectoryStream<Path> ds = Files.newDirectoryStream(etc)) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    78
            Iterator<Path> paths = ds.iterator();
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    79
            while (paths.hasNext()) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    80
                Path p = paths.next();
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    81
                if (Files.isReadable(p) && Files.isRegularFile(p)) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    82
                    path = p;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    83
                    break;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    84
                }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    85
            }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    86
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    87
        if (path == null) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    88
            System.err.println("No root test file found: skipping file test");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    89
            return;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    90
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    91
        System.out.format("Using root test file %s%n", path);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    92
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    93
        if (Files.isWritable(path)) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    94
            throw new RuntimeException("Test file " + path + " is writable");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    95
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    96
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    97
        FileTime creationTime =
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    98
            (FileTime)Files.getAttribute(path, "basic:creationTime");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
    99
        System.out.format("%s creation time: %s%n", path, creationTime);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   100
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   101
        long size = Files.size(path);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   102
        int capacity = (int)Math.min(1024, size);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   103
        ByteBuffer buf = ByteBuffer.allocate(capacity);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   104
        try (SeekableByteChannel sbc = Files.newByteChannel(path)) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   105
            int n = sbc.read(buf);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   106
            System.out.format("Read %d bytes from %s%n", n, path);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   107
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   108
    }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   109
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   110
    private static final void checkDataVolume() throws IOException {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   111
        System.out.format("--- Checking data volume %s ---%n", DATA_VOLUME);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   112
        Path data = Path.of(DATA_VOLUME, "tmp");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   113
        if (Files.getFileStore(data).isReadOnly()) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   114
            throw new RuntimeException("Data volume is read-only");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   115
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   116
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   117
        Path tempDir = Files.createTempDirectory(data, "tempDir");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   118
        tempDir.toFile().deleteOnExit();
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   119
        System.out.format("Temporary directory: %s%n", tempDir);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   120
        if (!Files.isWritable(tempDir)) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   121
            throw new RuntimeException("Temporary directory is not writable");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   122
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   123
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   124
        Path tempFile = Files.createTempFile(tempDir, "tempFile", null);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   125
        tempFile.toFile().deleteOnExit();
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   126
        System.out.format("Temporary file: %s%n", tempFile);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   127
        if (!Files.isWritable(tempFile)) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   128
            throw new RuntimeException("Temporary file is not writable");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   129
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   130
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   131
        byte[] bytes = new byte[42];
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   132
        new Random().nextBytes(bytes);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   133
        try (SeekableByteChannel sbc = Files.newByteChannel(tempFile,
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   134
            StandardOpenOption.WRITE)) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   135
            ByteBuffer src = ByteBuffer.wrap(bytes);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   136
            if (sbc.write(src) != bytes.length) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   137
                throw new RuntimeException("Incorrect number of bytes written");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   138
            }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   139
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   140
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   141
        try (SeekableByteChannel sbc = Files.newByteChannel(tempFile)) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   142
            ByteBuffer dst = ByteBuffer.allocate(bytes.length);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   143
            if (sbc.read(dst) != bytes.length) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   144
                throw new RuntimeException("Incorrect number of bytes read");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   145
            }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   146
            if (!Arrays.equals(dst.array(), bytes)) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   147
                throw new RuntimeException("Bytes read != bytes written");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   148
            }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   149
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   150
    }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   151
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   152
    static void checkFirmlinks() throws IOException {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   153
        System.out.format("--- Checking firmlinks %s ---%n", FIRMLINKS);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   154
        Path firmlinks = Path.of(FIRMLINKS);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   155
        if (!Files.exists(firmlinks)) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   156
            System.err.format("%s does not exist: skipping firmlinks test%n",
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   157
                firmlinks);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   158
            return;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   159
        } else if (!Files.isReadable(firmlinks)) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   160
            throw new RuntimeException(String.format("%s is not readable",
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   161
                firmlinks));
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   162
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   163
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   164
        try (BufferedReader br = Files.newBufferedReader(firmlinks)) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   165
            String line;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   166
            while ((line = br.readLine()) != null) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   167
                String file = line.split("\\s")[0];
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   168
                Path path = Path.of(file);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   169
                if (!Files.exists(path)) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   170
                    System.err.format("Firmlink %s does not exist: skipping%n",
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   171
                        file);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   172
                    continue;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   173
                }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   174
                if (Files.getFileStore(path).isReadOnly()) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   175
                    String msg = String.format("%s is read-only%n", file);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   176
                    throw new RuntimeException(msg);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   177
                } else {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   178
                    System.out.format("Firmlink %s OK%n", file);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   179
                }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   180
            }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   181
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   182
    }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   183
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   184
    public static void main(String[] args) throws Exception {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   185
        String[] osv = System.getProperty("os.version").split("\\.");
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   186
        int major = Integer.valueOf(osv[0]);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   187
        int minor = Integer.valueOf(osv[1]);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   188
        if (major < 10 || (major == 10 && minor < 15)) {
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   189
            System.out.format("macOS version %d.%d too old: skipping test%n",
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   190
                major, minor);
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   191
            return;
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   192
        }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   193
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   194
        // Check system volume for read-only.
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   195
        checkSystemVolume();
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   196
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   197
        // Check data volume for read-write.
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   198
        checkDataVolume();
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   199
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   200
        // Check firmlinks for read-write.
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   201
        checkFirmlinks();
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   202
    }
1e57d3774190 8231254: (fs) Add test for macOS Catalina changes to protect system software
bpb
parents:
diff changeset
   203
}