xref: /aosp_15_r20/external/tink/java_src/examples/hybrid/hybrid_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 hybrid encryption example.
21
22HYBRID_CLI="$1"
23PUBLIC_KEYSET_FILE="$2"
24PRIVATE_KEYSET_FILE="$3"
25
26INPUT_FILE="${TEST_TMPDIR}/example_data.txt"
27
28echo "This is some message to be encrypted." > ${INPUT_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#### Test correct encryption and decryption.
46test_name="test_encrypt_decrypt_succeeds"
47echo "+++ Starting test ${test_name}..."
48
49##### Run encryption
50test_command ${HYBRID_CLI} encrypt ${PUBLIC_KEYSET_FILE} ${INPUT_FILE} ${INPUT_FILE}.ciphertext
51if [[ ${TEST_STATUS} -eq 0 ]]; then
52  echo "+++ Encryption successful."
53else
54  echo "--- Encryption failed."
55  exit 1
56fi
57
58##### Run decryption
59test_command ${HYBRID_CLI} decrypt ${PRIVATE_KEYSET_FILE} ${INPUT_FILE}.ciphertext ${INPUT_FILE}.plaintext
60if [[ ${TEST_STATUS} -eq 0 ]]; then
61  echo "+++ Decryption successful."
62else
63  echo "--- Decryption failed."
64  exit 1
65fi
66
67cmp --silent ${INPUT_FILE} ${INPUT_FILE}.plaintext
68
69
70#############################################################################
71#### Test correct encryption and decryption with context
72test_name="test_encrypt_decrypt_succeeds_with_context"
73echo "+++ Starting test ${test_name}..."
74
75##### Run encryption
76CONTEXT_INFORMATION="context information"
77test_command ${HYBRID_CLI} encrypt ${PUBLIC_KEYSET_FILE} ${INPUT_FILE} ${INPUT_FILE}.ciphertext "${CONTEXT_INFORMATION}"
78if [[ ${TEST_STATUS} -eq 0 ]]; then
79  echo "+++ Encryption successful."
80else
81  echo "--- Encryption failed."
82  exit 1
83fi
84
85##### Run decryption
86test_command ${HYBRID_CLI} decrypt ${PRIVATE_KEYSET_FILE} ${INPUT_FILE}.ciphertext ${INPUT_FILE}.plaintext "${CONTEXT_INFORMATION}"
87if [[ ${TEST_STATUS} -eq 0 ]]; then
88  echo "+++ Decryption successful."
89else
90  echo "--- Decryption failed."
91  exit 1
92fi
93
94cmp --silent ${INPUT_FILE} ${INPUT_FILE}.plaintext
95
96#############################################################################
97#### Test decryption fails with missing context
98test_name="test_encrypt_decrypt_fails_with_context"
99echo "+++ Starting test ${test_name}..."
100
101##### Run encryption
102CONTEXT_INFORMATION="context information"
103test_command ${HYBRID_CLI} encrypt ${PUBLIC_KEYSET_FILE} ${INPUT_FILE} ${INPUT_FILE}.ciphertext "${CONTEXT_INFORMATION}"
104if [[ ${TEST_STATUS} -eq 0 ]]; then
105  echo "+++ Encryption successful."
106else
107  echo "--- Encryption failed."
108  exit 1
109fi
110
111##### Run decryption
112test_command ${HYBRID_CLI} decrypt ${PRIVATE_KEYSET_FILE} ${INPUT_FILE}.ciphertext ${INPUT_FILE}.plaintext
113if [[ ${TEST_STATUS} -eq 1 ]]; then
114  echo "+++ Decryption failed as expected."
115else
116  echo "--- Decryption succeeded but expected to fail."
117  exit 1
118fi
119
120#############################################################################
121#### Test enryption fails with wrong keyset
122test_name="test_encrypt_fails_with_wrong_keyset"
123echo "+++ Starting test ${test_name}..."
124
125##### Run encryption
126test_command ${HYBRID_CLI} encrypt ${PRIVATE_KEYSET_FILE} ${INPUT_FILE} ${INPUT_FILE}.ciphertext
127if [[ ${TEST_STATUS} -eq 1 ]]; then
128  echo "+++ Encryption failed as expected."
129else
130  echo "--- Encryption succeeded but expected to fail."
131  exit 1
132fi
133