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.nio.file.Path; 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 ../share 34 * @build JPackageHelper 35 * @build JPackagePath 36 * @build Base 37 * @requires (os.family == "windows") 38 * @modules jdk.jpackage 39 * @run main/othervm -Xmx512m WinConsoleTest 40 */ 41 42 public class WinConsoleTest { 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 "--package-type", "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 "--package-type", "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 Path path = Path.of(console ? OUTPUT_WIN_CONSOLE : OUTPUT, 71 NAME, NAME + ".exe"); 72 73 System.out.println("validate path: " + path.toString()); 74 Base.validate(path.toString()); 75 76 try (InputStream inputStream = new FileInputStream(path.toString())) { 77 byte [] bytes = new byte[BUFFER_SIZE]; 78 if (inputStream.read(bytes) != BUFFER_SIZE) { 79 throw new AssertionError("Wrong number of bytes read"); 80 } 81 82 // Check PE header for console or Win GUI app. 83 // https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_image_nt_headers 84 for (int i = 0; i < (bytes.length - 4); i++) { 85 if (bytes[i] == 0x50 && bytes[i + 1] == 0x45 && 86 bytes[i + 2] == 0x0 && bytes[i + 3] == 0x0) { 87 88 // Signature, File Header and subsystem offset. 89 i = i + 4 + 20 + 68; 90 byte subsystem = bytes[i]; 91 if (console) { 92 if (subsystem != CONSOLE_SUBSYSTEM) { 93 throw new AssertionError("Unexpected subsystem: " 94 + subsystem); 95 } else { 96 return; 97 } 98 } else { 99 if (subsystem != GUI_SUBSYSTEM) { 100 throw new AssertionError("Unexpected subsystem: " 101 + subsystem); 102 } else { 103 return; 104 } 105 } 106 } 107 } 108 } 109 110 throw new AssertionError("Unable to verify PE header"); 111 } 112 113 private static void validate() throws Exception { 114 checkSubsystem(false); 115 checkSubsystem(true); 116 } 117 118 private static void testCreateAppImage(String [] cmd) throws Exception { 119 JPackageHelper.executeCLI(true, cmd); 120 } 121 122 private static void testCreateAppImageToolProvider(String [] cmd) 123 throws Exception { 124 JPackageHelper.executeToolProvider(true, cmd); 125 } 126 127 public static void main(String[] args) throws Exception { 128 JPackageHelper.createHelloImageJar(); 129 testCreateAppImage(CMD); 130 testCreateAppImage(CMD_WIN_CONSOLE); 131 validate(); 132 JPackageHelper.deleteOutputFolder(OUTPUT); 133 JPackageHelper.deleteOutputFolder(OUTPUT_WIN_CONSOLE); 134 testCreateAppImageToolProvider(CMD); 135 testCreateAppImageToolProvider(CMD_WIN_CONSOLE); 136 validate(); 137 } 138 }