#!/bin/bash
#
# Copyright (C) 2020 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 GnuTLS; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

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

. `dirname $0`/common.sh

require_oc_client

eval "${GETPORT}"

echo "Testing whether group labels are translated to groups... "

# This is a necessary condition for some anyconnect clients

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

update_config test-group-name.config
launch_simple_sr_server -d 1 -f -c ${CONFIG}
PID=$!
wait_server $PID

oc_run init --group "Do not tunnel access LAN subnet" ||
	oc_fail "Requesting the auth form failed"
oc_expect 'Content-Type: text/xml; charset=utf-8$' \
	"Auth form does not declare UTF-8 charset"
oc_expect_not 'Group: value="Do not tunnel access LAN subnet"' \
	"Name was not translated"
oc_expect 'Group: value="group3" label="Do not tunnel access LAN subnet"' \
	"Translated group was not offered"
# A client-proposed group must be selected first without suppressing the
# remaining server-advertised groups.
oc_expect 'Group: value="group1" label="group1"' \
	"Selected group suppressed the rest of the group list"

oc_run init --group "Маршрут по умолчанию без BY" ||
	oc_fail "Requesting the auth form failed"
oc_expect_not 'Group: value="Маршрут по умолчанию без BY"' \
	"Localized friendly name was not translated"
oc_expect 'Group: value="group5" label="Маршрут по умолчанию без BY"' \
	"Localized friendly name was not offered"
oc_expect 'Group: value="group1" label="group1"' \
	"Localized selected group suppressed the rest of the group list"

oc_run init --group NotExistGroup || oc_fail "Requesting the auth form failed"
oc_expect_not 'Group: value="NotExistGroup"' \
	"Unknown group was offered back to the client"
oc_expect 'Group: value="group1" label="group1"' "Default group was not offered"
oc_expect 'Group: value="group3" label="Do not tunnel access LAN subnet"' \
	"Configured group list was not offered"

exit 0
