1 /* 2 * Copyright (c) 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 package jdk.jpackage.test; 24 25 import java.io.IOException; 26 import java.nio.file.Files; 27 import java.nio.file.Path; 28 import java.util.HashMap; 29 import java.util.Map; 30 import java.util.Optional; 31 import java.util.stream.Collectors; 32 import java.util.stream.Stream; 33 34 public class WindowsHelper { 35 36 static String getBundleName(JPackageCommand cmd) { 37 cmd.verifyIsOfType(PackageType.WINDOWS); 38 return String.format("%s-%s%s", cmd.name(), cmd.version(), 39 cmd.packageType().getSuffix()); 40 } 41 42 static Path getInstallationDirectory(JPackageCommand cmd) { 43 cmd.verifyIsOfType(PackageType.WINDOWS); 44 Path installDir = Path.of( 45 cmd.getArgumentValue("--install-dir", () -> cmd.name())); 46 if (isUserLocalInstall(cmd)) { 47 return USER_LOCAL.resolve(installDir); 48 } 49 return PROGRAM_FILES.resolve(installDir); 50 } 51 52 private static boolean isUserLocalInstall(JPackageCommand cmd) { 53 return cmd.hasArgument("--win-per-user-install"); 54 } 55 56 static class AppVerifier { 57 58 AppVerifier(JPackageCommand cmd) { 59 cmd.verifyIsOfType(PackageType.WINDOWS); 60 this.cmd = cmd; 61 verifyStartMenuShortcut(); 62 verifyDesktopShortcut(); 63 verifyFileAssociationsRegistry(); 64 } 65 66 private void verifyDesktopShortcut() { 67 boolean appInstalled = cmd.launcherInstallationPath().toFile().exists(); 68 if (cmd.hasArgument("--win-shortcut")) { 69 if (isUserLocalInstall(cmd)) { 70 verifyUserLocalDesktopShortcut(appInstalled); 71 verifySystemDesktopShortcut(false); 72 } else { 73 verifySystemDesktopShortcut(appInstalled); 74 verifyUserLocalDesktopShortcut(false); 75 } 76 } else { 77 verifySystemDesktopShortcut(false); 78 verifyUserLocalDesktopShortcut(false); 79 } 80 } 81 82 private Path desktopShortcutPath() { 83 return Path.of(cmd.name() + ".lnk"); 84 } 85 86 private void verifySystemDesktopShortcut(boolean exists) { 87 Path dir = Path.of(queryRegistryValueCache( 88 SYSTEM_SHELL_FOLDERS_REGKEY, "Common Desktop")); 89 Test.assertFileExists(dir.resolve(desktopShortcutPath()), exists); 90 } 91 92 private void verifyUserLocalDesktopShortcut(boolean exists) { 93 Path dir = Path.of( 94 queryRegistryValueCache(USER_SHELL_FOLDERS_REGKEY, "Desktop")); 95 Test.assertFileExists(dir.resolve(desktopShortcutPath()), exists); 96 } 97 98 private void verifyStartMenuShortcut() { 99 boolean appInstalled = cmd.launcherInstallationPath().toFile().exists(); 100 if (cmd.hasArgument("--win-menu")) { 101 if (isUserLocalInstall(cmd)) { 102 verifyUserLocalStartMenuShortcut(appInstalled); 103 verifySystemStartMenuShortcut(false); 104 } else { 105 verifySystemStartMenuShortcut(appInstalled); 106 verifyUserLocalStartMenuShortcut(false); 107 } 108 } else { 109 verifySystemStartMenuShortcut(false); 110 verifyUserLocalStartMenuShortcut(false); 111 } 112 } 113 114 private Path startMenuShortcutPath() { 115 return Path.of(cmd.getArgumentValue("--win-menu-group", 116 () -> "Unknown"), cmd.name() + ".lnk"); 117 } 118 119 private void verifySystemStartMenuShortcut(boolean exists) { 120 Path dir = Path.of(queryRegistryValueCache( 121 SYSTEM_SHELL_FOLDERS_REGKEY, "Common Programs")); 122 Test.assertFileExists(dir.resolve(startMenuShortcutPath()), exists); 123 } 124 125 private void verifyUserLocalStartMenuShortcut(boolean exists) { 126 Path dir = Path.of(queryRegistryValueCache( 127 USER_SHELL_FOLDERS_REGKEY, "Programs")); 128 Test.assertFileExists(dir.resolve(startMenuShortcutPath()), exists); 129 } 130 131 private void verifyFileAssociationsRegistry() { 132 Stream.of(cmd.getAllArgumentValues("--file-associations")).map( 133 Path::of).forEach(this::verifyFileAssociationsRegistry); 134 } 135 136 private void verifyFileAssociationsRegistry(Path faFile) { 137 boolean appInstalled = cmd.launcherInstallationPath().toFile().exists(); 138 try { 139 Test.trace(String.format( 140 "Get file association properties from [%s] file", 141 faFile)); 142 Map<String, String> faProps = Files.readAllLines(faFile).stream().filter( 143 line -> line.trim().startsWith("extension=") || line.trim().startsWith( 144 "mime-type=")).map( 145 line -> { 146 String[] keyValue = line.trim().split("=", 2); 147 return Map.entry(keyValue[0], keyValue[1]); 148 }).collect(Collectors.toMap( 149 entry -> entry.getKey(), 150 entry -> entry.getValue())); 151 String suffix = faProps.get("extension"); 152 String contentType = faProps.get("mime-type"); 153 Test.assertNotNull(suffix, String.format( 154 "Check file association suffix [%s] is found in [%s] property file", 155 suffix, faFile)); 156 Test.assertNotNull(contentType, String.format( 157 "Check file association content type [%s] is found in [%s] property file", 158 contentType, faFile)); 159 verifyFileAssociations(appInstalled, "." + suffix, contentType); 160 } catch (IOException ex) { 161 throw new RuntimeException(ex); 162 } 163 } 164 165 private void verifyFileAssociations(boolean exists, String suffix, 166 String contentType) { 167 String contentTypeFromRegistry = queryRegistryValue(Path.of( 168 "HKLM\\Software\\Classes", suffix).toString(), 169 "Content Type"); 170 String suffixFromRegistry = queryRegistryValue( 171 "HKLM\\Software\\Classes\\MIME\\Database\\Content Type\\" + contentType, 172 "Extension"); 173 174 if (exists) { 175 Test.assertEquals(suffix, suffixFromRegistry, 176 "Check suffix in registry is as expected"); 177 Test.assertEquals(contentType, contentTypeFromRegistry, 178 "Check content type in registry is as expected"); 179 } else { 180 Test.assertNull(suffixFromRegistry, 181 "Check suffix in registry not found"); 182 Test.assertNull(contentTypeFromRegistry, 183 "Check content type in registry not found"); 184 } 185 } 186 187 private final JPackageCommand cmd; 188 } 189 190 private static String queryRegistryValue(String keyPath, String valueName) { 191 Executor.Result status = new Executor() 192 .setExecutable("reg") 193 .saveOutput() 194 .addArguments("query", keyPath, "/v", valueName) 195 .execute(); 196 if (status.exitCode == 1) { 197 // Should be the case of no such registry value or key 198 String lookupString = "ERROR: The system was unable to find the specified registry key or value."; 199 status.getOutput().stream().filter(line -> line.equals(lookupString)).findFirst().orElseThrow( 200 () -> new RuntimeException(String.format( 201 "Failed to find [%s] string in the output", 202 lookupString))); 203 Test.trace(String.format( 204 "Registry value [%s] at [%s] path not found", valueName, 205 keyPath)); 206 return null; 207 } 208 209 String value = status.assertExitCodeIsZero().getOutput().stream().skip(2).findFirst().orElseThrow(); 210 // Extract the last field from the following line: 211 // Common Desktop REG_SZ C:\Users\Public\Desktop 212 value = value.split(" REG_SZ ")[1]; 213 214 Test.trace(String.format("Registry value [%s] at [%s] path is [%s]", 215 valueName, keyPath, value)); 216 217 return value; 218 } 219 220 private static String queryRegistryValueCache(String keyPath, 221 String valueName) { 222 String key = String.format("[%s][%s]", keyPath, valueName); 223 String value = REGISTRY_VALUES.get(key); 224 if (value == null) { 225 value = queryRegistryValue(keyPath, valueName); 226 REGISTRY_VALUES.put(key, value); 227 } 228 229 return value; 230 } 231 232 // jtreg resets %ProgramFiles% environment variable by some reason. 233 private final static Path PROGRAM_FILES = Path.of(Optional.ofNullable( 234 System.getenv("ProgramFiles")).orElse("C:\\Program Files")); 235 236 private final static Path USER_LOCAL = Path.of(System.getProperty( 237 "user.home"), 238 "AppData", "Local"); 239 240 private final static String SYSTEM_SHELL_FOLDERS_REGKEY = "HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"; 241 private final static String USER_SHELL_FOLDERS_REGKEY = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"; 242 243 private static final Map<String, String> REGISTRY_VALUES = new HashMap<>(); 244 }