1#!/bin/bash 2# Copyright 2020 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 Tink C++ digital signature example. 21############################################################################# 22 23: "${TEST_TMPDIR:=$(mktemp -d)}" 24 25readonly CLI="$1" 26readonly PRIVATE_KEYSET_FILE="$2" 27readonly PUBLIC_KEYSET_FILE="$3" 28readonly MESSAGE_FILE="${TEST_TMPDIR}/message.txt" 29readonly SIGNATURE_FILE="${TEST_TMPDIR}/signature.txt" 30readonly TEST_NAME="TinkCcExamplesDigitalSignaturesTest" 31 32echo "This is some message to be signed." > "${MESSAGE_FILE}" 33 34####################################### 35# A helper function for getting the return code of a command that may fail. 36# Temporarily disables error safety and stores return value in TEST_STATUS. 37# 38# Globals: 39# TEST_STATUS 40# Arguments: 41# Command to execute. 42####################################### 43test_command() { 44 set +e 45 "$@" 46 TEST_STATUS=$? 47 set -e 48} 49 50####################################### 51# Asserts that the outcome of the latest test command is 0. 52# 53# If not, it terminates the test execution. 54# 55# Globals: 56# TEST_STATUS 57# TEST_NAME 58# TEST_CASE 59####################################### 60assert_command_succeeded() { 61 if (( TEST_STATUS != 0 )); then 62 echo "[ FAILED ] ${TEST_NAME}.${TEST_CASE}" 63 exit 1 64 fi 65} 66 67####################################### 68# Asserts that the outcome of the latest test command is not 0. 69# 70# If not, it terminates the test execution. 71# 72# Globals: 73# TEST_STATUS 74# TEST_NAME 75# TEST_CASE 76####################################### 77assert_command_failed() { 78 if (( TEST_STATUS == 0 )); then 79 echo "[ FAILED ] ${TEST_NAME}.${TEST_CASE}" 80 exit 1 81 fi 82} 83 84####################################### 85# Starts a new test case; records the test case name to TEST_CASE. 86# 87# Globals: 88# TEST_NAME 89# TEST_CASE 90# Arguments: 91# test_case: The name of the test case. 92####################################### 93start_test_case() { 94 TEST_CASE="$1" 95 echo "[ RUN ] ${TEST_NAME}.${TEST_CASE}" 96} 97 98####################################### 99# Ends a test case printing a success message. 100# 101# Globals: 102# TEST_NAME 103# TEST_CASE 104####################################### 105end_test_case() { 106 echo "[ OK ] ${TEST_NAME}.${TEST_CASE}" 107} 108 109############################################################################# 110 111start_test_case "sign_verify_all_good" 112 113# Sign. 114test_command "${CLI}" \ 115 --mode sign \ 116 --keyset_filename "${PRIVATE_KEYSET_FILE}" \ 117 --input_filename "${MESSAGE_FILE}" \ 118 --signature_filename "${SIGNATURE_FILE}" 119assert_command_succeeded 120 121# Verify. 122test_command "${CLI}" \ 123 --mode verify \ 124 --keyset_filename "${PUBLIC_KEYSET_FILE}" \ 125 --input_filename "${MESSAGE_FILE}" \ 126 --signature_filename "${SIGNATURE_FILE}" 127assert_command_succeeded 128 129end_test_case 130 131############################################################################# 132 133start_test_case "verify_fails_with_modified_signature" 134 135# Sign. 136test_command "${CLI}" \ 137 --mode sign \ 138 --keyset_filename "${PRIVATE_KEYSET_FILE}" \ 139 --input_filename "${MESSAGE_FILE}" \ 140 --signature_filename "${SIGNATURE_FILE}" 141assert_command_succeeded 142 143# Modify signature. 144echo "modified" >> "${SIGNATURE_FILE}" 145 146# Verify. 147test_command "${CLI}" \ 148 --mode verify \ 149 --keyset_filename "${PUBLIC_KEYSET_FILE}" \ 150 --input_filename "${MESSAGE_FILE}" \ 151 --signature_filename "${SIGNATURE_FILE}" 152assert_command_failed 153 154end_test_case 155 156############################################################################# 157 158start_test_case "verify_fails_with_modified_message" 159 160# Sign. 161test_command "${CLI}" \ 162 --mode sign \ 163 --keyset_filename "${PRIVATE_KEYSET_FILE}" \ 164 --input_filename "${MESSAGE_FILE}" \ 165 --signature_filename "${SIGNATURE_FILE}" 166assert_command_succeeded 167 168# Modify message. 169echo "modified" >> "${MESSAGE_FILE}" 170 171# Verify. 172test_command "${CLI}" \ 173 --mode verify \ 174 --keyset_filename "${PUBLIC_KEYSET_FILE}" \ 175 --input_filename "${MESSAGE_FILE}" \ 176 --signature_filename "${SIGNATURE_FILE}" 177assert_command_failed 178 179end_test_case 180