xref: /aosp_15_r20/external/tink/java_src/examples/cleartextkeyset/cleartext_keyset_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 cleartext keyset example.
21
22CLI="$1"
23
24DATA_FILE="${TEST_TMPDIR}/example_data.txt"
25KEYSET_FILE="${TEST_TMPDIR}/example_keyset.json"
26
27echo "This is some plaintext to be encrypted." > ${DATA_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 initialization and key generation
45test_name="generate"
46echo "+++ Starting test ${test_name}..."
47
48##### Run encryption
49test_command ${CLI} generate ${KEYSET_FILE}
50
51if [[ ${TEST_STATUS} -eq 0 ]]; then
52  echo "+++ Success: key file was generated."
53else
54  echo "--- Failure: could not generate key file."
55  exit 1
56fi
57
58#############################################################################
59#### Test initialization and encryption
60test_name="encrypt"
61echo "+++ Starting test ${test_name}..."
62
63##### Run encryption
64test_command ${CLI} encrypt ${KEYSET_FILE} ${DATA_FILE} "${DATA_FILE}.encrypted"
65
66if [[ ${TEST_STATUS} -eq 0 ]]; then
67  echo "+++ Success: file was encrypted."
68else
69  echo "--- Failure: could not encrypt file."
70  exit 1
71fi
72
73#############################################################################
74#### Test if decryption succeeds and returns original file
75test_name="decrypt"
76echo "+++ Starting test $test_name..."
77
78##### Run decryption
79test_command ${CLI} decrypt ${KEYSET_FILE} ${DATA_FILE}.encrypted "${DATA_FILE}.decrypted"
80
81if [[ ${TEST_STATUS} -eq 0 ]]; then
82  echo "+++ Success: file was successfully decrypted."
83else
84  echo "--- Failure: could not decrypt file."
85  exit 1
86fi
87
88if cmp -s ${DATA_FILE} "${DATA_FILE}.decrypted"; then
89  echo "+++ Success: file content is the same after decryption."
90else
91  echo "--- Failure: file content is not the same after decryption."
92  exit 1
93fi
94