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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
|
/*
* Copyright (c) 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 8003992 8027155
* @summary Test a file whose path name is embedded with NUL character, and
* ensure it is handled correctly.
* @author Dan Xu
*/
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.InvalidPathException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
public class NulFile {
private static final char CHAR_NUL = '\u0000';
private static final String ExceptionMsg = "Invalid file path";
public static void main(String[] args) {
testFile();
testFileInUnix();
testFileInWindows();
testTempFile();
}
private static void testFile() {
test(new File(new StringBuilder().append(CHAR_NUL).toString()));
test(new File(
new StringBuilder().append("").append(CHAR_NUL).toString()));
test(new File(
new StringBuilder().append(CHAR_NUL).append("").toString()));
}
private static void testFileInUnix() {
String osName = System.getProperty("os.name");
if (osName.startsWith("Windows"))
return;
String unixFile = "/";
test(unixFile);
unixFile = "//";
test(unixFile);
unixFile = "data/info";
test(unixFile);
unixFile = "/data/info";
test(unixFile);
unixFile = "//data//info";
test(unixFile);
}
private static void testFileInWindows() {
String osName = System.getProperty("os.name");
if (!osName.startsWith("Windows"))
return;
String windowsFile = "\\";
test(windowsFile);
windowsFile = "\\\\";
test(windowsFile);
windowsFile = "/";
test(windowsFile);
windowsFile = "//";
test(windowsFile);
windowsFile = "/\\";
test(windowsFile);
windowsFile = "\\/";
test(windowsFile);
windowsFile = "data\\info";
test(windowsFile);
windowsFile = "\\data\\info";
test(windowsFile);
windowsFile = "\\\\server\\data\\info";
test(windowsFile);
windowsFile = "z:data\\info";
test(windowsFile);
windowsFile = "z:\\data\\info";
test(windowsFile);
}
private static void test(final String name) {
int length = name.length();
for (int i = 0; i <= length; i++) {
StringBuilder sbName = new StringBuilder(name);
sbName.insert(i, CHAR_NUL);
String curName = sbName.toString();
// test File(String parent, String child)
File testFile = new File(curName, "child");
test(testFile);
testFile = new File("parent", curName);
test(testFile);
// test File(String pathname)
testFile = new File(curName);
test(testFile);
// test File(File parent, String child)
testFile = new File(new File(curName), "child");
test(testFile);
testFile = new File(new File("parent"), curName);
test(testFile);
// test FileInputStream
testFileInputStream(curName);
// test FileOutputStream
testFileOutputStream(curName);
// test RandomAccessFile
testRandomAccessFile(curName);
}
}
private static void testFileInputStream(final String str) {
boolean exceptionThrown = false;
FileInputStream is = null;
try {
is = new FileInputStream(str);
} catch (FileNotFoundException ex) {
if (ExceptionMsg.equals(ex.getMessage()))
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("FileInputStream constructor"
+ " should throw FileNotFoundException");
}
if (is != null) {
throw new RuntimeException("FileInputStream constructor"
+ " should fail");
}
exceptionThrown = false;
is = null;
try {
is = new FileInputStream(new File(str));
} catch (FileNotFoundException ex) {
if (ExceptionMsg.equals(ex.getMessage()))
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("FileInputStream constructor"
+ " should throw FileNotFoundException");
}
if (is != null) {
throw new RuntimeException("FileInputStream constructor"
+ " should fail");
}
}
private static void testFileOutputStream(final String str) {
boolean exceptionThrown = false;
FileOutputStream os = null;
try {
os = new FileOutputStream(str);
} catch (FileNotFoundException ex) {
if (ExceptionMsg.equals(ex.getMessage()))
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("FileOutputStream constructor"
+ " should throw FileNotFoundException");
}
if (os != null) {
throw new RuntimeException("FileOutputStream constructor"
+ " should fail");
}
exceptionThrown = false;
os = null;
try {
os = new FileOutputStream(new File(str));
} catch (FileNotFoundException ex) {
if (ExceptionMsg.equals(ex.getMessage()))
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("FileOutputStream constructor"
+ " should throw FileNotFoundException");
}
if (os != null) {
throw new RuntimeException("FileOutputStream constructor"
+ " should fail");
}
}
private static void testRandomAccessFile(final String str) {
boolean exceptionThrown = false;
RandomAccessFile raf = null;
String[] modes = {"r", "rw", "rws", "rwd"};
for (String mode : modes) {
try {
raf = new RandomAccessFile(str, mode);
} catch (FileNotFoundException ex) {
if (ExceptionMsg.equals(ex.getMessage()))
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("RandomAccessFile constructor"
+ " should throw FileNotFoundException");
}
if (raf != null) {
throw new RuntimeException("RandomAccessFile constructor"
+ " should fail");
}
exceptionThrown = false;
raf = null;
try {
raf = new RandomAccessFile(new File(str), mode);
} catch (FileNotFoundException ex) {
if (ExceptionMsg.equals(ex.getMessage()))
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("RandomAccessFile constructor"
+ " should throw FileNotFoundException");
}
if (raf != null) {
throw new RuntimeException("RandomAccessFile constructor"
+ " should fail");
}
}
}
private static void test(File testFile) {
test(testFile, false);
// test serialization
testSerialization(testFile);
}
@SuppressWarnings("deprecation")
private static void test(File testFile, boolean derived) {
boolean exceptionThrown = false;
if (testFile == null) {
throw new RuntimeException("test file should not be null.");
}
// getPath()
if (testFile.getPath().indexOf(CHAR_NUL) < 0) {
throw new RuntimeException(
"File path should contain Nul character");
}
// getAbsolutePath()
if (testFile.getAbsolutePath().indexOf(CHAR_NUL) < 0) {
throw new RuntimeException(
"File absolute path should contain Nul character");
}
// getAbsoluteFile()
File derivedAbsFile = testFile.getAbsoluteFile();
if (derived) {
if (derivedAbsFile.getPath().indexOf(CHAR_NUL) < 0) {
throw new RuntimeException(
"Derived file path should also contain Nul character");
}
} else {
test(derivedAbsFile, true);
}
// getCanonicalPath()
try {
exceptionThrown = false;
testFile.getCanonicalPath();
} catch (IOException ex) {
if (ExceptionMsg.equals(ex.getMessage()))
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException(
"getCanonicalPath() should throw IOException with"
+ " message \"" + ExceptionMsg + "\"");
}
// getCanonicalFile()
try {
exceptionThrown = false;
testFile.getCanonicalFile();
} catch (IOException ex) {
if (ExceptionMsg.equals(ex.getMessage()))
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException(
"getCanonicalFile() should throw IOException with"
+ " message \"" + ExceptionMsg + "\"");
}
// toURL()
try {
exceptionThrown = false;
testFile.toURL();
} catch (MalformedURLException ex) {
if (ExceptionMsg.equals(ex.getMessage()))
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("toURL() should throw IOException with"
+ " message \"" + ExceptionMsg + "\"");
}
// canRead()
if (testFile.canRead())
throw new RuntimeException("File should not be readable");
// canWrite()
if (testFile.canWrite())
throw new RuntimeException("File should not be writable");
// exists()
if (testFile.exists())
throw new RuntimeException("File should not be existed");
// isDirectory()
if (testFile.isDirectory())
throw new RuntimeException("File should not be a directory");
// isFile()
if (testFile.isFile())
throw new RuntimeException("File should not be a file");
// isHidden()
if (testFile.isHidden())
throw new RuntimeException("File should not be hidden");
// lastModified()
if (testFile.lastModified() != 0L)
throw new RuntimeException("File last modified time should be 0L");
// length()
if (testFile.length() != 0L)
throw new RuntimeException("File length should be 0L");
// createNewFile()
try {
exceptionThrown = false;
testFile.createNewFile();
} catch (IOException ex) {
if (ExceptionMsg.equals(ex.getMessage()))
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException(
"createNewFile() should throw IOException with"
+ " message \"" + ExceptionMsg + "\"");
}
// delete()
if (testFile.delete())
throw new RuntimeException("Delete operation should fail");
// list()
if (testFile.list() != null)
throw new RuntimeException("File list() should return null");
// list(FilenameFilter)
FilenameFilter fnFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return false;
}
};
if (testFile.list(fnFilter) != null) {
throw new RuntimeException("File list(FilenameFilter) should"
+ " return null");
}
// listFiles()
if (testFile.listFiles() != null)
throw new RuntimeException("File listFiles() should return null");
// listFiles(FilenameFilter)
if (testFile.listFiles(fnFilter) != null) {
throw new RuntimeException("File listFiles(FilenameFilter)"
+ " should return null");
}
// listFiles(FileFilter)
FileFilter fFilter = new FileFilter() {
@Override
public boolean accept(File file) {
return false;
}
};
if (testFile.listFiles(fFilter) != null) {
throw new RuntimeException("File listFiles(FileFilter)"
+ " should return null");
}
// mkdir()
if (testFile.mkdir()) {
throw new RuntimeException("File should not be able to"
+ " create directory");
}
// mkdirs()
if (testFile.mkdirs()) {
throw new RuntimeException("File should not be able to"
+ " create directories");
}
// renameTo(File)
if (testFile.renameTo(new File("dest")))
throw new RuntimeException("File rename should fail");
if (new File("dest").renameTo(testFile))
throw new RuntimeException("File rename should fail");
try {
exceptionThrown = false;
testFile.renameTo(null);
} catch (NullPointerException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("File rename should thrown NPE");
}
// setLastModified(long)
if (testFile.setLastModified(0L)) {
throw new RuntimeException("File should fail to set"
+ " last modified time");
}
try {
exceptionThrown = false;
testFile.setLastModified(-1);
} catch (IllegalArgumentException ex) {
if ("Negative time".equals(ex.getMessage()))
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("File should fail to set"
+ " last modified time with message \"Negative time\"");
}
// setReadOnly()
if (testFile.setReadOnly())
throw new RuntimeException("File should fail to set read-only");
// setWritable(boolean writable, boolean ownerOnly)
if (testFile.setWritable(true, true))
throw new RuntimeException("File should fail to set writable");
if (testFile.setWritable(true, false))
throw new RuntimeException("File should fail to set writable");
if (testFile.setWritable(false, true))
throw new RuntimeException("File should fail to set writable");
if (testFile.setWritable(false, false))
throw new RuntimeException("File should fail to set writable");
// setWritable(boolean writable)
if (testFile.setWritable(false))
throw new RuntimeException("File should fail to set writable");
if (testFile.setWritable(true))
throw new RuntimeException("File should fail to set writable");
// setReadable(boolean readable, boolean ownerOnly)
if (testFile.setReadable(true, true))
throw new RuntimeException("File should fail to set readable");
if (testFile.setReadable(true, false))
throw new RuntimeException("File should fail to set readable");
if (testFile.setReadable(false, true))
throw new RuntimeException("File should fail to set readable");
if (testFile.setReadable(false, false))
throw new RuntimeException("File should fail to set readable");
// setReadable(boolean readable)
if (testFile.setReadable(false))
throw new RuntimeException("File should fail to set readable");
if (testFile.setReadable(true))
throw new RuntimeException("File should fail to set readable");
// setExecutable(boolean executable, boolean ownerOnly)
if (testFile.setExecutable(true, true))
throw new RuntimeException("File should fail to set executable");
if (testFile.setExecutable(true, false))
throw new RuntimeException("File should fail to set executable");
if (testFile.setExecutable(false, true))
throw new RuntimeException("File should fail to set executable");
if (testFile.setExecutable(false, false))
throw new RuntimeException("File should fail to set executable");
// setExecutable(boolean executable)
if (testFile.setExecutable(false))
throw new RuntimeException("File should fail to set executable");
if (testFile.setExecutable(true))
throw new RuntimeException("File should fail to set executable");
// canExecute()
if (testFile.canExecute())
throw new RuntimeException("File should not be executable");
// getTotalSpace()
if (testFile.getTotalSpace() != 0L)
throw new RuntimeException("The total space should be 0L");
// getFreeSpace()
if (testFile.getFreeSpace() != 0L)
throw new RuntimeException("The free space should be 0L");
// getUsableSpace()
if (testFile.getUsableSpace() != 0L)
throw new RuntimeException("The usable space should be 0L");
// compareTo(File null)
try {
exceptionThrown = false;
testFile.compareTo(null);
} catch (NullPointerException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("compareTo(null) should throw NPE");
}
// toString()
if (testFile.toString().indexOf(CHAR_NUL) < 0) {
throw new RuntimeException(
"File path should contain Nul character");
}
// toPath()
try {
exceptionThrown = false;
testFile.toPath();
} catch (InvalidPathException ex) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new RuntimeException("toPath() should throw"
+ " InvalidPathException");
}
}
private static void testSerialization(File testFile) {
String path = testFile.getPath();
try {
// serialize test file
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(testFile);
oos.close();
// deserialize test file
byte[] bytes = baos.toByteArray();
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(is);
File newFile = (File) ois.readObject();
// test
String newPath = newFile.getPath();
if (!path.equals(newPath)) {
throw new RuntimeException(
"Serialization should not change file path");
}
test(newFile, false);
} catch (IOException | ClassNotFoundException ex) {
System.err.println("Exception happens in testSerialization");
System.err.println(ex.getMessage());
}
}
private static void testTempFile() {
final String[] names = {"x", "xx", "xxx", "xxxx"};
final String shortPrefix = "sp";
final String prefix = "prefix";
final String suffix = "suffix";
File tmpDir = new File("tmpDir");
for (String name : names) {
int length = name.length();
for (int i = 0; i <= length; i++) {
StringBuilder sbName = new StringBuilder(name);
sbName.insert(i, CHAR_NUL);
String curName = sbName.toString();
// test prefix
testCreateTempFile(curName, suffix, tmpDir);
// test suffix
testCreateTempFile(shortPrefix, curName, tmpDir);
testCreateTempFile(prefix, curName, tmpDir);
// test directory
testCreateTempFile(shortPrefix, suffix, new File(curName));
testCreateTempFile(prefix, suffix, new File(curName));
}
}
}
private static void testCreateTempFile(String prefix, String suffix,
File directory) {
// createTempFile(String prefix, String suffix, File directory)
boolean exceptionThrown = false;
boolean shortPrefix = (prefix.length() < 3);
if (shortPrefix) {
try {
File.createTempFile(prefix, suffix, directory);
} catch (IllegalArgumentException ex) {
String actual = ex.getMessage();
String expected = "Prefix string \"" + prefix +
"\" too short: length must be at least 3";
if (actual != null && actual.equals(expected))
exceptionThrown = true;
} catch (IOException ioe) {
System.err.println("IOException happens in testCreateTempFile");
System.err.println(ioe.getMessage());
}
} else {
try {
File.createTempFile(prefix, suffix, directory);
} catch (IOException ex) {
String err = "Unable to create temporary file";
if (ex.getMessage() != null && ex.getMessage().startsWith(err))
exceptionThrown = true;
else {
throw new RuntimeException("Get IOException with message, "
+ ex.getMessage() + ", expect message, "+ err);
}
}
}
if (!exceptionThrown) {
throw new RuntimeException("createTempFile() should throw"
+ (shortPrefix ? " IllegalArgumentException"
: " IOException"));
}
}
}
|