Apply diff for patch

This commit is contained in:
0x1eef 2024-03-09 19:40:42 -03:00
parent a86304783f
commit e311d78c7c
6 changed files with 49 additions and 19 deletions

View file

@ -1,5 +1,6 @@
#include <libhbsdcontrol.h>
#include <unistd.h>
#include <errno.h>
#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");
}
}

View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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