1 /* 2 * Copyright (c) 2014, 2015, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.oracle.tools.packager.mac; 27 28 import com.oracle.tools.packager.AbstractBundler; 29 import com.oracle.tools.packager.AbstractImageBundler; 30 import com.oracle.tools.packager.BundlerParamInfo; 31 import com.oracle.tools.packager.ConfigException; 32 import com.oracle.tools.packager.IOUtils; 33 import com.oracle.tools.packager.Log; 34 import com.oracle.tools.packager.RelativeFileSet; 35 import com.oracle.tools.packager.UnsupportedPlatformException; 36 import org.junit.After; 37 import org.junit.Assume; 38 import org.junit.Before; 39 import org.junit.BeforeClass; 40 import org.junit.Test; 41 42 import java.io.ByteArrayOutputStream; 43 import java.io.File; 44 import java.io.IOException; 45 import java.io.PrintStream; 46 import java.nio.file.Files; 47 import java.nio.file.Paths; 48 import java.text.SimpleDateFormat; 49 import java.util.Arrays; 50 import java.util.Collection; 51 import java.util.Date; 52 import java.util.HashMap; 53 import java.util.HashSet; 54 import java.util.Map; 55 import java.util.Set; 56 import java.util.TreeMap; 57 import java.util.regex.Matcher; 58 import java.util.regex.Pattern; 59 60 import static com.oracle.tools.packager.StandardBundlerParam.*; 61 import static com.oracle.tools.packager.mac.MacAppBundler.*; 62 import static com.oracle.tools.packager.mac.MacAppStoreBundler.*; 63 import static org.junit.Assert.*; 64 65 public class MacAppStoreBundlerTest { 66 67 static final int MIN_SIZE = 0x100000; // 1MiB 68 69 static File tmpBase; 70 static File workDir; 71 static File appResourcesDir; 72 static File fakeMainJar; 73 static File hdpiIcon; 74 static String runtimeJdk; 75 static String runtimeJre; 76 static Set<File> appResources; 77 static boolean retain = false; 78 79 @BeforeClass 80 public static void prepareApp() throws IOException { 81 // only run on mac 82 Assume.assumeTrue(System.getProperty("os.name").toLowerCase().contains("os x")); 83 84 runtimeJdk = System.getenv("PACKAGER_JDK_ROOT"); 85 runtimeJre = System.getenv("PACKAGER_JRE_ROOT"); 86 87 // and only if we have the correct JRE settings 88 String jre = System.getProperty("java.home").toLowerCase(); 89 Assume.assumeTrue(runtimeJdk != null || jre.endsWith("/contents/home/jre") || jre.endsWith("/contents/home/jre")); 90 91 // make sure we have a default signing key 92 String signingKeyName = MacAppStoreBundler.MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(new TreeMap<>()); 93 Assume.assumeNotNull(signingKeyName); 94 try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos)) { 95 System.err.println("Checking for valid certificate"); 96 ProcessBuilder pb = new ProcessBuilder( 97 "security", 98 "find-certificate", "-c", signingKeyName); 99 100 IOUtils.exec(pb, Log.isDebug(), false, ps); 101 102 String commandOutput = baos.toString(); 103 Assume.assumeTrue(commandOutput.contains(signingKeyName)); 104 System.err.println("Valid certificate present"); 105 } catch (Throwable t) { 106 System.err.println("Valid certificate not present, skipping test."); 107 Assume.assumeTrue(false); 108 } 109 110 111 Log.setLogger(new Log.Logger(true)); 112 Log.setDebug(true); 113 114 retain = Boolean.parseBoolean(System.getProperty("RETAIN_PACKAGER_TESTS")); 115 116 workDir = new File("build/tmp/tests", "macappstore"); 117 hdpiIcon = new File("build/tmp/tests", "GenericAppHiDPI.icns"); 118 appResourcesDir = new File("build/tmp/tests", "appResources"); 119 fakeMainJar = new File(appResourcesDir, "mainApp.jar"); 120 121 appResources = new HashSet<>(Arrays.asList(fakeMainJar)); 122 } 123 124 @Before 125 public void createTmpDir() throws IOException { 126 if (retain) { 127 tmpBase = new File("build/tmp/tests/macappstore"); 128 } else { 129 tmpBase = BUILD_ROOT.fetchFrom(new TreeMap<>()); 130 } 131 tmpBase.mkdir(); 132 } 133 134 @After 135 public void maybeCleanupTmpDir() { 136 if (!retain) { 137 attemptDelete(tmpBase); 138 } 139 } 140 141 private void attemptDelete(File tmpBase) { 142 if (tmpBase.isDirectory()) { 143 File[] children = tmpBase.listFiles(); 144 if (children != null) { 145 for (File f : children) { 146 attemptDelete(f); 147 } 148 } 149 } 150 boolean success; 151 try { 152 success = !tmpBase.exists() || tmpBase.delete(); 153 } catch (SecurityException se) { 154 success = false; 155 } 156 if (!success) { 157 System.err.println("Could not clean up " + tmpBase.toString()); 158 } 159 } 160 161 @Test 162 public void showSigningKeyNames() { 163 System.err.println(MacBaseInstallerBundler.SIGNING_KEY_USER.fetchFrom(new TreeMap<>())); 164 System.err.println(MacAppStoreBundler.MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(new TreeMap<>())); 165 } 166 167 /** 168 * See if smoke comes out 169 */ 170 @Test 171 public void smokeTest() throws IOException, ConfigException, UnsupportedPlatformException { 172 AbstractBundler bundler = new MacAppStoreBundler(); 173 174 assertNotNull(bundler.getName()); 175 assertNotNull(bundler.getID()); 176 assertNotNull(bundler.getDescription()); 177 178 Map<String, Object> bundleParams = new HashMap<>(); 179 180 bundleParams.put(BUILD_ROOT.getID(), tmpBase); 181 182 bundleParams.put(APP_NAME.getID(), "Smoke Test"); 183 bundleParams.put(MAIN_CLASS.getID(), "hello.HelloRectangle"); 184 bundleParams.put(PREFERENCES_ID.getID(), "the/really/long/preferences/id"); 185 bundleParams.put(MAIN_JAR.getID(), 186 new RelativeFileSet(fakeMainJar.getParentFile(), 187 new HashSet<>(Arrays.asList(fakeMainJar))) 188 ); 189 bundleParams.put(MAC_CF_BUNDLE_VERSION.getID(), "1.0." + new SimpleDateFormat("YYYYMMddHHmm").format(new Date())); 190 bundleParams.put(CLASSPATH.getID(), "mainApp.jar"); 191 bundleParams.put(IDENTIFIER.getID(), "com.example.javapacakger.hello.TestPackager"); 192 bundleParams.put(MacAppBundler.MAC_CATEGORY.getID(), "public.app-category.developer-tools"); 193 bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources)); 194 bundleParams.put(VERBOSE.getID(), true); 195 196 if (runtimeJdk != null) { 197 bundleParams.put(MAC_RUNTIME.getID(), runtimeJdk); 198 } 199 200 boolean valid = bundler.validate(bundleParams); 201 assertTrue(valid); 202 203 File result = bundler.execute(bundleParams, new File(workDir, "smoke")); 204 System.err.println("Bundle at - " + result); 205 206 checkFiles(result, runtimeJdk); 207 } 208 209 private void checkFiles(File result, String runtimeRoot) throws IOException { 210 assertNotNull(result); 211 assertTrue(result.exists()); 212 assertTrue(result.length() > MIN_SIZE); 213 214 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 215 PrintStream printStream = new PrintStream(baos, true); 216 IOUtils.exec( 217 new ProcessBuilder("pkgutil", "--payload-files", result.getCanonicalPath()), 218 false, false, printStream); 219 220 String output = baos.toString(); 221 222 Pattern jreInfoPListPattern = Pattern.compile("/PlugIns/[^/]+/Contents/Info\\.plist"); 223 Matcher matcher = jreInfoPListPattern.matcher(output); 224 assertTrue("Insure that info.plist is packed in for embedded jre", matcher.find()); 225 226 Map<String, Object> params = new HashMap<>(); 227 String version; 228 229 if (runtimeRoot == null) { 230 version = System.getProperty("java.runtime.version"); 231 } else { 232 byte[] infoPlistBytes = Files.readAllBytes(Paths.get(runtimeRoot).getParent().resolve("Info.plist")); 233 String infoPlist = new String(infoPlistBytes); 234 235 Pattern cfBundleVersionMatcher = Pattern.compile("<key>CFBundleVersion</key>\\s*<string>([^<]+)</string>"); 236 Matcher m = cfBundleVersionMatcher.matcher(infoPlist); 237 assertTrue("Packed Info.plist presents a java version", m.find()); 238 version = m.group(1); 239 } 240 AbstractImageBundler.extractFlagsFromVersion(params, "java version \"" + version + "\"\n"); 241 242 int majorVersion = Integer.parseInt(params.get(".runtime.version.major").toString()); 243 int updateVersion = Integer.parseInt(params.get(".runtime.version.update").toString()); 244 245 if (majorVersion == 8 && updateVersion >= 40) { 246 assertFalse("Insure JFX Media QuickTime Partition isn't packed in", output.contains("/libjfxmedia_qtkit.dylib")); 247 } else { 248 assertFalse("Insure JFX Media isn't packed in", output.contains("/libjfxmedia.dylib")); 249 } 250 251 if (majorVersion == 8 && updateVersion >= 60) { 252 assertFalse("Insure WebView library isn't packed in", output.contains("/libjfxwebkit.dylib")); 253 } 254 } 255 256 @Test 257 public void configureEverything() throws Exception { 258 AbstractBundler bundler = new MacAppStoreBundler(); 259 Collection<BundlerParamInfo<?>> parameters = bundler.getBundleParameters(); 260 261 Map<String, Object> bundleParams = new HashMap<>(); 262 263 bundleParams.put(APP_NAME.getID(), "Everything App Name"); 264 bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources)); 265 bundleParams.put(ARGUMENTS.getID(), Arrays.asList("He Said", "She Said")); 266 bundleParams.put(BUNDLE_ID_SIGNING_PREFIX.getID(), "everything.signing.prefix."); 267 bundleParams.put(CLASSPATH.getID(), "mainApp.jar"); 268 bundleParams.put(ICON_ICNS.getID(), hdpiIcon); 269 bundleParams.put(INSTALLER_SUFFIX.getID(), "-MAS-TEST"); 270 bundleParams.put(JVM_OPTIONS.getID(), "-Xms128M"); 271 bundleParams.put(JVM_PROPERTIES.getID(), "everything.jvm.property=everything.jvm.property.value"); 272 bundleParams.put(MAC_CATEGORY.getID(), "public.app-category.developer-tools"); 273 bundleParams.put(MAC_CF_BUNDLE_IDENTIFIER.getID(), "com.example.everything.cf-bundle-identifier"); 274 bundleParams.put(MAC_CF_BUNDLE_NAME.getID(), "Everything CF Bundle Name"); 275 bundleParams.put(MAC_CF_BUNDLE_VERSION.getID(), "8.2.0"); 276 bundleParams.put(MAC_RUNTIME.getID(), runtimeJdk == null ? System.getProperty("java.home") : runtimeJdk); 277 bundleParams.put(MAIN_CLASS.getID(), "hello.HelloRectangle"); 278 bundleParams.put(MAIN_JAR.getID(), "mainApp.jar"); 279 bundleParams.put(PREFERENCES_ID.getID(), "everything/preferences/id"); 280 bundleParams.put(PRELOADER_CLASS.getID(), "hello.HelloPreloader"); 281 bundleParams.put(SIGNING_KEYCHAIN.getID(), ""); 282 bundleParams.put(USER_JVM_OPTIONS.getID(), "-Xmx=256M\n"); 283 bundleParams.put(VERSION.getID(), "1.2.3.4"); 284 285 bundleParams.put(MAC_APP_STORE_APP_SIGNING_KEY.getID(), "3rd Party Mac Developer Application"); 286 bundleParams.put(MAC_APP_STORE_ENTITLEMENTS.getID(), null); 287 bundleParams.put(MAC_APP_STORE_PKG_SIGNING_KEY.getID(), "3rd Party Mac Developer Installer"); 288 289 // assert they are set 290 for (BundlerParamInfo bi : parameters) { 291 assertNotNull("Bundle args Contains " + bi.getID(), bundleParams.containsKey(bi.getID())); 292 } 293 294 // and only those are set 295 bundleParamLoop: 296 for (String s : bundleParams.keySet()) { 297 for (BundlerParamInfo<?> bpi : parameters) { 298 if (s.equals(bpi.getID())) { 299 continue bundleParamLoop; 300 } 301 } 302 fail("Enumerated parameters does not contain " + s); 303 } 304 305 // assert they resolve 306 for (BundlerParamInfo bi : parameters) { 307 bi.fetchFrom(bundleParams); 308 } 309 310 // now that we are done scoping out parameters add more esoteric values 311 bundleParams.put(BUILD_ROOT.getID(), tmpBase); 312 bundleParams.put(VERBOSE.getID(), true); 313 314 // assert it validates 315 boolean valid = bundler.validate(bundleParams); 316 assertTrue(valid); 317 318 // only run the bundle with full tests 319 Assume.assumeTrue(Boolean.parseBoolean(System.getProperty("FULL_TEST"))); 320 321 File result = bundler.execute(bundleParams, new File(workDir, "everything")); 322 System.err.println("Bundle at - " + result); 323 324 checkFiles(result, runtimeJdk); 325 } 326 327 /** 328 * User a JRE instead of a JDK 329 */ 330 @Test 331 public void testJRE() throws IOException, ConfigException, UnsupportedPlatformException { 332 String jre = runtimeJre == null ? "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/" : runtimeJre; 333 Assume.assumeTrue(new File(jre).isDirectory()); 334 335 AbstractBundler bundler = new MacAppStoreBundler(); 336 337 assertNotNull(bundler.getName()); 338 assertNotNull(bundler.getID()); 339 assertNotNull(bundler.getDescription()); 340 341 Map<String, Object> bundleParams = new HashMap<>(); 342 343 bundleParams.put(BUILD_ROOT.getID(), tmpBase); 344 345 bundleParams.put(APP_NAME.getID(), "Smoke Test"); 346 bundleParams.put(MAIN_CLASS.getID(), "hello.HelloRectangle"); 347 bundleParams.put(PREFERENCES_ID.getID(), "the/really/long/preferences/id"); 348 bundleParams.put(MAIN_JAR.getID(), 349 new RelativeFileSet(fakeMainJar.getParentFile(), 350 new HashSet<>(Arrays.asList(fakeMainJar))) 351 ); 352 bundleParams.put(CLASSPATH.getID(), "mainApp.jar"); 353 bundleParams.put(IDENTIFIER.getID(), "com.example.javapacakger.hello.TestPackager"); 354 bundleParams.put(MacAppBundler.MAC_CATEGORY.getID(), "public.app-category.developer-tools"); 355 bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources)); 356 bundleParams.put(VERBOSE.getID(), true); 357 bundleParams.put(MAC_RUNTIME.getID(), jre); 358 359 boolean valid = bundler.validate(bundleParams); 360 assertTrue(valid); 361 362 File result = bundler.execute(bundleParams, new File(workDir, "jre")); 363 System.err.println("Bundle at - " + result); 364 365 checkFiles(result, runtimeJre); 366 367 } 368 369 /** 370 * Request no signature, should be a validaiton error 371 */ 372 @Test(expected = ConfigException.class) 373 public void invalidDoNotSign() throws IOException, ConfigException, UnsupportedPlatformException { 374 AbstractBundler bundler = new MacAppStoreBundler(); 375 376 Map<String, Object> bundleParams = new HashMap<>(); 377 378 bundleParams.put(BUILD_ROOT.getID(), tmpBase); 379 380 bundleParams.put(APP_NAME.getID(), "Smoke Test"); 381 bundleParams.put(MAIN_CLASS.getID(), "hello.HelloRectangle"); 382 bundleParams.put(PREFERENCES_ID.getID(), "the/really/long/preferences/id"); 383 bundleParams.put(MAIN_JAR.getID(), 384 new RelativeFileSet(fakeMainJar.getParentFile(), 385 new HashSet<>(Arrays.asList(fakeMainJar))) 386 ); 387 bundleParams.put(CLASSPATH.getID(), "mainApp.jar"); 388 bundleParams.put(IDENTIFIER.getID(), "com.example.javapacakger.hello.TestPackager"); 389 bundleParams.put(MacAppBundler.MAC_CATEGORY.getID(), "public.app-category.developer-tools"); 390 bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources)); 391 bundleParams.put(VERBOSE.getID(), true); 392 393 if (runtimeJdk != null) { 394 bundleParams.put(MAC_RUNTIME.getID(), runtimeJdk); 395 } 396 397 bundleParams.put(SIGN_BUNDLE.getID(), false); 398 399 bundler.validate(bundleParams); 400 } 401 }