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 MAC Python example. 21############################################################################# 22 23CLI="$1" 24KEYSET_FILE="$2" 25 26DATA_FILE="${TEST_TMPDIR}/example_data.txt" 27MAC_FILE="${TEST_TMPDIR}/expected_mac.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 45print_test() { 46 echo "+++ Starting test $1..." 47} 48 49############################################################################# 50 51print_test "mac_computation_and_verification" 52 53# Run computation. 54${CLI} --mode compute --keyset_path "${KEYSET_FILE}" \ 55 --data_path "${DATA_FILE}" --mac_path "${MAC_FILE}" 56 57# Run verification. 58test_command ${CLI} --mode verify --keyset_path "${KEYSET_FILE}" \ 59 --data_path "${DATA_FILE}" --mac_path "${MAC_FILE}" 60 61if (( TEST_STATUS == 0 )); then 62 echo "+++ Success: MAC computation was successful." 63else 64 echo "--- Failure: MAC computation was unsuccessful" 65 exit 1 66fi 67 68 69############################################################################# 70 71print_test "mac_verification_fails_with_incorrect_mac" 72 73# Run computation. 74${CLI} --mode compute --keyset_path "${KEYSET_FILE}" \ 75 --data_path "${DATA_FILE}" --mac_path "${MAC_FILE}" 76 77# Modify MAC. 78echo "DEADBEEF" >> "${MAC_FILE}" 79 80# Run verification. 81test_command ${CLI} --mode verify --keyset_path "${KEYSET_FILE}" \ 82 --data_path "${DATA_FILE}" --mac_path "${MAC_FILE}" 83 84if (( TEST_STATUS != 0 )); then 85 echo "+++ Success: MAC verification failed for a modified mac." 86else 87 echo "--- Failure: MAC verification passed for a modified mac." 88 exit 1 89fi 90 91 92############################################################################# 93 94print_test "mac_verification_fails_with_modified_message" 95 96# Run computation. 97${CLI} --mode compute --keyset_path "${KEYSET_FILE}" \ 98 --data_path "${DATA_FILE}" --mac_path "${MAC_FILE}" 99 100# Modify MAC. 101echo "modified" >> "${DATA_FILE}" 102 103# Run verification. 104test_command ${CLI} --mode verify --keyset_path "${KEYSET_FILE}" \ 105 --data_path "${DATA_FILE}" --mac_path "${MAC_FILE}" 106 107if (( TEST_STATUS != 0 )); then 108 echo "+++ Success: MAC verification failed for a modified message." 109else 110 echo "--- Failure: MAC verification passed for a modified message." 111 exit 1 112fi 113 114 115############################################################################# 116 117print_test "bad_key_computation" 118 119# Create a plaintext and bad keyset. 120BAD_KEY_FILE="${TEST_TMPDIR}/bad_key.txt" 121echo "not a key" > "${BAD_KEY_FILE}" 122 123# Run computation. 124test_command ${CLI} --mode compute --keyset_path "${BAD_KEY_FILE}" \ 125 --data_path "${DATA_FILE}" --mac_path "${MAC_FILE}" 126 127if (( TEST_STATUS != 0 )); then 128 echo "+++ Success: MAC computation failed with bad keyset." 129else 130 echo "--- Failure: MAC computation did not fail with bad keyset" 131 exit 1 132fi 133