1 #!/usr/bin/env bash 2 # 3 # Copyright (C) 2012 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 18 # 19 # This script sets up the execution of the test extraction script 20 # 21 22 STORAGE_DIR="res/tests/resources/nist-pkits" 23 TARGET="src/libcore/java/security/cert/X509CertificateNistPkitsTest.java" 24 25 26 set -e 27 trap "echo WARNING: Exiting on non-zero subprocess exit code" ERR; 28 29 usage() { 30 echo "$0: generates test cases from the NIST PKITS documentation" 31 echo "" 32 echo "Usage: $0 PKITS.pdf PKITS_data.zip" 33 exit 1 34 } 35 36 if [ $# -ne 2 ]; then 37 usage 38 fi 39 40 PDF="${1}" 41 ZIP="${2}" 42 43 if [ ! -f "${PDF}" -o "${PDF#${PDF%.pdf}}" != ".pdf" ]; then 44 echo "The first argument must point to PKITS.pdf" 45 echo "" 46 usage 47 elif [ ! -f "${ZIP}" -o "${ZIP#${ZIP%.zip}}" != ".zip" ]; then 48 echo "The second argument must point to PKITS_data.zip" 49 echo "" 50 usage 51 fi 52 53 if [ ! -f "${TARGET}" ]; then 54 echo "Can not file file:" 55 echo " ${TARGET}" 56 echo "" 57 usage 58 fi 59 60 PDFTOTEXT=$(which pdftotext) 61 if [ -z "${PDFTOTEXT}" -o ! -x "${PDFTOTEXT}" ]; then 62 echo "pdftotext must be installed. Try" 63 echo " apt-get install pdftotext" 64 exit 1 65 fi 66 67 TEMP_TEXT=$(mktemp --tmpdir PKITS.txt.XXXXXXXX) 68 TEMP_JAVA=$(mktemp --tmpdir generated-nist-tests.XXXXXXXXX) 69 TEMP_FILES=$(mktemp --tmpdir generated-nist-files.XXXXXXXXX) 70 71 ${PDFTOTEXT} -layout -nopgbrk -eol unix "${PDF}" "${TEMP_TEXT}" 72 73 "$(dirname $0)/extract-pkits-tests.pl" "${TEMP_TEXT}" "${TEMP_JAVA}" "${TEMP_FILES}" 74 sed -i '/DO NOT MANUALLY EDIT -- BEGIN AUTOMATICALLY GENERATED TESTS/,/DO NOT MANUALLY EDIT -- END AUTOMATICALLY GENERATED TESTS/{//!d}' "${TARGET}" 75 sed -i '/DO NOT MANUALLY EDIT -- BEGIN AUTOMATICALLY GENERATED TESTS/r '"${TEMP_JAVA}" "${TARGET}" 76 77 pushd "$(dirname $0)" 78 mkdir -p "${STORAGE_DIR}" 79 while IFS= read -r -d $'\n' file; do 80 unzip -q -o -d "${STORAGE_DIR}" "${ZIP}" "${file}" 81 done < ${TEMP_FILES} 82 popd 83 84 shasum_file() { 85 declare -r file="$1" 86 87 pushd "$(dirname "${file}")" > /dev/null 2>&1 88 sha256sum -b "$(basename "${file}")" 89 popd > /dev/null 2>&1 90 } 91 92 echo Writing pkits.version ... 93 echo "# sha256sum of PKITS" > pkits.version 94 shasum_file "${PDF}" >> pkits.version 95 shasum_file "${ZIP}" >> pkits.version 96 97 echo Updated tests: ${TARGET} 98