1/* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 8013827 8011950 8017212 8025128 27 * @summary Check whether File.createTempFile can handle special parameters 28 * @author Dan Xu 29 */ 30 31import java.io.File; 32import java.io.IOException; 33import java.nio.file.Files; 34import java.nio.file.Path; 35import java.nio.file.Paths; 36 37public class SpecialTempFile { 38 39 private static void test(String name, String[] prefix, String[] suffix, 40 boolean exceptionExpected) throws IOException 41 { 42 if (prefix == null || suffix == null 43 || prefix.length != suffix.length) 44 { 45 return; 46 } 47 48 final String exceptionMsg = "Unable to create temporary file"; 49 String[] dirs = { null, "." }; 50 51 Path testPath = Paths.get(System.getProperty("test.dir", ".")); 52 for (int i = 0; i < prefix.length; i++) { 53 boolean exceptionThrown = false; 54 File f = null; 55 56 for (String dir: dirs) { 57 Path tempDir = Files.createTempDirectory(testPath, dir); 58 System.out.println("In test " + name + 59 ", creating temp file with prefix, " + 60 prefix[i] + ", suffix, " + suffix[i] + 61 ", in dir, " + tempDir); 62 63 try { 64 f = File.createTempFile(prefix[i], suffix[i], 65 tempDir.toFile()); 66 } catch (IOException e) { 67 if (exceptionExpected) { 68 if (e.getMessage().startsWith(exceptionMsg)) 69 exceptionThrown = true; 70 else 71 System.out.println("Wrong error message:" + 72 e.getMessage()); 73 } else { 74 throw e; 75 } 76 } 77 78 if (exceptionExpected && (!exceptionThrown || f != null)) 79 throw new RuntimeException("IOException is expected"); 80 } 81 } 82 } 83 84 public static void main(String[] args) throws Exception { 85 // Common test 86 final String name = "SpecialTempFile"; 87 String[] nulPre = { name + "\u0000" }; 88 String[] nulSuf = { ".test" }; 89 test("NulName", nulPre, nulSuf, true); 90 91 // Test JDK-8025128 92 String[] goodPre = { "///..///", "/foo" }; 93 String[] goodSuf = { ".temp", ".tmp" }; 94 test("goodName", goodPre, goodSuf, false); 95 96 // Test JDK-8011950 97 String[] slashPre = { "temp", "///..///", "/foo" }; 98 String[] slashSuf = { "///..///..", "///..///..", "///..///.." }; 99 test("SlashedName", slashPre, slashSuf, true); 100 101 // Windows tests 102 if (!System.getProperty("os.name").startsWith("Windows")) 103 return; 104 105 // Test JDK-8013827 106 String[] resvPre = { "LPT1.package.zip", "com7.4.package.zip" }; 107 String[] resvSuf = { ".temp", ".temp" }; 108 test("ReservedName", resvPre, resvSuf, true); 109 } 110} 111