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