1#!/bin/bash 2# Copyright 2021 Google LLC 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 17set -euo pipefail 18 19############################################################################# 20##### Tests for digital signature Java example. 21 22FILE_SIGN_CLI="$1" 23KEYSET_FILE_PRIVATE="$2" 24KEYSET_FILE_PUBLIC="$3" 25 26DATA_FILE="$TEST_TMPDIR/example_data.txt" 27SIGNATURE_FILE="$TEST_TMPDIR/expected_signature.txt" 28 29echo "This is some message to be verified." > $DATA_FILE 30 31############################################################################# 32 33# A helper function for getting the return code of a command that may fail 34# Temporarily disables error safety and stores return value in $TEST_STATUS 35# Usage: 36# % test_command somecommand some args 37# % echo $TEST_STATUS 38test_command() { 39 set +e 40 "$@" 41 TEST_STATUS=$? 42 set -e 43} 44 45############################################################################# 46#### Test basic signature signing and verification. 47test_name="normal_signing_and_verification" 48echo "+++ Starting test $test_name..." 49 50##### Run signing 51test_command $FILE_SIGN_CLI sign $KEYSET_FILE_PRIVATE $DATA_FILE $SIGNATURE_FILE 52 53##### Run verification 54test_command $FILE_SIGN_CLI verify $KEYSET_FILE_PUBLIC $DATA_FILE $SIGNATURE_FILE 55 56if [[ $TEST_STATUS -eq 0 ]]; then 57 echo "+++ Success: Signature is valid." 58else 59 echo "--- Failure: the Signature is invalid." 60 exit 1 61fi 62 63############################################################################# 64#### Test verification fails with incorrect signature. 65test_name="signature_verification_fails_with_incorrect_signature" 66echo "+++ Starting test $test_name..." 67 68##### Create a wrong signature. 69echo "ABCABCABCD" > $SIGNATURE_FILE 70 71##### Run verification. 72test_command $FILE_SIGN_CLI verify $KEYSET_FILE_PUBLIC $DATA_FILE $SIGNATURE_FILE 73 74if [[ $TEST_STATUS -ne 0 ]]; then 75 echo "+++ Success: Signature verification failed for invalid signature." 76else 77 echo "--- Failure: Signature passed for an invalid signature." 78 exit 1 79fi 80 81 82############################################################################# 83#### Test verification fails with an incorrect data. 84test_name="signature_verification_fails_with_incorrect_data" 85echo "+++ Starting test $test_name..." 86 87##### Run signing 88test_command $FILE_SIGN_CLI sign $KEYSET_FILE_PRIVATE $DATA_FILE $SIGNATURE_FILE 89 90##### Modify the data. 91echo "ABCABCABCD" >> $DATA_FILE 92 93##### Run verification. 94test_command $FILE_SIGN_CLI verify $KEYSET_FILE_PUBLIC $DATA_FILE $SIGNATURE_FILE 95 96if [[ $TEST_STATUS -ne 0 ]]; then 97 echo "+++ Success: Signature verification failed for invalid signature." 98else 99 echo "--- Failure: Signature passed for an invalid signature." 100 exit 1 101fi 102 103 104############################################################################# 105#### Test signing fails with a wrong keyset. 106test_name="singing_fails_with_a_wrong_keyset" 107echo "+++ Starting test $test_name..." 108 109##### Run computation. 110test_command $FILE_SIGN_CLI sign $KEYSET_FILE_PUBLIC $DATA_FILE $SIGNATURE_FILE 111 112if [[ $TEST_STATUS -ne 0 ]]; then 113 echo "+++ Success: Signature computation failed with public keyset." 114else 115 echo "--- Failure: Signature computation did not fail with public keyset." 116 exit 1 117fi 118