xref: /aosp_15_r20/external/vboot_reference/tests/common.sh (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1#!/bin/bash
2
3# Copyright 2010 The ChromiumOS Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# Determine script directory.
8SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
9ROOT_DIR="$(dirname "${SCRIPT_DIR}")"
10SRCDIR="${ROOT_DIR}"
11# BUILD_RUN should be supplied from the Makefile.
12# Some test scripts change the cwd so use an absolute path.
13BUILD_RUN="$(realpath "${BUILD_RUN}")"
14BIN_DIR="${BUILD_RUN}/install_for_test/usr/bin"
15FUTILITY="${BIN_DIR}/futility"
16TEST_DIR="${BUILD_RUN}/tests"
17TESTKEY_DIR="${SCRIPT_DIR}/testkeys"
18TESTCASE_DIR="${SCRIPT_DIR}/testcases"
19TESTKEY_SCRATCH_DIR="${TEST_DIR}/testkeys"
20
21if [ ! -d "${TESTKEY_SCRATCH_DIR}" ]; then
22  mkdir -p "${TESTKEY_SCRATCH_DIR}"
23fi
24
25# Color output encodings.
26COL_RED='\E[31;1m'
27COL_GREEN='\E[32;1m'
28COL_YELLOW='\E[33;1m'
29COL_BLUE='\E[34;1m'
30COL_STOP='\E[0;m'
31
32hash_algos=( sha1 sha256 sha512 )
33key_lengths=( 1024 2048 4096 8192 2048_exp3 3072_exp3 )
34
35function happy {
36  echo -e "${COL_GREEN}$*${COL_STOP}" 1>&2
37}
38
39# args: [nested level [message]]
40function warning {
41  echo -e "${COL_YELLOW}WARNING: $*${COL_STOP}" 1>&2
42}
43
44# args: [nested level [message]]
45function error {
46  local lev=${1:-}
47  case "${1:-}" in
48    [0-9]*)
49      lev=$1
50      shift
51      ;;
52    *) lev=0
53      ;;
54  esac
55  local x
56  x=$(caller $lev)
57  local cline=${x%% *}
58  local cfunc=${x#* }
59  cfunc=${cfunc##*/}
60  local args="$*"
61  local spacer=${args:+: }
62  echo -e "${COL_RED}ERROR at ${cfunc}, line ${cline}${spacer}${args}" \
63    "${COL_STOP}" 1>&2
64  exit 1
65}
66
67function check_test_keys {
68  [ -d "${TESTKEY_DIR}" ] || \
69    error 1 "You must run gen_test_keys.sh to generate test keys first."
70}
71