xref: /aosp_15_r20/frameworks/base/ravenwood/tools/hoststubgen/invoketest/hoststubgen-invoke-test.sh (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1#!/bin/bash
2# Copyright (C) 2023 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# This command is expected to be executed with: atest hoststubgen-invoke-test
17
18set -e # Exit when any command files
19
20# This script runs HostStubGen directly with various arguments and make sure
21# the tool behaves in the expected way.
22
23
24echo "# Listing files in the test environment"
25ls -lR
26
27echo "# Dumping the environment variables"
28env
29
30# Set up the constants and variables
31
32# Bazel sets $TEST_TMPDIR.
33export TEMP=$TEST_TMPDIR
34
35if [[ "$TEMP" == "" ]] ; then
36  TEMP=./tmp
37  mkdir -p $TEMP
38fi
39
40cleanup_temp() {
41  rm -fr $TEMP/*
42}
43
44cleanup_temp
45
46INJAR=hoststubgen-test-tiny-framework.jar
47OUTJAR=$TEMP/host.jar
48
49ANNOTATION_FILTER=$TEMP/annotation-filter.txt
50
51HOSTSTUBGEN_OUT=$TEMP/output.txt
52
53EXTRA_ARGS=""
54
55# Because of `set -e`, we can't return non-zero from functions, so we store
56# HostStubGen result in it.
57HOSTSTUBGEN_RC=0
58
59# Note, because the build rule will only install hoststubgen.jar, but not the wrapper script,
60# we need to execute it manually with the java command.
61hoststubgen() {
62  echo "Running hoststubgen with: $*"
63  java -jar ./hoststubgen.jar "$@"
64}
65
66run_hoststubgen() {
67  local test_name="$1"
68  local annotation_filter="$2"
69
70  echo "# Test: $test_name"
71
72  cleanup_temp
73
74  local filter_arg=""
75
76  if [[ "$annotation_filter" != "" ]] ; then
77    echo "$annotation_filter" > $ANNOTATION_FILTER
78    filter_arg="--annotation-allowed-classes-file $ANNOTATION_FILTER"
79    echo "=== filter ==="
80    cat $ANNOTATION_FILTER
81  fi
82
83  local out_arg=""
84
85  if [[ "$OUTJAR" != "" ]] ; then
86    out_arg="--out-jar $OUTJAR"
87  fi
88
89  hoststubgen \
90      --debug \
91      --in-jar $INJAR \
92      $out_arg \
93      --keep-annotation \
94          android.hosttest.annotation.HostSideTestKeep \
95      --keep-class-annotation \
96          android.hosttest.annotation.HostSideTestWholeClassKeep \
97      --throw-annotation \
98          android.hosttest.annotation.HostSideTestThrow \
99      --remove-annotation \
100          android.hosttest.annotation.HostSideTestRemove \
101      --substitute-annotation \
102          android.hosttest.annotation.HostSideTestSubstitute \
103      --redirect-annotation \
104          android.hosttest.annotation.HostSideTestRedirect \
105      --redirection-class-annotation \
106          android.hosttest.annotation.HostSideTestRedirectionClass \
107      --class-load-hook-annotation \
108          android.hosttest.annotation.HostSideTestClassLoadHook \
109      --keep-static-initializer-annotation \
110          android.hosttest.annotation.HostSideTestStaticInitializerKeep \
111      $filter_arg \
112      $EXTRA_ARGS \
113      |& tee $HOSTSTUBGEN_OUT
114  HOSTSTUBGEN_RC=${PIPESTATUS[0]}
115  echo "HostStubGen exited with $HOSTSTUBGEN_RC"
116  return 0
117}
118
119assert_file_generated() {
120  local file="$1"
121  if [[ "$file" == "" ]] ; then
122    if [[ -f "$file" ]] ; then
123      echo "HostStubGen shouldn't have generated $file"
124      return 1
125    fi
126  else
127    if ! [[ -f "$file" ]] ; then
128      echo "HostStubGen didn't generate $file"
129      return 1
130    fi
131  fi
132}
133
134run_hoststubgen_for_success() {
135  run_hoststubgen "$@"
136
137  if (( $HOSTSTUBGEN_RC != 0 )) ; then
138    echo "HostStubGen expected to finish successfully, but failed with $rc"
139    return 1
140  fi
141
142  assert_file_generated "$STUB"
143  assert_file_generated "$IMPL"
144}
145
146run_hoststubgen_for_failure() {
147  local test_name="$1"
148  local expected_error_message="$2"
149  shift 2
150
151  run_hoststubgen "$test_name" "$@"
152
153  if (( $HOSTSTUBGEN_RC == 0 )) ; then
154    echo "HostStubGen expected to fail, but it didn't fail"
155    return 1
156  fi
157
158  # The output should contain the expected message. (note we se fgrep here.)
159  grep -Fq "$expected_error_message" $HOSTSTUBGEN_OUT
160}
161
162# Start the tests...
163
164# Pass "" as a filter to _not_ add `--annotation-allowed-classes-file`.
165run_hoststubgen_for_success "No annotation filter" ""
166
167# Now, we use " ", so we do add `--annotation-allowed-classes-file`.
168run_hoststubgen_for_failure "No classes are allowed to have annotations" \
169    "not allowed to have Ravenwood annotations" \
170    " "
171
172run_hoststubgen_for_success "All classes allowed (wildcard)" \
173    "
174* # Allow all classes
175"
176
177run_hoststubgen_for_failure "All classes disallowed (wildcard)" \
178    "not allowed to have Ravenwood annotations" \
179    "
180!* # Disallow all classes
181"
182
183run_hoststubgen_for_failure "Some classes not allowed (1)" \
184    "not allowed to have Ravenwood annotations" \
185    "
186android.hosttest.*
187com.android.hoststubgen.*
188com.supported.*
189"
190
191run_hoststubgen_for_failure "Some classes not allowed (2)" \
192    "not allowed to have Ravenwood annotations" \
193    "
194android.hosttest.*
195com.android.hoststubgen.*
196com.unsupported.*
197"
198
199run_hoststubgen_for_success "All classes allowed (package wildcard)" \
200    "
201android.hosttest.*
202com.android.hoststubgen.*
203com.supported.*
204com.unsupported.*
205"
206
207run_hoststubgen_for_failure "One specific class disallowed" \
208    "TinyFrameworkAnnotations is not allowed to have Ravenwood annotations" \
209    "
210!com.android.hoststubgen.test.tinyframework.TinyFrameworkAnnotations
211* # All other classes allowed
212"
213
214run_hoststubgen_for_success "One specific class disallowed, but it doesn't use annotations" \
215    "
216!com.android.hoststubgen.test.tinyframework.TinyFrameworkForTextPolicy
217* # All other classes allowed
218"
219
220OUTJAR="" run_hoststubgen_for_success "No output generation" ""
221
222EXTRA_ARGS="--in-jar abc" run_hoststubgen_for_failure "Duplicate arg" \
223    "Duplicate or conflicting argument found: --in-jar" \
224    ""
225
226
227echo "All tests passed"
228exit 0
229