File: toggle_options.sh

package info (click to toggle)
tpm2-tools 5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,988 kB
  • sloc: ansic: 45,737; sh: 14,915; xml: 8,342; makefile: 610; python: 51
file content (133 lines) | stat: -rw-r--r-- 4,289 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# SPDX-License-Identifier: BSD-3-Clause

source helpers.sh

# We don't need a TPM for this test, so unset the EXIT handler.
trap - EXIT

# Since this only tests tools options and docs
# and is not portable skip it on FreeBSD
if [ "$OS" == "FreeBSD" ]; then
    exit 0
fi

srcdir="$(readlink -e "$(dirname "$0")")"
toolsdir="$(readlink -e "${srcdir}"/../../../tools)"
mandir="$(readlink -e "${srcdir}"/../../../man)"


# Provide a sanitizing test on whether a toggle
# is effectively taken into account on option parsing.
# Also, checks if the options are documented in the
# respective man file consistently.
# Functionnal check is left for dedicated test cases.
#
# It assumes that the layout for describing toggles and
# option is coherent among the tools
function check_toggle() {
    toggle=${1}

    if [ ${#toggle} -ne 1 ]; then
        echo "toggle should be one character only !"
        exit 254
    fi

    for i in $(grep "'${toggle}'[[:space:]]*}" "${toolsdir}"/*.c | \
    sed "s%[[:space:]]*%%g"); do
        # An example:
        # i:     tools/tpm2_nvdefine.c:{"hierarchy",required_argument,NULL,'a'},
        # filename:    tools/tpm2_nvdefine.c
        # match:       {"hierarchy",required_argument,NULL,'a'},
        # option:      a
        # option_long: hierarchy
        # optionlist:  "x:a:s:b:P:p:L:"
        # getcase:     case'a':

        filename=${i%%:*};
        match=${i##*:};
        option="$(sed -r "s%.*'([^'])'.*%\1%g" <<< "${match}")"
        option_long="$(grep -oP '(?<={").*(?=")' <<< "${match}")"
        optionlist="$(grep -R "tpm2_options_new" "${filename}" | \
        sed -r 's%.*("[^"]+").*%\1%g')"
        getcase="$(grep "case '${option}'" "${filename}" | \
        sed "s%[[:space:]]*%%g")"

        echo "filename: $filename"
        echo "    match:        $match"
        echo "    option:       $option"
        echo "    option_long:  $option_long"
        echo "    optionlist:   $optionlist"
        echo "    getcase:      $getcase"

        if [[ "${filename}" =~ tpm2_options.c$ ]]; then
            continue
        fi

        if ! grep -q "${option}" <<< "${optionlist}"; then
            echo "$filename: option -$option (--$option_long) not found in \
            option list $optionlist"
            exit 1
        fi

        if ! test -n "${getcase}"; then
            echo "$filename: switch case '$option' not found for option \
            -$option (--$option_long)"
            exit 1
        fi

        ####################### check man page #######################
        man_filename="$(basename $filename)"            # tpm2_nvdefine.c
        man_filename="$mandir/${man_filename%.*}.1.md"  # man/tpm2_nvdefine.1.md
        man=$(cat "$man_filename")

        # resolve markdown includes
        man_resolved="$man"
        for md_include in $(grep -Po '(?<=\]\()common/.*(?=\))' <<< "$man"); do
            man_resolved="$man_resolved $(cat $mandir/$md_include)"
        done

        # search markdown for option (short and long)
        man_opt=$(grep -oe "\*\*-$option\*\*, \*\*\\\--$option_long\*\*" \
        <<< "$man_resolved") || true

        if [ -n "$man_opt" ]; then
            echo "    man_opt:      $man_opt"
        else
            echo "$filename: missing option -$option/--$option_long in \
            $man_filename"
            exit 1
        fi
    done
}

fail=0

# For each detected option toggle, check if it is actually declared to be used
# and documented
for i in $(grep -rn "case '.'" "${toolsdir}"/*.c | \
cut -d"'" -f2-2 | sort | uniq); do
    check_toggle "${i}"
done

# For each documented option toggle in the man pages, look if it is present in
# the code
for j in $(grep -oe "\*\*-.\*\*, \*\*\\\--.*\*\*" "${mandir}"/*.1.md | \
sed -r 's/\s//g' ); do
    filename=${j%%:*};
    option="$(grep -oP '(?<=\*\*-).(?=\*\*)' <<< "$j")"
    option_long="$(grep -oP '(?<=\*\*\\\--).*(?=\*\*)' <<< "$j")"

    c_filename=$(basename ${filename%.1.md}).c

    echo "$filename: looking for -$option (--$option_long) in $c_filename"
    found=$(grep -r "case '${option}'" "$toolsdir" --include="$c_filename") \
    || true

    if [ -z "$found" ]; then
        echo "$filename: missing option -$option (--$option_long) in \
        $c_filename"
        exit 1
    fi
done

exit $fail