1 /*
   2  * Copyright (c) 2014, 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 Set<File> appResources;
  70     static boolean retain = false;
  71 
  72     @BeforeClass
  73     public static void prepareApp() throws IOException {
  74         // only run on mac
  75         Assume.assumeTrue(System.getProperty("os.name").toLowerCase().contains("os x"));
  76 
  77         // and only if we have the correct JRE settings
  78         String jre = System.getProperty("java.home").toLowerCase();
  79         Assume.assumeTrue(jre.endsWith("/contents/home/jre") || jre.endsWith("/contents/home/jre"));
  80 
  81         // make sure we have a default signing key
  82         String signingKeyName = MacAppStoreBundler.MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(new TreeMap<>());
  83         Assume.assumeNotNull(signingKeyName);
  84         try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos)) {
  85             System.err.println("Checking for valid certificate");
  86             ProcessBuilder pb = new ProcessBuilder(
  87                     "security",
  88                     "find-certificate", "-c", signingKeyName);
  89 
  90             IOUtils.exec(pb, Log.isDebug(), false, ps);
  91 
  92             String commandOutput = baos.toString();
  93             Assume.assumeTrue(commandOutput.contains(signingKeyName));
  94             System.err.println("Valid certificate present");
  95         } catch (Throwable t) {
  96             System.err.println("Valid certificate not present, skipping test.");
  97             Assume.assumeTrue(false);
  98         }
  99 
 100 
 101         Log.setLogger(new Log.Logger(true));
 102 
 103         retain = Boolean.parseBoolean(System.getProperty("RETAIN_PACKAGER_TESTS"));
 104 
 105         workDir = new File("build/tmp/tests", "macappstore");
 106         hdpiIcon = new File("build/tmp/tests", "GenericAppHiDPI.icns");
 107         appResourcesDir = new File("build/tmp/tests", "appResources");
 108         fakeMainJar = new File(appResourcesDir, "mainApp.jar");
 109 
 110         appResources = new HashSet<>(Arrays.asList(fakeMainJar));
 111     }
 112 
 113     @Before
 114     public void createTmpDir() throws IOException {
 115         if (retain) {
 116             tmpBase = new File("build/tmp/tests/macappstore");
 117         } else {
 118             tmpBase = BUILD_ROOT.fetchFrom(new TreeMap<>());
 119         }
 120         tmpBase.mkdir();
 121     }
 122 
 123     @After
 124     public void maybeCleanupTmpDir() {
 125         if (!retain) {
 126             attemptDelete(tmpBase);
 127         }
 128     }
 129 
 130     private void attemptDelete(File tmpBase) {
 131         if (tmpBase.isDirectory()) {
 132             File[] children = tmpBase.listFiles();
 133             if (children != null) {
 134                 for (File f : children) {
 135                     attemptDelete(f);
 136                 }
 137             }
 138         }
 139         boolean success;
 140         try {
 141             success = !tmpBase.exists() || tmpBase.delete();
 142         } catch (SecurityException se) {
 143             success = false;
 144         }
 145         if (!success) {
 146             System.err.println("Could not clean up " + tmpBase.toString());
 147         }
 148     }
 149 
 150     @Test
 151     public void showSigningKeyNames() {
 152         System.err.println(MacBaseInstallerBundler.SIGNING_KEY_USER.fetchFrom(new TreeMap<>()));
 153         System.err.println(MacAppStoreBundler.MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(new TreeMap<>()));
 154     }
 155 
 156     /**
 157      * See if smoke comes out
 158      */
 159     @Test
 160     public void smokeTest() throws IOException, ConfigException, UnsupportedPlatformException {
 161         AbstractBundler bundler = new MacAppStoreBundler();
 162 
 163         assertNotNull(bundler.getName());
 164         assertNotNull(bundler.getID());
 165         assertNotNull(bundler.getDescription());
 166 
 167         Map<String, Object> bundleParams = new HashMap<>();
 168 
 169         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 170 
 171         bundleParams.put(APP_NAME.getID(), "Smoke Test");
 172         bundleParams.put(MAIN_CLASS.getID(), "hello.TestPackager");
 173         bundleParams.put(PREFERENCES_ID.getID(), "the/really/long/preferences/id");
 174         bundleParams.put(MAIN_JAR.getID(),
 175                 new RelativeFileSet(fakeMainJar.getParentFile(),
 176                         new HashSet<>(Arrays.asList(fakeMainJar)))
 177         );
 178         bundleParams.put(CLASSPATH.getID(), fakeMainJar.toString());
 179         bundleParams.put(IDENTIFIER.getID(), "com.example.javapacakger.hello.TestPackager");
 180         bundleParams.put(MacAppBundler.MAC_CATEGORY.getID(), "public.app-category.developer-tools");
 181         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 182         bundleParams.put(VERBOSE.getID(), true);
 183 
 184         boolean valid = bundler.validate(bundleParams);
 185         assertTrue(valid);
 186 
 187         File result = bundler.execute(bundleParams, new File(workDir, "smoke"));
 188         System.err.println("Bundle at - " + result);
 189 
 190         checkFiles(result);
 191     }
 192 
 193     private void checkFiles(File result) throws IOException {
 194         assertNotNull(result);
 195         assertTrue(result.exists());
 196         assertTrue(result.length() > MIN_SIZE);
 197 
 198         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 199         PrintStream printStream = new PrintStream(baos, true);
 200         IOUtils.exec(
 201                 new ProcessBuilder("pkgutil", "--payload-files", result.getCanonicalPath()),
 202                 false, false, printStream);
 203 
 204         String output = baos.toString();
 205 
 206         Pattern jreInfoPListPattern = Pattern.compile("/PlugIns/[^/]+/Contents/Info\\.plist");
 207         Matcher matcher = jreInfoPListPattern.matcher(output);
 208         assertTrue("Insure that info.plist is packed in for embedded jre", matcher.find());
 209 
 210         assertFalse("Insure JFX Media isn't packed in", output.contains("/libjfxmedia.dylib"));
 211     }
 212 
 213     @Test
 214     public void configureEverything() throws Exception {
 215         AbstractBundler bundler = new MacAppStoreBundler();
 216         Collection<BundlerParamInfo<?>> parameters = bundler.getBundleParameters();
 217 
 218         Map<String, Object> bundleParams = new HashMap<>();
 219 
 220         bundleParams.put(APP_NAME.getID(), "Everything App Name");
 221         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 222         bundleParams.put(BUNDLE_ID_SIGNING_PREFIX.getID(), "everything.signing.prefix.");
 223         bundleParams.put(ICON_ICNS.getID(), hdpiIcon);
 224         bundleParams.put(JVM_OPTIONS.getID(), "-Xms128M");
 225         bundleParams.put(JVM_PROPERTIES.getID(), "everything.jvm.property=everything.jvm.property.value");
 226         bundleParams.put(MAC_CATEGORY.getID(), "public.app-category.developer-tools");
 227         bundleParams.put(MAC_CF_BUNDLE_IDENTIFIER.getID(), "com.example.everything.cf-bundle-identifier");
 228         bundleParams.put(MAC_CF_BUNDLE_NAME.getID(), "Everything CF Bundle Name");
 229         bundleParams.put(MAC_RUNTIME.getID(), System.getProperty("java.home"));
 230         bundleParams.put(MAIN_CLASS.getID(), "hello.TestPackager");
 231         bundleParams.put(MAIN_JAR.getID(), "mainApp.jar");
 232         bundleParams.put(CLASSPATH.getID(), "mainApp.jar");
 233         bundleParams.put(PREFERENCES_ID.getID(), "everything/preferences/id");
 234         bundleParams.put(USER_JVM_OPTIONS.getID(), "-Xmx=256M\n");
 235         bundleParams.put(VERSION.getID(), "1.2.3.4");
 236 
 237         bundleParams.put(MAC_APP_STORE_APP_SIGNING_KEY.getID(), "3rd Party Mac Developer Application");
 238         bundleParams.put(MAC_APP_STORE_ENTITLEMENTS.getID(), null);
 239         bundleParams.put(MAC_APP_STORE_PKG_SIGNING_KEY.getID(), "3rd Party Mac Developer Installer");
 240 
 241                 // assert they are set
 242         for (BundlerParamInfo bi :parameters) {
 243             assertNotNull("Bundle args Contains " + bi.getID(), bundleParams.containsKey(bi.getID()));
 244         }
 245 
 246         // and only those are set
 247         bundleParamLoop:
 248         for (String s :bundleParams.keySet()) {
 249             for (BundlerParamInfo<?> bpi : parameters) {
 250                 if (s.equals(bpi.getID())) {
 251                     continue bundleParamLoop;
 252                 }
 253             }
 254             fail("Enumerated parameters does not contain " + s);
 255         }
 256 
 257         // assert they resolve
 258         for (BundlerParamInfo bi :parameters) {
 259             bi.fetchFrom(bundleParams);
 260         }
 261 
 262         // now that we are done scoping out parameters add more esoteric values
 263         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 264         bundleParams.put(VERBOSE.getID(), true);
 265 
 266         // assert it validates
 267         boolean valid = bundler.validate(bundleParams);
 268         assertTrue(valid);
 269 
 270         // only run the bundle with full tests
 271         Assume.assumeTrue(Boolean.parseBoolean(System.getProperty("FULL_TEST")));
 272 
 273         File result = bundler.execute(bundleParams, new File(workDir, "everything"));
 274         System.err.println("Bundle at - " + result);
 275 
 276         checkFiles(result);
 277     }
 278 }