xref: /aosp_15_r20/external/tink/java_src/examples/mac/mac_example_test.sh (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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 example.
21
22MAC_CLI="$1"
23KEYSET_FILE="$2"
24
25DATA_FILE="$TEST_TMPDIR/example_data.txt"
26MAC_FILE="$TEST_TMPDIR/expected_mac.txt"
27
28echo "This is some message to be verified." > $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
44
45#############################################################################
46#### Test MAC computation and verification.
47test_name="mac_computation_and_verification"
48echo "+++ Starting test $test_name..."
49
50##### Run computation.
51$MAC_CLI compute $KEYSET_FILE $DATA_FILE $MAC_FILE
52
53##### Run verification.
54test_command $MAC_CLI verify $KEYSET_FILE $DATA_FILE $MAC_FILE
55
56if [[ $TEST_STATUS -eq 0 ]]; then
57  echo "+++ Success: MAC computation was successful."
58else
59  echo "--- Failure: MAC computation was unsuccessful"
60  exit 1
61fi
62
63
64#############################################################################
65#### Test MAC verification fails with incorrect MAC.
66test_name="mac_verification_fails_with_incorrect_mac"
67echo "+++ Starting test $test_name..."
68
69##### Run computation.
70$MAC_CLI compute $KEYSET_FILE $DATA_FILE $MAC_FILE
71
72# Modify MAC.
73echo "DEADBEEF" >> "$MAC_FILE"
74
75##### Run verification.
76test_command $MAC_CLI verify $KEYSET_FILE $DATA_FILE $MAC_FILE
77
78if [[ $TEST_STATUS -ne 0 ]]; then
79  echo "+++ Success: MAC verification failed for a modified mac."
80else
81  echo "--- Failure: MAC verification passed for a modified mac."
82  exit 1
83fi
84
85
86#############################################################################
87#### Test MAC verification fails with modified message.
88test_name="mac_verification_fails_with_modified_message"
89echo "+++ Starting test $test_name..."
90
91##### Run computation.
92$MAC_CLI compute $KEYSET_FILE $DATA_FILE $MAC_FILE
93
94# Modify MAC.
95echo "modified" >> "$DATA_FILE"
96
97##### Run verification.
98test_command $MAC_CLI verify $KEYSET_FILE $DATA_FILE $MAC_FILE
99
100if [[ $TEST_STATUS -ne 0 ]]; then
101  echo "+++ Success: MAC verification failed for a modified message."
102else
103  echo "--- Failure: MAC verification passed for a modified message."
104  exit 1
105fi
106
107
108#############################################################################
109#### Test bad key MAC computation.
110test_name="bad_key_computation"
111echo "+++ Starting test $test_name..."
112
113##### Create a plaintext and bad keyset.
114BAD_KEY_FILE="$TEST_TMPDIR/bad_key.txt"
115echo "not a key" > $BAD_KEY_FILE
116
117##### Run computation.
118test_command $MAC_CLI compute $BAD_KEY_FILE $DATA_FILE $MAC_FILE
119
120if [[ $TEST_STATUS -ne 0 ]]; then
121  echo "+++ Success: MAC computation failed with bad keyset."
122else
123  echo "--- Failure: MAC computation did not fail with bad keyset"
124  exit 1
125fi
126