diff --git a/bin/run-superuser-tests b/bin/run-superuser-tests index 141e966..a6e6484 100755 --- a/bin/run-superuser-tests +++ b/bin/run-superuser-tests @@ -2,10 +2,11 @@ set -e if [ $(id -u) = 0 ]; then rake clean clobber compile + rm -rf tmp/ for file in test/superuser/*_test.rb; do ruby -Ilib ${file} --no-use-color done else - echo "You must be root to run superuser tests." + echo "You must be the root user to run these tests." exit 1 fi diff --git a/bin/run-unprivileged-tests b/bin/run-unprivileged-tests index 24ad22b..852965e 100755 --- a/bin/run-unprivileged-tests +++ b/bin/run-unprivileged-tests @@ -1,6 +1,11 @@ #!/bin/sh set -e -rake clean clobber compile -for file in test/unprivileged/*_test.rb; do - ruby -Ilib ${file} --no-use-color -done +if [ $(id -u) -ne 0 ]; then + rake clean clobber compile + for file in test/unprivileged/*_test.rb; do + ruby -Ilib ${file} --no-use-color + done +else + echo "You must be an unprivileged user to run these tests." + exit 1 +fi diff --git a/test/superuser/enable_feature_test.rb b/test/superuser/enable_feature_test.rb index c146818..53a4d7f 100644 --- a/test/superuser/enable_feature_test.rb +++ b/test/superuser/enable_feature_test.rb @@ -12,6 +12,15 @@ module BSD::Control rm(file) end + def test_enable_mprotect_zero_permissions + touch(file) + chmod(0, file) + assert BSD::Control.feature!(:mprotect).enable!(file), + "The enable! method should have returned true" + ensure + rm(file) + end + def test_enable_mprotect_nonexistent_file assert_raises(BSD::Control::Error) do BSD::Control.feature!(:mprotect).enable!(file) diff --git a/test/unprivileged/enable_feature_test.rb b/test/unprivileged/enable_feature_test.rb new file mode 100644 index 0000000..1ad434a --- /dev/null +++ b/test/unprivileged/enable_feature_test.rb @@ -0,0 +1,25 @@ +require_relative "../setup" +module BSD::Control + class EnableFeatureTest < Test::Unit::TestCase + require "fileutils" + include FileUtils + + def test_enable_feature_lacks_privileges + touch(file) + assert_raises( + BSD::Control::Error, + "This operation requires root privileges." + ) do + BSD::Control.feature!(:mprotect).enable!(file) + end + ensure + rm(file) + end + + private + + def file + File.join(__dir__, "file") + end + end +end