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_RUNTIME_ROOT=" 77 + "${ANDROID_BUILD_TOP}/out/host/linux-x86/com.android.runtime" 78 + " ANDROID_TZDATA_ROOT=" 79 + "${ANDROID_BUILD_TOP}/out/host/linux-x86/com.android.tzdata" 80 + " LD_LIBRARY_PATH=${ANDROID_BUILD_TOP}/out/host/linux-x86/lib" 81 + " DYLD_LIBRARY_PATH=${ANDROID_BUILD_TOP}/out/host/linux-x86/lib" 82 + " LD_USE_LOAD_BIAS=1" 83 + " ${ANDROID_BUILD_TOP}/out/host/linux-x86/bin/dalvikvm" 84 + " -classpath classes" 85 + " -Xbootclasspath" 86 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/core-oj-hostdex.jar" 87 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/core-libart-hostdex.jar" 88 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/conscrypt-hostdex.jar" 89 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/okhttp-hostdex.jar" 90 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/bouncycastle-hostdex.jar" 91 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/apache-xml-hostdex.jar" 92 + " -Duser.language=en" 93 + " -Duser.region=US" 94 + " -Xcheck:jni" 95 + " -Xjnigreflimit:2000" 96 + " mainclass" 97 + " -x a\\ b"; 98 assertEquals(Arrays.asList("sh", "-c", expectedCommandString), args); 99 } 100 replaceEnvironmentVariables(List<String> args)101 public List<String> replaceEnvironmentVariables(List<String> args) { 102 String androidBuildTop = System.getenv("ANDROID_BUILD_TOP"); 103 assertNotNull("ANDROID_BUILD_TOP not set", androidBuildTop); 104 Path path = Paths.get(androidBuildTop).normalize(); 105 final String prefix = Pattern.quote(path.toString() + "/"); 106 args = Lists.transform(args, new Function<String, String>() { 107 @Override 108 public String apply(String input) { 109 return input.replaceAll(prefix, ANDROID_BUILD_TOP_REFERENCE); 110 } 111 }); 112 return args; 113 } 114 115 @Test 116 @VogarArgs({"--benchmark", "action"}) testLocalTarget_Benchmark()117 public void testLocalTarget_Benchmark() 118 throws IOException { 119 120 Mode hostRuntime = new HostRuntime(run, ModeId.HOST, Variant.X32); 121 122 VmCommandBuilder builder = newVmCommandBuilder(hostRuntime) 123 .classpath(classpath) 124 .mainClass("mainclass") 125 .args("-x", "a b"); 126 Command command = builder.build(run.target); 127 List<String> args = command.getArgs(); 128 args = replaceEnvironmentVariables(args); 129 130 String expectedCommandString = "" 131 + "ANDROID_PRINTF_LOG=tag" 132 + " ANDROID_LOG_TAGS=*:i" 133 + " ANDROID_DATA=" + run.localFile("android-data") 134 + " ANDROID_ROOT=${ANDROID_BUILD_TOP}/out/host/linux-x86" 135 + " ANDROID_RUNTIME_ROOT=" 136 + "${ANDROID_BUILD_TOP}/out/host/linux-x86/com.android.runtime" 137 + " ANDROID_TZDATA_ROOT=" 138 + "${ANDROID_BUILD_TOP}/out/host/linux-x86/com.android.tzdata" 139 + " LD_LIBRARY_PATH=${ANDROID_BUILD_TOP}/out/host/linux-x86/lib" 140 + " DYLD_LIBRARY_PATH=${ANDROID_BUILD_TOP}/out/host/linux-x86/lib" 141 + " LD_USE_LOAD_BIAS=1" 142 + " ${ANDROID_BUILD_TOP}/out/host/linux-x86/bin/dalvikvm" 143 + " -classpath classes" 144 + " -Xbootclasspath" 145 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/core-oj-hostdex.jar" 146 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/core-libart-hostdex.jar" 147 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/conscrypt-hostdex.jar" 148 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/okhttp-hostdex.jar" 149 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/bouncycastle-hostdex.jar" 150 + ":${ANDROID_BUILD_TOP}/out/host/linux-x86/framework/apache-xml-hostdex.jar" 151 + " -Duser.language=en" 152 + " -Duser.region=US" 153 + " -Xjnigreflimit:2000" 154 + " mainclass" 155 + " -x a\\ b"; 156 assertEquals(Arrays.asList("sh", "-c", expectedCommandString), args); 157 } 158 } 159