| Vladimir Marko | 0975ee0 | 2019-04-02 10:29:55 +0100 | [diff] [blame] | 1 | #!/bin/bash | 
|  | 2 | # | 
|  | 3 | # Copyright (C) 2019 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 -e | 
|  | 18 |  | 
|  | 19 | if [[ $# -le 1 ]]; then | 
|  | 20 | cat <<EOF | 
|  | 21 | Usage: | 
|  | 22 | package-check.sh <jar-file> <package-list> | 
|  | 23 | Checks that the class files in the <jar file> are in the <package-list> or | 
|  | 24 | sub-packages. | 
|  | 25 | EOF | 
|  | 26 | exit 1 | 
|  | 27 | fi | 
|  | 28 |  | 
|  | 29 | jar_file=$1 | 
|  | 30 | shift | 
|  | 31 | if [[ ! -f ${jar_file} ]]; then | 
|  | 32 | echo "jar file \"${jar_file}\" does not exist." | 
|  | 33 | exit 1 | 
|  | 34 | fi | 
|  | 35 |  | 
|  | 36 | prefixes=() | 
|  | 37 | while [[ $# -ge 1 ]]; do | 
|  | 38 | package="$1" | 
|  | 39 | if [[ "${package}" = */* ]]; then | 
|  | 40 | echo "Invalid package \"${package}\". Use dot notation for packages." | 
|  | 41 | exit 1 | 
|  | 42 | fi | 
|  | 43 | # Transform to a slash-separated path and add a trailing slash to enforce | 
|  | 44 | # package name boundary. | 
| Jerome Gaillard | 03c64c8 | 2021-10-04 15:28:07 +0000 | [diff] [blame] | 45 | prefixes+=("${package//\.//}/") | 
| Vladimir Marko | 0975ee0 | 2019-04-02 10:29:55 +0100 | [diff] [blame] | 46 | shift | 
|  | 47 | done | 
|  | 48 |  | 
|  | 49 | # Get the file names from the jar file. | 
|  | 50 | zip_contents=`zipinfo -1 $jar_file` | 
|  | 51 |  | 
|  | 52 | # Check all class file names against the expected prefixes. | 
|  | 53 | old_ifs=${IFS} | 
|  | 54 | IFS=$'\n' | 
| Paul Duffin | 63d8feb | 2020-05-28 11:36:14 +0100 | [diff] [blame] | 55 | failed=false | 
| Vladimir Marko | 0975ee0 | 2019-04-02 10:29:55 +0100 | [diff] [blame] | 56 | for zip_entry in ${zip_contents}; do | 
|  | 57 | # Check the suffix. | 
|  | 58 | if [[ "${zip_entry}" = *.class ]]; then | 
|  | 59 | # Match against prefixes. | 
|  | 60 | found=false | 
|  | 61 | for prefix in ${prefixes[@]}; do | 
|  | 62 | if [[ "${zip_entry}" = "${prefix}"* ]]; then | 
|  | 63 | found=true | 
|  | 64 | break | 
|  | 65 | fi | 
|  | 66 | done | 
|  | 67 | if [[ "${found}" == "false" ]]; then | 
|  | 68 | echo "Class file ${zip_entry} is outside specified packages." | 
| Paul Duffin | 63d8feb | 2020-05-28 11:36:14 +0100 | [diff] [blame] | 69 | failed=true | 
| Vladimir Marko | 0975ee0 | 2019-04-02 10:29:55 +0100 | [diff] [blame] | 70 | fi | 
|  | 71 | fi | 
|  | 72 | done | 
| Paul Duffin | 63d8feb | 2020-05-28 11:36:14 +0100 | [diff] [blame] | 73 | if [[ "${failed}" == "true" ]]; then | 
|  | 74 | exit 1 | 
|  | 75 | fi | 
| Vladimir Marko | 0975ee0 | 2019-04-02 10:29:55 +0100 | [diff] [blame] | 76 | IFS=${old_ifs} |