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.nio.file.Path; 25 import java.io.InputStream; 26 import java.io.FileInputStream; 27 import java.io.IOException; 28 29 import jdk.jpackage.internal.IOUtils; 30 import jdk.jpackage.test.Test; 31 import jdk.jpackage.test.HelloApp; 32 import jdk.jpackage.test.JPackageCommand; 33 34 /* 35 * @test 36 * @summary jpackage with --win-console 37 * @library ../helpers 38 * @requires (os.family == "windows") 39 * @modules jdk.jpackage/jdk.jpackage.internal 40 * @run main/othervm -Xmx512m WinConsoleTest 41 */ 42 public class WinConsoleTest { 43 44 public static void main(String[] args) throws IOException { 45 JPackageCommand cmd = JPackageCommand.helloAppImage(); 46 final Path launcherPath = cmd.appImage().resolve( 47 cmd.launcherPathInAppImage()); 48 49 IOUtils.deleteRecursive(cmd.outputDir().toFile()); 50 cmd.execute().assertExitCodeIsZero(); 51 HelloApp.executeAndVerifyOutput(launcherPath); 52 checkSubsystem(launcherPath, false); 53 54 IOUtils.deleteRecursive(cmd.outputDir().toFile()); 55 cmd.addArgument("--win-console").execute().assertExitCodeIsZero(); 56 HelloApp.executeAndVerifyOutput(launcherPath); 57 checkSubsystem(launcherPath, true); 58 } 59 60 private static void checkSubsystem(Path path, boolean isConsole) throws 61 IOException { 62 final int subsystemGui = 2; 63 final int subsystemConsole = 3; 64 final int bufferSize = 512; 65 66 final int expectedSubsystem = isConsole ? subsystemConsole : subsystemGui; 67 68 try (InputStream inputStream = new FileInputStream(path.toString())) { 69 byte[] bytes = new byte[bufferSize]; 70 Test.assertEquals(bufferSize, inputStream.read(bytes), 71 String.format("Check %d bytes were read from %s file", 72 bufferSize, path)); 73 74 // Check PE header for console or Win GUI app. 75 // https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_image_nt_headers 76 for (int i = 0; i < (bytes.length - 4); i++) { 77 if (bytes[i] == 0x50 && bytes[i + 1] == 0x45 78 && bytes[i + 2] == 0x0 && bytes[i + 3] == 0x0) { 79 80 // Signature, File Header and subsystem offset. 81 i = i + 4 + 20 + 68; 82 byte subsystem = bytes[i]; 83 Test.assertEquals(expectedSubsystem, subsystem, 84 String.format("Check subsystem of PE [%s] file", 85 path)); 86 return; 87 } 88 } 89 } 90 91 Test.assertUnexpected(String.format( 92 "Subsystem not found in PE header of [%s] file", path)); 93 } 94 }