helpers 3.2 KB

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