Skip to content

Instantly share code, notes, and snippets.

@rik0
Last active August 29, 2015 14:07
Show Gist options
  • Save rik0/7226fed1c70c82ad8c83 to your computer and use it in GitHub Desktop.
Save rik0/7226fed1c70c82ad8c83 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# tested with bash 4.3
cd_into() {
(cd $1)
}
list_directory() {
(ls $1 > /dev/null)
}
cd_and_list() {
(cd $1 && ls > /dev/null)
}
rename_file() {
act_on_file --rename $@
}
delete_file() {
act_on_file --delete $@
}
append_to_file() {
act_on_file --append $@
}
write_to_file() {
act_on_file --write $@
}
read_file() {
act_on_file --read $@
}
act_on_file() {
local action=${1:2}
local pre_or_post=${2:2}
local current_dir=$3
local perm=$4
local filename="$current_dir/${perm}_${pre_or_post}_$action"
case $action in
rename) mv $filename "$filename.other";;
append) echo something >> $filename;;
write) echo something > $filename;;
delete) rm $filename; return $? ;;
read) cat $filename;;
*) echo "Invalid Option"; exit 1;;
esac
}
act_all() {
execute_action rename_file $@
execute_action delete_file $@
execute_action append_to_file $@
execute_action write_to_file $@
execute_action read_file $@
}
execute_action() {
local action=$1
shift
eval $action $@
if (( $? )); then
echo "$action $@ FAILURE"
else
echo "$action $@ SUCCESS"
fi
}
setup_directory() {
local pre_or_post=${1:2}
local current_dir=$2
local action
local perm
touch "$current_dir/can_create_file_${pre_or_post}"
if (( $? )); then
return 1
fi
for action in $ACTIONS; do
for perm in $PERMS; do
local filename="$current_dir/${perm}_${pre_or_post}_$action"
touch $filename && chmod $perm $filename
done
done
return 0
}
setup() {
local perm
mkdir -p $ROOT_DIR
for perm in $PERMS; do
current_dir="$ROOT_DIR/$perm"
mkdir $current_dir
setup_directory --pre $current_dir
chmod $perm $current_dir
setup_directory --post $current_dir
[[ $? == 0 ]] && CREATION_SUCCESS[$perm]=1
done
}
LOGFILE=${1:-`pwd`/test-permissions.log}
ACTIONS="read write append delete rename"
OPERATIONS=(cd_into list_directory
cd_and_list)
ROOT_DIR="/tmp/dirtest.$RANDOM"
PERMS="000 111 222 333 444 555 666 777"
CREATION_SUCCESS=([000]=0 [111]=0 [222]=0
[333]=0 [444]=0 [555]=0
[666]=0 [777]=0)
echo "" > $LOGFILE
exec > >(tee -a $LOGFILE)
exec 2> >(tee -a $LOGFILE >/dev/null)
setup
cd $ROOT_DIR
for current_dir in *; do
echo "=== $current_dir ==="
for operation in ${OPERATIONS[@]}; do
execute_action $operation $current_dir
done
for perm in $PERMS; do
act_all --pre $current_dir $perm
done
if (( ${CREATION_SUCCESS[$perm]} )); then
echo "create_file $perm SUCCESS"
for perm in $PERMS; do
act_all --post $current_dir $perm
done
else
echo "create_file $perm FAILURE"
fi
done
# cleanup
find $ROOT_DIR -type d -exec chmod 777 {} \;
find $ROOT_DIR -exec chmod 777 {} \;
rm -rf $ROOT_DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment