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