xref: /aosp_15_r20/external/tink/cc/examples/hybrid_encryption/hybrid_cli_test.sh (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1#!/bin/bash
2# Copyright 2022 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 Tink C++ hybrid encryption example.
21#############################################################################
22
23: "${TEST_TMPDIR:=$(mktemp -d)}"
24
25readonly CLI="$1"
26readonly PRIVATE_KEYSET_FILE="$2"
27readonly PUBLIC_KEYSET_FILE="$3"
28readonly DATA_FILE="${TEST_TMPDIR}/example_data.txt"
29readonly TEST_NAME="TinkExamplesCcHybridTest"
30
31echo "This is some message to be encrypted." > "${DATA_FILE}"
32
33#######################################
34# A helper function for getting the return code of a command that may fail.
35# Temporarily disables error safety and stores return value in TEST_STATUS.
36#
37# Globals:
38#   TEST_STATUS
39# Arguments:
40#   Command to execute.
41#######################################
42test_command() {
43  set +e
44  "$@"
45  TEST_STATUS=$?
46  set -e
47}
48
49#######################################
50# Asserts that the outcome of the latest test command is 0.
51#
52# If not, it terminates the test execution.
53#
54# Globals:
55#   TEST_STATUS
56#   TEST_NAME
57#   TEST_CASE
58#######################################
59assert_command_succeeded() {
60  if (( TEST_STATUS != 0 )); then
61    echo "[   FAILED ] ${TEST_NAME}.${TEST_CASE}"
62    exit 1
63  fi
64}
65
66#######################################
67# Asserts that the outcome of the latest test command is not 0.
68#
69# If not, it terminates the test execution.
70#
71# Globals:
72#   TEST_STATUS
73#   TEST_NAME
74#   TEST_CASE
75#######################################
76assert_command_failed() {
77  if (( TEST_STATUS == 0 )); then
78      echo "[   FAILED ] ${TEST_NAME}.${TEST_CASE}"
79      exit 1
80  fi
81}
82
83#######################################
84# Starts a new test case; records the test case name to TEST_CASE.
85#
86# Globals:
87#   TEST_NAME
88#   TEST_CASE
89# Arguments:
90#   test_case: The name of the test case.
91#######################################
92start_test_case() {
93  TEST_CASE="$1"
94  echo "[ RUN      ] ${TEST_NAME}.${TEST_CASE}"
95}
96
97#######################################
98# Ends a test case printing a success message.
99#
100# Globals:
101#   TEST_NAME
102#   TEST_CASE
103#######################################
104end_test_case() {
105  echo "[       OK ] ${TEST_NAME}.${TEST_CASE}"
106}
107
108#############################################################################
109
110start_test_case "encrypt"
111
112# Run encryption.
113test_command "${CLI}" \
114  --mode encrypt \
115  --keyset_filename "${PUBLIC_KEYSET_FILE}" \
116  --input_filename "${DATA_FILE}" \
117  --output_filename "${DATA_FILE}.encrypted"
118assert_command_succeeded
119
120end_test_case
121
122#############################################################################
123
124start_test_case "decrypt"
125
126# Run decryption.
127test_command "${CLI}" \
128  --mode decrypt \
129  --keyset_filename "${PRIVATE_KEYSET_FILE}" \
130  --input_filename "${DATA_FILE}.encrypted" \
131  --output_filename "${DATA_FILE}.decrypted"
132assert_command_succeeded
133
134test_command cmp -s "${DATA_FILE}" "${DATA_FILE}.decrypted"
135assert_command_succeeded
136
137end_test_case
138
139#############################################################################
140
141start_test_case "encrypt_decrypt_fails_with_modified_ciphertext"
142
143# Run encryption
144test_command "${CLI}" \
145  --mode encrypt \
146  --keyset_filename "${PUBLIC_KEYSET_FILE}" \
147  --input_filename "${DATA_FILE}" \
148  --output_filename "${DATA_FILE}.encrypted"
149assert_command_succeeded
150
151# Modify ciphertext.
152echo "modified" >> "${DATA_FILE}.encrypted"
153
154# Run decryption.
155test_command "${CLI}" \
156  --mode decrypt \
157  --keyset_filename "${PRIVATE_KEYSET_FILE}" \
158  --input_filename "${DATA_FILE}.encrypted" \
159  --output_filename "${DATA_FILE}.decrypted"
160assert_command_failed
161
162end_test_case
163
164#############################################################################
165
166start_test_case "encrypt_decrypt_succeeds_with_context_info"
167
168# Run encryption.
169CONTEXT_INFO="header information"
170test_command "${CLI}" \
171  --mode encrypt \
172  --keyset_filename "${PUBLIC_KEYSET_FILE}" \
173  --input_filename "${DATA_FILE}" \
174  --output_filename "${DATA_FILE}.encrypted" \
175  --context_info "${CONTEXT_INFO}"
176assert_command_succeeded
177
178# Run decryption.
179test_command "${CLI}" \
180  --mode decrypt \
181  --keyset_filename "${PRIVATE_KEYSET_FILE}" \
182  --input_filename "${DATA_FILE}.encrypted" \
183  --output_filename "${DATA_FILE}.decrypted" \
184  --context_info "${CONTEXT_INFO}"
185assert_command_succeeded
186
187cmp --silent "${DATA_FILE}" "${DATA_FILE}.decrypted"
188assert_command_succeeded
189
190end_test_case
191
192#############################################################################
193
194start_test_case "encrypt_decrypt_fails_with_modified_context_info"
195
196# Run encryption.
197CONTEXT_INFO="header information"
198test_command "${CLI}" \
199  --mode encrypt \
200  --keyset_filename "${PUBLIC_KEYSET_FILE}" \
201  --input_filename "${DATA_FILE}" \
202  --output_filename "${DATA_FILE}.encrypted" \
203  --context_info "${CONTEXT_INFO}"
204assert_command_succeeded
205
206# Run decryption.
207MODIFIED_CONTEXT_INFO="modified header information"
208test_command "${CLI}" \
209  --mode decrypt \
210  --keyset_filename "${PRIVATE_KEYSET_FILE}" \
211  --input_filename "${DATA_FILE}.encrypted" \
212  --output_filename "${DATA_FILE}.decrypted" \
213  --context_info "${MODIFIED_CONTEXT_INFO}"
214assert_command_failed
215
216end_test_case
217