helpers 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/bin/sh -ex
  2. # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2016 Red Hat, Inc.
  5. # Author: Nathaniel McCallum <npmccallum@redhat.com>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. fetch() {
  21. curl -sfg "http://127.0.0.1:${PORT}${1}"
  22. }
  23. ver() {
  24. jose jws ver -i- -k "${1}"
  25. }
  26. random_port() {
  27. if [ -n "${TANG_BSD}" ]; then
  28. jot -r 1 1024 65536
  29. else
  30. shuf -i 1024-65536 -n 1
  31. fi
  32. }
  33. start_server() {
  34. "${SOCAT}" TCP-LISTEN:"${1}",bind=127.0.0.1,fork SYSTEM:"${VALGRIND} tangd ${TMP}/db" &
  35. }
  36. on_exit() {
  37. if [ "$PID" ]; then kill "${PID}"; wait "${PID}" || true; fi
  38. [ -d "${TMP}" ] && rm -rf "${TMP}"
  39. }
  40. validate() {
  41. if ! _jwks="$(jose fmt --json="${1}" -Og payload -SyOg keys \
  42. -AUo- 2>/dev/null)"; then
  43. echo "Advertisement is malformed" >&2
  44. exit 1
  45. fi
  46. _ver="$(printf '%s' "${_jwks}" | jose jwk use -i- -r -u verify -o-)"
  47. if ! printf '%s' "${_ver}" | jose jws ver -i "${1}" -k- -a; then
  48. echo "Advertisement is missing signatures" >&2
  49. exit 1
  50. fi
  51. }
  52. validate_sig() {
  53. jose fmt --json "${1}" --output=- | jose jwk use --input=- --required \
  54. --use verify 2>/dev/null
  55. }
  56. validate_exc() {
  57. jose fmt --json "${1}" --output=- | jose jwk use --input=- --required \
  58. --use deriveKey 2>/dev/null
  59. }
  60. sanity_check() {
  61. # Skip test if socat is not available.
  62. [ -n "${SOCAT}" ] || exit 77
  63. }
  64. die() {
  65. echo "${1}" >&2
  66. exit 1
  67. }
  68. valid_key_perm() {
  69. if [ -n "${TANG_BSD}" ]; then
  70. _perm="$(stat -f %Lp "${1}")"
  71. else
  72. _perm="$(stat -c %a "${1}")"
  73. fi
  74. [ "${_perm}" = "440" ]
  75. }
  76. expected_fail () {
  77. echo "Test was expected to fail" >&2
  78. exit 1
  79. }