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
  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 
  34 public class SpecialTempFile {
  35 
  36     private static void test(String name, String[] prefix, String[] suffix) {
  37         if (prefix == null || suffix == null
  38             || prefix.length != suffix.length)
  39         {
  40             return;
  41         }
  42 
  43         final String exceptionMsg = "Unable to create temporary file";
  44         final String errMsg = "IOException is expected";
  45 
  46         for (int i = 0; i < prefix.length; i++) {
  47             boolean exceptionThrown = false;
  48             File f = null;
  49             System.out.println("In test " + name
  50                                + ", creating temp file with prefix, "
  51                                + prefix[i] + ", suffix, " + suffix[i]);
  52             try {
  53                 f = File.createTempFile(prefix[i], suffix[i]);
  54             } catch (IOException e) {
  55                 if (exceptionMsg.equals(e.getMessage()))
  56                     exceptionThrown = true;
  57                 else
  58                     System.out.println("Wrong error message:" + e.getMessage());
  59             }
  60             if (!exceptionThrown || f != null)
  61                 throw new RuntimeException(errMsg);
  62         }
  63     }
  64 
  65     public static void main(String[] args) throws Exception {
  66         // Common test
  67         final String name = "SpecialTempFile";
  68         File f = new File(System.getProperty("java.io.tmpdir"), name);
  69         if (!f.exists()) {
  70             f.createNewFile();
  71         }
  72         String[] nulPre = { name + "\u0000" };
  73         String[] nulSuf = { ".test" };
  74         test("NulName", nulPre, nulSuf);
  75 
  76         // Windows tests
  77         if (!System.getProperty("os.name").startsWith("Windows"))
  78             return;
  79 
  80         // Test JDK-8013827
  81         String[] resvPre = { "LPT1.package.zip", "com7.4.package.zip" };
  82         String[] resvSuf = { ".temp", ".temp" };
  83         test("ReservedName", resvPre, resvSuf);
  84 
  85         // Test JDK-8011950
  86         String[] slashPre = { "///..///", "temp", "///..///" };
  87         String[] slashSuf = { ".temp", "///..///..", "///..///.." };
  88         test("SlashedName", slashPre, slashSuf);
  89     }
  90 }