Add testcases

This commit is contained in:
0x1eef 2024-03-07 20:27:20 -03:00
parent 6ea77f862f
commit 819601ef44
9 changed files with 98 additions and 0 deletions

View file

@ -6,9 +6,12 @@ PATH
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
power_assert (2.0.3)
rake (13.1.0) rake (13.1.0)
rake-compiler (1.2.0) rake-compiler (1.2.0)
rake rake
test-unit (3.6.2)
power_assert
PLATFORMS PLATFORMS
amd64-freebsd-14 amd64-freebsd-14
@ -17,6 +20,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
hbsdctl.rb! hbsdctl.rb!
rake-compiler (= 1.2.0) rake-compiler (= 1.2.0)
test-unit (~> 3.6)
BUNDLED WITH BUNDLED WITH
2.5.3 2.5.3

View file

@ -7,3 +7,15 @@ desc "Run C linter"
task :styleguide do task :styleguide do
sh "uncrustify -c .styleguide.cfg --no-backup ext/hbsdctl.rb/*.c" sh "uncrustify -c .styleguide.cfg --no-backup ext/hbsdctl.rb/*.c"
end end
namespace :test do
desc "Run unprivileged tests"
task :unprivileged do
sh "./bin/run-unprivileged-tests"
end
desc "Run superuser tests"
task :superuser do
sh "./bin/run-superuser-tests"
end
end

11
bin/run-superuser-tests Executable file
View file

@ -0,0 +1,11 @@
#!/bin/sh
set -e
if [ $(id -u) = 0 ]; then
rake clean clobber compile
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."
exit 1
fi

6
bin/run-unprivileged-tests Executable file
View file

@ -0,0 +1,6 @@
#!/bin/sh
set -e
rake clean clobber compile
for file in test/unprivileged/*_test.rb; do
ruby -Ilib ${file} --no-use-color
done

View file

@ -11,4 +11,5 @@ Gem::Specification.new do |gem|
gem.summary = "Ruby bindings for libhbsdcontrol" gem.summary = "Ruby bindings for libhbsdcontrol"
gem.description = gem.summary gem.description = gem.summary
gem.add_development_dependency "rake-compiler", "= 1.2.0" gem.add_development_dependency "rake-compiler", "= 1.2.0"
gem.add_development_dependency "test-unit", "~> 3.6"
end end

2
test/setup.rb Normal file
View file

@ -0,0 +1,2 @@
require "test/unit"
require "hbsdctl"

View file

@ -0,0 +1,27 @@
require_relative "../setup"
module BSD::Control
class EnableFeatureTest < Test::Unit::TestCase
require 'fileutils'
include FileUtils
def test_enable_mprotect
touch(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)
end
end
private
def file
File.join(__dir__, "file")
end
end
end

View file

@ -0,0 +1,20 @@
require_relative "../setup"
module BSD::Control
class AvailableFeaturesTest < Test::Unit::TestCase
def test_available_features_not_empty
refute available_features.empty?,
"There should have been at least one available feature"
end
def test_available_features_instance_of
assert available_features.all? { _1.instance_of?(BSD::Control::Feature) },
"All available features should be an instance of `BSD::Control::Feature`"
end
private
def available_features
BSD::Control.available_features
end
end
end

View file

@ -0,0 +1,15 @@
require_relative "../setup"
module BSD::Control
class FeatureBangTest < Test::Unit::TestCase
def test_mprotect_feature
assert_instance_of BSD::Control::Feature,
BSD::Control.feature!(:mprotect)
end
def test_nonexistent_feature
assert_raises(BSD::Control::Error) do
BSD::Control.feature!(:non_existent_feature)
end
end
end
end