Path: blob/master/test/jdk/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java
41155 views
/*1* Copyright (c) 2015, 2016, 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 813913325* @summary Verify ability to set time attributes of socket files with no device26* @requires os.family == "linux"27*/2829import java.io.File;30import java.io.InputStream;31import java.io.IOException;32import java.nio.file.FileSystem;33import java.nio.file.FileSystems;34import java.nio.file.Files;35import java.nio.file.Path;36import java.nio.file.Paths;37import java.nio.file.StandardWatchEventKinds;38import java.nio.file.WatchKey;39import java.nio.file.WatchService;40import java.nio.file.attribute.BasicFileAttributeView;41import java.nio.file.attribute.BasicFileAttributes;42import java.nio.file.attribute.FileTime;4344public class UnixSocketFile {45private static final String TEST_SUB_DIR = "UnixSocketFile";46private static final String SOCKET_FILE_NAME = "mysocket";47private static final String CMD_BASE = "nc -lU";4849public static void main(String[] args)50throws InterruptedException, IOException {5152// Use 'which' to verify that 'nc' is available and skip the test53// if it is not.54Process proc = Runtime.getRuntime().exec("which nc");55InputStream stdout = proc.getInputStream();56int b = stdout.read();57proc.destroy();58if (b == -1) {59System.err.println("Netcat command unavailable; skipping test.");60return;61}6263// Verify that 'nc' accepts '-U' for Unix domain sockets.64// Skip the test if it is not.65Process procHelp = Runtime.getRuntime().exec(CMD_BASE + " -h");66if (procHelp.waitFor() != 0) {67System.err.println("Netcat does not accept required options; skipping test.");68return;69}7071// Create a new sub-directory of the nominal test directory in which72// 'nc' will create the socket file.73String testSubDir = System.getProperty("test.dir", ".")74+ File.separator + TEST_SUB_DIR;75Path socketTestDir = Paths.get(testSubDir);76Files.createDirectory(socketTestDir);7778// Set the path of the socket file.79String socketFilePath = testSubDir + File.separator80+ SOCKET_FILE_NAME;8182// Create a process which executes the nc (netcat) utility to create83// a socket file at the indicated location.84FileSystem fs = FileSystems.getDefault();85try (WatchService ws = fs.newWatchService()) {86// Watch the test sub-directory to receive notification when an87// entry, i.e., the socket file, is added to the sub-directory.88WatchKey wk = socketTestDir.register(ws,89StandardWatchEventKinds.ENTRY_CREATE);9091// Execute the 'nc' command.92proc = Runtime.getRuntime().exec(CMD_BASE + " " + socketFilePath);9394// Wait until the socket file is created.95WatchKey key = ws.take();96if (key != wk) {97throw new RuntimeException("Unknown entry created - expected: "98+ wk.watchable() + ", actual: " + key.watchable());99}100wk.cancel();101}102103// Verify that the socket file in fact exists.104Path socketPath = fs.getPath(socketFilePath);105if (!Files.exists(socketPath)) {106throw new RuntimeException("Socket file " + socketFilePath107+ " was not created by \"nc\" command.");108}109110// Retrieve the most recent access and modification times of the111// socket file; print the values.112BasicFileAttributeView attributeView = Files.getFileAttributeView(113socketPath, BasicFileAttributeView.class);114BasicFileAttributes oldAttributes = attributeView.readAttributes();115FileTime oldAccessTime = oldAttributes.lastAccessTime();116FileTime oldModifiedTime = oldAttributes.lastModifiedTime();117System.out.println("Old times: " + oldAccessTime118+ " " + oldModifiedTime);119120// Calculate the time to which the access and modification times of the121// socket file will be changed.122FileTime newFileTime =123FileTime.fromMillis(oldAccessTime.toMillis() + 1066);124125try {126// Set the access and modification times of the socket file.127attributeView.setTimes(newFileTime, newFileTime, null);128129// Retrieve the updated access and modification times of the130// socket file; print the values.131FileTime newAccessTime = null;132FileTime newModifiedTime = null;133BasicFileAttributes newAttributes = attributeView.readAttributes();134newAccessTime = newAttributes.lastAccessTime();135newModifiedTime = newAttributes.lastModifiedTime();136System.out.println("New times: " + newAccessTime + " "137+ newModifiedTime);138139// Verify that the updated times have the expected values.140if ((newAccessTime != null && !newAccessTime.equals(newFileTime))141|| (newModifiedTime != null142&& !newModifiedTime.equals(newFileTime))) {143throw new RuntimeException("Failed to set correct times.");144}145} finally {146// Destry the process running netcat and delete the socket file.147proc.destroy();148Files.delete(socketPath);149}150}151}152153154