##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.20-locate/%M%
#   Revision:		%I%
#   First Edition:	2017/04/23
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		load, display, whatis, avail
#   Modulefiles:
#   Sub-Command:
#
#   Comment:	%C{
#			Localization of deep modulefile 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 {

# ensure avail tests are made using in depth mode
setenv_var MODULES_AVAIL_INDEPTH 1

# ensure regular avail search match is set for these tests
setenv_var MODULES_SEARCH_MATCH starts_with

#
# Variables for this test
#

set m1 "modalias/dir1/1.0"
set lm1 "$m1"
set lmf1 "$modpath/$lm1"
set m2 "modalias/dir1/2.0"
set lm2 "modalias/dir2/2.0"
set lmf2 "$modpath/$lm2"
set m3 "modload/dir2/md1"
set lm3 "modload/dir2/1.0"
set lmf3 "$modpath/$lm3"
set m4 "moddef"
set lm4 "moddef/dir2/1.0"
set lmf4 "$modpath/$lm4"
set m5 "plain/dir2"
set lm5 "$m5/2.0"
set lmf5 "$modpath/$lm5"
set rlm5 "$m5/1.0"
set rlmf5 "$modpath/$rlm5"
set rlmfre5 "$modpathre/$rlm5"
set m6 "plain/dir2"
set lm6 "$m6/1.0"
set lmf6 "$modpath/$lm6"
set m7 "modalias"
set lm7 "$m7/3.0"
set lmf7 "$modpath/$lm7"
set m8 "$m2"
set lmrc8 "$modpath/modalias/dir1/.modulerc"
set m9 "$m3"
set lmrc9 "$modpath/modload/dir2/.modulerc"
set m10 "$m4"
set lmrc10 "$modpath/moddef/dir2/.modulerc"
set rlm10 "$m10/dir2/2.0"
set rlmf10 "$modpath/$rlm10"
set rlmfre10 "$modpathre/$rlm10"
set m12 "$m1"
set lmd12 [file dirname $lmf1]
set m13 "$m2"
set lmd13 [file dirname $lmf2]
set m14 "$m3"
set lmd14 [file dirname $lmf3]
set m15 "$m4"
set lmd15 [file dirname $lmf4]
set m16 "$m5"
set lmd16 [file dirname $lmf5]
set m17 "$m13"
set lmd17 [file dirname $lmd13]
set m18 "$m15"
set lmd18 [file dirname $lmd15]
set m19 "modvirt/dir1/1.0"
set lmd19 "$modpath/modvirt/.common"
set m20 "modvirt/dir1/2.0"
set m21 "modsym"
set lm21 "$m21/dir2/2.0"
set lmf21 "$modpath/$lm21"
set lmfre21 "$modpathre/$lm21"
set lmfh21 "$modpath/$m21/dir1/1.0"

set pheader "$modlin $modpathre $modlin"
set theader "$modpathre:"
set access_msg "$error_msgs: Permission denied on"
set hide_perms "ugo-rx"

#
# Test access to modulefile when access not granted on this modulefile
#

change_file_perms $lmf1 $hide_perms

testouterr_cmd "sh" "load $m1" "ERR" "$access_msg '$lmf1'"
testouterr_cmd "sh" "display $m1" "ERR" "$access_msg '$lmf1'"
testouterr_cmd "sh" "whatis $m1" "ERR" "$access_msg '$lmf1'"
testouterr_cmd "sh" "avail -t $m1" "OK" ""

restore_file_perms $lmf1

#
# Test access to alias when access not granted on target modulefile
#

change_file_perms $lmf2 $hide_perms

testouterr_cmd "sh" "load $m2" "ERR" "$access_msg '$lmf2'"
testouterr_cmd "sh" "display $m2" "ERR" "$access_msg '$lmf2'"
testouterr_cmd "sh" "whatis $m2" "ERR" "$access_msg '$lmf2'"
# alias defined so it appears even if access to target modulefile is denied
testouterr_cmd_re "sh" "avail -t $m2" "OK" "$theader\n$m2\\(@\\)"

restore_file_perms $lmf2

#
# Test access to symbol when access not granted on target modulefile
#

change_file_perms $lmf3 $hide_perms

testouterr_cmd "sh" "load $m3" "ERR" "$access_msg '$lmf3'"
testouterr_cmd "sh" "display $m3" "ERR" "$access_msg '$lmf3'"
testouterr_cmd "sh" "whatis $m3" "ERR" "$access_msg '$lmf3'"
# symbol is attached to the modulefile so it does not appear if the access
# to the target modulefile is denied
testouterr_cmd "sh" "avail -t $m3" "OK" ""

restore_file_perms $lmf3

#
# Test access to default symbol when access not granted on target modulefile
#

change_file_perms $lmf4 $hide_perms

# shout error as if we were trying to access the symbol directly
testouterr_cmd "sh" "load $m4" "ERR" [if_implicitdefault "$access_msg '$lmf4'" "$err_nodefault'$m4'"]
testouterr_cmd "sh" "display $m4" "ERR" [if_implicitdefault "$access_msg '$lmf4'" "$err_nodefault'$m4'"]
# no error on whatis/avail, other versions are listed
testouterr_cmd_re "sh" "whatis $m4" "OK" "$pheader\n\\s+$m4/dir2/2.0: $m4/dir2/2.0"
testouterr_cmd_re "sh" "avail -t $m4" "OK" "$theader\n$m4/dir2/2.0"

restore_file_perms $lmf4

#
# Test access to implicit default modulefile when access not granted on target
# modulefile
#

change_file_perms $lmf5 $hide_perms

lappend ans [list set TEST $rlm5]
lappend ans [list setpath LOADEDMODULES $rlm5]
lappend ans [list setpath _LMFILES_ $rlmf5]
lappend ans [list setpath MODULES_LMCONFLICT "$rlm5&$m5"]
if {[is_conf_enabled advversspec implicitdefault]} {
    lappend ans [list setpath MODULES_LMALTNAME $rlm5&as|plain/default&as|plain/latest&as|$m5/default&as|$m5/latest]
}

# another implicit default is chosen
testouterr_cmd "sh" "load $m5" [if_implicitdefault $ans ERR] [if_implicitdefault {} "$err_nodefault'$m5'"]
testouterr_cmd_re "sh" "display $m5" [if_implicitdefault OK ERR] [if_implicitdefault "$modlin\n$rlmfre5:\n\nmodule-whatis\t$rlm5\nconflict\tplain/dir2\nsetenv\t\tTEST $rlm5\n$modlin" "$err_nodefault'$m5'"]
testouterr_cmd_re "sh" "whatis $m5" "OK" "$pheader\n\\s+$rlm5: $rlm5"
testouterr_cmd_re "sh" "avail -t $m5" "OK" "$theader\n$rlm5"

# keep file permissions as it is for next test

#
# Test access to implicit default modulefile when access not granted on target
# modulefile and there is no other implicit default available
#

change_file_perms $lmf6 $hide_perms

# nothing found
testouterr_cmd "sh" "load $m6" "ERR" "$err_path'$m6'"
testouterr_cmd "sh" "display $m6" "ERR" "$err_path'$m6'"
testouterr_cmd "sh" "whatis $m6" "ERR" "$err_path'$m6'"
testouterr_cmd "sh" "avail -t $m6" "OK" ""

restore_file_perms $lmf6
restore_file_perms $lmf5

#
# Test access to implicit default alias when access not granted on target modulefile
#

change_file_perms $lmf7 $hide_perms

# shout error as if we were trying to access the alias directly
testouterr_cmd "sh" "load $m7" "ERR" [if_implicitdefault "$access_msg '$lmf7'" "$err_nodefault'$m7'"]
testouterr_cmd "sh" "display $m7" "ERR" [if_implicitdefault "$access_msg '$lmf7'" "$err_nodefault'$m7'"]
# no error on whatis/avail, other versions are listed
testouterr_cmd_re "sh" "whatis $m7" "OK" "$pheader\n\\s+$m7/dir1/1.0: $m7/dir1/1.0\n\\s+$m7/dir2/2.0: $m7/dir2/2.0"
# alias defined so it appears even if access to target modulefile is denied
testouterr_cmd_re "sh" "avail -t $m7" "OK" "$theader\n$m7/1.0\\(@\\)\n$m7/dir1/1.0\n$m7/dir1/2.0\\(@\\)\n$m7/dir2/2.0\n$m7/dir2/3.0\\(@\\)"

restore_file_perms $lmf7

#
# Test access to alias when access not granted on modulerc defining it
#

change_file_perms $lmrc8 $hide_perms

# cannot locate module
testouterr_cmd "sh" "load $m8" "ERR" "$err_path'$m8'"
testouterr_cmd "sh" "display $m8" "ERR" "$err_path'$m8'"
testouterr_cmd "sh" "whatis $m8" "ERR" "$err_path'$m8'"
testouterr_cmd "sh" "avail -t $m8" "OK" ""

restore_file_perms $lmrc8

#
# Test access to symbol when access not granted on modulerc defining it
#

change_file_perms $lmrc9 $hide_perms

# cannot locate module
testouterr_cmd "sh" "load $m9" "ERR" "$err_path'$m9'"
testouterr_cmd "sh" "display $m9" "ERR" "$err_path'$m9'"
testouterr_cmd "sh" "whatis $m9" "ERR" "$err_path'$m9'"
testouterr_cmd "sh" "avail -t $m9" "OK" ""

restore_file_perms $lmrc9

#
# Test access to default symbol when access not granted on modulerc defining it
#

change_file_perms $lmrc10 $hide_perms

set ans [list]
lappend ans [list set TEST $rlm10]
lappend ans [list setpath LOADEDMODULES $rlm10]
lappend ans [list setpath _LMFILES_ $rlmf10]
if {[is_conf_enabled advversspec implicitdefault]} {
    lappend ans [list setpath MODULES_LMALTNAME $rlm10&as|moddef/default&as|moddef/latest&as|moddef/dir2/default&as|moddef/dir2/latest]
}

# another implicit default is chosen
testouterr_cmd "sh" "load $m10" [if_implicitdefault $ans ERR] [if_implicitdefault {} "$err_nodefault'$m10'"]
testouterr_cmd_re "sh" "display $m10" [if_implicitdefault OK ERR] [if_implicitdefault "$modlin\n$rlmfre10:\n\nmodule-whatis\t$rlm10\nsetenv\t\tTEST $rlm10\n$modlin" "$err_nodefault'$m10'"]
testouterr_cmd_re "sh" "whatis $m10" "OK" "$pheader\n\\s+$m10/dir2/1.0: $m10/dir2/1.0\n\\s+$m10/dir2/2.0: $m10/dir2/2.0"
testouterr_cmd_re "sh" "avail -t $m10" "OK" "$theader\n$m10/dir2/1.0\n$m10/dir2/2.0"

restore_file_perms $lmrc10

#
# Test access to modulefile when access not granted on its upper directory
#

change_file_perms $lmd12 $hide_perms

# cannot locate module
testouterr_cmd "sh" "load $m12" "ERR" "$err_path'$m12'"
testouterr_cmd "sh" "display $m12" "ERR" "$err_path'$m12'"
testouterr_cmd "sh" "whatis $m12" "ERR" "$err_path'$m12'"
testouterr_cmd "sh" "avail -t $m12" "OK" ""

restore_file_perms $lmd12

#
# Test access to alias when access not granted on its upper directory
#

change_file_perms $lmd13 $hide_perms

# cannot locate module
testouterr_cmd "sh" "load $m13" "ERR" "$err_path'$lm2'"
testouterr_cmd "sh" "display $m13" "ERR" "$err_path'$lm2'"
testouterr_cmd "sh" "whatis $m13" "ERR" "$err_path'$lm2'"
testouterr_cmd_re "sh" "avail -t $m13" "OK" "$theader\n$m13\\(@\\)"

restore_file_perms $lmd13

#
# Test access to symbol when access not granted on its upper directory
#

change_file_perms $lmd14 $hide_perms

# cannot locate module
testouterr_cmd "sh" "load $m14" "ERR" "$err_path'$m14'"
testouterr_cmd "sh" "display $m14" "ERR" "$err_path'$m14'"
testouterr_cmd "sh" "whatis $m14" "ERR" "$err_path'$m14'"
testouterr_cmd "sh" "avail -t $m14" "OK" ""

restore_file_perms $lmd14

#
# Test access to default symbol when access not granted on its upper directory
#

change_file_perms $lmd15 $hide_perms

# permission denied on sub-directory, so cannot locate module
testouterr_cmd "sh" "load $m15" "ERR" "$err_path'$m15'"
testouterr_cmd "sh" "display $m15" "ERR" "$err_path'$m15'"
testouterr_cmd "sh" "whatis $m15" "ERR" "$err_path'$m15'"
testouterr_cmd "sh" "avail -t $m15" "OK" ""

restore_file_perms $lmd15

#
# Test access to implicit default modulefile when access not granted on its
# upper directory
#

change_file_perms $lmd16 $hide_perms

# permission denied on directory
testouterr_cmd "sh" "load $m16" "ERR" "$access_msg '$lmd16'"
testouterr_cmd "sh" "display $m16" "ERR" "$access_msg '$lmd16'"
testouterr_cmd "sh" "whatis $m16" "ERR" "$access_msg '$lmd16'"
testouterr_cmd "sh" "avail -t $m16" "OK" ""

restore_file_perms $lmd16

#
# Test access to alias when access not granted on its top upper directory
#

change_file_perms $lmd17 $hide_perms

# cannot locate module
testouterr_cmd "sh" "load $m17" "ERR" "$err_path'$m17'"
testouterr_cmd "sh" "display $m17" "ERR" "$err_path'$m17'"
testouterr_cmd "sh" "whatis $m17" "ERR" "$err_path'$m17'"
testouterr_cmd "sh" "avail -t $m17" "OK" ""

restore_file_perms $lmd17

#
# Test access to default symbol when access not granted on its top upper directory
#

change_file_perms $lmd18 $hide_perms

# permission denied on directory
testouterr_cmd "sh" "load $m18" "ERR" "$access_msg '$lmd18'"
testouterr_cmd "sh" "display $m18" "ERR" "$access_msg '$lmd18'"
testouterr_cmd "sh" "whatis $m18" "ERR" "$access_msg '$lmd18'"
testouterr_cmd "sh" "avail -t $m18" "OK" ""

restore_file_perms $lmd18

#
# Test access to virtual module when access not granted on its target file
# lying in upper directory
#

change_file_perms $lmd19 $hide_perms

# permission denied on virtual target file
testouterr_cmd "sh" "load $m19" "ERR" "$access_msg '$lmd19'"
testouterr_cmd "sh" "display $m19" "ERR" "$access_msg '$lmd19'"
testouterr_cmd "sh" "whatis $m19" "ERR" "$access_msg '$lmd19'"
testouterr_cmd "sh" "avail -t $m19" "OK" ""

#
# Test access to alias when access not granted on its virtual module target
# lying in upper directory
#

# permission denied on virtual target file
testouterr_cmd "sh" "load $m20" "ERR" "$access_msg '$lmd19'"
testouterr_cmd "sh" "display $m20" "ERR" "$access_msg '$lmd19'"
testouterr_cmd "sh" "whatis $m20" "ERR" "$access_msg '$lmd19'"
# alias defined so it appears even if access to target modulefile is denied
testouterr_cmd_re "sh" "avail -t $m20" "OK" "$theader\n$m20\\(@\\)"

restore_file_perms $lmd19


#
# Test access to implicit default modulefile when access not granted on sibbling
# modulefile leading to removing sibbling directory
#

change_file_perms $lmfh21 $hide_perms

set ans [list]
lappend ans [list set TEST $lm21]
lappend ans [list setpath LOADEDMODULES $lm21]
lappend ans [list setpath _LMFILES_ $lmf21]
lappend ans [list setpath MODULES_LMCONFLICT "$lm21&$m21"]
if {[is_conf_enabled advversspec implicitdefault]} {
    lappend ans [list setpath MODULES_LMALTNAME "$lm21&modsym/dir2/3.0&as|modsym/default&as|modsym/latest&as|modsym/dir2/default&as|modsym/dir2/latest"]
} else {
    lappend ans [list setpath MODULES_LMALTNAME "$lm21&modsym/dir2/3.0"]
}

# implicit default is not impacted
testouterr_cmd "sh" "load $m21" [if_implicitdefault $ans ERR] [if_implicitdefault {} "$err_nodefault'$m21'"]
testouterr_cmd_re "sh" "display $m21" [if_implicitdefault OK ERR] [if_implicitdefault "$modlin\n$lmfre21:\n\nmodule-whatis\t$lm21\nconflict\t$m21\nsetenv\t\tTEST $lm21\n$modlin" "$err_nodefault'$m21'"]
testouterr_cmd_re "sh" "whatis $m21" "OK" "$pheader\n\\s+$lm21: $lm21"
testouterr_cmd_re "sh" "avail -t $m21" "OK" "$theader\n$lm21\\(3.0\\)"

restore_file_perms $lmfh21


#
# Cleanup
#

reset_test_env

}
