#!/bin/bash -ex
# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
#
# Copyright (c) 2024 Oldřich Jedlička
# Author: Oldřich Jedlička <oldium.pro@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

TEST=$(basename "${0}")
. luks-common-test-functions

on_exit() {
    [ -d "${TMP}" ] && rm -rf "${TMP}"
}

trap 'on_exit' EXIT
trap 'exit' ERR

TMP="$(mktemp -d)"

ADV="${TMP}/adv.jws"
tang_create_adv "${TMP}" "${ADV}"
PIN1="sss"
PINS1="sss tang"
CFG1=$(printf '
{
  "t": 1,
  "pins": {
    "tang": [
      {
        "url": "ADDR","adv": "%s"
      }
    ]
  }
}
' "${ADV}")
PIN2="null"
PINS2="null"
CFG2='{}'
ALLPINS="null sss tang"

# LUKS1
DEV="${TMP}/luks1-device"
new_device "luks1" "${DEV}"

if ! clevis luks bind -f -d "${DEV}" "${PIN1}" "${CFG1}" <<< "${DEFAULT_PASS}"; then
    error "${TEST}: Binding is expected to succeed when given a correct (${DEFAULT_PASS}) password."
fi

if ! clevis luks bind -f -d "${DEV}" "${PIN2}" "${CFG2}" <<< "${DEFAULT_PASS}"; then
    error "${TEST}: Binding is expected to succeed for null pin."
fi

test_values() {
    local SLT="$1"
    local slot="$2"
    local pin="$3"
    local cfg="$4"

    case $SLT in
        1)
            if [[ "${slot}" != "${SLT}:" ]]; then
                error "${TEST}: slot (${slot}) is expected to be ${SLT}"
            fi

            if [[ "${pin}" != "${PIN1}" ]]; then
                error "${TEST}: pin (${pin}) is expected to be '${PIN1}'"
            fi

            to_remove_from_cfg=$(printf ',"adv": "%s"' "${ADV}")
            cfg_for_cmp=${CFG1//"${to_remove_from_cfg}"/} #"
            if ! pin_cfg_equal "${cfg}" "${cfg_for_cmp}"; then
                error "${TEST}: config obtained from clevis luks list (${cfg}) is expected to match the one used to bind the test (${cfg_for_cmp})"
            fi
            ;;
        2)
            if [[ "${slot}" != "${SLT}:" ]]; then
                error "${TEST}: slot (${slot}) is expected to be ${SLT}"
            fi

            if [[ "${pin}" != "${PIN2}" ]]; then
                error "${TEST}: pin (${pin}) is expected to be '${PIN2}'"
            fi

            if ! pin_cfg_equal "${cfg}" "${CFG2}"; then
                error "${TEST}: config obtained from clevis luks list (${cfg}) is expected to match the one used to bind the test (${CFG2})"
            fi
            ;;
        *)
            error "${TEST}: unexpected slot ${SLT}"
            ;;
    esac
}

test_pin_values() {
    local SLT="$1"
    local slot="$2"
    local pins="$3"

    case $SLT in
        1)
            if [[ "${slot}" != "${SLT}:" ]]; then
                error "${TEST}: used pins slot (${slot}) is expected to be ${SLT}"
            fi

            if [[ "${pins}" != "${PINS1}" ]]; then
                error "${TEST}: used pins (${pins}) are expected to be '${PINS1}'"
            fi
            ;;
        2)
            if [[ "${slot}" != "${SLT}:" ]]; then
                error "${TEST}: used pins slot (${slot}) is expected to be ${SLT}"
            fi

            if [[ "${pins}" != "${PINS2}" ]]; then
                error "${TEST}: used pins (${pins}) are expected to be '${PINS2}'"
            fi
            ;;
        *)
            error "${TEST}: unexpected slot ${SLT}"
            ;;
    esac
}

SLT=1
if ! read -r slot pin cfg < <(clevis luks list -d "${DEV}" -s "${SLT}"); then
    error "${TEST}: clevis luks list is expected to succeed for device(${DEV}) and slot (${SLT})"
fi

test_values "${SLT}" "${slot}" "${pin}" "${cfg}"

if ! read -r slot pins < <(clevis luks list -d "${DEV}" -s "${SLT}" -p); then
    error "${TEST}: clevis luks list -p is expected to succeed for device(${DEV}) and slot (${SLT})"
fi

test_pin_values "${SLT}" "${slot}" "${pins}"

SLT=2
if ! read -r slot pin cfg < <(clevis luks list -d "${DEV}" -s "${SLT}"); then
    error "${TEST}: clevis luks list is expected to succeed for device(${DEV}) and slot (${SLT})"
fi

test_values "${SLT}" "${slot}" "${pin}" "${cfg}"

if ! read -r slot pins < <(clevis luks list -d "${DEV}" -s "${SLT}" -p); then
    error "${TEST}: clevis luks list -p is expected to succeed for device(${DEV}) and slot (${SLT})"
fi

test_pin_values "${SLT}" "${slot}" "${pins}"

# Test both slots reading
if ! slots=$(clevis luks list -d "${DEV}"); then
    error "${TEST}: clevis luks list is expected to succeed for device(${DEV})"
fi

read_slots=
while read -r slot pin cfg; do
    read_slots="${read_slots}${read_slots:+ }${slot%:}"
    test_values "${slot%:}" "${slot}" "${pin}" "${cfg}"
done <<< "$slots"

if [[ "${read_slots}" != "1 2" ]]; then
    error "${TEST}: clevis luks list did not return all expected slots (1 2), it was (${read_slots}) for device(${DEV})"
fi

if ! slots=$(clevis luks list -d "${DEV}" -p); then
    error "${TEST}: clevis luks list -p is expected to succeed for device(${DEV})"
fi

read_slots=
while read -r slot pins; do
    read_slots="${read_slots}${read_slots:+ }${slot%:}"
    test_pin_values "${slot%:}" "${slot}" "${pins}"
done <<< "$slots"

if [[ "${read_slots}" != "1 2" ]]; then
    error "${TEST}: clevis luks list -p did not return all expected slots (1 2), it was (${read_slots}) for device(${DEV})"
fi

# Test clevis_luks_read_used_pins
. clevis-luks-common-functions

if ! pins=$(clevis_luks_read_used_pins "${DEV}"); then
    error "${TEST}: clevis_luks_read_used_pins is expected to succeed for device(${DEV})"
fi

if [[ "${pins}" != "${ALLPINS}" ]]; then
    error "${TEST}: clevis_luks_read_used_pins did not return all expected pins (${ALLPINS}), it was (${pins}) for device(${DEV})"
fi
