test/jdk/tools/jpackage/createappimage/windows/JPackageCreateAppImageWinConsoleTest.java
branchJDK-8200758-branch
changeset 57405 539d8b3f9e1e
child 57407 2c14fbeff1dc
equal deleted inserted replaced
57404:a477b26bf888 57405:539d8b3f9e1e
       
     1 /*
       
     2  * Copyright (c) 2018, 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.IOException;
       
    25 import java.io.File;
       
    26 import java.io.InputStream;
       
    27 import java.io.FileInputStream;
       
    28 
       
    29 /*
       
    30  * @test
       
    31  * @summary jpackage create image win console test
       
    32  * @library ../../helpers
       
    33  * @library ../
       
    34  * @build JPackageHelper
       
    35  * @build JPackagePath
       
    36  * @build JPackageCreateAppImageBase
       
    37  * @requires (os.family == "windows")
       
    38  * @modules jdk.jpackage
       
    39  * @run main/othervm -Xmx512m JPackageCreateAppImageWinConsoleTest
       
    40  */
       
    41 
       
    42 public class JPackageCreateAppImageWinConsoleTest {
       
    43     private static final String NAME = "test";
       
    44     private static final String OUTPUT = "output";
       
    45     private static final String OUTPUT_WIN_CONSOLE = "outputWinConsole";
       
    46     private static final int BUFFER_SIZE = 512;
       
    47     private static final int GUI_SUBSYSTEM = 2;
       
    48     private static final int CONSOLE_SUBSYSTEM = 3;
       
    49 
       
    50     private static final String [] CMD = {
       
    51         "create-app-image",
       
    52         "--input", "input",
       
    53         "--output", OUTPUT,
       
    54         "--name", NAME,
       
    55         "--main-jar", "hello.jar",
       
    56         "--main-class", "Hello",
       
    57     };
       
    58 
       
    59     private static final String [] CMD_WIN_CONSOLE = {
       
    60         "create-app-image",
       
    61         "--input", "input",
       
    62         "--output", OUTPUT_WIN_CONSOLE,
       
    63         "--name", NAME,
       
    64         "--main-jar", "hello.jar",
       
    65         "--main-class", "Hello",
       
    66         "--win-console"
       
    67     };
       
    68 
       
    69     private static void checkSubsystem(boolean console) throws Exception {
       
    70         String file = console ? OUTPUT_WIN_CONSOLE : OUTPUT;
       
    71         file += File.separator;
       
    72         file += NAME;
       
    73         file += File.separator;
       
    74         file += NAME + ".exe";
       
    75 
       
    76         JPackageCreateAppImageBase.validate(file);
       
    77 
       
    78         try (InputStream inputStream = new FileInputStream(file)) {
       
    79             byte [] bytes = new byte[BUFFER_SIZE];
       
    80             if (inputStream.read(bytes) != BUFFER_SIZE) {
       
    81                 throw new AssertionError("Wrong number of bytes read");
       
    82             }
       
    83 
       
    84             // Check PE header for console or Win GUI app.
       
    85             // https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_image_nt_headers
       
    86             for (int i = 0;  i < (bytes.length - 4); i++) {
       
    87                 if (bytes[i] == 0x50 && bytes[i + 1] == 0x45 &&
       
    88                         bytes[i + 2] == 0x0 && bytes[i + 3] == 0x0) {
       
    89                     i = i + 4 + 20 + 68; // Signature, File Header and subsystem offset.
       
    90                     byte subsystem = bytes[i];
       
    91                     if (console) {
       
    92                         if (subsystem != CONSOLE_SUBSYSTEM) {
       
    93                             throw new AssertionError("Unexpected subsystem: " + subsystem);
       
    94                         } else {
       
    95                             return; // done
       
    96                         }
       
    97                     } else {
       
    98                         if (subsystem != GUI_SUBSYSTEM) {
       
    99                             throw new AssertionError("Unexpected subsystem: " + subsystem);
       
   100                         } else {
       
   101                             return; // done
       
   102                         }
       
   103                     }
       
   104                 }
       
   105             }
       
   106         }
       
   107 
       
   108         throw new AssertionError("Unable to verify PE header");
       
   109     }
       
   110 
       
   111     private static void validate() throws Exception {
       
   112         checkSubsystem(false);
       
   113         checkSubsystem(true);
       
   114     }
       
   115 
       
   116     private static void testCreateAppImage(String [] cmd) throws Exception {
       
   117         JPackageHelper.executeCLI(true, cmd);
       
   118     }
       
   119 
       
   120     private static void testCreateAppImageToolProvider(String [] cmd) throws Exception {
       
   121         JPackageHelper.executeToolProvider(true, cmd);
       
   122     }
       
   123 
       
   124     public static void main(String[] args) throws Exception {
       
   125         JPackageHelper.createHelloImageJar();
       
   126         testCreateAppImage(CMD);
       
   127         testCreateAppImage(CMD_WIN_CONSOLE);
       
   128         validate();
       
   129         JPackageHelper.deleteOutputFolder(OUTPUT);
       
   130         JPackageHelper.deleteOutputFolder(OUTPUT_WIN_CONSOLE);
       
   131         testCreateAppImageToolProvider(CMD);
       
   132         testCreateAppImageToolProvider(CMD_WIN_CONSOLE);
       
   133         validate();
       
   134     }
       
   135 }