helpers 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. check_if_port_listening() {
  34. if [ -n "${TANG_BSD}" ]; then
  35. sockstat -l|grep "[\:\.]${1}" >/dev/null 2>&1
  36. else
  37. ss -anl|grep "[\:\.]${1}"|grep LISTEN >/dev/null 2>&1
  38. fi
  39. }
  40. wait_for_port()
  41. {
  42. local port="${1}"
  43. sleep 1
  44. local i=0
  45. while [ "${i}" -lt 90 ]; do
  46. check_if_port_listening "${port}" && return 0
  47. i=$((i + 1))
  48. echo "try ${i}: waiting for port" >&2
  49. sleep 1
  50. done
  51. return 1
  52. }
  53. start_server() {
  54. "${SOCAT}" TCP-LISTEN:"${1}",bind=127.0.0.1,fork SYSTEM:"${VALGRIND} tangd ${TMP}/db" &
  55. }
  56. start_standalone_server() {
  57. ${VALGRIND} tangd -p ${1} -l ${TMP}/db &
  58. }
  59. on_exit() {
  60. if [ "${PID}" ]; then
  61. kill "${PID}" || true
  62. wait "${PID}" || true
  63. fi
  64. [ -d "${TMP}" ] && rm -rf "${TMP}"
  65. }
  66. validate() {
  67. if ! _jwks="$(jose fmt --json="${1}" -Og payload -SyOg keys \
  68. -AUo- 2>/dev/null)"; then
  69. echo "Advertisement is malformed" >&2
  70. exit 1
  71. fi
  72. _ver="$(printf '%s' "${_jwks}" | jose jwk use -i- -r -u verify -o-)"
  73. if ! printf '%s' "${_ver}" | jose jws ver -i "${1}" -k- -a; then
  74. echo "Advertisement is missing signatures" >&2
  75. exit 1
  76. fi
  77. }
  78. validate_sig() {
  79. jose fmt --json "${1}" --output=- | jose jwk use --input=- --required \
  80. --use verify 2>/dev/null
  81. }
  82. validate_exc() {
  83. jose fmt --json "${1}" --output=- | jose jwk use --input=- --required \
  84. --use deriveKey 2>/dev/null
  85. }
  86. sanity_check() {
  87. # Skip test if socat is not available.
  88. [ -n "${SOCAT}" ] || exit 77
  89. }
  90. die() {
  91. echo "${1}" >&2
  92. exit 1
  93. }
  94. valid_key_perm() {
  95. if [ -n "${TANG_BSD}" ]; then
  96. _perm="$(stat -f %Lp "${1}")"
  97. else
  98. _perm="$(stat -c %a "${1}")"
  99. fi
  100. [ "${_perm}" = "440" ]
  101. }
  102. expected_fail () {
  103. echo "Test was expected to fail" >&2
  104. exit 1
  105. }