1#!/bin/sh
2
3help () {
4    cat <<EOF
5Usage: $0 [OPTION] [PLATFORM]...
6Run all the metatests whose platform matches any of the given PLATFORM.
7A PLATFORM can contain shell wildcards.
8
9Expected output: a lot of scary-looking error messages, since each
10metatest is expected to report a failure. The final line should be
11"Ran N metatests, all good."
12
13If something goes wrong: the final line should be
14"Ran N metatests, X unexpected successes". Look for "Unexpected success"
15in the logs above.
16
17  -l  List the available metatests, don't run them.
18EOF
19}
20
21# Copyright The Mbed TLS Contributors
22# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
23
24set -e -u
25
26if [ -d programs ]; then
27    METATEST_PROGRAM=programs/test/metatest
28elif [ -d ../programs ]; then
29    METATEST_PROGRAM=../programs/test/metatest
30elif [ -d ../../programs ]; then
31    METATEST_PROGRAM=../../programs/test/metatest
32else
33    echo >&2 "$0: FATAL: programs/test/metatest not found"
34    exit 120
35fi
36
37LIST_ONLY=
38while getopts hl OPTLET; do
39    case $OPTLET in
40        h) help; exit;;
41        l) LIST_ONLY=1;;
42        \?) help >&2; exit 120;;
43    esac
44done
45shift $((OPTIND - 1))
46
47list_matches () {
48    while read name platform junk; do
49        for pattern in "$@"; do
50            case $platform in
51                $pattern) echo "$name"; break;;
52            esac
53        done
54    done
55}
56
57count=0
58errors=0
59run_metatest () {
60    ret=0
61    "$METATEST_PROGRAM" "$1" || ret=$?
62    if [ $ret -eq 0 ]; then
63        echo >&2 "$0: Unexpected success: $1"
64        errors=$((errors + 1))
65    fi
66    count=$((count + 1))
67}
68
69# Don't pipe the output of metatest so that if it fails, this script exits
70# immediately with a failure status.
71full_list=$("$METATEST_PROGRAM" list)
72matching_list=$(printf '%s\n' "$full_list" | list_matches "$@")
73
74if [ -n "$LIST_ONLY" ]; then
75    printf '%s\n' $matching_list
76    exit
77fi
78
79for name in $matching_list; do
80    run_metatest "$name"
81done
82
83if [ $errors -eq 0 ]; then
84    echo "Ran $count metatests, all good."
85    exit 0
86else
87    echo "Ran $count metatests, $errors unexpected successes."
88    exit 1
89fi
90