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 envelope encryption AEAD example. 21 22CLI="$1" 23KEY_URI="$2" 24CRED_FILE="$3" 25PROJECT_ID="$4" 26GCS_BUCKET="$5" 27 28DATA_FILE="$TEST_TMPDIR/example_data.txt" 29 30echo "This is some plaintext to be encrypted." > ${DATA_FILE} 31 32############################################################################# 33 34# A helper function for getting the return code of a command that may fail 35# Temporarily disables error safety and stores return value in ${TEST_STATUS} 36# Usage: 37# % test_command somecommand some args 38# % echo ${TEST_STATUS} 39test_command() { 40 set +e 41 "$@" 42 TEST_STATUS=$? 43 set -e 44} 45 46############################################################################# 47#### Test initialization and encryption 48test_name="encrypt" 49echo "+++ Starting test $test_name..." 50 51# ##### Run encryption 52test_command ${CLI} encrypt ${KEY_URI} ${CRED_FILE} ${PROJECT_ID} \ 53 ${DATA_FILE} "${GCS_BUCKET}/example_data.txt.encrypted" 54 55if [[ ${TEST_STATUS} -eq 0 ]]; then 56 echo "+++ Success: file was encrypted." 57else 58 echo "--- Failure: could not encrypt file." 59 exit 1 60fi 61 62############################################################################# 63#### Test if decryption succeeds and returns original file 64test_name="decrypt" 65echo "+++ Starting test $test_name..." 66 67##### Run decryption 68test_command ${CLI} decrypt ${KEY_URI} ${CRED_FILE} ${PROJECT_ID} \ 69 "${GCS_BUCKET}/example_data.txt.encrypted" "${DATA_FILE}.decrypted" 70 71if [[ ${TEST_STATUS} -eq 0 ]]; then 72 echo "+++ Success: file was successfully decrypted." 73else 74 echo "--- Failure: could not decrypt file." 75 exit 1 76fi 77 78if cmp -s ${DATA_FILE} "${DATA_FILE}.decrypted"; then 79 echo "+++ Success: file content is the same after decryption." 80else 81 echo "--- Failure: file content is not the same after decryption." 82 exit 1 83fi 84