#!/bin/bash
#
# Copyright (C) 2026 Nikos Mavrogiannopoulos
#
# This file is part of ocserv.
#
# ocserv 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 2 of the License, or (at
# your option) any later version.
#
# ocserv 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 <https://www.gnu.org/licenses/>.

SERV="${SERV:-../src/ocserv}"
srcdir=${srcdir:-.}
NO_NEED_ROOT=1

. `dirname $0`/common.sh
. `dirname $0`/random-vpnnet.sh

require_oc_client

eval "${GETPORT}"

echo "Testing the select-group list offered by named vhosts... "

function finish {
  set +e
  echo " * Cleaning up..."
  test -n "${CONFIG}" && rm -f ${CONFIG} >/dev/null 2>&1
  rm -f ${SERVER_LOG}
  cleanup
}
trap finish EXIT

SERVER_LOG=$(mktemp)

update_config test-vhost-select-group.config
# VERBOSE keeps the server's log, which oc_fail prints
VERBOSE=1 launch_simple_sr_server -d 1 -f -c ${CONFIG} >${SERVER_LOG} 2>&1
PID=$!
wait_server $PID

# expect_groups HOST PROPOSED EXPECTED WHAT: the groups HOST offers to a
# client proposing PROPOSED are exactly EXPECTED, in that order. A group the
# server resolves is offered first.
expect_groups() {
	oc_run --host $1 groups --group "$2" || oc_fail "$4: listing the groups failed"
	got=$(cat ${OC_OUT})
	if test "$got" != "$3"; then
		oc_fail "$4: expected groups '$3', got '$got'"
	fi
}

# expect_login HOST GROUP RESULT WHAT: logging in as test to HOST proposing
# GROUP exits with RESULT (0 accepted, 1 rejected; 2 is a broken exchange)
expect_login() {
	oc_run --host $1 login --group "$2" --user test --password test
	ret=$?
	if test $ret != $3; then
		oc_fail "$4: expected exit $3, got $ret"
	fi
}

expect_groups inherits.example.com "group1" $'group1\tgroup1\ngroup2\tLabel Two' \
	"inherited groups with their labels"
expect_groups inherits.example.com "Label Two" $'group2\tLabel Two\ngroup1\tgroup1' \
	"inherited label resolved"
expect_groups replaces.example.com "group3" $'group3\tLabel Three' \
	"vhost's own groups replace the inherited ones"

# user test is a member of group1 and group3, not group4. The vhost's list
# only decides what is offered: a hidden group is left to the backend (#742).
expect_login replaces.example.com group3 0 "vhost's own group"
expect_login replaces.example.com group1 0 "hidden group of which the user is a member"
expect_login replaces.example.com group4 1 "group of which the user is not a member"

kill $PID
wait
exit 0
