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
49ALL_DEX_FILES_JAR=all-dex-files.jar
50
51# Parse command-line options
52UPDATE="no"
53USAGE="no"
54while [ $# -ne 0 ]; do
55  case "$1" in
56    --update)
57      UPDATE="yes"
58      ;;
59    *)
60      echo "Unknown option $1" 1>&2
61      USAGE="yes"
62      ;;
63  esac
64  shift
65done
66
67if [ "${USAGE}" = "yes" ]; then
68  cat 1>&2 <<USAGE_END
69Usage:
70  ${prog##*/} [--update]
71Options:
72  --update     Update reference outputs
73USAGE_END
74  exit 1
75fi
76
77if [ "${UPDATE}" = "yes" ]; then
78  for dex in *.dex; do
79    for suffix in ${!SUFFIX_COMMAND_MAP[@]}; do
80      new_output=${dex%%.*}.${suffix}
81      ${SUFFIX_COMMAND_MAP[${suffix}]} ${dex} > ${new_output}
82      if [ $? -ne 0 ]; then
83        echo "Failed running ${SUFFIX_COMMAND_MAP[${suffix}]} ${dex} > ${new_output}" 2>&1
84        exit 1
85      fi
86    done
87  done
88
89  # Create a .jar file containing all dex files, renamed as expected by the runtime.
90  count=0
91  for i in *.dex; do
92    if [ ${count} = 0 ] ; then
93      dex_file=classes.dex
94      flags="-cf"
95    else
96      dex_file=classes${count}.dex
97      flags="-uf"
98    fi
99    cp $i ${tmpdir}/${dex_file}
100    jar ${flags} ${ALL_DEX_FILES_JAR} -C ${tmpdir} ${dex_file}
101    ((count += 1))
102    rm ${tmpdir}/${dex_file}
103  done
104
105  prefix=$(basename ${ALL_DEX_FILES_JAR} .jar)
106  for suffix in ${!SUFFIX_COMMAND_MAP[@]}; do
107    new_output=${prefix}.${suffix}
108    ${SUFFIX_COMMAND_MAP[${suffix}]} ${ALL_DEX_FILES_JAR} > ${new_output}
109  done
110
111  exit 0
112fi
113
114# Run the tests.
115passed=0
116failed=0
117for input in *.dex *.jar; do
118  echo ${input}
119  for suffix in ${!SUFFIX_COMMAND_MAP[@]}; do
120    expected_output=${input%%.*}.${suffix}
121    actual_output=${tmpdir}/${expected_output}
122    cmd="${SUFFIX_COMMAND_MAP[${suffix}]} ${input}"
123    ${cmd} > ${actual_output}
124    cmp ${expected_output} ${actual_output}
125    if [ "$?" = "0" ]; then
126      ((passed += 1))
127    else
128      ((failed += 1))
129      echo failed: ${cmd}
130    fi
131  done
132done
133
134# Report results.
135echo
136echo "passed: ${passed} test(s)"
137echo "failed: ${failed} test(s)"
138echo
139
140# Clean up, cd back to original dir.
141rm -rf ${tmpdir}
142cd ${oldwd}
143
144# Return status.
145if [ "${failed}" != "0" ]; then
146  echo failed
147  exit 1
148fi
149exit 0
150
151