From 275d622843375589444bd515a065783ce823799b Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Wed, 20 Mar 2024 21:58:18 -0300 Subject: [PATCH] Map HBSDCTRL_STATE_(ENABLED|DISABLED) into Ruby And fix `BSD::Control::Feature#disable!`. --- ext/bsdcontrol.rb/bsdcontrol.c | 2 ++ lib/bsd/control/feature.rb | 4 ++-- test/superuser/disable_feature_test.rb | 12 ++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/ext/bsdcontrol.rb/bsdcontrol.c b/ext/bsdcontrol.rb/bsdcontrol.c index baa6d79..2d9d39e 100644 --- a/ext/bsdcontrol.rb/bsdcontrol.c +++ b/ext/bsdcontrol.rb/bsdcontrol.c @@ -20,4 +20,6 @@ Init_bsdcontrol(void) rb_define_method(rb_cFeature, "status", bsdcontrol_feature_status, 1); rb_define_method(rb_cFeature, "sysdef!", bsdcontrol_feature_sysdef, 1); rb_define_private_method(rb_cFeature, "set!", bsdcontrol_feature_set, 2); + rb_define_const(rb_cFeature, "ENABLED", INT2NUM(HBSDCTRL_STATE_ENABLED)); + rb_define_const(rb_cFeature, "DISABLED", INT2NUM(HBSDCTRL_STATE_DISABLED)); } diff --git a/lib/bsd/control/feature.rb b/lib/bsd/control/feature.rb index 898cda9..495b663 100644 --- a/lib/bsd/control/feature.rb +++ b/lib/bsd/control/feature.rb @@ -22,7 +22,7 @@ module BSD::Control # @return [Boolean] # Returns true on success. def enable!(path) - set!(path, 1) + set!(path, ENABLED) end ## @@ -37,7 +37,7 @@ module BSD::Control # @return [Boolean] # Returns true on success. def disable!(path) - set!(path, 0) + set!(path, DISABLED) end ## diff --git a/test/superuser/disable_feature_test.rb b/test/superuser/disable_feature_test.rb index 71f04ec..0942c8a 100644 --- a/test/superuser/disable_feature_test.rb +++ b/test/superuser/disable_feature_test.rb @@ -4,6 +4,18 @@ module BSD::Control require 'fileutils' include FileUtils + def test_disable_pageexec + touch(file) + assert BSD::Control.feature(:pageexec).disable!(file), + "The disable! method should have returned true" + assert_equal( + :disabled, + BSD::Control.feature(:pageexec).status(file) + ) + ensure + rm(file) + end + def test_disable_pageexec_nonexistent_file assert_raises(Errno::ENOENT) do BSD::Control.feature(:pageexec).disable!(file)