xref: /aosp_15_r20/external/tink/cc/examples/key_derivation/key_derivation_cli_test.sh (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1#!/bin/bash
2# Copyright 2023 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++ Key Derivation example.
21#############################################################################
22
23: "${TEST_TMPDIR:=$(mktemp -d)}"
24
25readonly CLI="$1"
26readonly KEYSET_FILE="$2"
27readonly SALT_FILE="${TEST_TMPDIR}/salt.txt"
28readonly DERIVED_KEYSET_FILE="${TEST_TMPDIR}/derived_keyset.json"
29readonly TEST_NAME="TinkCcExamplesKeyDerivationTest"
30
31echo "This is the salt used to derive keys." > "${SALT_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 "derive_key"
111
112test_command "${CLI}" \
113  --keyset_filename "${KEYSET_FILE}" \
114  --salt_filename "${SALT_FILE}" \
115  --derived_keyset_filename "${DERIVED_KEYSET_FILE}"
116assert_command_succeeded
117
118end_test_case
119