Path: blob/master/test/jdk/java/nio/file/DirectoryStream/SecureDS.java
41153 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)) {47if (System.getProperty("os.name").equals("Linux"))48throw new AssertionError(49"SecureDirectoryStream not supported.");50System.out.println("SecureDirectoryStream not supported.");51return;52}5354supportsLinks = TestUtil.supportsLinks(dir);5556// run tests57doBasicTests(dir);58doMoveTests(dir);59miscTests(dir);6061} finally {62TestUtil.removeAll(dir);63}64}6566// Exercise each of SecureDirectoryStream's method (except move)67static void doBasicTests(Path dir) throws IOException {68Path dir1 = createDirectory(dir.resolve("dir1"));69Path dir2 = dir.resolve("dir2");7071// create a file, directory, and two sym links in the directory72Path fileEntry = Paths.get("myfile");73createFile(dir1.resolve(fileEntry));74Path dirEntry = Paths.get("mydir");75createDirectory(dir1.resolve(dirEntry));76// myfilelink -> myfile77Path link1Entry = Paths.get("myfilelink");78if (supportsLinks)79createSymbolicLink(dir1.resolve(link1Entry), fileEntry);80// mydirlink -> mydir81Path link2Entry = Paths.get("mydirlink");82if (supportsLinks)83createSymbolicLink(dir1.resolve(link2Entry), dirEntry);8485// open directory and then move it so that it is no longer accessible86// via its original path.87SecureDirectoryStream<Path> stream =88(SecureDirectoryStream<Path>)newDirectoryStream(dir1);89move(dir1, dir2);9091// Test: iterate over all entries92int count = 0;93for (Path entry: stream) { count++; }94assertTrue(count == (supportsLinks ? 4 : 2));9596// Test: getFileAttributeView to access directory's attributes97assertTrue(stream98.getFileAttributeView(BasicFileAttributeView.class)99.readAttributes()100.isDirectory());101102// Test: getFileAttributeView to access attributes of entries103assertTrue(stream104.getFileAttributeView(fileEntry, BasicFileAttributeView.class)105.readAttributes()106.isRegularFile());107assertTrue(stream108.getFileAttributeView(fileEntry, BasicFileAttributeView.class, NOFOLLOW_LINKS)109.readAttributes()110.isRegularFile());111assertTrue(stream112.getFileAttributeView(dirEntry, BasicFileAttributeView.class)113.readAttributes()114.isDirectory());115assertTrue(stream116.getFileAttributeView(dirEntry, BasicFileAttributeView.class, NOFOLLOW_LINKS)117.readAttributes()118.isDirectory());119if (supportsLinks) {120assertTrue(stream121.getFileAttributeView(link1Entry, BasicFileAttributeView.class)122.readAttributes()123.isRegularFile());124assertTrue(stream125.getFileAttributeView(link1Entry, BasicFileAttributeView.class, NOFOLLOW_LINKS)126.readAttributes()127.isSymbolicLink());128assertTrue(stream129.getFileAttributeView(link2Entry, BasicFileAttributeView.class)130.readAttributes()131.isDirectory());132assertTrue(stream133.getFileAttributeView(link2Entry, BasicFileAttributeView.class, NOFOLLOW_LINKS)134.readAttributes()135.isSymbolicLink());136}137138// Test: newByteChannel139Set<StandardOpenOption> opts = Collections.emptySet();140stream.newByteChannel(fileEntry, opts).close();141if (supportsLinks) {142stream.newByteChannel(link1Entry, opts).close();143try {144Set<OpenOption> mixed = new HashSet<>();145mixed.add(READ);146mixed.add(NOFOLLOW_LINKS);147stream.newByteChannel(link1Entry, mixed).close();148shouldNotGetHere();149} catch (IOException x) { }150}151152// Test: newDirectoryStream153stream.newDirectoryStream(dirEntry).close();154stream.newDirectoryStream(dirEntry, LinkOption.NOFOLLOW_LINKS).close();155if (supportsLinks) {156stream.newDirectoryStream(link2Entry).close();157try {158stream.newDirectoryStream(link2Entry, LinkOption.NOFOLLOW_LINKS)159.close();160shouldNotGetHere();161} catch (IOException x) { }162}163164// Test: delete165if (supportsLinks) {166stream.deleteFile(link1Entry);167stream.deleteFile(link2Entry);168}169stream.deleteDirectory(dirEntry);170stream.deleteFile(fileEntry);171172// clean-up173stream.close();174delete(dir2);175}176177// Exercise SecureDirectoryStream's move method178static void doMoveTests(Path dir) throws IOException {179Path dir1 = createDirectory(dir.resolve("dir1"));180Path dir2 = createDirectory(dir.resolve("dir2"));181182// create dir1/myfile, dir1/mydir, dir1/mylink183Path fileEntry = Paths.get("myfile");184createFile(dir1.resolve(fileEntry));185Path dirEntry = Paths.get("mydir");186createDirectory(dir1.resolve(dirEntry));187Path linkEntry = Paths.get("mylink");188if (supportsLinks)189createSymbolicLink(dir1.resolve(linkEntry), Paths.get("missing"));190191// target name192Path target = Paths.get("newfile");193194// open stream to both directories195SecureDirectoryStream<Path> stream1 =196(SecureDirectoryStream<Path>)newDirectoryStream(dir1);197SecureDirectoryStream<Path> stream2 =198(SecureDirectoryStream<Path>)newDirectoryStream(dir2);199200// Test: move dir1/myfile -> dir2/newfile201stream1.move(fileEntry, stream2, target);202assertTrue(notExists(dir1.resolve(fileEntry)));203assertTrue(exists(dir2.resolve(target)));204stream2.deleteFile(target);205206// Test: move dir1/mydir -> dir2/newfile207stream1.move(dirEntry, stream2, target);208assertTrue(notExists(dir1.resolve(dirEntry)));209assertTrue(exists(dir2.resolve(target)));210stream2.deleteDirectory(target);211212// Test: move dir1/mylink -> dir2/newfile213if (supportsLinks) {214stream1.move(linkEntry, stream2, target);215assertTrue(isSymbolicLink(dir2.resolve(target)));216stream2.deleteFile(target);217}218219// Test: move between devices220String testDirAsString = System.getProperty("test.dir");221if (testDirAsString != null) {222Path testDir = Paths.get(testDirAsString);223if (!getFileStore(dir1).equals(getFileStore(testDir))) {224SecureDirectoryStream<Path> ts =225(SecureDirectoryStream<Path>)newDirectoryStream(testDir);226createFile(dir1.resolve(fileEntry));227try {228stream1.move(fileEntry, ts, target);229shouldNotGetHere();230} catch (AtomicMoveNotSupportedException x) { }231ts.close();232stream1.deleteFile(fileEntry);233}234}235236// clean-up237delete(dir1);238delete(dir2);239}240241// null and ClosedDirectoryStreamException242static void miscTests(Path dir) throws IOException {243Path file = Paths.get("file");244createFile(dir.resolve(file));245246SecureDirectoryStream<Path> stream =247(SecureDirectoryStream<Path>)newDirectoryStream(dir);248249// NullPointerException250try {251stream.getFileAttributeView(null);252shouldNotGetHere();253} catch (NullPointerException x) { }254try {255stream.getFileAttributeView(null, BasicFileAttributeView.class);256shouldNotGetHere();257} catch (NullPointerException x) { }258try {259stream.getFileAttributeView(file, null);260shouldNotGetHere();261} catch (NullPointerException x) { }262try {263stream.newByteChannel(null, EnumSet.of(CREATE,WRITE));264shouldNotGetHere();265} catch (NullPointerException x) { }266try {267stream.newByteChannel(null, EnumSet.of(CREATE,WRITE,null));268shouldNotGetHere();269} catch (NullPointerException x) { }270try {271stream.newByteChannel(file, null);272shouldNotGetHere();273} catch (NullPointerException x) { }274try {275stream.move(null, stream, file);276shouldNotGetHere();277} catch (NullPointerException x) { }278try {279stream.move(file, null, file);280shouldNotGetHere();281} catch (NullPointerException x) { }282try {283stream.move(file, stream, null);284shouldNotGetHere();285} catch (NullPointerException x) { }286try {287stream.newDirectoryStream(null);288shouldNotGetHere();289} catch (NullPointerException x) { }290try {291stream.deleteFile(null);292shouldNotGetHere();293} catch (NullPointerException x) { }294try {295stream.deleteDirectory(null);296shouldNotGetHere();297} catch (NullPointerException x) { }298299// close stream300stream.close();301stream.close(); // should be no-op302303// ClosedDirectoryStreamException304try {305stream.newDirectoryStream(file);306shouldNotGetHere();307} catch (ClosedDirectoryStreamException x) { }308try {309stream.newByteChannel(file, EnumSet.of(READ));310shouldNotGetHere();311} catch (ClosedDirectoryStreamException x) { }312try {313stream.move(file, stream, file);314shouldNotGetHere();315} catch (ClosedDirectoryStreamException x) { }316try {317stream.deleteFile(file);318shouldNotGetHere();319} catch (ClosedDirectoryStreamException x) { }320321// clean-up322delete(dir.resolve(file));323}324325static void assertTrue(boolean b) {326if (!b) throw new RuntimeException("Assertion failed");327}328329static void shouldNotGetHere() {330assertTrue(false);331}332}333334335