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 | 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 |