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 AEAD example. 21 22CLI="$1" 23KEYSET_FILE="$2" 24 25DATA_FILE="${TEST_TMPDIR}/example_data.txt" 26 27echo "This is some plaintext to be encrypted." > ${DATA_FILE} 28 29############################################################################# 30 31# A helper function for getting the return code of a command that may fail 32# Temporarily disables error safety and stores return value in ${TEST_STATUS} 33# Usage: 34# % test_command somecommand some args 35# % echo ${TEST_STATUS} 36test_command() { 37 set +e 38 "$@" 39 TEST_STATUS=$? 40 set -e 41} 42 43############################################################################# 44#### Test initialization and encryption 45test_name="encrypt" 46echo "+++ Starting test ${test_name}..." 47 48##### Run encryption 49test_command ${CLI} encrypt ${KEYSET_FILE} ${DATA_FILE} "${DATA_FILE}.encrypted" 50 51if [[ ${TEST_STATUS} -eq 0 ]]; then 52 echo "+++ Success: file was encrypted." 53else 54 echo "--- Failure: could not encrypt file." 55 exit 1 56fi 57 58############################################################################# 59#### Test if decryption succeeds and returns original file 60test_name="decrypt" 61echo "+++ Starting test $test_name..." 62 63##### Run decryption 64test_command ${CLI} decrypt ${KEYSET_FILE} ${DATA_FILE}.encrypted "${DATA_FILE}.decrypted" 65 66if [[ ${TEST_STATUS} -eq 0 ]]; then 67 echo "+++ Success: file was successfully decrypted." 68else 69 echo "--- Failure: could not decrypt file." 70 exit 1 71fi 72 73if cmp -s $DATA_FILE "$DATA_FILE.decrypted"; then 74 echo "+++ Success: file content is the same after decryption." 75else 76 echo "--- Failure: file content is not the same after decryption." 77 exit 1 78fi 79 80############################################################################# 81#### Test decryption fails with modified ciphertext 82test_name="test_encrypt_decrypt_fails_with_modified_ciphertext" 83echo "+++ Starting test ${test_name}..." 84 85##### Run encryption 86test_command ${CLI} encrypt ${KEYSET_FILE} ${DATA_FILE} "${DATA_FILE}.encrypted" 87if [[ ${TEST_STATUS} -eq 0 ]]; then 88 echo "+++ Encryption successful." 89else 90 echo "--- Encryption failed." 91 exit 1 92fi 93 94# Modify ciphertext 95echo "modified" >> ${DATA_FILE}.encrypted 96 97##### Run decryption 98test_command ${CLI} decrypt ${KEYSET_FILE} ${DATA_FILE}.encrypted "${DATA_FILE}.decrypted" 99if [[ ${TEST_STATUS} -eq 1 ]]; then 100 echo "+++ Decryption failed as expected." 101else 102 echo "--- Decryption succeeded but expected to fail." 103 exit 1 104fi 105 106############################################################################# 107#### Test correct encryption and decryption with associated data 108test_name="test_encrypt_decrypt_succeeds_with_associated_data" 109echo "+++ Starting test ${test_name}..." 110 111##### Run encryption 112ASSOCIATED_DATA="header information" 113test_command ${CLI} encrypt ${KEYSET_FILE} ${DATA_FILE} "${DATA_FILE}.encrypted" "${ASSOCIATED_DATA}" 114if [[ ${TEST_STATUS} -eq 0 ]]; then 115 echo "+++ Encryption successful." 116else 117 echo "--- Encryption failed." 118 exit 1 119fi 120 121##### Run decryption 122test_command ${CLI} decrypt ${KEYSET_FILE} ${DATA_FILE}.encrypted "${DATA_FILE}.decrypted" "${ASSOCIATED_DATA}" 123if [[ ${TEST_STATUS} -eq 0 ]]; then 124 echo "+++ Decryption successful." 125else 126 echo "--- Decryption failed." 127 exit 1 128fi 129 130cmp --silent ${DATA_FILE} ${DATA_FILE}.decrypted 131 132############################################################################# 133#### Test decryption fails with modified associated data 134test_name="test_encrypt_decrypt_fails_with_modified_associated_data" 135echo "+++ Starting test ${test_name}..." 136 137##### Run encryption 138ASSOCIATED_DATA="header information" 139test_command ${CLI} encrypt ${KEYSET_FILE} ${DATA_FILE} "${DATA_FILE}.encrypted" "${ASSOCIATED_DATA}" 140if [[ ${TEST_STATUS} -eq 0 ]]; then 141 echo "+++ Encryption successful." 142else 143 echo "--- Encryption failed." 144 exit 1 145fi 146 147##### Run decryption 148MODIFIED_ASSOCIATED_DATA="modified header information" 149test_command ${CLI} decrypt ${KEYSET_FILE} ${DATA_FILE}.encrypted "${DATA_FILE}.decrypted" "${MODIFIED_ASSOCIATED_DATA}" 150if [[ ${TEST_STATUS} -eq 1 ]]; then 151 echo "+++ Decryption failed as expected." 152else 153 echo "--- Decryption succeeded but expected to fail." 154 exit 1 155fi 156