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 cleartext keyset example. 21############################################################################# 22 23CLI="$1" 24 25DATA_FILE="${TEST_TMPDIR}/example_data.txt" 26KEYSET_FILE="${TEST_TMPDIR}/example_keyset.json" 27 28echo "This is some plaintext to be encrypted." > "${DATA_FILE}" 29 30############################################################################# 31 32# A helper function for getting the return code of a command that may fail 33# Temporarily disables error safety and stores return value in ${TEST_STATUS} 34# Usage: 35# % test_command somecommand some args 36# % echo ${TEST_STATUS} 37test_command() { 38 set +e 39 "$@" 40 TEST_STATUS=$? 41 set -e 42} 43 44print_test() { 45 echo "+++ Starting test $1..." 46} 47 48############################################################################# 49 50print_test "generate" 51 52# Run encryption 53test_command ${CLI} --mode generate --keyset_path "${KEYSET_FILE}" 54 55if [[ ${TEST_STATUS} -eq 0 ]]; then 56 echo "+++ Success: key file was generated." 57else 58 echo "--- Failure: could not generate key file." 59 exit 1 60fi 61 62############################################################################# 63 64print_test "encrypt" 65 66# Run encryption 67test_command ${CLI} --mode encrypt --keyset_path "${KEYSET_FILE}" \ 68 --input_path "${DATA_FILE}" --output_path "${DATA_FILE}.encrypted" 69 70if (( TEST_STATUS == 0 )); then 71 echo "+++ Success: file was encrypted." 72else 73 echo "--- Failure: could not encrypt file." 74 exit 1 75fi 76 77############################################################################# 78 79print_test "decrypt" 80 81# Run decryption 82test_command ${CLI} --mode decrypt --keyset_path "${KEYSET_FILE}" \ 83 --input_path "${DATA_FILE}.encrypted" --output_path "${DATA_FILE}.decrypted" 84 85if (( TEST_STATUS == 0 )); then 86 echo "+++ Success: file was successfully decrypted." 87else 88 echo "--- Failure: could not decrypt file." 89 exit 1 90fi 91 92if cmp -s "${DATA_FILE}" "${DATA_FILE}.decrypted"; then 93 echo "+++ Success: file content is the same after decryption." 94else 95 echo "--- Failure: file content is not the same after decryption." 96 exit 1 97fi 98