xref: /aosp_15_r20/external/tink/python/examples/streaming_aead/streaming_aead_test.sh (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1#!/bin/bash
2# Copyright 2020 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 python example.
21#############################################################################
22
23
24CLI="$1"
25KEYSET_FILE="$2"
26
27INPUT_FILE="${TEST_TMPDIR}/example_data.txt"
28
29echo "This is some message to be encrypted." > "${INPUT_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 "test_encrypt_decrypt"
52
53# Run verification
54test_command ${CLI} --mode=encrypt \
55  --keyset_path="${KEYSET_FILE}" \
56  --input_path="${INPUT_FILE}" \
57  --output_path="${INPUT_FILE}.ciphertext"
58
59if (( TEST_STATUS == 0 )); then
60  echo "+++ Encryption successful."
61else
62  echo "--- Encryption failed."
63  exit 1
64fi
65
66test_command ${CLI} --mode=decrypt \
67  --keyset_path="${KEYSET_FILE}" \
68  --input_path="${INPUT_FILE}.ciphertext" \
69  --output_path="${INPUT_FILE}.plaintext"
70
71if (( TEST_STATUS == 0 )); then
72  echo "+++ Decryption successful."
73else
74  echo "--- Decryption failed."
75  exit 1
76fi
77
78cmp --silent "${INPUT_FILE}" "${INPUT_FILE}.plaintext"
79
80#############################################################################
81
82print_test "test_encrypt_decrypt_with_ad"
83
84# Run verification
85HEADER_INFORMATION="header information"
86test_command ${CLI} --mode=encrypt \
87  --associated_data="${HEADER_INFORMATION}" \
88  --keyset_path="${KEYSET_FILE}" \
89  --input_path="${INPUT_FILE}" \
90  --output_path="${INPUT_FILE}.ciphertext"
91
92if (( TEST_STATUS == 0 )); then
93  echo "+++ Encryption successful."
94else
95  echo "--- Encryption failed."
96  exit 1
97fi
98
99test_command ${CLI} --mode=decrypt \
100  --associated_data="${HEADER_INFORMATION}" \
101  --keyset_path="${KEYSET_FILE}" \
102  --input_path="${INPUT_FILE}.ciphertext" \
103  --output_path="${INPUT_FILE}.plaintext"
104
105if (( TEST_STATUS == 0 )); then
106  echo "+++ Decryption successful."
107else
108  echo "--- Decryption failed."
109  exit 1
110fi
111
112cmp --silent "${INPUT_FILE}" "${INPUT_FILE}.plaintext"
113
114#############################################################################
115
116print_test "test_modified_ciphertext"
117
118# Run verification
119test_command ${CLI} --mode=encrypt \
120  --keyset_path="${KEYSET_FILE}" \
121  --input_path="${INPUT_FILE}" \
122  --output_path="${INPUT_FILE}.ciphertext"
123
124if (( TEST_STATUS == 0 )); then
125  echo "+++ Encryption successful."
126else
127  echo "--- Encryption failed."
128  exit 1
129fi
130
131# Modify ciphertext so it becomes invalid
132echo "modification" >> "${INPUT_FILE}.ciphertext"
133
134test_command ${CLI} --mode=decrypt \
135  --keyset_path="${KEYSET_FILE}" \
136  --input_path="${INPUT_FILE}.ciphertext" \
137  --output_path="${INPUT_FILE}.plaintext"
138
139if (( TEST_STATUS == 1 )); then
140  echo "+++ Decryption failed as expected."
141else
142  echo "--- Decryption successful but expected to fail."
143  exit 1
144fi
145