Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/DirectoryStream/SecureDS.java
38828 views
/*1* Copyright (c) 2008, 2011, 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 4313887 683833325* @summary Unit test for java.nio.file.SecureDirectoryStream26* @library ..27*/2829import java.nio.file.*;30import static java.nio.file.Files.*;31import static java.nio.file.StandardOpenOption.*;32import static java.nio.file.LinkOption.*;33import java.nio.file.attribute.*;34import java.nio.channels.*;35import java.io.IOException;36import java.util.*;3738public class SecureDS {39static boolean supportsLinks;4041public static void main(String[] args) throws IOException {42Path dir = TestUtil.createTemporaryDirectory();43try {44DirectoryStream<Path> stream = newDirectoryStream(dir);45stream.close();46if (!(stream instanceof SecureDirectoryStream)) {47System.out.println("SecureDirectoryStream not supported.");48return;49}5051supportsLinks = TestUtil.supportsLinks(dir);5253// run tests54doBasicTests(dir);55doMoveTests(dir);56miscTests(dir);5758} finally {59TestUtil.removeAll(dir);60}61}6263// Exercise each of SecureDirectoryStream's method (except move)64static void doBasicTests(Path dir) throws IOException {65Path dir1 = createDirectory(dir.resolve("dir1"));66Path dir2 = dir.resolve("dir2");6768// create a file, directory, and two sym links in the directory69Path fileEntry = Paths.get("myfile");70createFile(dir1.resolve(fileEntry));71Path dirEntry = Paths.get("mydir");72createDirectory(dir1.resolve(dirEntry));73// myfilelink -> myfile74Path link1Entry = Paths.get("myfilelink");75if (supportsLinks)76createSymbolicLink(dir1.resolve(link1Entry), fileEntry);77// mydirlink -> mydir78Path link2Entry = Paths.get("mydirlink");79if (supportsLinks)80createSymbolicLink(dir1.resolve(link2Entry), dirEntry);8182// open directory and then move it so that it is no longer accessible83// via its original path.84SecureDirectoryStream<Path> stream =85(SecureDirectoryStream<Path>)newDirectoryStream(dir1);86move(dir1, dir2);8788// Test: iterate over all entries89int count = 0;90for (Path entry: stream) { count++; }91assertTrue(count == (supportsLinks ? 4 : 2));9293// Test: getFileAttributeView to access directory's attributes94assertTrue(stream95.getFileAttributeView(BasicFileAttributeView.class)96.readAttributes()97.isDirectory());9899// Test: getFileAttributeView to access attributes of entries100assertTrue(stream101.getFileAttributeView(fileEntry, BasicFileAttributeView.class)102.readAttributes()103.isRegularFile());104assertTrue(stream105.getFileAttributeView(fileEntry, BasicFileAttributeView.class, NOFOLLOW_LINKS)106.readAttributes()107.isRegularFile());108assertTrue(stream109.getFileAttributeView(dirEntry, BasicFileAttributeView.class)110.readAttributes()111.isDirectory());112assertTrue(stream113.getFileAttributeView(dirEntry, BasicFileAttributeView.class, NOFOLLOW_LINKS)114.readAttributes()115.isDirectory());116if (supportsLinks) {117assertTrue(stream118.getFileAttributeView(link1Entry, BasicFileAttributeView.class)119.readAttributes()120.isRegularFile());121assertTrue(stream122.getFileAttributeView(link1Entry, BasicFileAttributeView.class, NOFOLLOW_LINKS)123.readAttributes()124.isSymbolicLink());125assertTrue(stream126.getFileAttributeView(link2Entry, BasicFileAttributeView.class)127.readAttributes()128.isDirectory());129assertTrue(stream130.getFileAttributeView(link2Entry, BasicFileAttributeView.class, NOFOLLOW_LINKS)131.readAttributes()132.isSymbolicLink());133}134135// Test: newByteChannel136Set<StandardOpenOption> opts = Collections.emptySet();137stream.newByteChannel(fileEntry, opts).close();138if (supportsLinks) {139stream.newByteChannel(link1Entry, opts).close();140try {141Set<OpenOption> mixed = new HashSet<>();142mixed.add(READ);143mixed.add(NOFOLLOW_LINKS);144stream.newByteChannel(link1Entry, mixed).close();145shouldNotGetHere();146} catch (IOException x) { }147}148149// Test: newDirectoryStream150stream.newDirectoryStream(dirEntry).close();151stream.newDirectoryStream(dirEntry, LinkOption.NOFOLLOW_LINKS).close();152if (supportsLinks) {153stream.newDirectoryStream(link2Entry).close();154try {155stream.newDirectoryStream(link2Entry, LinkOption.NOFOLLOW_LINKS)156.close();157shouldNotGetHere();158} catch (IOException x) { }159}160161// Test: delete162if (supportsLinks) {163stream.deleteFile(link1Entry);164stream.deleteFile(link2Entry);165}166stream.deleteDirectory(dirEntry);167stream.deleteFile(fileEntry);168169// clean-up170stream.close();171delete(dir2);172}173174// Exercise SecureDirectoryStream's move method175static void doMoveTests(Path dir) throws IOException {176Path dir1 = createDirectory(dir.resolve("dir1"));177Path dir2 = createDirectory(dir.resolve("dir2"));178179// create dir1/myfile, dir1/mydir, dir1/mylink180Path fileEntry = Paths.get("myfile");181createFile(dir1.resolve(fileEntry));182Path dirEntry = Paths.get("mydir");183createDirectory(dir1.resolve(dirEntry));184Path linkEntry = Paths.get("mylink");185if (supportsLinks)186createSymbolicLink(dir1.resolve(linkEntry), Paths.get("missing"));187188// target name189Path target = Paths.get("newfile");190191// open stream to both directories192SecureDirectoryStream<Path> stream1 =193(SecureDirectoryStream<Path>)newDirectoryStream(dir1);194SecureDirectoryStream<Path> stream2 =195(SecureDirectoryStream<Path>)newDirectoryStream(dir2);196197// Test: move dir1/myfile -> dir2/newfile198stream1.move(fileEntry, stream2, target);199assertTrue(notExists(dir1.resolve(fileEntry)));200assertTrue(exists(dir2.resolve(target)));201stream2.deleteFile(target);202203// Test: move dir1/mydir -> dir2/newfile204stream1.move(dirEntry, stream2, target);205assertTrue(notExists(dir1.resolve(dirEntry)));206assertTrue(exists(dir2.resolve(target)));207stream2.deleteDirectory(target);208209// Test: move dir1/mylink -> dir2/newfile210if (supportsLinks) {211stream1.move(linkEntry, stream2, target);212assertTrue(isSymbolicLink(dir2.resolve(target)));213stream2.deleteFile(target);214}215216// Test: move between devices217String testDirAsString = System.getProperty("test.dir");218if (testDirAsString != null) {219Path testDir = Paths.get(testDirAsString);220if (!getFileStore(dir1).equals(getFileStore(testDir))) {221SecureDirectoryStream<Path> ts =222(SecureDirectoryStream<Path>)newDirectoryStream(testDir);223createFile(dir1.resolve(fileEntry));224try {225stream1.move(fileEntry, ts, target);226shouldNotGetHere();227} catch (AtomicMoveNotSupportedException x) { }228ts.close();229stream1.deleteFile(fileEntry);230}231}232233// clean-up234delete(dir1);235delete(dir2);236}237238// null and ClosedDirectoryStreamException239static void miscTests(Path dir) throws IOException {240Path file = Paths.get("file");241createFile(dir.resolve(file));242243SecureDirectoryStream<Path> stream =244(SecureDirectoryStream<Path>)newDirectoryStream(dir);245246// NullPointerException247try {248stream.getFileAttributeView(null);249shouldNotGetHere();250} catch (NullPointerException x) { }251try {252stream.getFileAttributeView(null, BasicFileAttributeView.class);253shouldNotGetHere();254} catch (NullPointerException x) { }255try {256stream.getFileAttributeView(file, null);257shouldNotGetHere();258} catch (NullPointerException x) { }259try {260stream.newByteChannel(null, EnumSet.of(CREATE,WRITE));261shouldNotGetHere();262} catch (NullPointerException x) { }263try {264stream.newByteChannel(null, EnumSet.of(CREATE,WRITE,null));265shouldNotGetHere();266} catch (NullPointerException x) { }267try {268stream.newByteChannel(file, null);269shouldNotGetHere();270} catch (NullPointerException x) { }271try {272stream.move(null, stream, file);273shouldNotGetHere();274} catch (NullPointerException x) { }275try {276stream.move(file, null, file);277shouldNotGetHere();278} catch (NullPointerException x) { }279try {280stream.move(file, stream, null);281shouldNotGetHere();282} catch (NullPointerException x) { }283try {284stream.newDirectoryStream(null);285shouldNotGetHere();286} catch (NullPointerException x) { }287try {288stream.deleteFile(null);289shouldNotGetHere();290} catch (NullPointerException x) { }291try {292stream.deleteDirectory(null);293shouldNotGetHere();294} catch (NullPointerException x) { }295296// close stream297stream.close();298stream.close(); // should be no-op299300// ClosedDirectoryStreamException301try {302stream.newDirectoryStream(file);303shouldNotGetHere();304} catch (ClosedDirectoryStreamException x) { }305try {306stream.newByteChannel(file, EnumSet.of(READ));307shouldNotGetHere();308} catch (ClosedDirectoryStreamException x) { }309try {310stream.move(file, stream, file);311shouldNotGetHere();312} catch (ClosedDirectoryStreamException x) { }313try {314stream.deleteFile(file);315shouldNotGetHere();316} catch (ClosedDirectoryStreamException x) { }317318// clean-up319delete(dir.resolve(file));320}321322static void assertTrue(boolean b) {323if (!b) throw new RuntimeException("Assertion failed");324}325326static void shouldNotGetHere() {327assertTrue(false);328}329}330331332