tests-common-functions.in 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/bin/bash -ex
  2. # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2019 Red Hat, Inc.
  5. # Author: Sergio Correia <scorreia@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. error() {
  21. echo "${1}" >&2
  22. exit 1
  23. }
  24. skip_test() {
  25. echo "${1}" >&2
  26. exit 77
  27. }
  28. # We require cryptsetup >= 2.0.4 to fully support LUKSv2.
  29. # Support is determined at build time.
  30. luks2_supported() {
  31. return @OLD_CRYPTSETUP@
  32. }
  33. # Creates a tang adv to be used in the test.
  34. create_tang_adv() {
  35. local adv="${1}"
  36. local SIG="${TMP}/sig.jwk"
  37. jose jwk gen -i '{"alg":"ES512"}' > "${SIG}"
  38. local EXC="${TMP}/exc.jwk"
  39. jose jwk gen -i '{"alg":"ECMR"}' > "${EXC}"
  40. local TEMPLATE='{"protected":{"cty":"jwk-set+json"}}'
  41. jose jwk pub -s -i "${SIG}" -i "${EXC}" \
  42. | jose jws sig -I- -s "${TEMPLATE}" -k "${SIG}" -o "${adv}"
  43. }
  44. # Creates a new LUKS1 or LUKS2 device to be used.
  45. new_device() {
  46. local LUKS="${1}"
  47. local DEV="${2}"
  48. local PASS="${3}"
  49. # Some builders fail if the cryptsetup steps are not ran as root, so let's
  50. # skip the test now if not running as root.
  51. if [ $(id -u) != 0 ]; then
  52. skip_test "WARNING: You must be root to run this test; test skipped."
  53. fi
  54. # Using a default password, if none has been provided.
  55. if [ -z "${PASS}" ]; then
  56. PASS="${DEFAULT_PASS}"
  57. fi
  58. local DEV_CACHED="${TMP}/${LUKS}.cached"
  59. # Let's reuse an existing device, if there is one.
  60. if [ -f "${DEV_CACHED}" ]; then
  61. echo "Reusing cached ${LUKS} device..."
  62. cp -f "${DEV_CACHED}" "${DEV}"
  63. return 0
  64. fi
  65. fallocate -l16M "${DEV}"
  66. local extra_options='--pbkdf pbkdf2 --pbkdf-force-iterations 1000'
  67. cryptsetup luksFormat --type "${LUKS}" ${extra_options} --batch-mode \
  68. --force-password "${DEV}" <<< "${PASS}"
  69. # Caching the just-formatted device for possible reuse.
  70. cp -f "${DEV}" "${DEV_CACHED}"
  71. }
  72. # Creates a new LUKS1 or LUKS2 device to be used, using a keyfile.
  73. new_device_keyfile() {
  74. local LUKS="${1}"
  75. local DEV="${2}"
  76. local KEYFILE="${3}"
  77. # Some builders fail if the cryptsetup steps are not ran as root, so let's
  78. # skip the test now if not running as root.
  79. if [ $(id -u) != 0 ]; then
  80. skip_test "WARNING: You must be root to run this test; test skipped."
  81. fi
  82. if [[ -z "${KEYFILE}" ]] || [[ ! -f "${KEYFILE}" ]]; then
  83. error "Invalid keyfile (${KEYFILE})."
  84. fi
  85. fallocate -l16M "${DEV}"
  86. local extra_options='--pbkdf pbkdf2 --pbkdf-force-iterations 1000'
  87. cryptsetup luksFormat --type "${LUKS}" ${extra_options} --batch-mode \
  88. "${DEV}" "${KEYFILE}"
  89. }
  90. pin_cfg_equal() {
  91. local cfg1="${1}"
  92. local cfg2="${1}"
  93. diff <(jq -S . < <(echo -n "${cfg1}")) \
  94. <(jq -S . < <(echo -n "${cfg2}"))
  95. }
  96. export DEFAULT_PASS='just-some-test-password-here'