diff --git a/.gitignore b/.gitignore index 8436db2..970a122 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.gems/ /tmp/ +test/**/file *.so diff --git a/test/setup.rb b/test/setup.rb index 5235f26..5136d9c 100644 --- a/test/setup.rb +++ b/test/setup.rb @@ -3,3 +3,25 @@ require "bundler/setup" require "test/unit" require "bsdcontrol" + +module BSD::Control + class Test < Test::Unit::TestCase + require "fileutils" + include FileUtils + + def setup + File.exist?(file) ? rm(file) : nil + touch(file) + end + + def teardown + File.exist?(file) ? rm(file) : nil + end + + private + + def file + File.join(__dir__, "file") + end + end +end diff --git a/test/superuser/disable_feature_test.rb b/test/superuser/disable_feature_test.rb index 39a25d9..3af4f84 100644 --- a/test/superuser/disable_feature_test.rb +++ b/test/superuser/disable_feature_test.rb @@ -2,32 +2,19 @@ require_relative "../setup" module BSD::Control - class DisableFeatureTest < Test::Unit::TestCase - require "fileutils" - include FileUtils - + class DisableFeatureTest < BSD::Control::Test 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) + assert_equal true, + BSD::Control.feature(:pageexec).disable!(file) + assert_equal :disabled, + BSD::Control.feature(:pageexec).status(file) end def test_disable_pageexec_nonexistent_file + rm(file) assert_raises(Errno::ENOENT) do BSD::Control.feature(:pageexec).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 e0317ef..9c9762a 100644 --- a/test/superuser/enable_feature_test.rb +++ b/test/superuser/enable_feature_test.rb @@ -2,37 +2,23 @@ require_relative "../setup" module BSD::Control - class EnableFeatureTest < Test::Unit::TestCase - require "fileutils" - include FileUtils - + class EnableFeatureTest < BSD::Control::Test def test_enable_pageexec - touch(file) - assert BSD::Control.feature(:pageexec).enable!(file), - "The enable! method should have returned true" - ensure - rm(file) + assert_equal true, + BSD::Control.feature(:pageexec).enable!(file) end - def test_enable_pageexec_zero_permissions - touch(file) + def test_enable_pageexec_mode_zero chmod(0, file) - assert BSD::Control.feature(:pageexec).enable!(file), - "The enable! method should have returned true" - ensure - rm(file) + assert_equal true, + BSD::Control.feature(:pageexec).enable!(file) end def test_enable_pageexec_nonexistent_file + rm(file) assert_raises(Errno::ENOENT) do BSD::Control.feature(:pageexec).enable!(file) end end - - private - - def file - File.join(__dir__, "file") - end end end diff --git a/test/superuser/feature_status_test.rb b/test/superuser/feature_status_test.rb index 24b7ad6..89ae73c 100644 --- a/test/superuser/feature_status_test.rb +++ b/test/superuser/feature_status_test.rb @@ -2,40 +2,22 @@ require_relative "../setup" module BSD::Control - class FeatureStatusTest < Test::Unit::TestCase - require "fileutils" - include FileUtils - + class FeatureStatusTest < BSD::Control::Test def test_pageexec_sysdef_status - touch(file) assert_equal :sysdef, BSD::Control.feature(:pageexec).status(file) - ensure - rm(file) end def test_pageexec_enabled_status - touch(file) BSD::Control.feature(:pageexec).enable!(file) assert_equal :enabled, BSD::Control.feature(:pageexec).status(file) - ensure - rm(file) end def test_pageexec_disabled_status - touch(file) BSD::Control.feature(:pageexec).disable!(file) assert_equal :disabled, BSD::Control.feature(:pageexec).status(file) - ensure - rm(file) - end - - private - - def file - File.join(__dir__, "file") end end end diff --git a/test/superuser/sysdef_feature_test.rb b/test/superuser/sysdef_feature_test.rb index 702c7fd..ddad3f7 100644 --- a/test/superuser/sysdef_feature_test.rb +++ b/test/superuser/sysdef_feature_test.rb @@ -2,38 +2,23 @@ require_relative "../setup" module BSD::Control - class SysDefFeatureTest < Test::Unit::TestCase - require "fileutils" - include FileUtils - + class SysDefFeatureTest < BSD::Control::Test def test_sysdef_pageexec - touch(file) - assert BSD::Control.feature(:pageexec).enable!(file), - "The enable! method should have returned true" - assert_equal( - :enabled, - BSD::Control.feature(:pageexec).status(file) - ) - assert BSD::Control.feature(:pageexec).sysdef!(file), - "The sysdef! method should have returned true" - assert_equal( - :sysdef, - BSD::Control.feature(:pageexec).status(file) - ) - ensure - rm(file) + assert_equal true, + BSD::Control.feature(:pageexec).enable!(file) + assert_equal :enabled, + BSD::Control.feature(:pageexec).status(file) + assert_equal true, + BSD::Control.feature(:pageexec).sysdef!(file) + assert_equal :sysdef, + BSD::Control.feature(:pageexec).status(file) end def test_enable_pageexec_nonexistent_file + rm(file) assert_raises(Errno::ENOENT) do BSD::Control.feature(:pageexec).sysdef!(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 index efaea01..26a93a6 100644 --- a/test/unprivileged/available_features_test.rb +++ b/test/unprivileged/available_features_test.rb @@ -2,15 +2,14 @@ require_relative "../setup" module BSD::Control - class AvailableFeaturesTest < Test::Unit::TestCase + class AvailableFeaturesTest < BSD::Control::Test def test_available_features_not_empty - refute available_features.empty?, - "There should have been at least one available feature" + assert_equal false, available_features.empty? 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`" + assert_equal true, + available_features.all? { _1.instance_of?(BSD::Control::Feature) } end private diff --git a/test/unprivileged/disable_feature_test.rb b/test/unprivileged/disable_feature_test.rb index f843e08..1757f9b 100644 --- a/test/unprivileged/disable_feature_test.rb +++ b/test/unprivileged/disable_feature_test.rb @@ -2,20 +2,12 @@ require_relative "../setup" module BSD::Control - class DisableFeatureTest < Test::Unit::TestCase - require "fileutils" - include FileUtils - + class DisableFeatureTest < BSD::Control::Test def test_disable_pageexec_nonexistent_file + rm(file) assert_raises(Errno::ENOENT) do BSD::Control.feature(:pageexec).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 c2be5b9..9182e26 100644 --- a/test/unprivileged/enable_feature_test.rb +++ b/test/unprivileged/enable_feature_test.rb @@ -2,23 +2,11 @@ require_relative "../setup" module BSD::Control - class EnableFeatureTest < Test::Unit::TestCase - require "fileutils" - include FileUtils - - def test_enable_feature_lacks_privileges - touch(file) + class EnableFeatureTest < BSD::Control::Test + def test_enable_feature_insufficient_permissions assert_raises(Errno::EPERM) do BSD::Control.feature(:pageexec).enable!(file) end - ensure - rm(file) - end - - private - - def file - File.join(__dir__, "file") end end end diff --git a/test/unprivileged/feature_test.rb b/test/unprivileged/feature_test.rb index 1fec4f9..1d68c1e 100644 --- a/test/unprivileged/feature_test.rb +++ b/test/unprivileged/feature_test.rb @@ -2,7 +2,7 @@ require_relative "../setup" module BSD::Control - class FeatureTest < Test::Unit::TestCase + class FeatureTest < BSD::Control::Test def test_pageexec_feature assert_instance_of BSD::Control::Feature, BSD::Control.feature(:pageexec) diff --git a/test/unprivileged/sysdef_feature_test.rb b/test/unprivileged/sysdef_feature_test.rb index 99c6018..78234a3 100644 --- a/test/unprivileged/sysdef_feature_test.rb +++ b/test/unprivileged/sysdef_feature_test.rb @@ -2,23 +2,11 @@ require_relative "../setup" module BSD::Control - class SysDefTest < Test::Unit::TestCase - require "fileutils" - include FileUtils - - def test_sysdef!_lacks_privileges - touch(file) + class SysDefTest < BSD::Control::Test + def test_sysdef!_insufficient_permissions assert_raises(Errno::EPERM) do BSD::Control.feature(:pageexec).sysdef!(file) end - ensure - rm(file) - end - - private - - def file - File.join(__dir__, "file") end end end