toggle_options.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. # We don't need a TPM for this test, so unset the EXIT handler.
  4. trap - EXIT
  5. # Since this only tests tools options and docs
  6. # and is not portable skip it on FreeBSD
  7. if [ "$OS" == "FreeBSD" ]; then
  8. exit 0
  9. fi
  10. srcdir="$(readlink -e "$(dirname "$0")")"
  11. toolsdir="$(readlink -e "${srcdir}"/../../../tools)"
  12. mandir="$(readlink -e "${srcdir}"/../../../man)"
  13. # Provide a sanitizing test on whether a toggle
  14. # is effectively taken into account on option parsing.
  15. # Also, checks if the options are documented in the
  16. # respective man file consistently.
  17. # Functionnal check is left for dedicated test cases.
  18. #
  19. # It assumes that the layout for describing toggles and
  20. # option is coherent among the tools
  21. function check_toggle() {
  22. toggle=${1}
  23. if [ ${#toggle} -ne 1 ]; then
  24. echo "toggle should be one character only !"
  25. exit 254
  26. fi
  27. for i in $(grep "'${toggle}'[[:space:]]*}" "${toolsdir}"/*.c | \
  28. sed "s%[[:space:]]*%%g"); do
  29. # An example:
  30. # i: tools/tpm2_nvdefine.c:{"hierarchy",required_argument,NULL,'a'},
  31. # filename: tools/tpm2_nvdefine.c
  32. # match: {"hierarchy",required_argument,NULL,'a'},
  33. # option: a
  34. # option_long: hierarchy
  35. # optionlist: "x:a:s:b:P:p:L:"
  36. # getcase: case'a':
  37. filename=${i%%:*};
  38. match=${i##*:};
  39. option="$(sed -r "s%.*'([^'])'.*%\1%g" <<< "${match}")"
  40. option_long="$(grep -oP '(?<={").*(?=")' <<< "${match}")"
  41. optionlist="$(grep -R "tpm2_options_new" "${filename}" | \
  42. sed -r 's%.*("[^"]+").*%\1%g')"
  43. getcase="$(grep "case '${option}'" "${filename}" | \
  44. sed "s%[[:space:]]*%%g")"
  45. echo "filename: $filename"
  46. echo " match: $match"
  47. echo " option: $option"
  48. echo " option_long: $option_long"
  49. echo " optionlist: $optionlist"
  50. echo " getcase: $getcase"
  51. if [[ "${filename}" =~ tpm2_options.c$ ]]; then
  52. continue
  53. fi
  54. if ! grep -q "${option}" <<< "${optionlist}"; then
  55. echo "$filename: option -$option (--$option_long) not found in \
  56. option list $optionlist"
  57. exit 1
  58. fi
  59. if ! test -n "${getcase}"; then
  60. echo "$filename: switch case '$option' not found for option \
  61. -$option (--$option_long)"
  62. exit 1
  63. fi
  64. ####################### check man page #######################
  65. man_filename="$(basename $filename)" # tpm2_nvdefine.c
  66. man_filename="$mandir/${man_filename%.*}.1.md" # man/tpm2_nvdefine.1.md
  67. man=$(cat "$man_filename")
  68. # resolve markdown includes
  69. man_resolved="$man"
  70. for md_include in $(grep -Po '(?<=\]\()common/.*(?=\))' <<< "$man"); do
  71. man_resolved="$man_resolved $(cat $mandir/$md_include)"
  72. done
  73. # search markdown for option (short and long)
  74. man_opt=$(grep -oe "\*\*-$option\*\*, \*\*\\\--$option_long\*\*" \
  75. <<< "$man_resolved") || true
  76. if [ -n "$man_opt" ]; then
  77. echo " man_opt: $man_opt"
  78. else
  79. echo "$filename: missing option -$option/--$option_long in \
  80. $man_filename"
  81. exit 1
  82. fi
  83. done
  84. }
  85. fail=0
  86. # For each detected option toggle, check if it is actually declared to be used
  87. # and documented
  88. for i in $(grep -rn "case '.'" "${toolsdir}"/*.c | \
  89. cut -d"'" -f2-2 | sort | uniq); do
  90. check_toggle "${i}"
  91. done
  92. # For each documented option toggle in the man pages, look if it is present in
  93. # the code
  94. for j in $(grep -oe "\*\*-.\*\*, \*\*\\\--.*\*\*" "${mandir}"/*.1.md | \
  95. sed -r 's/\s//g' ); do
  96. filename=${j%%:*};
  97. option="$(grep -oP '(?<=\*\*-).(?=\*\*)' <<< "$j")"
  98. option_long="$(grep -oP '(?<=\*\*\\\--).*(?=\*\*)' <<< "$j")"
  99. c_filename=$(basename ${filename%.1.md}).c
  100. echo "$filename: looking for -$option (--$option_long) in $c_filename"
  101. found=$(grep -r "case '${option}'" "$toolsdir" --include="$c_filename") \
  102. || true
  103. if [ -z "$found" ]; then
  104. echo "$filename: missing option -$option (--$option_long) in \
  105. $c_filename"
  106. exit 1
  107. fi
  108. done
  109. exit $fail