diff --git a/ext/hbsdctl.rb/feature.c b/ext/hbsdctl.rb/feature.c index e4a5899..53f9a93 100644 --- a/ext/hbsdctl.rb/feature.c +++ b/ext/hbsdctl.rb/feature.c @@ -1,5 +1,6 @@ #include #include +#include #include "include/feature.h" static VALUE __rb_eError(void); static VALUE __set(VALUE, VALUE, VALUE); @@ -12,15 +13,9 @@ feature_set(VALUE self, VALUE rb_path, VALUE rb_state) { Check_Type(rb_path, T_STRING); Check_Type(rb_state, T_FIXNUM); - if (getuid() != 0) { - rb_raise(__rb_eError(), "This operation requires root privileges."); - } else if (rb_funcall(rb_cFile, rb_intern("exist?"), 1, rb_path) == Qfalse) { - rb_raise(__rb_eError(), "The given path does not exist."); - } else { - VALUE rb_feature = rb_funcall(self, rb_intern("name"), 0); - Check_Type(rb_feature, T_STRING); - return (__set(rb_path, rb_feature, rb_state)); - } + VALUE rb_feature = rb_funcall(self, rb_intern("name"), 0); + Check_Type(rb_feature, T_STRING); + return (__set(rb_path, rb_feature, rb_state)); } @@ -35,7 +30,7 @@ __set(VALUE rb_path, VALUE rb_feature, VALUE rb_state) if (result == 0) { return (Qtrue); } else { - rb_raise(__rb_eError(), "hbsdcontrol_set_feature_state failed"); + rb_syserr_fail(errno, "hbsdcontrol_set_feature_state"); } } diff --git a/lib/bsd/control/feature.rb b/lib/bsd/control/feature.rb index a397e26..a796d87 100644 --- a/lib/bsd/control/feature.rb +++ b/lib/bsd/control/feature.rb @@ -16,8 +16,8 @@ module BSD::Control # @param [String] path # The path to a file. # - # @raise [BSD::Control::Error] - # When the operation fails. + # @raise [SystemCallError] + # Might raise a number of Errno exceptions. # # @return [Boolean] # Returns true on success. @@ -31,8 +31,8 @@ module BSD::Control # @param [String] path # The path to a file. # - # @raise [BSD::Control::Error] - # When the operation fails. + # @raise [SystemCallError] + # Might raise a number of Errno exceptions. # # @return [Boolean] # Returns true on success. diff --git a/test/superuser/disable_feature_test.rb b/test/superuser/disable_feature_test.rb new file mode 100644 index 0000000..7ca374e --- /dev/null +++ b/test/superuser/disable_feature_test.rb @@ -0,0 +1,19 @@ +require_relative "../setup" +module BSD::Control + class DisableFeatureTest < Test::Unit::TestCase + require 'fileutils' + include FileUtils + + def test_disable_mprotect_nonexistent_file + assert_raises Errno::ENOENT do + BSD::Control.feature(:mprotect).disable!(file) + end + end + + private + + def file + File.join(__dir__, "file") + end + end +end diff --git a/test/superuser/enable_feature_test.rb b/test/superuser/enable_feature_test.rb index 1213d72..c420fe9 100644 --- a/test/superuser/enable_feature_test.rb +++ b/test/superuser/enable_feature_test.rb @@ -22,7 +22,7 @@ module BSD::Control end def test_enable_mprotect_nonexistent_file - assert_raises(BSD::Control::Error) do + assert_raises(Errno::ENOENT) do BSD::Control.feature(:mprotect).enable!(file) end end diff --git a/test/unprivileged/disable_feature_test.rb b/test/unprivileged/disable_feature_test.rb new file mode 100644 index 0000000..11468f0 --- /dev/null +++ b/test/unprivileged/disable_feature_test.rb @@ -0,0 +1,19 @@ +require_relative "../setup" +module BSD::Control + class DisableFeatureTest < Test::Unit::TestCase + require 'fileutils' + include FileUtils + + def test_disable_mprotect_nonexistent_file + assert_raises(Errno::ENOENT) do + BSD::Control.feature(:mprotect).disable!(file) + end + end + + private + + def file + File.join(__dir__, "file") + end + end +end diff --git a/test/unprivileged/enable_feature_test.rb b/test/unprivileged/enable_feature_test.rb index 77f264e..f1f6e08 100644 --- a/test/unprivileged/enable_feature_test.rb +++ b/test/unprivileged/enable_feature_test.rb @@ -6,10 +6,7 @@ module BSD::Control def test_enable_feature_lacks_privileges touch(file) - assert_raises( - BSD::Control::Error, - "This operation requires root privileges." - ) do + assert_raises(Errno::EPERM) do BSD::Control.feature(:mprotect).enable!(file) end ensure