##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.61-coll/%M%
#   Revision:		%I%
#   First Edition:	2017/06/27
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		savelist, save, restore, saverm, saveshow
#   Modulefiles:
#   Sub-Command:
#
#   Comment:	%C{
#			Handling of collections when access rights to get to it
#           does not allow it
#		}C%
#
##############################################################################

# cannot test access if cannot change file permission
if {!$is_file_perms_editable} {
    send_user "\tskipping access tests as file permissions cannot be changed\n"
# cannot test access if superuser privileges are in use
} elseif {$tcl_platform(user) eq "root"} {
    send_user "\tskipping access tests as tests are run by superuser\n"
# cannot test access if restricted file permissions have no effect
} elseif {$is_locked_dir_file_readable && $is_locked_file_readable} {
    send_user "\tskipping access tests as locked files or directories are still readable\n"
} else {

#
# Variables for this test
#

set header_coll "Named collection list:"
set coll_list "$coll1\n${coll1}.save\n$coll2\n$coll3\n$coll4f\n$coll6f\n$colld\ninfocmd\ninfocmdexp"

if {$install_implicitdefault eq {y}} {
    lappend ans [list set testsuite yes]
    lappend ans [list setpath MODULEPATH $c1modpath]
    lappend ans [list setpath MODULES_LMALTNAME $c1lmalt]
    lappend ans [list unset mload]
    lappend ans [list setpath LOADEDMODULES $c1lm]
    lappend ans [list setpath _LMFILES_ $c1lmf]
    lappend ans [list set t15 t15]
    lappend ans [list set MODULE_VERSION "1.10"]
    set c1errmsg "Loading loc_rc1/1.0\nLoading load/15\nLoading verssort/1.10\nload $cdmod4\nLoading $cdmod4\nload $c1mod5\nLoading $c1mod5\nload $c1mod6\nLoading $c1mod6"
} else {
    lappend ans [list set testsuite yes]
    lappend ans [list setpath MODULEPATH $c1modpath]
    lappend ans [list setpath MODULES_LMALTNAME $c1lmalt]
    lappend ans [list unset mload]
    lappend ans [list setpath LOADEDMODULES $c1lmnodefault]
    lappend ans [list setpath _LMFILES_ $c1lmfnodefault]
    lappend ans [list set t15 t15]
    lappend ans [list ERR]
    set c1errmsg "Loading loc_rc1/1.0\nLoading load/15\n$err_nodefault'verssort'\nload $cdmod4\nLoading $cdmod4\nload $c1mod5\nLoading $c1mod5\nload $c1mod6\nLoading $c1mod6"
}

set pheader "$modlin $modpathre $modlin"
set read_msg "$error_msgs: Collection $coll1 cannot be read."
set save_msg "$error_msgs: Collection $coll1 cannot be saved."
set found_msg "$error_msgs: Collection $coll1 cannot be found"
set perm_msg "  couldn't open \"$coll1fp\": permission denied"
set accessdir_msg "$error_msgs: Cannot access collection directory."
set permdir_msg "  couldn't read directory \"$homewithcolldotmod/\": permission denied"
set delete_msg "$error_msgs: Collection $coll1 cannot be removed."
set permdel_msg "  error deleting \"$coll1fp\": permission denied"
set rmc1errmsg "couldn't open \"$coll1fp\": no such file or directory"
set hide_perms "ugo-rx"
set write_perms "ugo-w"

# set HOME to location containing collections
if { $verbose > 0 } {
    send_user "\tSetup HOME = $homewithcoll\n"
}
set env(HOME) "$homewithcoll"

# backup collection that will be altered by tests
set coll1fpsave "${coll1fp}.save"
file copy -force $coll1fp $coll1fpsave


#
# Test access to collection when read access not granted on this collection
#

change_file_perms $coll1fp $hide_perms

testouterr_cmd "sh" "savelist -t" "OK" "$header_coll\n$coll_list"
testouterr_cmd "sh" "saveshow $coll1" "ERR" "$read_msg\n$perm_msg"
testouterr_cmd "sh" "restore $coll1" "ERR" "$read_msg\n$perm_msg"
testouterr_cmd "sh" "save $coll1" "OK" ""
testoutfile_cmd "sh" "saverm $coll1" "OK" "$coll1fp" "$rmc1errmsg"

# restore altered collection and permissions
file copy -force $coll1fpsave $coll1fp
restore_file_perms $coll1fp


#
# Test access to collection when write access not granted on this collection
#

change_file_perms $coll1fp $write_perms

testouterr_cmd "sh" "savelist -t" "OK" "$header_coll\n$coll_list"
testouterr_cmd_re "sh" "saveshow $coll1" "OK" "$modlin\n$coll1fpre:\n\n$presavedc1cont1re\n$modlin"
testouterr_cmd_re "sh" "restore $coll1" $ans "$c1errmsg"
testouterr_cmd "sh" "save $coll1" "ERR" "$save_msg\n$perm_msg"
testoutfile_cmd "sh" "saverm $coll1" "OK" "$coll1fp" "$rmc1errmsg"

# restore altered collection and permissions
file copy -force $coll1fpsave $coll1fp
restore_file_perms $coll1fp


#
# Test access to collection when read access not granted on its upper directory
#

change_file_perms $homewithcolldotmod $hide_perms

testouterr_cmd "sh" "savelist -t" "ERR" "$accessdir_msg\n$permdir_msg"
# different result if a file in a locked directory still readable on this system
if {$is_locked_dir_file_readable} {
    testouterr_cmd_re "sh" "saveshow $coll1" "OK" "$modlin\n$coll1fpre:\n\n$presavedc1cont1re\n$modlin"
    testouterr_cmd_re "sh" "restore $coll1" $ans "$c1errmsg"
    testouterr_cmd "sh" "save $coll1" "OK" ""
    testoutfile_cmd "sh" "saverm $coll1" "OK" "$coll1fp" "$rmc1errmsg"
} else {
    testouterr_cmd "sh" "saveshow $coll1" "ERR" "$found_msg"
    testouterr_cmd "sh" "restore $coll1" "ERR" "$found_msg"
    testouterr_cmd "sh" "save $coll1" "ERR" "$save_msg\n$perm_msg"
    testouterr_cmd "sh" "saverm $coll1" "ERR" "$found_msg"
}

# restore altered collection and permissions
restore_file_perms $homewithcolldotmod
file copy -force $coll1fpsave $coll1fp


#
# Test access to collection when write access not granted on its upper directory
#

change_file_perms $homewithcolldotmod $write_perms

testouterr_cmd "sh" "savelist -t" "OK" "$header_coll\n$coll_list"
testouterr_cmd_re "sh" "saveshow $coll1" "OK" "$modlin\n$coll1fpre:\n\n$presavedc1cont1re\n$modlin"
testouterr_cmd_re "sh" "restore $coll1" $ans "$c1errmsg"
testouterr_cmd "sh" "save $coll1" "OK" ""
# different result if a file in a locked directory still readable on this system
if {$is_locked_dir_file_readable} {
    testoutfile_cmd "sh" "saverm $coll1" "OK" "$coll1fp" "$rmc1errmsg"
} else {
    testouterr_cmd "sh" "saverm $coll1" "ERR" "$delete_msg\n$permdel_msg"
}

# restore altered collection and permissions
restore_file_perms $homewithcolldotmod
file copy -force $coll1fpsave $coll1fp


#
# Cleanup
#

# remove collection backup
file delete $coll1fpsave

reset_test_env

}
