1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package vogar.android; 18 19 import com.google.common.base.Function; 20 import com.google.common.collect.Lists; 21 import java.io.IOException; 22 import java.nio.file.Path; 23 import java.nio.file.Paths; 24 import java.util.Arrays; 25 import java.util.List; 26 import java.util.regex.Matcher; 27 import java.util.regex.Pattern; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.mockito.runners.MockitoJUnitRunner; 31 import vogar.LocalTarget; 32 import vogar.Mode; 33 import vogar.ModeId; 34 import vogar.Target; 35 import vogar.Variant; 36 import vogar.commands.Command; 37 import vogar.commands.VmCommandBuilder; 38 39 import static org.junit.Assert.assertEquals; 40 import static org.junit.Assert.assertNotNull; 41 42 /** 43 * Test the behaviour of the {@link HostRuntime} class when run with {@link LocalTarget}. 44 */ 45 @RunWith(MockitoJUnitRunner.class) 46 public class HostRuntimeLocalTargetTest extends AbstractModeTest { 47 48 public static final String ANDROID_BUILD_TOP_REFERENCE = 49 Matcher.quoteReplacement("${ANDROID_BUILD_TOP}/"); 50 51 @Override createTarget()52 protected Target createTarget() { 53 return new LocalTarget(console, mkdir, rm); 54 } 55 56 @Test 57 @VogarArgs({"action"}) testLocalTarget()58 public void testLocalTarget() 59 throws IOException { 60 61 Mode hostRuntime = new HostRuntime(run, ModeId.HOST, Variant.X32); 62 63 VmCommandBuilder builder = newVmCommandBuilder(hostRuntime) 64 .classpath(classpath) 65 .mainClass("mainclass") 66 .args("-x", "a b"); 67 Command command = builder.build(run.target); 68 List<String> args = command.getArgs(); 69 args = replaceEnvironmentVariables(args); 70 71 String expectedCommandString = "" 72 + "ANDROID_PRINTF_LOG=tag" 73 + " ANDROID_LOG_TAGS=*:i" 74 + " ANDROID_DATA=" + run.localFile("android-data") 75 + " ANDROID_ROOT=${ANDROID_BUILD_TOP}/out/host/linux-x86" 76 + " ANDROID_ART_ROOT=" 77 + "${ANDROID_BUILD_TOP}/out/host/linux-x86/com.android.art" 78 + " ANDROID_I18N_ROOT=" 79 + "${ANDROID_BUILD_TOP}/out/host/linux-x86/com.android.i18n" 80 + " ANDROID_TZDATA_ROOT=" 81 + "${ANDROID_BUILD_TOP}/out/host/linux-x86/com.android.tzdata" 82 + " LD_LIBRARY_PATH=${ANDROID_BUILD_TOP}/out/host/linux-x86/lib" 83 + " DYLD_LIBRARY_PATH=${ANDROID_BUILD_TOP}/out/host/linux-x86/lib" 84 + " LD_USE_LOAD_BIAS=1" 85 + " ${ANDROID_BUILD_TOP}/out/host/linux-x86/bin/dalvikvm" 86 + " -classpath classes" 87 + " -Xbootclasspath" 88 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/core-oj-hostdex.jar" 89 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/core-libart-hostdex.jar" 90 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/core-icu4j-hostdex.jar" 91 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/conscrypt-hostdex.jar" 92 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/okhttp-hostdex.jar" 93 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/bouncycastle-hostdex.jar" 94 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/apache-xml-hostdex.jar" 95 + " -Duser.language=en" 96 + " -Duser.region=US" 97 + " -Xcheck:jni" 98 + " -Xjnigreflimit:2000" 99 + " mainclass" 100 + " -x a\\ b"; 101 assertEquals(Arrays.asList("sh", "-c", expectedCommandString), args); 102 } 103 replaceEnvironmentVariables(List<String> args)104 public List<String> replaceEnvironmentVariables(List<String> args) { 105 String androidBuildTop = System.getenv("ANDROID_BUILD_TOP"); 106 assertNotNull("ANDROID_BUILD_TOP not set", androidBuildTop); 107 Path path = Paths.get(androidBuildTop).normalize(); 108 final String prefix = Pattern.quote(path.toString() + "/"); 109 args = Lists.transform(args, new Function<String, String>() { 110 @Override 111 public String apply(String input) { 112 return input.replaceAll(prefix, ANDROID_BUILD_TOP_REFERENCE); 113 } 114 }); 115 return args; 116 } 117 118 @Test 119 @VogarArgs({"--benchmark", "action"}) testLocalTarget_Benchmark()120 public void testLocalTarget_Benchmark() 121 throws IOException { 122 123 Mode hostRuntime = new HostRuntime(run, ModeId.HOST, Variant.X32); 124 125 VmCommandBuilder builder = newVmCommandBuilder(hostRuntime) 126 .classpath(classpath) 127 .mainClass("mainclass") 128 .args("-x", "a b"); 129 Command command = builder.build(run.target); 130 List<String> args = command.getArgs(); 131 args = replaceEnvironmentVariables(args); 132 133 String expectedCommandString = "" 134 + "ANDROID_PRINTF_LOG=tag" 135 + " ANDROID_LOG_TAGS=*:i" 136 + " ANDROID_DATA=" + run.localFile("android-data") 137 + " ANDROID_ROOT=${ANDROID_BUILD_TOP}/out/host/linux-x86" 138 + " ANDROID_ART_ROOT=" 139 + "${ANDROID_BUILD_TOP}/out/host/linux-x86/com.android.art" 140 + " ANDROID_I18N_ROOT=" 141 + "${ANDROID_BUILD_TOP}/out/host/linux-x86/com.android.i18n" 142 + " ANDROID_TZDATA_ROOT=" 143 + "${ANDROID_BUILD_TOP}/out/host/linux-x86/com.android.tzdata" 144 + " LD_LIBRARY_PATH=${ANDROID_BUILD_TOP}/out/host/linux-x86/lib" 145 + " DYLD_LIBRARY_PATH=${ANDROID_BUILD_TOP}/out/host/linux-x86/lib" 146 + " LD_USE_LOAD_BIAS=1" 147 + " ${ANDROID_BUILD_TOP}/out/host/linux-x86/bin/dalvikvm" 148 + " -classpath classes" 149 + " -Xbootclasspath" 150 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/core-oj-hostdex.jar" 151 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/core-libart-hostdex.jar" 152 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/core-icu4j-hostdex.jar" 153 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/conscrypt-hostdex.jar" 154 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/okhttp-hostdex.jar" 155 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/bouncycastle-hostdex.jar" 156 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/apache-xml-hostdex.jar" 157 + " -Duser.language=en" 158 + " -Duser.region=US" 159 + " -Xjnigreflimit:2000" 160 + " mainclass" 161 + " -x a\\ b"; 162 assertEquals(Arrays.asList("sh", "-c", expectedCommandString), args); 163 } 164 } 165