jdk/test/java/io/File/createTempFile/SpecialTempFile.java
author dxu
Mon, 10 Jun 2013 11:06:26 -0700
changeset 18157 ee3bda8e26c6
child 18807 7d65f90d7348
permissions -rw-r--r--
8013827: File.createTempFile hangs with temp file starting with 'com1.4' 8011950: java.io.File.createTempFile enters infinite loop when passed invalid data Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18157
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
     1
/*
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
     4
 *
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
     8
 *
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    13
 * accompanied this code).
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    14
 *
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    18
 *
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    21
 * questions.
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    22
 */
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    23
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    24
/*
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    25
 * @test
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    26
 * @bug 8013827 8011950
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    27
 * @summary Check whether File.createTempFile can handle special parameters
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    28
 *          on Windows platforms
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    29
 * @author Dan Xu
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    30
 */
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    31
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    32
import java.io.File;
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    33
import java.io.IOException;
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    34
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    35
public class SpecialTempFile {
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    36
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    37
    private static void test(String name, String[] prefix, String[] suffix) {
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    38
        if (prefix == null || suffix == null
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    39
            || prefix.length != suffix.length)
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    40
        {
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    41
            return;
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    42
        }
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    43
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    44
        final String exceptionMsg = "Unable to create temporary file";
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    45
        final String errMsg = "IOException is expected";
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    46
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    47
        for (int i = 0; i < prefix.length; i++) {
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    48
            boolean exceptionThrown = false;
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    49
            File f = null;
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    50
            System.out.println("In test " + name
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    51
                               + ", creating temp file with prefix, "
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    52
                               + prefix[i] + ", suffix, " + suffix[i]);
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    53
            try {
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    54
                f = File.createTempFile(prefix[i], suffix[i]);
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    55
            } catch (IOException e) {
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    56
                if (exceptionMsg.equals(e.getMessage()))
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    57
                    exceptionThrown = true;
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    58
                else
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    59
                    System.out.println("Wrong error message:" + e.getMessage());
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    60
            }
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    61
            if (!exceptionThrown || f != null)
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    62
                throw new RuntimeException(errMsg);
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    63
        }
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    64
    }
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    65
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    66
    public static void main(String[] args) throws Exception {
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    67
        if (!System.getProperty("os.name").startsWith("Windows"))
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    68
            return;
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    69
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    70
        // Test JDK-8013827
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    71
        String[] resvPre = { "LPT1.package.zip", "com7.4.package.zip" };
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    72
        String[] resvSuf = { ".temp", ".temp" };
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    73
        test("ReservedName", resvPre, resvSuf);
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    74
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    75
        // Test JDK-8011950
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    76
        String[] slashPre = { "///..///", "temp", "///..///" };
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    77
        String[] slashSuf = { ".temp", "///..///..", "///..///.." };
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    78
        test("SlashedName", slashPre, slashSuf);
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    79
    }
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents:
diff changeset
    80
}