1#!/bin/bash
2#
3# Copyright (C) 2015 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.
19prog="$0"
20while [ -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
29done
30oldwd=`pwd`
31progdir=`dirname "${prog}"`
32cd "${progdir}"
33progdir=`pwd`
34prog="${progdir}"/`basename "${prog}"`
35
36# Set up a temp directory for output.
37tmpdir=/tmp/test-$$
38mkdir ${tmpdir}
39
40# Set up tools and commands to run
41DEXDUMP="${ANDROID_HOST_OUT}/bin/dexdump"
42DEXLIST="${ANDROID_HOST_OUT}/bin/dexlist"
43
44declare -A SUFFIX_COMMAND_MAP
45SUFFIX_COMMAND_MAP[txt]="${DEXDUMP} -adfh"
46SUFFIX_COMMAND_MAP[xml]="${DEXDUMP} -e -l xml"
47SUFFIX_COMMAND_MAP[lst]="${DEXLIST}"
48
49# Parse command-line options
50UPDATE="no"
51USAGE="no"
52while [ $# -ne 0 ]; do
53  case "$1" in
54    --update)
55      UPDATE="yes"
56      ;;
57    *)
58      echo "Unknown option $1" 1>&2
59      USAGE="yes"
60      ;;
61  esac
62  shift
63done
64
65if [ "${USAGE}" = "yes" ]; then
66  cat 1>&2 <<USAGE_END
67Usage:
68  ${prog##*/} [--update]
69Options:
70  --update     Update reference outputs
71USAGE_END
72  exit 1
73fi
74
75if [ "${UPDATE}" = "yes" ]; then
76  for dex in *.dex; do
77    for suffix in ${!SUFFIX_COMMAND_MAP[@]}; do
78      new_output=${dex%%.*}.${suffix}
79      ${SUFFIX_COMMAND_MAP[${suffix}]} ${dex} > ${new_output}
80      if [ $? -ne 0 ]; then
81        echo "Failed running ${SUFFIX_COMMAND_MAP[${suffix}]} ${dex} > ${new_output}" 2>&1
82        exit 1
83      fi
84    done
85  done
86  exit 0
87fi
88
89# Run the tests.
90passed=0
91failed=0
92for dex in *.dex; do
93  echo ${dex}
94  for suffix in ${!SUFFIX_COMMAND_MAP[@]}; do
95    expected_output=${dex%%.*}.${suffix}
96    actual_output=${tmpdir}/${expected_output}
97    cmd="${SUFFIX_COMMAND_MAP[${suffix}]} ${dex}"
98    ${cmd} > ${actual_output}
99    cmp ${expected_output} ${actual_output}
100    if [ "$?" = "0" ]; then
101        ((passed += 1))
102    else
103        ((failed += 1))
104        echo failed: ${cmd}
105    fi
106  done
107done
108
109# Report results.
110echo
111echo "passed: ${passed} test(s)"
112echo "failed: ${failed} test(s)"
113echo
114
115# Clean up, cd back to original dir.
116rm -rf ${tmpdir}
117cd ${oldwd}
118
119# Return status.
120if [ "${failed}" != "0" ]; then
121  echo failed
122  exit 1
123fi
124exit 0
125
126