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.*;
  29 import com.oracle.tools.packager.Bundler;
  30 import com.oracle.tools.packager.IOUtils;
  31 import org.junit.After;
  32 import org.junit.Assume;
  33 import org.junit.Before;
  34 import org.junit.BeforeClass;
  35 import org.junit.Test;
  36 
  37 import java.io.File;
  38 import java.io.IOException;
  39 import java.nio.file.Files;
  40 import java.util.*;
  41 
  42 import static com.oracle.tools.packager.StandardBundlerParam.*;
  43 import static com.oracle.tools.packager.mac.MacAppBundler.DEVELOPER_ID_APP_SIGNING_KEY;
  44 import static com.oracle.tools.packager.mac.MacAppBundler.MAC_CF_BUNDLE_NAME;
  45 import static org.junit.Assert.assertNotNull;
  46 import static org.junit.Assert.assertTrue;
  47 
  48 public class MacAppBundlerTest {
  49 
  50     static File tmpBase;
  51     static File workDir;
  52     static File appResourcesDir;
  53     static File fakeMainJar;
  54     static Set<File> appResources;
  55     static boolean retain = false;
  56 
  57     @BeforeClass
  58     public static void prepareApp() {
  59         // only run on mac
  60         Assume.assumeTrue(System.getProperty("os.name").toLowerCase().contains("os x"));
  61 
  62         Log.setLogger(new Log.Logger(true));
  63 
  64         retain = Boolean.parseBoolean(System.getProperty("RETAIN_PACKAGER_TESTS"));
  65 
  66         workDir = new File("build/tmp/tests", "macapp");
  67         appResourcesDir = new File("build/tmp/tests", "appResources");
  68         fakeMainJar = new File(appResourcesDir, "mainApp.jar");
  69 
  70         appResources = new HashSet<>(Arrays.asList(fakeMainJar));
  71     }
  72 
  73     @Before
  74     public void createTmpDir() throws IOException {
  75         if (retain) {
  76             tmpBase = new File("build/tmp/tests/macapp");
  77         } else {
  78             tmpBase = Files.createTempDirectory("fxpackagertests").toFile();
  79         }
  80         tmpBase.mkdir();
  81     }
  82 
  83     @After
  84     public void maybeCleanupTmpDir() {
  85         if (!retain) {
  86             attemptDelete(tmpBase);
  87         }
  88     }
  89 
  90     private void attemptDelete(File tmpBase) {
  91         if (tmpBase.isDirectory()) {
  92             File[] children = tmpBase.listFiles();
  93             if (children != null) {
  94                 for (File f : children) {
  95                     attemptDelete(f);
  96                 }
  97             }
  98         }
  99         boolean success;
 100         try {
 101             success = !tmpBase.exists() || tmpBase.delete();
 102         } catch (SecurityException se) {
 103             success = false;
 104         }
 105         if (!success) {
 106             System.err.println("Could not clean up " + tmpBase.toString());
 107         }
 108     }
 109 
 110     /**
 111      * See if smoke comes out
 112      */
 113     @Test
 114     public void smokeTest() throws IOException, ConfigException, UnsupportedPlatformException {
 115         AbstractBundler bundler = new MacAppBundler();
 116 
 117         assertNotNull(bundler.getName());
 118         assertNotNull(bundler.getID());
 119         assertNotNull(bundler.getDescription());
 120         //assertNotNull(bundler.getBundleParameters());
 121 
 122         Map<String, Object> bundleParams = new HashMap<>();
 123 
 124         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 125 
 126         bundleParams.put(APP_NAME.getID(), "Smoke Test App");
 127         bundleParams.put(MAC_CF_BUNDLE_NAME.getID(), "Smoke");
 128         bundleParams.put(MAIN_CLASS.getID(), "hello.TestPackager");
 129         bundleParams.put(PREFERENCES_ID.getID(), "the/really/long/preferences/id");
 130         bundleParams.put(MAIN_JAR.getID(),
 131                 new RelativeFileSet(fakeMainJar.getParentFile(),
 132                         new HashSet<>(Arrays.asList(fakeMainJar)))
 133         );
 134         bundleParams.put(MAIN_JAR_CLASSPATH.getID(), fakeMainJar.toString());
 135         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 136         bundleParams.put(VERBOSE.getID(), true);
 137         bundleParams.put(DEVELOPER_ID_APP_SIGNING_KEY.getID(), null); // force no signing
 138         bundleParams.put(ICON.getID(), "java-logo2.gif"); // force no signing
 139 
 140         boolean valid = bundler.validate(bundleParams);
 141         assertTrue(valid);
 142 
 143         File result = bundler.execute(bundleParams, new File(workDir, "smoke"));
 144         System.err.println("Bundle at - " + result);
 145         assertNotNull(result);
 146         assertTrue(result.exists());
 147     }
 148 
 149     /**
 150      * Build smoke test and mark it as quarantined, possibly signed
 151      */
 152     @Test
 153     public void quarantinedAppTest() throws IOException, ConfigException, UnsupportedPlatformException {
 154         AbstractBundler bundler = new MacAppBundler();
 155 
 156         assertNotNull(bundler.getName());
 157         assertNotNull(bundler.getID());
 158         assertNotNull(bundler.getDescription());
 159         //assertNotNull(bundler.getBundleParameters());
 160 
 161         Map<String, Object> bundleParams = new HashMap<>();
 162 
 163         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 164 
 165         bundleParams.put(APP_NAME.getID(), "Quarantined Test App");
 166         bundleParams.put(MAC_CF_BUNDLE_NAME.getID(), "Quarantine");
 167         bundleParams.put(MAIN_CLASS.getID(), "hello.TestPackager");
 168         bundleParams.put(PREFERENCES_ID.getID(), "the/really/long/preferences/id");
 169         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 170         bundleParams.put(VERBOSE.getID(), true);
 171 
 172         boolean valid = bundler.validate(bundleParams);
 173         assertTrue(valid);
 174 
 175         File result = bundler.execute(bundleParams, new File(workDir, "quarantine"));
 176         System.err.println("Bundle at - " + result);
 177         assertNotNull(result);
 178         assertTrue(result.exists());
 179 
 180         // mark it as though it's been downloaded
 181         ProcessBuilder pb = new ProcessBuilder(
 182                 "xattr", "-w", "com.apple.quarantine",
 183                 "0000;" + Long.toHexString(System.currentTimeMillis() / 1000L) + ";Java Unit Tests;|com.oracle.jvm.8u",
 184                 result.toString());
 185         IOUtils.exec(pb, true);
 186     }
 187 
 188     /**
 189      * The bare minimum configuration needed to make it work
 190      * <ul>
 191      *     <li>Where to build it</li>
 192      *     <li>The jar containing the application (with a main-class attribute)</li>
 193      * </ul>
 194      *
 195      * All other values will be driven off of those two values.
 196      */
 197     @Test
 198     public void minimumConfig() throws IOException, ConfigException, UnsupportedPlatformException {
 199         Bundler bundler = new MacAppBundler();
 200 
 201         Map<String, Object> bundleParams = new HashMap<>();
 202 
 203         // not part of the typical setup, for testing
 204         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 205 
 206         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 207 
 208         File output = bundler.execute(bundleParams, new File(workDir, "BareMinimum"));
 209         System.err.println("Bundle at - " + output);
 210         assertNotNull(output);
 211         assertTrue(output.exists());
 212     }
 213 }