1 /*
   2  * Copyright (c) 2013, 2017, 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 
  31 import java.io.File;
  32 import java.io.IOException;
  33 import java.nio.file.Files;
  34 import java.nio.file.Paths;
  35 
  36 public class SpecialTempFile {
  37 
  38     private static void test(String name, String[] prefix, String[] suffix,
  39                              boolean exceptionExpected) throws IOException
  40     {
  41         if (prefix == null || suffix == null
  42             || prefix.length != suffix.length)
  43         {
  44             return;
  45         }
  46 
  47         final String exceptionMsg = "Unable to create temporary file";
  48         String[] dirs = { null, "." };
  49 
  50         for (int i = 0; i < prefix.length; i++) {
  51             boolean exceptionThrown = false;
  52             File f = null;
  53 
  54             for (String dir: dirs) {
  55                 System.out.println("In test " + name +
  56                                    ", creating temp file with prefix, " +
  57                                    prefix[i] + ", suffix, " + suffix[i] +
  58                                    ", in dir, " + dir);
  59 
  60                 try {
  61                     if (dir == null || dir.isEmpty())
  62                         f = File.createTempFile(prefix[i], suffix[i]);
  63                     else
  64                         f = File.createTempFile(prefix[i], suffix[i], new File(dir));
  65                 } catch (IOException e) {
  66                     if (exceptionExpected) {
  67                         if (e.getMessage().startsWith(exceptionMsg))
  68                             exceptionThrown = true;
  69                         else
  70                             System.out.println("Wrong error message:" +
  71                                                e.getMessage());
  72                     } else {
  73                         throw e;
  74                     }
  75                 }
  76 
  77                 if (exceptionExpected && (!exceptionThrown || f != null))
  78                     throw new RuntimeException("IOException is expected");
  79             }
  80         }
  81     }
  82 
  83     public static void main(String[] args) throws Exception {
  84         // Common test
  85         final String name = "SpecialTempFile";
  86         String testDir = System.getProperty("test.dir", ".");
  87         File f = Files.createDirectory(Paths.get(testDir, "tmpdir")).toFile();
  88         String[] nulPre = { name + "\u0000" };
  89         String[] nulSuf = { ".test" };
  90         test("NulName", nulPre, nulSuf, true);
  91 
  92         // Test JDK-8025128
  93         String[] goodPre = { "///..///", "/foo" };
  94         String[] goodSuf = { ".temp", ".tmp" };
  95         test("goodName", goodPre, goodSuf, false);
  96 
  97         // Test JDK-8011950
  98         String[] slashPre = { "temp", "///..///", "/foo" };
  99         String[] slashSuf = { "///..///..", "///..///..", "///..///.." };
 100         test("SlashedName", slashPre, slashSuf, true);
 101 
 102         // Windows tests
 103         if (!System.getProperty("os.name").startsWith("Windows"))
 104             return;
 105 
 106         // Test JDK-8013827
 107         String[] resvPre = { "LPT1.package.zip", "com7.4.package.zip" };
 108         String[] resvSuf = { ".temp", ".temp" };
 109         test("ReservedName", resvPre, resvSuf, true);
 110     }
 111 }