test/langtools/tools/javac/file/FSInfoTest.java
changeset 59229 fba8fa613d1a
equal deleted inserted replaced
59228:ddb327877207 59229:fba8fa613d1a
       
     1 /*
       
     2  * Copyright (c) 2019, 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 import com.sun.tools.javac.file.FSInfo;
       
    25 import com.sun.tools.javac.util.Context;
       
    26 import org.testng.annotations.Test;
       
    27 
       
    28 import java.io.IOException;
       
    29 import java.nio.file.Files;
       
    30 import java.nio.file.Path;
       
    31 import java.util.Locale;
       
    32 import java.util.jar.JarOutputStream;
       
    33 import java.util.jar.Manifest;
       
    34 
       
    35 /*
       
    36  * @test
       
    37  * @bug 8232170
       
    38  * @summary Test com.sun.tools.javac.file.FSInfo
       
    39  * @modules jdk.compiler/com.sun.tools.javac.util
       
    40  *          jdk.compiler/com.sun.tools.javac.file
       
    41  * @run testng FSInfoTest
       
    42  */
       
    43 public class FSInfoTest {
       
    44 
       
    45     /**
       
    46      * Tests that if a jar file has a manifest with a invalid path value for {@code Class-Path} attribute,
       
    47      * then parsing such a jar file through {@link FSInfo#getJarClassPath(Path)} doesn't throw any other
       
    48      * exception other than {@link IOException}
       
    49      *
       
    50      * @throws Exception
       
    51      */
       
    52     @Test
       
    53     public void testInvalidClassPath() throws Exception {
       
    54         final String invalidOSPath = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows")
       
    55                 ? "C:\\*" : "foo\u0000bar";
       
    56         final Path jarFile = Files.createTempFile(null, ".jar");
       
    57         jarFile.toFile().deleteOnExit();
       
    58         final Manifest mf = new Manifest();
       
    59         mf.getMainAttributes().putValue("Manifest-Version", "1.0");
       
    60         // add Class-Path which points to an invalid path
       
    61         System.out.println("Intentionally using an invalid Class-Path entry " + invalidOSPath + " in manifest");
       
    62         mf.getMainAttributes().putValue("Class-Path", invalidOSPath + " " + "/some/other-random/path");
       
    63 
       
    64         // create a jar file with the manifest
       
    65         try (final JarOutputStream jar = new JarOutputStream(Files.newOutputStream(jarFile), mf)) {
       
    66         }
       
    67         final FSInfo fsInfo = FSInfo.instance(new Context());
       
    68         try {
       
    69             fsInfo.getJarClassPath(jarFile);
       
    70             // we don't rely on fsInfo.getJarClassPath to throw an exception for invalid
       
    71             // paths. Hence no Assert.fail(...) call here. But if it does throw some exception,
       
    72             // then that exception should always be a IOException.
       
    73         } catch (IOException ioe) {
       
    74             // expected
       
    75             System.out.println("(As expected) FSInfo.getJarClassPath threw an IOException - " + ioe.getMessage());
       
    76         }
       
    77     }
       
    78 }