• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
22STORAGE_DIR="res/tests/resources/nist-pkits"
23TARGET="src/libcore/java/security/cert/X509CertificateNistPkitsTest.java"
24
25
26set -e
27trap "echo WARNING: Exiting on non-zero subprocess exit code" ERR;
28
29usage() {
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
36if [ $# -ne 2 ]; then
37    usage
38fi
39
40PDF="${1}"
41ZIP="${2}"
42
43if [ ! -f "${PDF}" -o "${PDF#${PDF%.pdf}}" != ".pdf" ]; then
44    echo "The first argument must point to PKITS.pdf"
45    echo ""
46    usage
47elif [ ! -f "${ZIP}" -o "${ZIP#${ZIP%.zip}}" != ".zip" ]; then
48    echo "The second argument must point to PKITS_data.zip"
49    echo ""
50    usage
51fi
52
53if [ ! -f "${TARGET}" ]; then
54    echo "Can not file file:"
55    echo "    ${TARGET}"
56    echo ""
57    usage
58fi
59
60PDFTOTEXT=$(which pdftotext)
61if [ -z "${PDFTOTEXT}" -o ! -x "${PDFTOTEXT}" ]; then
62    echo "pdftotext must be installed. Try"
63    echo "    apt-get install pdftotext"
64    exit 1
65fi
66
67TEMP_TEXT=$(mktemp --tmpdir PKITS.txt.XXXXXXXX)
68TEMP_JAVA=$(mktemp --tmpdir generated-nist-tests.XXXXXXXXX)
69TEMP_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}"
74sed -i '/DO NOT MANUALLY EDIT -- BEGIN AUTOMATICALLY GENERATED TESTS/,/DO NOT MANUALLY EDIT -- END AUTOMATICALLY GENERATED TESTS/{//!d}' "${TARGET}"
75sed -i '/DO NOT MANUALLY EDIT -- BEGIN AUTOMATICALLY GENERATED TESTS/r '"${TEMP_JAVA}" "${TARGET}"
76
77pushd "$(dirname "$0")"
78mkdir -p "${STORAGE_DIR}"
79while IFS= read -r -d $'\n' file; do
80    unzip -q -o -d "${STORAGE_DIR}" "${ZIP}" "${file}"
81done < "${TEMP_FILES}"
82popd
83
84shasum_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
92echo Writing pkits.version ...
93echo "# sha256sum of PKITS" > pkits.version
94shasum_file "${PDF}" >> pkits.version
95shasum_file "${ZIP}" >> pkits.version
96
97echo Updated tests: ${TARGET}
98