Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 1 | #!/bin/sh |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 2 | # |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame^] | 3 | # Copyright The Mbed TLS Contributors |
Bence Szépkúti | c7da1fe | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
Gilles Peskine | 0c12e30 | 2019-05-22 18:22:45 +0200 | [diff] [blame] | 17 | |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 18 | set -eu |
| 19 | |
Gilles Peskine | 0c12e30 | 2019-05-22 18:22:45 +0200 | [diff] [blame] | 20 | if [ $# -ne 0 ] && [ "$1" = "--help" ]; then |
| 21 | cat <<EOF |
| 22 | $0 [-v] |
| 23 | This script confirms that the naming of all symbols and identifiers in mbed |
| 24 | TLS are consistent with the house style and are also self-consistent. |
| 25 | |
| 26 | -v If the script fails unexpectedly, print a command trace. |
| 27 | EOF |
| 28 | exit |
| 29 | fi |
| 30 | |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 31 | if grep --version|head -n1|grep GNU >/dev/null; then :; else |
Ron Eldor | bf007d2 | 2016-12-15 14:42:37 +0200 | [diff] [blame] | 32 | echo "This script requires GNU grep.">&2 |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 33 | exit 1 |
| 34 | fi |
| 35 | |
Gilles Peskine | 36428d3 | 2019-05-15 17:29:15 +0200 | [diff] [blame] | 36 | trace= |
| 37 | if [ $# -ne 0 ] && [ "$1" = "-v" ]; then |
| 38 | shift |
| 39 | trace='-x' |
| 40 | exec 2>check-names.err |
| 41 | trap 'echo "FAILED UNEXPECTEDLY, status=$?"; |
| 42 | cat check-names.err' EXIT |
| 43 | set -x |
| 44 | fi |
| 45 | |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 46 | printf "Analysing source code...\n" |
| 47 | |
Gilles Peskine | 36428d3 | 2019-05-15 17:29:15 +0200 | [diff] [blame] | 48 | sh $trace tests/scripts/list-macros.sh |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 49 | tests/scripts/list-enum-consts.pl |
Gilles Peskine | 36428d3 | 2019-05-15 17:29:15 +0200 | [diff] [blame] | 50 | sh $trace tests/scripts/list-identifiers.sh |
| 51 | sh $trace tests/scripts/list-symbols.sh |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 52 | |
| 53 | FAIL=0 |
| 54 | |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 55 | printf "\nExported symbols declared in header: " |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 56 | UNDECLARED=$( diff exported-symbols identifiers | sed -n -e 's/^< //p' ) |
Manuel Pégourié-Gonnard | 9afdc83 | 2015-08-04 17:15:13 +0200 | [diff] [blame] | 57 | if [ "x$UNDECLARED" = "x" ]; then |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 58 | echo "PASS" |
| 59 | else |
| 60 | echo "FAIL" |
| 61 | echo "$UNDECLARED" |
| 62 | FAIL=1 |
| 63 | fi |
| 64 | |
| 65 | diff macros identifiers | sed -n -e 's/< //p' > actual-macros |
| 66 | |
| 67 | for THING in actual-macros enum-consts; do |
| 68 | printf "Names of $THING: " |
| 69 | test -r $THING |
Nir Sonnenschein | 03091d1 | 2019-01-08 18:15:50 +0200 | [diff] [blame] | 70 | BAD=$( grep -E -v '^(MBEDTLS|PSA)_[0-9A-Z_]*[0-9A-Z]$' $THING || true ) |
Unknown | 712f7a8 | 2019-08-21 03:34:00 -0400 | [diff] [blame] | 71 | UNDERSCORES=$( grep -E '.*__.*' $THING || true ) |
| 72 | |
| 73 | if [ "x$BAD" = "x" ] && [ "x$UNDERSCORES" = "x" ]; then |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 74 | echo "PASS" |
| 75 | else |
| 76 | echo "FAIL" |
| 77 | echo "$BAD" |
Unknown | 712f7a8 | 2019-08-21 03:34:00 -0400 | [diff] [blame] | 78 | echo "$UNDERSCORES" |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 79 | FAIL=1 |
| 80 | fi |
| 81 | done |
| 82 | |
| 83 | for THING in identifiers; do |
| 84 | printf "Names of $THING: " |
| 85 | test -r $THING |
Gilles Peskine | 2d9d6db | 2018-06-18 23:25:28 +0200 | [diff] [blame] | 86 | BAD=$( grep -E -v '^(mbedtls|psa)_[0-9a-z_]*[0-9a-z]$' $THING || true ) |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 87 | if [ "x$BAD" = "x" ]; then |
| 88 | echo "PASS" |
| 89 | else |
| 90 | echo "FAIL" |
| 91 | echo "$BAD" |
| 92 | FAIL=1 |
| 93 | fi |
| 94 | done |
| 95 | |
Manuel Pégourié-Gonnard | f9aae83 | 2015-04-09 12:20:53 +0200 | [diff] [blame] | 96 | printf "Likely typos: " |
| 97 | sort -u actual-macros enum-consts > _caps |
Nir Sonnenschein | 03091d1 | 2019-01-08 18:15:50 +0200 | [diff] [blame] | 98 | HEADERS=$( ls include/mbedtls/*.h include/psa/*.h | egrep -v 'compat-1\.3\.h' ) |
Christoph M. Wintersteiger | 8a0f5bb | 2018-12-14 15:46:34 +0000 | [diff] [blame] | 99 | HEADERS="$HEADERS 3rdparty/everest/include/everest/everest.h 3rdparty/everest/include/everest/x25519.h" |
Gilles Peskine | 9ab9621 | 2020-02-12 21:06:45 +0100 | [diff] [blame] | 100 | LIBRARY="$( ls library/*.c )" |
| 101 | LIBRARY="$LIBRARY 3rdparty/everest/library/everest.c 3rdparty/everest/library/x25519.c" |
Manuel Pégourié-Gonnard | f9aae83 | 2015-04-09 12:20:53 +0200 | [diff] [blame] | 102 | NL=' |
| 103 | ' |
Manuel Pégourié-Gonnard | 6ad5d35 | 2015-05-28 15:08:28 +0200 | [diff] [blame] | 104 | sed -n 's/MBED..._[A-Z0-9_]*/\'"$NL"'&\'"$NL"/gp \ |
Christoph M. Wintersteiger | 8a0f5bb | 2018-12-14 15:46:34 +0000 | [diff] [blame] | 105 | $HEADERS $LIBRARY \ |
Manuel Pégourié-Gonnard | f9aae83 | 2015-04-09 12:20:53 +0200 | [diff] [blame] | 106 | | grep MBEDTLS | sort -u > _MBEDTLS_XXX |
| 107 | TYPOS=$( diff _caps _MBEDTLS_XXX | sed -n 's/^> //p' \ |
Manuel Pégourié-Gonnard | 32da9f6 | 2015-07-31 15:52:30 +0200 | [diff] [blame] | 108 | | egrep -v 'XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$' || true ) |
Manuel Pégourié-Gonnard | f9aae83 | 2015-04-09 12:20:53 +0200 | [diff] [blame] | 109 | rm _MBEDTLS_XXX _caps |
Manuel Pégourié-Gonnard | 9afdc83 | 2015-08-04 17:15:13 +0200 | [diff] [blame] | 110 | if [ "x$TYPOS" = "x" ]; then |
Manuel Pégourié-Gonnard | f9aae83 | 2015-04-09 12:20:53 +0200 | [diff] [blame] | 111 | echo "PASS" |
| 112 | else |
| 113 | echo "FAIL" |
| 114 | echo "$TYPOS" |
| 115 | FAIL=1 |
| 116 | fi |
| 117 | |
Gilles Peskine | 36428d3 | 2019-05-15 17:29:15 +0200 | [diff] [blame] | 118 | if [ -n "$trace" ]; then |
| 119 | set +x |
| 120 | trap - EXIT |
| 121 | rm check-names.err |
| 122 | fi |
| 123 | |
Simon Butcher | 71ebc58 | 2016-06-23 20:02:07 +0100 | [diff] [blame] | 124 | printf "\nOverall: " |
Manuel Pégourié-Gonnard | e137ea6 | 2015-04-09 10:47:44 +0200 | [diff] [blame] | 125 | if [ "$FAIL" -eq 0 ]; then |
| 126 | rm macros actual-macros enum-consts identifiers exported-symbols |
| 127 | echo "PASSED" |
| 128 | exit 0 |
| 129 | else |
| 130 | echo "FAILED" |
| 131 | exit 1 |
| 132 | fi |