#!/bin/sh -efu

WORKDIR=/opt

_p4_6_2_1_check_stat()
{
	local expected="$1 $2"
	local actual=

	cd "$WORKDIR"
	actual="$(stat -c '%A %U %n' "$2")"
	assertEquals "$expected" "$actual"
}

_p4_6_2_1_check_read()
{
	local expected="$1"
	local result=

	cd "$WORKDIR"

	result="$(cat "$2" 2>&1 1>/dev/null)" ||:
	assertEquals "$expected" "${result##*: }"
}

_p4_6_2_1_check_write()
{
	local expected="$1"
	local result=

	cd "$WORKDIR"

	result="$(echo "" | tee "$2" 2>&1 1>/dev/null)" ||:
	assertEquals "$expected" "${result##*: }"
}

_p4_6_2_1_check_exec()
{
	local expected="$1"
	local result=

	cd "$WORKDIR"

	result="$(sh -c "./$2" 2>&1)" ||:
	assertEquals "$expected" "${result##*: }"
}

####
p4_6_2_1_test01_stat_writeonly() { # UnitTest
	_p4_6_2_1_check_stat "--w------- $USER" writeonly
}

p4_6_2_1_test02_stat_readonly() { # UnitTest
	_p4_6_2_1_check_stat "-r-------- $USER" readonly
}

p4_6_2_1_test03_stat_readwrite() { # UnitTest
	_p4_6_2_1_check_stat "-rw------- $USER" readwrite
}

p4_6_2_1_test04_stat_execonly() { # UnitTest
	_p4_6_2_1_check_stat "---x------ $USER" execonly
}

####
p4_6_2_1_test05_read_writeonly() { # UnitTest
	_p4_6_2_1_check_read "Permission denied" writeonly
}

p4_6_2_1_test06_read_readonly() { # UnitTest
	_p4_6_2_1_check_read "" readonly
}

p4_6_2_1_test07_read_readwrite() { # UnitTest
	_p4_6_2_1_check_read "" readwrite
}

p4_6_2_1_test08_read_execonly() { # UnitTest
	_p4_6_2_1_check_read "Permission denied" execonly
}

####
p4_6_2_1_test09_write_writeonly() { # UnitTest
	_p4_6_2_1_check_write "" writeonly
}

p4_6_2_1_test10_write_readonly() { # UnitTest
	_p4_6_2_1_check_write "Permission denied" readonly
}

p4_6_2_1_test11_write_readwrite() { # UnitTest
	_p4_6_2_1_check_write "" readwrite
}

p4_6_2_1_test12_write_execonly() { # UnitTest
	_p4_6_2_1_check_write "Permission denied" execonly
}

####
p4_6_2_1_test13_exec_writeonly() { # UnitTest
	_p4_6_2_1_check_exec "Permission denied" writeonly
}

p4_6_2_1_test14_exec_readonly() { # UnitTest
	_p4_6_2_1_check_exec "Permission denied" readonly
}

p4_6_2_1_test15_exec_readwrite() { # UnitTest
	_p4_6_2_1_check_exec "Permission denied" readwrite
}

p4_6_2_1_test16_exec_execonly() { # UnitTest
	_p4_6_2_1_check_exec "" execonly
}
