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.IOUtils;
  30 import org.junit.After;
  31 import org.junit.Assume;
  32 import org.junit.Before;
  33 import org.junit.BeforeClass;
  34 import org.junit.Test;
  35 
  36 import java.io.ByteArrayOutputStream;
  37 import java.io.File;
  38 import java.io.IOException;
  39 import java.io.PrintStream;
  40 import java.nio.file.Files;
  41 import java.util.*;
  42 
  43 import static com.oracle.tools.packager.StandardBundlerParam.*;
  44 import static org.junit.Assert.assertNotNull;
  45 import static org.junit.Assert.assertTrue;
  46 
  47 public class MacAppStoreBundlerTest {
  48 
  49     static File tmpBase;
  50     static File workDir;
  51     static File appResourcesDir;
  52     static File fakeMainJar;
  53     static Set<File> appResources;
  54     static boolean retain = false;
  55 
  56     @BeforeClass
  57     public static void prepareApp() throws IOException {
  58         // only run on mac
  59         Assume.assumeTrue(System.getProperty("os.name").toLowerCase().contains("os x"));
  60 
  61         // make sure we have a default signing key
  62         String signingKeyName = MacAppStoreBundler.MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(new TreeMap<>());
  63         Assume.assumeNotNull(signingKeyName);
  64         try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos)) {
  65             System.err.println("Checking for valid certificate");
  66             ProcessBuilder pb = new ProcessBuilder(
  67                     "security",
  68                     "find-certificate", "-c", signingKeyName);
  69 
  70             IOUtils.exec(pb, Log.isDebug(), false, ps);
  71 
  72             String commandOutput = baos.toString();
  73             Assume.assumeTrue(commandOutput.contains(signingKeyName));
  74             System.err.println("Valid certificate present");
  75         } catch (Throwable t) {
  76             System.err.println("Valid certificate not present, skipping test.");
  77             Assume.assumeTrue(false);
  78         }
  79 
  80 
  81         Log.setLogger(new Log.Logger(true));
  82 
  83         retain = Boolean.parseBoolean(System.getProperty("RETAIN_PACKAGER_TESTS"));
  84 
  85         workDir = new File("build/tmp/tests", "macappstore");
  86         appResourcesDir = new File("build/tmp/tests", "appResources");
  87         fakeMainJar = new File(appResourcesDir, "mainApp.jar");
  88 
  89         appResources = new HashSet<>(Arrays.asList(fakeMainJar));
  90     }
  91 
  92     @Before
  93     public void createTmpDir() throws IOException {
  94         if (retain) {
  95             tmpBase = new File("build/tmp/tests/macappstore");
  96         } else {
  97             tmpBase = Files.createTempDirectory("fxpackagertests").toFile();
  98         }
  99         tmpBase.mkdir();
 100     }
 101 
 102     @After
 103     public void maybeCleanupTmpDir() {
 104         if (!retain) {
 105             attemptDelete(tmpBase);
 106         }
 107     }
 108 
 109     private void attemptDelete(File tmpBase) {
 110         if (tmpBase.isDirectory()) {
 111             File[] children = tmpBase.listFiles();
 112             if (children != null) {
 113                 for (File f : children) {
 114                     attemptDelete(f);
 115                 }
 116             }
 117         }
 118         boolean success;
 119         try {
 120             success = !tmpBase.exists() || tmpBase.delete();
 121         } catch (SecurityException se) {
 122             success = false;
 123         }
 124         if (!success) {
 125             System.err.println("Could not clean up " + tmpBase.toString());
 126         }
 127     }
 128 
 129     @Test
 130     public void showSigningKeyNames() {
 131         System.err.println(MacBaseInstallerBundler.SIGNING_KEY_USER.fetchFrom(new TreeMap<>()));
 132         System.err.println(MacAppStoreBundler.MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(new TreeMap<>()));
 133     }
 134 
 135     /**
 136      * See if smoke comes out
 137      */
 138     @Test
 139     public void smokeTest() throws IOException, ConfigException, UnsupportedPlatformException {
 140         AbstractBundler bundler = new MacAppStoreBundler();
 141 
 142         assertNotNull(bundler.getName());
 143         assertNotNull(bundler.getID());
 144         assertNotNull(bundler.getDescription());
 145 
 146         Map<String, Object> bundleParams = new HashMap<>();
 147 
 148         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 149 
 150         bundleParams.put(APP_NAME.getID(), "Smoke");
 151         bundleParams.put(MAIN_CLASS.getID(), "hello.TestPackager");
 152         bundleParams.put(PREFERENCES_ID.getID(), "the/really/long/preferences/id");
 153         bundleParams.put(MAIN_JAR.getID(),
 154                 new RelativeFileSet(fakeMainJar.getParentFile(),
 155                         new HashSet<>(Arrays.asList(fakeMainJar)))
 156         );
 157         bundleParams.put(MAIN_JAR_CLASSPATH.getID(), fakeMainJar.toString());
 158         bundleParams.put(IDENTIFIER.getID(), "com.example.javapacakger.hello.TestPackager");
 159         bundleParams.put(MacAppBundler.MAC_CATEGORY.getID(), "public.app-category.developer-tools");
 160         bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 161         bundleParams.put(VERBOSE.getID(), true);
 162 
 163         boolean valid = bundler.validate(bundleParams);
 164         assertTrue(valid);
 165 
 166         File result = bundler.execute(bundleParams, new File(workDir, "smoke"));
 167         System.err.println("Bundle at - " + result);
 168         assertNotNull(result);
 169         assertTrue(result.exists());
 170     }
 171 
 172 //    /**
 173 //     * The bare minimum configuration needed to make it work
 174 //     * <ul>
 175 //     *     <li>Where to build it</li>
 176 //     *     <li>The jar containing the application (with a main-class attribute)</li>
 177 //     * </ul>
 178 //     *
 179 //     * All other values will be driven off of those two values.
 180 //     */
 181 //    @Test
 182 //    public void minimumConfig() throws IOException, ConfigException, UnsupportedPlatformException {
 183 //        Bundler bundler = new MacAppStoreBundler();
 184 //
 185 //        Map<String, Object> bundleParams = new HashMap<>();
 186 //
 187 //        bundleParams.put(StandardBundlerParam.BUILD_ROOT.getID(), tmpBase);
 188 //
 189 //        bundleParams.put(StandardBundlerParam.APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 190 //
 191 //        File output = bundler.execute(bundleParams, new File(workDir, "BareMinimum"));
 192 //        System.err.println("Bundle written to " + output);
 193 //        assertTrue(output.isFile());
 194 //        //TODO assert file name
 195 //    }
 196 }