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