From 819601ef4469fe7aa0ede87684d0090c258ca66d Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Thu, 7 Mar 2024 20:27:20 -0300 Subject: [PATCH] Add testcases --- Gemfile.lock | 4 +++ Rakefile.rb | 12 +++++++++ bin/run-superuser-tests | 11 ++++++++ bin/run-unprivileged-tests | 6 +++++ hbsdctl.rb.gemspec | 1 + test/setup.rb | 2 ++ test/superuser/enable_feature_test.rb | 27 ++++++++++++++++++++ test/unprivileged/available_features_test.rb | 20 +++++++++++++++ test/unprivileged/feature_bang_test.rb | 15 +++++++++++ 9 files changed, 98 insertions(+) create mode 100755 bin/run-superuser-tests create mode 100755 bin/run-unprivileged-tests create mode 100644 test/setup.rb create mode 100644 test/superuser/enable_feature_test.rb create mode 100644 test/unprivileged/available_features_test.rb create mode 100644 test/unprivileged/feature_bang_test.rb diff --git a/Gemfile.lock b/Gemfile.lock index 6663880..122acd3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,9 +6,12 @@ PATH GEM remote: https://rubygems.org/ specs: + power_assert (2.0.3) rake (13.1.0) rake-compiler (1.2.0) rake + test-unit (3.6.2) + power_assert PLATFORMS amd64-freebsd-14 @@ -17,6 +20,7 @@ PLATFORMS DEPENDENCIES hbsdctl.rb! rake-compiler (= 1.2.0) + test-unit (~> 3.6) BUNDLED WITH 2.5.3 diff --git a/Rakefile.rb b/Rakefile.rb index b682638..5fa30e7 100644 --- a/Rakefile.rb +++ b/Rakefile.rb @@ -7,3 +7,15 @@ desc "Run C linter" task :styleguide do sh "uncrustify -c .styleguide.cfg --no-backup ext/hbsdctl.rb/*.c" 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 diff --git a/bin/run-superuser-tests b/bin/run-superuser-tests new file mode 100755 index 0000000..141e966 --- /dev/null +++ b/bin/run-superuser-tests @@ -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 diff --git a/bin/run-unprivileged-tests b/bin/run-unprivileged-tests new file mode 100755 index 0000000..24ad22b --- /dev/null +++ b/bin/run-unprivileged-tests @@ -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 diff --git a/hbsdctl.rb.gemspec b/hbsdctl.rb.gemspec index 4834e4f..65939d3 100644 --- a/hbsdctl.rb.gemspec +++ b/hbsdctl.rb.gemspec @@ -11,4 +11,5 @@ Gem::Specification.new do |gem| gem.summary = "Ruby bindings for libhbsdcontrol" gem.description = gem.summary gem.add_development_dependency "rake-compiler", "= 1.2.0" + gem.add_development_dependency "test-unit", "~> 3.6" end diff --git a/test/setup.rb b/test/setup.rb new file mode 100644 index 0000000..017ac51 --- /dev/null +++ b/test/setup.rb @@ -0,0 +1,2 @@ +require "test/unit" +require "hbsdctl" diff --git a/test/superuser/enable_feature_test.rb b/test/superuser/enable_feature_test.rb new file mode 100644 index 0000000..c146818 --- /dev/null +++ b/test/superuser/enable_feature_test.rb @@ -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 diff --git a/test/unprivileged/available_features_test.rb b/test/unprivileged/available_features_test.rb new file mode 100644 index 0000000..23da739 --- /dev/null +++ b/test/unprivileged/available_features_test.rb @@ -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 diff --git a/test/unprivileged/feature_bang_test.rb b/test/unprivileged/feature_bang_test.rb new file mode 100644 index 0000000..583c2c5 --- /dev/null +++ b/test/unprivileged/feature_bang_test.rb @@ -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