helpers 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. sanity_check() {
  53. # Skip test if socat is not available.
  54. [ -n "${SOCAT}" ] || exit 77
  55. }