#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Verify that an sbcast credential is properly validated.
############################################################################
# Copyright (C) SchedMD LLC.
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm 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.
#
# Slurm 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 Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set bcast_file  "/tmp/${test_name}_sbcast"
set job_id      0

if {[get_config_param "FrontendName"] ne "MISSING"} {
	skip "This test is incompatible with front-end systems"
}
if {![check_run_as_user $testsuite_user]} {
	skip "This test needs testsuite_user configured in globals.local"
}

proc cleanup {} {
	global job_id sacctmgr testsuite_user test_name

	cancel_job $job_id
	run_command -none "$sacctmgr -vi delete user name=$testsuite_user"
	run_command -none "$sacctmgr -vi delete account name=$test_name"
}

if { [get_config_param -dbd "AllowNoDefAcct"] eq "Yes" } {
	set def_acct " defaultaccount=$test_name"
} else {
	set def_acct ""
}

# Setup the test user
run_command -fail "$sacctmgr -vi add account name=$test_name"
run_command -fail "$sacctmgr -vi add user name=$testsuite_user account=$test_name$def_acct"
run_command -fail "$sacctmgr -vi update user $testsuite_user set adminlevel=None"

#
# Test that testsuite_user can sbcast to own jobs
#
log_info "Test that a user can sbcast to its own jobs"

# Spawn an salloc job and obtain the job id
set salloc_output [run_command_output -fail -user $testsuite_user "$salloc -v -N1 -t2 --no-shell"]
if {![regexp {Granted job allocation (\d+)} $salloc_output - job_id]} {
	fail "Allocation not granted ($output)"
}

# Clear out the broadcast file
run_command -fail -user $testsuite_user "$srun --jobid $job_id $bin_rm -f $bcast_file"

# Attempt to broadcast the file
set sbcast_output [run_command_output -user $testsuite_user -subtest "$sbcast --jobid $job_id $sbcast $bcast_file"]
subtest {![regexp -line {error:.*Invalid} $sbcast_output]} "sbcast should NOT generate an error"

# Verify the file was broadcasted
set ls_output [run_command_output -user $testsuite_user -subtest "$srun --jobid $job_id ls $bcast_file"]
subtest [regexp $bcast_file $ls_output] "The file should have been broadcasted"

# Clear out the broadcast file
run_command -fail -user $testsuite_user "$srun --jobid $job_id $bin_rm -f $bcast_file"
run_command -fail -user $testsuite_user "$scancel $job_id"

#
# Test that testsuite_user cannot sbcast to jobs from other users
#
log_info "Test that unauthorized users cannot sbcast to a job"

# Spawn an salloc job and obtain the job id
set salloc_output [run_command_output -fail "$salloc -v -N1 -t2 --no-shell"]
if {![regexp {Granted job allocation (\d+)} $salloc_output - job_id]} {
    fail "Allocation not granted ($output)"
}

# Clear out the broadcast file
run_command -fail "$srun --jobid $job_id $bin_rm -f $bcast_file"

# Attempt to broadcast the file as a different (and unauthorized) user
set sbcast_output [run_command_output -user $testsuite_user -subtest -xfail "$sbcast --jobid $job_id $sbcast $bcast_file"]
subtest [regexp -line {error:.*Invalid} $sbcast_output] "sbcast should generate an error"

# Verify the file was not broadcast
set ls_output [run_command_output -subtest -xfail "$srun --jobid $job_id ls $bcast_file"]
subtest [regexp -line {ls.* (?:No such file|does not exist|not found)} $ls_output] "The file should not have been broadcast"
