Reduce the use of grep

Avoid using the external command grep for simple string-based checks.
Prefer a case statement. This improves performance.

The performance improvement is moderate but noticeable when skipping
most tests. When a test is run, the cost of the associated grep calls
is negligible. In this commit, I focused on the uses of grep that can
be easily replaced and that are executed a large number of times.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2020-08-26 21:53:33 +02:00
parent 5b428d7d2a
commit 6445749d3c

View File

@ -178,6 +178,14 @@ case "$MBEDTLS_TEST_OUTCOME_FILE" in
;; ;;
esac esac
# Read boolean configuration options from config.h for easy and quick
# testing. Skip non-boolean options (with something other than spaces
# and a comment after "#define SYMBOL"). The variable contains a
# space-separated list of symbols.
CONFIGS_ENABLED=" $(<"$CONFIG_H" \
sed -n 's!^ *#define *\([A-Za-z][0-9A-Z_a-z]*\) *\(/*\)*!\1!p' |
tr '\n' ' ')"
# Skip next test; use this macro to skip tests which are legitimate # Skip next test; use this macro to skip tests which are legitimate
# in theory and expected to be re-introduced at some point, but # in theory and expected to be re-introduced at some point, but
# aren't expected to succeed at the moment due to problems outside # aren't expected to succeed at the moment due to problems outside
@ -188,16 +196,17 @@ skip_next_test() {
# skip next test if the flag is not enabled in config.h # skip next test if the flag is not enabled in config.h
requires_config_enabled() { requires_config_enabled() {
if grep "^#define $1" $CONFIG_H > /dev/null; then :; else case $CONFIGS_ENABLED in
SKIP_NEXT="YES" *" $1 "*) :;;
fi *) SKIP_NEXT="YES";;
esac
} }
# skip next test if the flag is enabled in config.h # skip next test if the flag is enabled in config.h
requires_config_disabled() { requires_config_disabled() {
if grep "^#define $1" $CONFIG_H > /dev/null; then case $CONFIGS_ENABLED in
SKIP_NEXT="YES" *" $1 "*) SKIP_NEXT="YES";;
fi esac
} }
get_config_value_or_default() { get_config_value_or_default() {
@ -233,10 +242,16 @@ requires_config_value_at_most() {
fi fi
} }
# Space-separated list of ciphersuites supported by this build of
# Mbed TLS.
P_CIPHERSUITES=" $($P_CLI --help 2>/dev/null |
grep TLS- |
tr -s ' \n' ' ')"
requires_ciphersuite_enabled() { requires_ciphersuite_enabled() {
if [ -z "$($P_CLI --help 2>/dev/null | grep $1)" ]; then case $P_CIPHERSUITES in
SKIP_NEXT="YES" *" $1 "*) :;;
fi *) SKIP_NEXT="YES";;
esac
} }
# maybe_requires_ciphersuite_enabled CMD [RUN_TEST_OPTION...] # maybe_requires_ciphersuite_enabled CMD [RUN_TEST_OPTION...]
@ -462,17 +477,21 @@ fail() {
# is_polar <cmd_line> # is_polar <cmd_line>
is_polar() { is_polar() {
echo "$1" | grep 'ssl_server2\|ssl_client2' > /dev/null case "$1" in
*ssl_client2*) true;;
*ssl_server2*) true;;
*) false;;
esac
} }
# openssl s_server doesn't have -www with DTLS # openssl s_server doesn't have -www with DTLS
check_osrv_dtls() { check_osrv_dtls() {
if echo "$SRV_CMD" | grep 's_server.*-dtls' >/dev/null; then case "$SRV_CMD" in
*s_server*-dtls*)
NEEDS_INPUT=1 NEEDS_INPUT=1
SRV_CMD="$( echo $SRV_CMD | sed s/-www// )" SRV_CMD="$( echo $SRV_CMD | sed s/-www// )";;
else *) NEEDS_INPUT=0;;
NEEDS_INPUT=0 esac
fi
} }
# provide input to commands that need it # provide input to commands that need it
@ -627,11 +646,10 @@ wait_client_done() {
# check if the given command uses dtls and sets global variable DTLS # check if the given command uses dtls and sets global variable DTLS
detect_dtls() { detect_dtls() {
if echo "$1" | grep 'dtls=1\|-dtls1\|-u' >/dev/null; then case "$1" in
DTLS=1 *dtls=1*|-dtls|-u) DTLS=1;;
else *) DTLS=0;;
DTLS=0 esac
fi
} }
# Usage: run_test name [-p proxy_cmd] srv_cmd cli_cmd cli_exit [option [...]] # Usage: run_test name [-p proxy_cmd] srv_cmd cli_cmd cli_exit [option [...]]
@ -657,10 +675,11 @@ run_test() {
print_name "$NAME" print_name "$NAME"
# Do we only run numbered tests? # Do we only run numbered tests?
if [ "X$RUN_TEST_NUMBER" = "X" ]; then : if [ -n "$RUN_TEST_NUMBER" ]; then
elif echo ",$RUN_TEST_NUMBER," | grep ",$TESTS," >/dev/null; then : case ",$RUN_TEST_NUMBER," in
else *",$TESTS,"*) :;;
SKIP_NEXT="YES" *) SKIP_NEXT="YES";;
esac
fi fi
# does this test use a proxy? # does this test use a proxy?
@ -678,10 +697,10 @@ run_test() {
shift 3 shift 3
# Check if test uses files # Check if test uses files
TEST_USES_FILES=$(echo "$SRV_CMD $CLI_CMD" | grep "\.\(key\|crt\|pem\)" ) case "$SRV_CMD $CLI_CMD" in
if [ ! -z "$TEST_USES_FILES" ]; then *data_files/*)
requires_config_enabled MBEDTLS_FS_IO requires_config_enabled MBEDTLS_FS_IO;;
fi esac
# If the client or serve requires a ciphersuite, check that it's enabled. # If the client or serve requires a ciphersuite, check that it's enabled.
maybe_requires_ciphersuite_enabled "$SRV_CMD" "$@" maybe_requires_ciphersuite_enabled "$SRV_CMD" "$@"