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