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 import jdk.jpackage.test.TKit; 29 import jdk.jpackage.test.HelloApp; 30 import jdk.jpackage.test.JPackageCommand; 31 import jdk.jpackage.test.Annotations.Test; 32 import jdk.jpackage.test.Annotations.Parameter; 33 34 /* 35 * @test 36 * @summary jpackage with --win-console 37 * @library ../helpers 38 * @build jdk.jpackage.test.* 39 * @requires (os.family == "windows") 40 * @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal 41 * @compile WinConsoleTest.java 42 * 43 * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main 44 * --jpt-before-run=jdk.jpackage.test.JPackageCommand.useToolProviderByDefault 45 * --jpt-run=WinConsoleTest 46 * 47 * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main 48 * --jpt-run=WinConsoleTest 49 */ 50 public class WinConsoleTest { 51 52 @Test 53 @Parameter("true") 54 @Parameter("false") 55 public static void test(boolean withWinConsole) throws IOException { 56 JPackageCommand cmd = JPackageCommand.helloAppImage(); 57 if (!withWinConsole) { 58 cmd.removeArgument("--win-console"); 59 } 60 cmd.executeAndAssertHelloAppImageCreated(); 61 checkSubsystem(cmd.appLauncherPath(), withWinConsole); 62 63 // Run launcher with a number of arguments to make sure they go through 64 // regardless the launcher has or doesn't have console. 65 HelloApp.executeLauncherAndVerifyOutput(cmd, "a", "b", "c"); 66 } 67 68 private static void checkSubsystem(Path path, boolean isConsole) throws 69 IOException { 70 final int subsystemGui = 2; 71 final int subsystemConsole = 3; 72 final int bufferSize = 512; 73 74 final int expectedSubsystem = isConsole ? subsystemConsole : subsystemGui; 75 76 try (InputStream inputStream = new FileInputStream(path.toString())) { 77 byte[] bytes = new byte[bufferSize]; 78 TKit.assertEquals(bufferSize, inputStream.read(bytes), 79 String.format("Check %d bytes were read from %s file", 80 bufferSize, path)); 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 TKit.assertEquals(expectedSubsystem, subsystem, 92 String.format("Check subsystem of PE [%s] file", 93 path)); 94 return; 95 } 96 } 97 } 98 99 TKit.assertUnexpected(String.format( 100 "Subsystem not found in PE header of [%s] file", path)); 101 } 102 }