Path: blob/master/test/jdk/tools/jpackage/windows/WinConsoleTest.java
41149 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.nio.file.Path;24import java.io.InputStream;25import java.io.FileInputStream;26import java.io.IOException;27import jdk.jpackage.test.TKit;28import jdk.jpackage.test.HelloApp;29import jdk.jpackage.test.JPackageCommand;30import jdk.jpackage.test.Annotations.Test;31import jdk.jpackage.test.Annotations.Parameter;3233/*34* @test35* @summary jpackage with --win-console36* @library ../helpers37* @build jdk.jpackage.test.*38* @requires (os.family == "windows")39* @modules jdk.jpackage/jdk.jpackage.internal40* @compile WinConsoleTest.java41*42* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main43* --jpt-before-run=jdk.jpackage.test.JPackageCommand.useToolProviderByDefault44* --jpt-run=WinConsoleTest45*46* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main47* --jpt-run=WinConsoleTest48*/49public class WinConsoleTest {5051@Test52@Parameter("true")53@Parameter("false")54public static void test(boolean withWinConsole) throws IOException {55JPackageCommand cmd = JPackageCommand.helloAppImage();56if (!withWinConsole) {57cmd.removeArgument("--win-console");58}59cmd.executeAndAssertHelloAppImageCreated();60checkSubsystem(cmd.appLauncherPath(), withWinConsole);6162// Run launcher with a number of arguments to make sure they go through63// regardless the launcher has or doesn't have console.64HelloApp.executeLauncherAndVerifyOutput(cmd, "a", "b", "c");65}6667private static void checkSubsystem(Path path, boolean isConsole) throws68IOException {69final int subsystemGui = 2;70final int subsystemConsole = 3;71final int bufferSize = 512;7273final int expectedSubsystem = isConsole ? subsystemConsole : subsystemGui;7475try (InputStream inputStream = new FileInputStream(path.toString())) {76byte[] bytes = new byte[bufferSize];77TKit.assertEquals(bufferSize, inputStream.read(bytes),78String.format("Check %d bytes were read from %s file",79bufferSize, path));8081// Check PE header for console or Win GUI app.82// https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_image_nt_headers83for (int i = 0; i < (bytes.length - 4); i++) {84if (bytes[i] == 0x50 && bytes[i + 1] == 0x4585&& bytes[i + 2] == 0x0 && bytes[i + 3] == 0x0) {8687// Signature, File Header and subsystem offset.88i = i + 4 + 20 + 68;89byte subsystem = bytes[i];90TKit.assertEquals(expectedSubsystem, subsystem,91String.format("Check subsystem of PE [%s] file",92path));93return;94}95}96}9798TKit.assertUnexpected(String.format(99"Subsystem not found in PE header of [%s] file", path));100}101}102103104