1#! /bin/bash
2#
3
4# Copyright (C) 2023 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7# in compliance with the License. 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 distributed under the License
12# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13# or implied. See the License for the specific language governing permissions and limitations under
14# the License.
15
16# This script runs the tests for the lockedregioninjectioncode.  See
17# TestMain.java for the invocation.  The script expects that a full build has
18# already been done and artifacts are in $TOP/out.
19
20# Compute the default top of the workspace.  The following code copies the
21# strategy of croot.  (croot cannot be usd directly because it is a function and
22# functions are not carried over into subshells.)  This gives the correct answer
23# if run from inside a workspace.  If run from outside a workspace, supply TOP
24# on the command line.
25TOPFILE=build/make/core/envsetup.mk
26TOP=$(dirname $(realpath $0))
27while [[ ! $TOP = / && ! -f $TOP/$TOPFILE ]]; do
28  TOP=$(dirname $TOP)
29done
30# TOP is "/" if this script is located outside a workspace.
31
32# If the user supplied a top directory, use it instead
33if [[ -n $1 ]]; then
34  TOP=$1
35  shift
36fi
37if [[ -z $TOP || $TOP = / ]]; then
38  echo "usage: $0 <workspace-root>"
39  exit 1
40elif [[ ! -d $TOP ]]; then
41  echo "$TOP is not a directory"
42  exit 1
43elif [[ ! -d $TOP/prebuilts/misc/common ]]; then
44  echo "$TOP does not look like w workspace"
45  exit 1
46fi
47echo "Using workspace $TOP"
48
49# Pick up the current java compiler.  The lunch target is not very important,
50# since most, if not all, will use the same host binaries.
51pushd $TOP > /dev/null
52. build/envsetup.sh > /dev/null 2>&1
53lunch redfin-userdebug > /dev/null 2>&1
54popd > /dev/null
55
56# Bail on any error
57set -o pipefail
58trap 'exit 1' ERR
59
60# Create the two sources
61pushd $TOP > /dev/null
62m lockedregioncodeinjection
63m lockedregioncodeinjection_input
64popd > /dev/null
65
66# Create a temporary directory outside of the workspace.
67OUT=$TOP/out/host/test/lockedregioncodeinjection
68echo
69
70# Clean the directory
71if [[ -d $OUT ]]; then rm -r $OUT; fi
72mkdir -p $OUT
73
74ROOT=$TOP/out/host/linux-x86
75EXE=$ROOT/bin/lockedregioncodeinjection
76INP=$ROOT/framework/lockedregioncodeinjection_input.jar
77
78# Run tool on unit tests.
79$EXE \
80    -i $INP -o $OUT/test_output.jar \
81    --targets 'Llockedregioncodeinjection/TestTarget;' \
82    --pre     'lockedregioncodeinjection/TestTarget.boost' \
83    --post    'lockedregioncodeinjection/TestTarget.unboost' \
84    --scoped  'Llockedregioncodeinjection/TestScopedLock;,monitorEnter,monitorExit'
85
86# Run unit tests.
87java -ea -cp $OUT/test_output.jar \
88    org.junit.runner.JUnitCore lockedregioncodeinjection.TestMain
89
90# Extract the class files and decompile them for possible post-analysis.
91pushd $OUT > /dev/null
92jar -x --file test_output.jar lockedregioncodeinjection
93for class in lockedregioncodeinjection/*.class; do
94  javap -c -v $class > ${class%.class}.asm
95done
96popd > /dev/null
97
98echo "artifacts are in $OUT"
99