xref: /aosp_15_r20/external/tink/java_src/examples/streamingaead/streaming_aead_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 Streaming AEAD example.
21
22CLI="$1"
23KEYSET_FILE="$2"
24
25INPUT_FILE="${TEST_TMPDIR}/example_data.txt"
26
27echo "This is some message to be encrypted." > ${INPUT_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 correct encryption and decryption.
45test_name="test_encrypt_decrypt"
46echo "+++ Starting test ${test_name}..."
47
48##### Run verification
49test_command ${CLI} encrypt ${KEYSET_FILE} ${INPUT_FILE} ${INPUT_FILE}.ciphertext
50if [[ ${TEST_STATUS} -eq 0 ]]; then
51  echo "+++ Encryption successful."
52else
53  echo "--- Encryption failed."
54  exit 1
55fi
56
57test_command ${CLI} decrypt ${KEYSET_FILE} ${INPUT_FILE}.ciphertext ${INPUT_FILE}.plaintext
58if [[ ${TEST_STATUS} -eq 0 ]]; then
59  echo "+++ Decryption successful."
60else
61  echo "--- Decryption failed."
62  exit 1
63fi
64
65cmp --silent ${INPUT_FILE} ${INPUT_FILE}.plaintext
66
67#############################################################################
68#### Test correct encryption and decryption with associated data
69test_name="test_encrypt_decrypt_with_ad"
70echo "+++ Starting test ${test_name}..."
71
72##### Run verification
73HEADER_INFORMATION="header information"
74test_command ${CLI} encrypt ${KEYSET_FILE} ${INPUT_FILE} ${INPUT_FILE}.ciphertext "${HEADER_INFORMATION}"
75if [[ ${TEST_STATUS} -eq 0 ]]; then
76  echo "+++ Encryption successful."
77else
78  echo "--- Encryption failed."
79  exit 1
80fi
81
82test_command ${CLI} decrypt ${KEYSET_FILE} ${INPUT_FILE}.ciphertext ${INPUT_FILE}.plaintext "${HEADER_INFORMATION}"
83if [[ ${TEST_STATUS} -eq 0 ]]; then
84  echo "+++ Decryption successful."
85else
86  echo "--- Decryption failed."
87  exit 1
88fi
89
90cmp --silent ${INPUT_FILE} ${INPUT_FILE}.plaintext
91
92#############################################################################
93#### Test that modified ciphertext does not decrypt
94test_name="test_modified_ciphertext"
95echo "+++ Starting test ${test_name}..."
96
97##### Run verification
98test_command ${CLI} encrypt ${KEYSET_FILE} ${INPUT_FILE} ${INPUT_FILE}.ciphertext
99if [[ ${TEST_STATUS} -eq 0 ]]; then
100  echo "+++ Encryption successful."
101else
102  echo "--- Encryption failed."
103  exit 1
104fi
105
106# Modify ciphertext so it becomes invalid
107echo "modification" >> ${INPUT_FILE}.ciphertext
108
109test_command ${CLI} decrypt ${KEYSET_FILE} ${INPUT_FILE}.ciphertext ${INPUT_FILE}.plaintext
110if [[ ${TEST_STATUS} -eq 1 ]]; then
111  echo "+++ Decryption failed as expected."
112else
113  echo "--- Decryption successful but expected to fail."
114  exit 1
115fi
116
117#############################################################################
118#### Test that modified associated data does not decrypt
119test_name="test_modified_ciphertext"
120echo "+++ Starting test ${test_name}..."
121
122##### Run verification
123HEADER_INFORMATION="header information"
124test_command ${CLI} encrypt ${KEYSET_FILE} ${INPUT_FILE} ${INPUT_FILE}.ciphertext "${HEADER_INFORMATION}"
125if [[ ${TEST_STATUS} -eq 0 ]]; then
126  echo "+++ Encryption successful."
127else
128  echo "--- Encryption failed."
129  exit 1
130fi
131
132# Modify header
133MODIFIED_HEADER_INFORMATION="modified header information"
134
135test_command ${CLI} decrypt ${KEYSET_FILE} ${INPUT_FILE}.ciphertext ${INPUT_FILE}.plaintext "${MODIFIED_HEADER_INFORMATION}"
136if [[ ${TEST_STATUS} -eq 1 ]]; then
137  echo "+++ Decryption failed as expected."
138else
139  echo "--- Decryption successful but expected to fail."
140  exit 1
141fi
142