Home | History | Annotate | Download | only in libc
      1 #!/bin/sh
      2 #
      3 # Copyright (C) 2010 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 #  This shell script is used to run one test on a device emulator.
     18 #
     19 
     20 PROGDIR=`dirname $0`
     21 
     22 #
     23 # Parse options
     24 #
     25 VERBOSE=no
     26 VERBOSE2=no
     27 ADB_CMD=adb
     28 
     29 while [ -n "$1" ]; do
     30     opt="$1"
     31     optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
     32     case "$opt" in
     33         --help|-h|-\?)
     34             OPTION_HELP=yes
     35             ;;
     36         --verbose)
     37             if [ "$VERBOSE" = "yes" ] ; then
     38                 VERBOSE2=yes
     39             else
     40                 VERBOSE=yes
     41             fi
     42             ;;
     43         --adb=*)
     44             ADB_CMD="$optarg"
     45             ;;
     46         -*) # unknown options
     47             echo "ERROR: Unknown option '$opt', use --help for list of valid ones."
     48             exit 1
     49         ;;
     50         *)  # Simply record parameter
     51             if [ -z "$PARAMETERS" ] ; then
     52                 PARAMETERS="$opt"
     53             else
     54                 PARAMETERS="$PARAMETERS $opt"
     55             fi
     56             ;;
     57     esac
     58     shift
     59 done
     60 
     61 if [ "$OPTION_HELP" = "yes" ] ; then
     62     echo "Usage: $PROGNAME [options] <test-name>"
     63     echo ""
     64     echo "Run one C library test on a device/emulator through ADB."
     65     echo ""
     66     echo "Valid options:"
     67     echo ""
     68     echo "    --help|-h|-?      Print this help"
     69     echo "    --verbose         Enable verbose mode"
     70     echo "    --adb=<file>      Specify adb executable for device tests"
     71     echo ""
     72     exit 0
     73 fi
     74 
     75 if [ -z "$ANDROID_PRODUCT_OUT" ] ; then
     76     echo "ERROR: ANDROID_PRODUCT_OUT not defined. Please run the 'lunch' command"
     77     exit 1
     78 fi
     79 
     80 if [ ! -f "$ANDROID_PRODUCT_OUT/system.img" ] ; then
     81     echo "ERROR: Missing file: $ANDROID_PRODUCT_OUT/system.img"
     82     echo "Are you sure you built the proper system image?"
     83     exit 1
     84 fi
     85 
     86 EXEC_ROOT_PATH="$ANDROID_PRODUCT_OUT/obj/EXECUTABLES"
     87 if [ ! -d "$EXEC_ROOT_PATH" ] ; then
     88     echo "ERROR: Missing directory: $EXEC_ROOT_PATH"
     89     echo "Are you sure you built the proper system image?"
     90     exit 1
     91 fi
     92 
     93 if [ -z "$PARAMETERS" ] ; then
     94     echo "ERROR: Please specify test name."
     95     echo "Must be one of the following:"
     96     for FILE in `cd $EXEC_ROOT_PATH && ls -d test_*`; do
     97         TEST=`echo "$FILE" | sed -e "s!test_\(.*\)_intermediates!\\1!g"`
     98         echo "  $TEST"
     99     done
    100     exit 1
    101 fi
    102 
    103 TEST="$PARAMETERS"
    104 # Normalize test name, i.e. remove test_ prefix
    105 TEST=`echo "$TEST" | sed -e "s!^test_!!g"`
    106 
    107 TESTDIR="$EXEC_ROOT_PATH/test_${TEST}_intermediates"
    108 if [ ! -d "$TESTDIR" ] ; then
    109     echo "ERROR: No test by that name: test_$TEST!"
    110     exit 1
    111 fi
    112 
    113 TESTNAME="test_$TEST"
    114 TESTEXE="$TESTDIR/$TESTNAME"
    115 if [ ! -f "$TESTEXE" ] ; then
    116     echo "ERROR: Missing file: $TESTEXE"
    117     echo "Are you sure your last test build was complete?"
    118     exit 1
    119 fi
    120 
    121 # Run a command in ADB and return 0 in case of success, or 1 otherwise.
    122 # This is needed because "adb shell" does not return the proper status
    123 # of the launched command.
    124 #
    125 # NOTE: You must call set_adb_cmd_log before that to set the location
    126 #        of the temporary log file that will be used.
    127 #
    128 adb_cmd ()
    129 {
    130     local RET
    131     if [ -z "$ADB_CMD_LOG" ] ; then
    132         dump "INTERNAL ERROR: ADB_CMD_LOG not set!"
    133         exit 1
    134     fi
    135     if [ $VERBOSE = "yes" ] ; then
    136         echo "$ADB_CMD shell $@"
    137         $ADB_CMD shell $@ "&&" echo OK "||" echo KO | tee $ADB_CMD_LOG
    138     else
    139         $ADB_CMD shell $@ "&&" echo OK "||" echo KO > $ADB_CMD_LOG
    140     fi
    141     # Get last line in log, should be OK or KO
    142     RET=`tail -n1 $ADB_CMD_LOG`
    143     # Get rid of \r at the end of lines
    144     RET=`echo "$RET" | sed -e 's![[:cntrl:]]!!g'`
    145     [ "$RET" = "OK" ]
    146 }
    147 
    148 set_adb_cmd_log ()
    149 {
    150     ADB_CMD_LOG="$1"
    151 }
    152 
    153 # Returns 0 if a variable containing one or more items separated
    154 # by spaces contains a given value.
    155 # $1: variable name (e.g. FOO)
    156 # $2: value to test
    157 var_list_contains ()
    158 {
    159     echo `var_value $1` | tr ' ' '\n' | fgrep -q -e "$2"
    160 }
    161 
    162 TMPDIR=/tmp/bionic-tests
    163 mkdir -p $TMPDIR
    164 set_adb_cmd_log $TMPDIR/adb.log.txt
    165 
    166 DEVICE_TEST_DIR=/data/local/bionic-test
    167 DEVICE_TEST=$DEVICE_TEST_DIR/$TESTNAME
    168 adb_cmd mkdir $DEVICE_TEST_DIR
    169 $ADB_CMD push $TESTEXE $DEVICE_TEST_DIR/
    170 if [ $? != 0 ] ; then
    171     echo "ERROR: Can't push test to device!"
    172     exit 1
    173 fi
    174 
    175 adb_cmd chmod 0755 $DEVICE_TEST &&
    176 adb_cmd $DEVICE_TEST
    177 RET=$?
    178 adb_cmd rm -r $DEVICE_TEST_DIR
    179 
    180 if [ "$RET" != 0 ] ; then
    181     echo "FAIL!"
    182 else
    183     echo "OK!"
    184 fi
    185 exit 0
    186