jdk/test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java
author ihse
Tue, 09 May 2017 12:57:30 +0200
changeset 45028 b0ea3c0bfb81
parent 34881 c519b013f50e
permissions -rw-r--r--
8179889: Fix typographic errors in copyright headers Reviewed-by: erikj, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34537
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
     1
/*
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 34881
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
34537
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
     4
 *
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
     8
 *
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    13
 * accompanied this code).
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    14
 *
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    18
 *
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    21
 * questions.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    22
 */
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    23
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    24
/* @test
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    25
 * @bug 8139133
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    26
 * @summary Verify ability to set time attributes of socket files with no device
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    27
 * @requires os.family == "linux"
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    28
 */
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    29
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    30
import java.io.File;
34881
c519b013f50e 8146359: test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java fails when nc is not available
bpb
parents: 34537
diff changeset
    31
import java.io.InputStream;
34537
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    32
import java.io.IOException;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    33
import java.nio.file.FileSystem;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    34
import java.nio.file.FileSystems;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    35
import java.nio.file.Files;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    36
import java.nio.file.Path;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    37
import java.nio.file.Paths;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    38
import java.nio.file.StandardWatchEventKinds;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    39
import java.nio.file.WatchKey;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    40
import java.nio.file.WatchService;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    41
import java.nio.file.attribute.BasicFileAttributeView;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    42
import java.nio.file.attribute.BasicFileAttributes;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    43
import java.nio.file.attribute.FileTime;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    44
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    45
public class UnixSocketFile {
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    46
    private static final String TEST_SUB_DIR = "UnixSocketFile";
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    47
    private static final String SOCKET_FILE_NAME = "mysocket";
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    48
    private static final String CMD_BASE = "nc -lU";
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    49
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    50
    public static void main(String[] args)
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    51
        throws InterruptedException, IOException {
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    52
34881
c519b013f50e 8146359: test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java fails when nc is not available
bpb
parents: 34537
diff changeset
    53
        // Use 'which' to verify that 'nc' is available and skip the test
c519b013f50e 8146359: test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java fails when nc is not available
bpb
parents: 34537
diff changeset
    54
        // if it is not.
c519b013f50e 8146359: test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java fails when nc is not available
bpb
parents: 34537
diff changeset
    55
        Process proc = Runtime.getRuntime().exec("which nc");
c519b013f50e 8146359: test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java fails when nc is not available
bpb
parents: 34537
diff changeset
    56
        InputStream stdout = proc.getInputStream();
c519b013f50e 8146359: test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java fails when nc is not available
bpb
parents: 34537
diff changeset
    57
        int b = stdout.read();
c519b013f50e 8146359: test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java fails when nc is not available
bpb
parents: 34537
diff changeset
    58
        proc.destroy();
c519b013f50e 8146359: test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java fails when nc is not available
bpb
parents: 34537
diff changeset
    59
        if (b == -1) {
c519b013f50e 8146359: test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java fails when nc is not available
bpb
parents: 34537
diff changeset
    60
            System.err.println("Netcat command unavailable; skipping test.");
c519b013f50e 8146359: test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java fails when nc is not available
bpb
parents: 34537
diff changeset
    61
            return;
c519b013f50e 8146359: test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java fails when nc is not available
bpb
parents: 34537
diff changeset
    62
        }
c519b013f50e 8146359: test/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java fails when nc is not available
bpb
parents: 34537
diff changeset
    63
34537
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    64
        // Create a new sub-directory of the nominal test directory in which
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    65
        // 'nc' will create the socket file.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    66
        String testSubDir = System.getProperty("test.dir", ".")
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    67
            + File.separator + TEST_SUB_DIR;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    68
        Path socketTestDir = Paths.get(testSubDir);
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    69
        Files.createDirectory(socketTestDir);
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    70
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    71
        // Set the path of the socket file.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    72
        String socketFilePath = testSubDir + File.separator
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    73
            + SOCKET_FILE_NAME;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    74
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    75
        // Create a process which executes the nc (netcat) utility to create
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    76
        // a socket file at the indicated location.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    77
        FileSystem fs = FileSystems.getDefault();
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    78
        try (WatchService ws = fs.newWatchService()) {
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    79
            // Watch the test sub-directory to receive notification when an
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    80
            // entry, i.e., the socket file, is added to the sub-directory.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    81
            WatchKey wk = socketTestDir.register(ws,
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    82
                    StandardWatchEventKinds.ENTRY_CREATE);
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    83
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    84
            // Execute the 'nc' command.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    85
            proc = Runtime.getRuntime().exec(CMD_BASE + " " + socketFilePath);
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    86
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    87
            // Wait until the socket file is created.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    88
            WatchKey key = ws.take();
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    89
            if (key != wk) {
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    90
                throw new RuntimeException("Unknown entry created - expected: "
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    91
                    + wk.watchable() + ", actual: " + key.watchable());
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    92
            }
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    93
            wk.cancel();
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    94
        }
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    95
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    96
        // Verify that the socket file in fact exists.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    97
        Path socketPath = fs.getPath(socketFilePath);
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    98
        if (!Files.exists(socketPath)) {
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
    99
            throw new RuntimeException("Socket file " + socketFilePath
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   100
                + " was not created by \"nc\" command.");
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   101
        }
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   102
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   103
        // Retrieve the most recent access and modification times of the
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   104
        // socket file; print the values.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   105
        BasicFileAttributeView attributeView = Files.getFileAttributeView(
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   106
                socketPath, BasicFileAttributeView.class);
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   107
        BasicFileAttributes oldAttributes = attributeView.readAttributes();
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   108
        FileTime oldAccessTime = oldAttributes.lastAccessTime();
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   109
        FileTime oldModifiedTime = oldAttributes.lastModifiedTime();
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   110
        System.out.println("Old times: " + oldAccessTime
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   111
            + " " + oldModifiedTime);
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   112
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   113
        // Calculate the time to which the access and modification times of the
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   114
        // socket file will be changed.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   115
        FileTime newFileTime =
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   116
            FileTime.fromMillis(oldAccessTime.toMillis() + 1066);
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   117
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   118
        try {
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   119
            // Set the access and modification times of the socket file.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   120
            attributeView.setTimes(newFileTime, newFileTime, null);
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   121
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   122
            // Retrieve the updated access and modification times of the
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   123
            // socket file; print the values.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   124
            FileTime newAccessTime = null;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   125
            FileTime newModifiedTime = null;
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   126
            BasicFileAttributes newAttributes = attributeView.readAttributes();
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   127
            newAccessTime = newAttributes.lastAccessTime();
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   128
            newModifiedTime = newAttributes.lastModifiedTime();
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   129
            System.out.println("New times: " + newAccessTime + " "
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   130
                + newModifiedTime);
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   131
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   132
            // Verify that the updated times have the expected values.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   133
            if ((newAccessTime != null && !newAccessTime.equals(newFileTime))
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   134
                || (newModifiedTime != null
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   135
                    && !newModifiedTime.equals(newFileTime))) {
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   136
                throw new RuntimeException("Failed to set correct times.");
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   137
            }
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   138
        } finally {
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   139
            // Destry the process running netcat and delete the socket file.
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   140
            proc.destroy();
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   141
            Files.delete(socketPath);
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   142
        }
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   143
    }
ca5ca0e04c96 8139133: Changing the modification time on a unix domain socket file fails
bpb
parents:
diff changeset
   144
}