Home | History | Annotate | Download | only in test
      1 #!/bin/bash
      2 #
      3 # Copyright (C) 2007 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #     http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 
     17 # Set up prog to be the path of this script, including following symlinks,
     18 # and set up progdir to be the fully-qualified pathname of its directory.
     19 prog="$0"
     20 while [ -h "${prog}" ]; do
     21     newProg=`/bin/ls -ld "${prog}"`
     22     newProg=`expr "${newProg}" : ".* -> \(.*\)$"`
     23     if expr "x${newProg}" : 'x/' >/dev/null; then
     24         prog="${newProg}"
     25     else
     26         progdir=`dirname "${prog}"`
     27         prog="${progdir}/${newProg}"
     28     fi
     29 done
     30 oldwd=`pwd`
     31 progdir=`dirname "${prog}"`
     32 cd "${progdir}"
     33 progdir=`pwd`
     34 prog="${progdir}"/`basename "${prog}"`
     35 test_dir="test-$$"
     36 if [ -z "$TMPDIR" ]; then
     37   tmp_dir="/tmp/$USER/${test_dir}"
     38 else
     39   tmp_dir="${TMPDIR}/$USER/${test_dir}"
     40 fi
     41 
     42 export JAVA="java"
     43 export JAVAC="javac -g"
     44 export RUN="${progdir}/etc/push-and-run-test-jar"
     45 export DEX_LOCATION=/data/run-test/${test_dir}
     46 export NEED_DEX="true"
     47 
     48 # If dx was not set by the environment variable, assume it is in the path.
     49 if [ -z "$DX" ]; then
     50   export DX="dx"
     51 fi
     52 
     53 # If jasmin was not set by the environment variable, assume it is in the path.
     54 if [ -z "$JASMIN" ]; then
     55   export JASMIN="jasmin"
     56 fi
     57 
     58 
     59 info="info.txt"
     60 build="build"
     61 run="run"
     62 expected="expected.txt"
     63 check_cmd="check"
     64 output="output.txt"
     65 build_output="build-output.txt"
     66 lib="libartd.so"
     67 run_args="--quiet"
     68 
     69 prebuild_mode="yes"
     70 target_mode="yes"
     71 dev_mode="no"
     72 update_mode="no"
     73 debug_mode="no"
     74 relocate="yes"
     75 runtime="art"
     76 usage="no"
     77 build_only="no"
     78 suffix64=""
     79 trace="false"
     80 always_clean="no"
     81 
     82 while true; do
     83     if [ "x$1" = "x--host" ]; then
     84         target_mode="no"
     85         DEX_LOCATION=$tmp_dir
     86         shift
     87     elif [ "x$1" = "x--jvm" ]; then
     88         target_mode="no"
     89         runtime="jvm"
     90         NEED_DEX="false"
     91         shift
     92     elif [ "x$1" = "x-O" ]; then
     93         lib="libart.so"
     94         shift
     95     elif [ "x$1" = "x--dalvik" ]; then
     96         lib="libdvm.so"
     97         runtime="dalvik"
     98         shift
     99     elif [ "x$1" = "x--relocate" ]; then
    100         relocate="yes"
    101         shift
    102     elif [ "x$1" = "x--no-relocate" ]; then
    103         relocate="no"
    104         shift
    105     elif [ "x$1" = "x--prebuild" ]; then
    106         prebuild_mode="yes"
    107         shift;
    108     elif [ "x$1" = "x--no-prebuild" ]; then
    109         prebuild_mode="no"
    110         shift;
    111     elif [ "x$1" = "x--image" ]; then
    112         shift
    113         image="$1"
    114         run_args="${run_args} --image $image"
    115         shift
    116     elif [ "x$1" = "x-Xcompiler-option" ]; then
    117         shift
    118         option="$1"
    119         run_args="${run_args} -Xcompiler-option $option"
    120         shift
    121     elif [ "x$1" = "x--runtime-option" ]; then
    122         shift
    123         option="$1"
    124         run_args="${run_args} --runtime-option $option"
    125         shift
    126     elif [ "x$1" = "x--debug" ]; then
    127         run_args="${run_args} --debug"
    128         shift
    129     elif [ "x$1" = "x--gdb" ]; then
    130         run_args="${run_args} --gdb"
    131         dev_mode="yes"
    132         shift
    133     elif [ "x$1" = "x--zygote" ]; then
    134         run_args="${run_args} --zygote"
    135         shift
    136     elif [ "x$1" = "x--interpreter" ]; then
    137         run_args="${run_args} --interpreter"
    138         shift
    139     elif [ "x$1" = "x--no-verify" ]; then
    140         run_args="${run_args} --no-verify"
    141         shift
    142     elif [ "x$1" = "x--no-optimize" ]; then
    143         run_args="${run_args} --no-optimize"
    144         shift
    145     elif [ "x$1" = "x--no-precise" ]; then
    146         run_args="${run_args} --no-precise"
    147         shift
    148     elif [ "x$1" = "x--invoke-with" ]; then
    149         shift
    150         what="$1"
    151         if [ "x$what" = "x" ]; then
    152             echo "$0 missing argument to --invoke-with" 1>&2
    153             usage="yes"
    154             break
    155         fi
    156         run_args="${run_args} --invoke-with ${what}"
    157         shift
    158     elif [ "x$1" = "x--dev" ]; then
    159         run_args="${run_args} --dev"
    160         dev_mode="yes"
    161         shift
    162     elif [ "x$1" = "x--build-only" ]; then
    163         build_only="yes"
    164         shift
    165     elif [ "x$1" = "x--output-path" ]; then
    166         shift
    167         tmp_dir=$1
    168         if [ "x$tmp_dir" = "x" ]; then
    169             echo "$0 missing argument to --output-path" 1>&2
    170             usage="yes"
    171             break
    172         fi
    173         shift
    174     elif [ "x$1" = "x--update" ]; then
    175         update_mode="yes"
    176         shift
    177     elif [ "x$1" = "x--help" ]; then
    178         usage="yes"
    179         shift
    180     elif [ "x$1" = "x--64" ]; then
    181         run_args="${run_args} --64"
    182         suffix64="64"
    183         shift
    184     elif [ "x$1" = "x--trace" ]; then
    185         trace="true"
    186         shift
    187     elif [ "x$1" = "x--always-clean" ]; then
    188         always_clean="yes"
    189         shift
    190     elif [ "x$1" = "x--dex2oat-swap" ]; then
    191         run_args="${run_args} --dex2oat-swap"
    192         shift
    193     elif expr "x$1" : "x--" >/dev/null 2>&1; then
    194         echo "unknown $0 option: $1" 1>&2
    195         usage="yes"
    196         break
    197     else
    198         break
    199     fi
    200 done
    201 
    202 # tmp_dir may be relative, resolve.
    203 #
    204 # Cannot use realpath, as it does not exist on Mac.
    205 # Cannot us a simple "cd", as the path might not be created yet.
    206 # Cannot use readlink -m, as it does not exist on Mac.
    207 # Fallback to nuclear option:
    208 tmp_dir="`cd $oldwd ; python -c "import os; print os.path.realpath('$tmp_dir')"`"
    209 mkdir -p $tmp_dir
    210 
    211 if [ "$trace" = "true" ]; then
    212     run_args="${run_args} --runtime-option -Xmethod-trace --runtime-option -Xmethod-trace-file:${DEX_LOCATION}/trace.bin --runtime-option -Xmethod-trace-file-size:2000000"
    213 fi
    214 
    215 # Most interesting target architecture variables are Makefile variables, not environment variables.
    216 # Try to map the suffix64 flag and what we find in ${ANDROID_PRODUCT_OUT}/data/art-test to an architecture name.
    217 function guess_arch_name() {
    218     grep32bit=`ls ${ANDROID_PRODUCT_OUT}/data/art-test | grep -E '^(arm|x86|mips)$'`
    219     grep64bit=`ls ${ANDROID_PRODUCT_OUT}/data/art-test | grep -E '^(arm64|x86_64)$'`
    220     if [ "x${suffix64}" = "x64" ]; then
    221         target_arch_name=${grep64bit}
    222     else
    223         target_arch_name=${grep32bit}
    224     fi
    225 }
    226 
    227 if [ "$target_mode" = "no" ]; then
    228     if [ "$runtime" = "jvm" ]; then
    229         RUN="${progdir}/etc/reference-run-test-classes"
    230         if [ "$prebuild_mode" = "yes" ]; then
    231             echo "--prebuild with --jvm is unsupported";
    232             exit 1;
    233         fi
    234     else
    235         RUN="${progdir}/etc/host-run-test-jar"
    236         if [ "$prebuild_mode" = "yes" ]; then
    237             run_args="${run_args} --prebuild"
    238         fi
    239     fi
    240 else
    241     if [ "$prebuild_mode" = "yes" ]; then
    242         RUN="${progdir}/etc/push-and-run-prebuilt-test-jar"
    243     fi
    244 fi
    245 
    246 if [ ! "$runtime" = "jvm" ]; then
    247   run_args="${run_args} --lib $lib"
    248 fi
    249 
    250 if [ "$runtime" = "dalvik" ]; then
    251     if [ "$target_mode" = "no" ]; then
    252         framework="${ANDROID_PRODUCT_OUT}/system/framework"
    253         bpath="${framework}/core.jar:${framework}/conscrypt.jar:${framework}/okhttp.jar:${framework}/core-junit.jar:${framework}/bouncycastle.jar:${framework}/ext.jar"
    254         run_args="${run_args} --boot -Xbootclasspath:${bpath}"
    255     else
    256         true # defaults to using target BOOTCLASSPATH
    257     fi
    258 elif [ "$runtime" = "art" ]; then
    259     if [ "$target_mode" = "no" ]; then
    260 	# ANDROID_BUILD_TOP and ANDROID_HOST_OUT are not set in a build environment.
    261         if [ -z "$ANDROID_BUILD_TOP" ]; then
    262 	    export ANDROID_BUILD_TOP=$oldwd
    263         fi
    264         if [ -z "$ANDROID_HOST_OUT" ]; then
    265 	    export ANDROID_HOST_OUT=$ANDROID_BUILD_TOP/out/host/linux-x86
    266         fi
    267         run_args="${run_args} --boot -Ximage:${ANDROID_HOST_OUT}/framework/core.art"
    268         run_args="${run_args} --runtime-option -Djava.library.path=${ANDROID_HOST_OUT}/lib${suffix64}"
    269     else
    270         guess_arch_name
    271         run_args="${run_args} --runtime-option -Djava.library.path=/data/art-test/${target_arch_name}"
    272         run_args="${run_args} --boot -Ximage:/data/art-test/core.art"
    273     fi
    274     if [ "$relocate" = "yes" ]; then
    275       run_args="${run_args} --relocate"
    276     else
    277       run_args="${run_args} --no-relocate"
    278     fi
    279 fi
    280 
    281 if [ "$dev_mode" = "yes" -a "$update_mode" = "yes" ]; then
    282     echo "--dev and --update are mutually exclusive" 1>&2
    283     usage="yes"
    284 fi
    285 
    286 if [ "$usage" = "no" ]; then
    287     if [ "x$1" = "x" -o "x$1" = "x-" ]; then
    288         test_dir=`basename "$oldwd"`
    289     else
    290         test_dir="$1"
    291     fi
    292 
    293     if [ '!' -d "$test_dir" ]; then
    294         td2=`echo ${test_dir}-*`
    295         if [ '!' -d "$td2" ]; then
    296             echo "${test_dir}: no such test directory" 1>&2
    297             usage="yes"
    298         fi
    299         test_dir="$td2"
    300     fi
    301     # Shift to get rid of the test name argument. The rest of the arguments
    302     # will get passed to the test run.
    303     shift
    304 fi
    305 
    306 if [ "$usage" = "yes" ]; then
    307     prog=`basename $prog`
    308     (
    309         echo "usage:"
    310         echo "  $prog --help                          Print this message."
    311         echo "  $prog [options] [test-name]           Run test normally."
    312         echo "  $prog --dev [options] [test-name]     Development mode" \
    313              "(dumps to stdout)."
    314         echo "  $prog --update [options] [test-name]  Update mode" \
    315              "(replaces expected.txt)."
    316         echo '  Omitting the test name or specifying "-" will use the' \
    317              "current directory."
    318         echo "  Runtime Options:"
    319         echo "    -O                   Run non-debug rather than debug build (off by default)."
    320         echo "    -Xcompiler-option    Pass an option to the compiler."
    321         echo "    --runtime-option     Pass an option to the runtime."
    322         echo "    --debug              Wait for a debugger to attach."
    323         echo "    --gdb                Run under gdb; incompatible with some tests."
    324         echo "    --build-only         Build test files only (off by default)."
    325         echo "    --interpreter        Enable interpreter only mode (off by default)."
    326         echo "    --no-verify          Turn off verification (on by default)."
    327         echo "    --no-optimize        Turn off optimization (on by default)."
    328         echo "    --no-precise         Turn off precise GC (on by default)."
    329         echo "    --zygote             Spawn the process from the Zygote." \
    330              "If used, then the"
    331         echo "                         other runtime options are ignored."
    332         echo "    --prebuild           Run dex2oat on the files before starting test. (default)"
    333         echo "    --no-prebuild        Do not run dex2oat on the files before starting"
    334         echo "                         the test."
    335         echo "    --relocate           Force the use of relocating in the test, making"
    336         echo "                         the image and oat files be relocated to a random"
    337         echo "                         address before running. (default)"
    338         echo "    --no-relocate        Force the use of no relocating in the test"
    339         echo "    --host               Use the host-mode virtual machine."
    340         echo "    --invoke-with        Pass --invoke-with option to runtime."
    341         echo "    --dalvik             Use Dalvik (off by default)."
    342         echo "    --jvm                Use a host-local RI virtual machine."
    343         echo "    --output-path [path] Location where to store the build" \
    344              "files."
    345         echo "    --64                 Run the test in 64-bit mode"
    346         echo "    --trace              Run with method tracing"
    347         echo "    --always-clean       Delete the test files even if the test fails."
    348         echo "    --dex2oat-swap       Use a dex2oat swap file."
    349     ) 1>&2
    350     exit 1
    351 fi
    352 
    353 cd "$test_dir"
    354 test_dir=`pwd`
    355 
    356 td_info="${test_dir}/${info}"
    357 td_expected="${test_dir}/${expected}"
    358 
    359 if [ ! -r $td_info ]; then
    360     echo "${test_dir}: missing file $td_info" 1>&2
    361     exit 1
    362 fi
    363 
    364 if [ ! -r $td_expected ]; then
    365     echo "${test_dir}: missing file $td_expected" 1>&2
    366     exit 1
    367 fi
    368 
    369 # copy the test to a temp dir and run it
    370 
    371 echo "${test_dir}: building..." 1>&2
    372 
    373 rm -rf "$tmp_dir"
    374 cp -Rp "$test_dir" "$tmp_dir"
    375 cd "$tmp_dir"
    376 
    377 if [ '!' -r "$build" ]; then
    378     cp "${progdir}/etc/default-build" build
    379 fi
    380 
    381 if [ '!' -r "$run" ]; then
    382     cp "${progdir}/etc/default-run" run
    383 fi
    384 
    385 if [ '!' -r "$check_cmd" ]; then
    386     cp "${progdir}/etc/default-check" check
    387 fi
    388 
    389 chmod 755 "$build"
    390 chmod 755 "$run"
    391 chmod 755 "$check_cmd"
    392 
    393 export TEST_NAME=`basename ${test_dir}`
    394 
    395 # To cause tests to fail fast, limit the file sizes created by dx, dex2oat and ART output to 2MB.
    396 file_size_limit=2048
    397 if echo "$test_dir" | grep 089; then
    398   file_size_limit=5120
    399 elif echo "$test_dir" | grep 083; then
    400   file_size_limit=5120
    401 elif echo "$test_dir" | grep 115; then
    402 # Native bridge test copies libarttest.so into its directory, which needs 2MB already.
    403   file_size_limit=5120
    404 fi
    405 if ! ulimit -S "$file_size_limit"; then
    406    echo "ulimit file size setting failed"
    407 fi
    408 
    409 good="no"
    410 if [ "$dev_mode" = "yes" ]; then
    411     "./${build}" 2>&1
    412     build_exit="$?"
    413     echo "build exit status: $build_exit" 1>&2
    414     if [ "$build_exit" = '0' ]; then
    415         echo "${test_dir}: running..." 1>&2
    416         "./${run}" $run_args "$@" 2>&1
    417         run_exit="$?"
    418         echo "run exit status: $run_exit" 1>&2
    419         if [ "$run_exit" = "0" ]; then
    420             good="yes"
    421         fi
    422     fi
    423     exit
    424 elif [ "$update_mode" = "yes" ]; then
    425     "./${build}" >"$build_output" 2>&1
    426     build_exit="$?"
    427     if [ "$build_exit" = '0' ]; then
    428         echo "${test_dir}: running..." 1>&2
    429         "./${run}" $run_args "$@" >"$output" 2>&1
    430         sed -e 's/[[:cntrl:]]$//g' < "$output" >"${td_expected}"
    431         good="yes"
    432     else
    433         cat "$build_output" 1>&2
    434         echo "build exit status: $build_exit" 1>&2
    435     fi
    436 elif [ "$build_only" = "yes" ]; then
    437     good="yes"
    438     "./${build}" >"$build_output" 2>&1
    439     build_exit="$?"
    440     if [ "$build_exit" '!=' '0' ]; then
    441         cp "$build_output" "$output"
    442         echo "build exit status: $build_exit" >>"$output"
    443         diff --strip-trailing-cr -q "$expected" "$output" >/dev/null
    444         if [ "$?" '!=' "0" ]; then
    445             good="no"
    446             echo "BUILD FAILED For ${TEST_NAME}"
    447         fi
    448     fi
    449     # Clean up extraneous files that are not used by tests.
    450     find $tmp_dir -mindepth 1  ! -regex ".*/\(.*jar\|$output\|$expected\)" | xargs rm -rf
    451     exit 0
    452 else
    453     "./${build}" >"$build_output" 2>&1
    454     build_exit="$?"
    455     if [ "$build_exit" = '0' ]; then
    456         echo "${test_dir}: running..." 1>&2
    457         "./${run}" $run_args "$@" >"$output" 2>&1
    458     else
    459         cp "$build_output" "$output"
    460         echo "build exit status: $build_exit" >>"$output"
    461     fi
    462     ./$check_cmd "$expected" "$output"
    463     if [ "$?" = "0" ]; then
    464         # output == expected
    465         good="yes"
    466         echo "${test_dir}: succeeded!" 1>&2
    467     fi
    468 fi
    469 
    470 (
    471     if [ "$good" != "yes" -a "$update_mode" != "yes" ]; then
    472         echo "${test_dir}: FAILED!"
    473         echo ' '
    474         echo '#################### info'
    475         cat "${td_info}" | sed 's/^/# /g'
    476         echo '#################### diffs'
    477         diff --strip-trailing-cr -u "$expected" "$output" | tail -n 500
    478         echo '####################'
    479         echo ' '
    480     fi
    481 
    482 ) 1>&2
    483 
    484 # Clean up test files.
    485 if [ "$always_clean" = "yes" -o "$good" = "yes" ]; then
    486     cd "$oldwd"
    487     rm -rf "$tmp_dir"
    488     if [ "$target_mode" = "yes" -a "$build_exit" = "0" ]; then
    489         adb shell rm -rf $DEX_LOCATION
    490     fi
    491     if [ "$good" = "yes" ]; then
    492         exit 0
    493     fi
    494 fi
    495 
    496 
    497 (
    498     if [ "$always_clean" = "yes" ]; then
    499         echo "${TEST_NAME} files deleted from host "
    500         if [ "$target_mode" == "yes" ]; then
    501             echo "and from target"
    502         fi
    503     else
    504         echo "${TEST_NAME} files left in ${tmp_dir} on host"
    505         if [ "$target_mode" == "yes" ]; then
    506             echo "and in ${DEX_LOCATION} on target"
    507         fi
    508     fi
    509 
    510 ) 1>&2
    511 
    512 exit 1
    513