1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6244047
* @author Jim Gish
* @summary throw more precise IOException when pattern specifies invalid directory
*
* @run main/othervm CheckLockLocationTest
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.AccessDeniedException;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.attribute.UserPrincipal;
import java.util.UUID;
import java.util.logging.FileHandler;
public class CheckLockLocationTest {
private static final String NON_WRITABLE_DIR = "non-writable-dir";
private static final String NOT_A_DIR = "not-a-dir";
private static final String WRITABLE_DIR = "writable-dir";
private static final String NON_EXISTENT_DIR = "non-existent-dir";
private static boolean runNonWritableDirTest;
public static void main(String... args) throws IOException {
// we'll base all file creation attempts on the system temp directory,
// %t and also try specifying non-existent directories and plain files
// that should be directories, and non-writable directories,
// to exercise all code paths of checking the lock location
// Note that on platforms like Windows that don't support
// setWritable() on a directory, we'll skip the non-writable
// directory test if setWritable(false) returns false.
//
File writableDir = setup();
// we now have three files/directories to work with:
// writableDir
// notAdir
// nonWritableDir (may not be possible on some platforms)
// nonExistentDir (which doesn't exist)
runTests(writableDir);
}
/**
* @param writableDir in which log and lock file are created
* @throws SecurityException
* @throws RuntimeException
* @throws IOException
*/
private static void runTests(File writableDir) throws SecurityException,
RuntimeException, IOException {
// Test 1: make sure we can create FileHandler in writable directory
try {
new FileHandler("%t/" + WRITABLE_DIR + "/log.log");
} catch (IOException ex) {
throw new RuntimeException("Test failed: should have been able"
+ " to create FileHandler for " + "%t/" + WRITABLE_DIR
+ "/log.log in writable directory"
+ (!writableDir.canRead() // concurrent tests running or user conf issue?
? ": directory not readable.\n\tPlease check your "
+ "environment and machine configuration."
: "."), ex);
} finally {
// the above test leaves files in the directory. Get rid of the
// files created and the directory
delete(writableDir);
}
// Test 2: creating FileHandler in non-writable directory should fail
if (runNonWritableDirTest) {
try {
new FileHandler("%t/" + NON_WRITABLE_DIR + "/log.log");
throw new RuntimeException("Test failed: should not have been able"
+ " to create FileHandler for " + "%t/" + NON_WRITABLE_DIR
+ "/log.log in non-writable directory.");
} catch (AccessDeniedException ex) {
// the right exception was thrown, so continue.
} catch (IOException ex) {
throw new RuntimeException(
"Test failed: Expected exception was not an "
+ "AccessDeniedException", ex);
}
}
// Test 3: creating FileHandler in non-directory should fail
try {
new FileHandler("%t/" + NOT_A_DIR + "/log.log");
throw new RuntimeException("Test failed: should not have been able"
+ " to create FileHandler for " + "%t/" + NOT_A_DIR
+ "/log.log in non-directory.");
} catch (FileSystemException ex) {
// the right exception was thrown, so continue.
} catch (IOException ex) {
throw new RuntimeException("Test failed: exception thrown was not a "
+ "FileSystemException", ex);
}
// Test 4: make sure we can't create a FileHandler in a non-existent dir
try {
new FileHandler("%t/" + NON_EXISTENT_DIR + "/log.log");
throw new RuntimeException("Test failed: should not have been able"
+ " to create FileHandler for " + "%t/" + NON_EXISTENT_DIR
+ "/log.log in a non-existent directory.");
} catch (NoSuchFileException ex) {
// the right exception was thrown, so continue.
} catch (IOException ex) {
throw new RuntimeException("Test failed: Expected exception "
+ "was not a NoSuchFileException", ex);
}
}
/**
* Setup all the files and directories needed for the tests
*
* @return writable directory created that needs to be deleted when done
* @throws RuntimeException
*/
private static File setup() throws RuntimeException {
// First do some setup in the temporary directory (using same logic as
// FileHandler for %t pattern)
String tmpDir = System.getProperty("java.io.tmpdir"); // i.e. %t
if (tmpDir == null) {
tmpDir = System.getProperty("user.home");
}
File tmpOrHomeDir = new File(tmpDir);
// Create a writable directory here (%t/writable-dir)
File writableDir = new File(tmpOrHomeDir, WRITABLE_DIR);
if (!createFile(writableDir, true)) {
throw new RuntimeException("Test setup failed: unable to create"
+ " writable working directory "
+ writableDir.getAbsolutePath() );
}
if (!writableDir.canRead()) {
throw new RuntimeException("Test setup failed: can't read "
+ " writable working directory "
+ writableDir.getAbsolutePath() );
}
// writableDirectory and its contents will be deleted after the test
// that uses it.
// check that we can write in the new writable dir.
File dummyFile = new File(writableDir, UUID.randomUUID().toString() + ".txt" );
try {
if (!dummyFile.createNewFile()) {
throw new RuntimeException("Test setup failed: can't create "
+ " dummy file in writable working directory "
+ dummyFile.getAbsolutePath() );
}
try (OutputStream os = new FileOutputStream(dummyFile)) {
os.write('A');
} finally {
dummyFile.delete();
}
if (dummyFile.canRead()) {
throw new RuntimeException("Test setup failed: can't delete "
+ " dummy file in writable working directory "
+ dummyFile.getAbsolutePath() );
}
System.out.println("Successfully created and deleted dummy file: " +
dummyFile.getAbsolutePath());
} catch(IOException x) {
throw new RuntimeException("Test setup failed: can't write "
+ " or delete dummy file in writable working directory "
+ dummyFile.getAbsolutePath(), x);
}
// Create a plain file which we will attempt to use as a directory
// (%t/not-a-dir)
File notAdir = new File(tmpOrHomeDir, NOT_A_DIR);
if (!createFile(notAdir, false)) {
throw new RuntimeException("Test setup failed: unable to a plain"
+ " working file " + notAdir.getAbsolutePath() );
}
notAdir.deleteOnExit();
// Create a non-writable directory (%t/non-writable-dir)
File nonWritableDir = new File(tmpOrHomeDir, NON_WRITABLE_DIR);
if (!createFile(nonWritableDir, true)) {
throw new RuntimeException("Test setup failed: unable to create"
+ " a non-"
+ "writable working directory "
+ nonWritableDir.getAbsolutePath() );
}
nonWritableDir.deleteOnExit();
// make it non-writable
Path path = nonWritableDir.toPath();
final boolean nonWritable = nonWritableDir.setWritable(false);
final boolean isWritable = Files.isWritable(path);
if (nonWritable && !isWritable) {
runNonWritableDirTest = true;
System.out.println("Created non writable dir for "
+ getOwner(path) + " at: " + path.toString());
} else {
runNonWritableDirTest = false;
System.out.println( "Test Setup WARNING: unable to make"
+ " working directory " + nonWritableDir.getAbsolutePath()
+ "\n\t non-writable for " + getOwner(path)
+ " on platform " + System.getProperty("os.name"));
}
// make sure non-existent directory really doesn't exist
File nonExistentDir = new File(tmpOrHomeDir, NON_EXISTENT_DIR);
if (nonExistentDir.exists()) {
nonExistentDir.delete();
}
System.out.println("Setup completed - writableDir is: " + writableDir.getPath());
return writableDir;
}
private static String getOwner(Path path) {
UserPrincipal user = null;
try {
user = Files.getOwner(path);
} catch (Exception x) {
System.err.println("Failed to get owner of: " + path);
System.err.println("\terror is: " + x);
}
return user == null ? "???" : user.getName();
}
/**
* @param newFile
* @return true if file already exists or creation succeeded
*/
private static boolean createFile(File newFile, boolean makeDirectory) {
if (newFile.exists()) {
return true;
}
if (makeDirectory) {
return newFile.mkdir();
} else {
try {
return newFile.createNewFile();
} catch (IOException ioex) {
ioex.printStackTrace();
return false;
}
}
}
/*
* Recursively delete all files starting at specified file
*/
private static void delete(File f) {
if (f != null && f.isDirectory()) {
for (File c : f.listFiles())
delete(c);
}
if (!f.delete())
System.err.println(
"WARNING: unable to delete/cleanup writable test directory: "
+ f );
}
}
|