test/jdk/java/util/jar/Manifest/IncludeInExceptionsTest.java
changeset 53243 8bea4144b21c
equal deleted inserted replaced
53242:e81edc1f6f7e 53243:8bea4144b21c
       
     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 java.io.File;
       
    25 import java.io.IOException;
       
    26 import java.io.OutputStream;
       
    27 import java.nio.file.Files;
       
    28 import java.nio.file.Paths;
       
    29 import java.util.concurrent.Callable;
       
    30 import java.util.jar.JarEntry;
       
    31 import java.util.jar.JarFile;
       
    32 import java.util.jar.JarOutputStream;
       
    33 
       
    34 import static java.nio.charset.StandardCharsets.UTF_8;
       
    35 
       
    36 /**
       
    37  * @test
       
    38  * @bug 8216362
       
    39  * @run main/othervm -Djdk.includeInExceptions=jar IncludeInExceptionsTest yes
       
    40  * @run main/othervm IncludeInExceptionsTest
       
    41  * @summary Verify that the property jdk.net.includeInExceptions works as expected
       
    42  * when an error occurs while reading an invalid Manifest file.
       
    43  */
       
    44 /*
       
    45  * @see Manifest#Manifest(JarVerifier,InputStream,String)
       
    46  * @see Manifest#getErrorPosition
       
    47  */
       
    48 public class IncludeInExceptionsTest {
       
    49 
       
    50     static final String FILENAME = "Unique-Filename-Expected-In_Msg.jar";
       
    51 
       
    52     static final byte[] INVALID_MANIFEST = (
       
    53             "Manifest-Version: 1.0\r\n" +
       
    54             "\r\n" +
       
    55             "Illegal\r\n" +
       
    56             "\r\n").getBytes(UTF_8);
       
    57 
       
    58     static String createJarInvalidManifest(String jar) throws IOException {
       
    59         try (OutputStream out = Files.newOutputStream(Paths.get(jar));
       
    60             JarOutputStream jos = new JarOutputStream(out)) {
       
    61             JarEntry je = new JarEntry(JarFile.MANIFEST_NAME);
       
    62             jos.putNextEntry(je);
       
    63             jos.write(INVALID_MANIFEST);
       
    64             jos.closeEntry();
       
    65         }
       
    66         return jar;
       
    67     }
       
    68 
       
    69     static void test(Callable<?> attempt, boolean includeInExceptions) throws Exception {
       
    70         try {
       
    71             attempt.call();
       
    72             throw new AssertionError("Excpected Exception not thrown");
       
    73         } catch (IOException e) {
       
    74             boolean foundFileName = e.getMessage().contains(FILENAME);
       
    75             if(includeInExceptions && !foundFileName) {
       
    76                 throw new AssertionError("JAR file name expected but not found in error message");
       
    77             } else if (foundFileName && !includeInExceptions) {
       
    78                 throw new AssertionError("JAR file name found but should not be in error message");
       
    79             }
       
    80         }
       
    81     }
       
    82 
       
    83     public static void main(String[] args) throws Exception {
       
    84 
       
    85         boolean includeInExceptions;
       
    86         if(args.length > 0) {
       
    87             includeInExceptions = true;
       
    88             System.out.println("**** Running test WITH -Djdk.includeInExceptions=jar");
       
    89         } else {
       
    90             includeInExceptions = false;
       
    91             System.out.println("**** Running test WITHOUT -Djdk.includeInExceptions=jar");
       
    92         }
       
    93 
       
    94         test(() -> new JarFile(createJarInvalidManifest(FILENAME)).getManifest(),
       
    95                 includeInExceptions);
       
    96         test(() -> new JarFile(createJarInvalidManifest("Verifying-" + FILENAME),
       
    97                 true).getManifest(), includeInExceptions);
       
    98     }
       
    99 
       
   100 }
       
   101