diff --git a/README.md b/README.md index 3762a27..c269cfa 100644 --- a/README.md +++ b/README.md @@ -29,15 +29,23 @@ Or install it yourself as: Basic synopsis: ```ruby -Capsicum.in_capability_mode? # => false -Capsicum.enter! # => true -Capsicum.in_capability_mode? # => true +require "capsicum" -File.new("/dev/null") # => Errno::ECAPMODE: Not permitted in capability mode @ rb_sysopen - /dev/null -TCPSocket.new("0", 80) # => Errno::ECAPMODE: Not permitted in capability mode - connect(2) for "0" port 80 -`rm -rf /` # => Errno::ECAPMODE: Not permitted in capability mode - rm -system "rm -rf /" # => nil -require 'time' # => LoadError: cannot load such file -- time +print "In capability mode: ", Capsicum.in_capability_mode? ? "yes" : "no", "\n" +print "Enter capability mode: ", Capsicum.enter! ? "ok" : "error", "\n" +print "In capability mode: ", Capsicum.in_capability_mode? ? "yes" : "no", "\n" + +begin + File.new(File::NULL) +rescue Errno::ECAPMODE => ex + print "Error: #{ex.message} (#{ex.class})", "\n" +end + +## +# In capability mode: no +# Enter capability mode: ok +# In capability mode: yes +# Error: Not permitted in capability mode @ rb_sysopen - /dev/null (Errno::ECAPMODE) ``` i.e. anything that involves opening a file, connecting a socket, or executing a diff --git a/Rakefile b/Rakefile index d949205..24c37f4 100644 --- a/Rakefile +++ b/Rakefile @@ -2,10 +2,7 @@ require "bundler/setup" require "bundler/gem_tasks" require "rake/testtask" -Rake::TestTask.new(:test) do |t| - t.libs << "test" - t.libs << "lib" - t.test_files = FileList['test/**/*_test.rb'] +task :test do + sh "bin/test-runner" end - task :default => :test diff --git a/bin/test-runner b/bin/test-runner new file mode 100755 index 0000000..feef3fe --- /dev/null +++ b/bin/test-runner @@ -0,0 +1,6 @@ +#!/bin/sh +set -e + +for t in test/*_test.rb; do + ruby -Itest "${t}" +done diff --git a/capsicum.gemspec b/capsicum.gemspec index 5b70b68..0cb4be7 100644 --- a/capsicum.gemspec +++ b/capsicum.gemspec @@ -33,4 +33,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rake", "~> 13.2" spec.add_development_dependency "minitest", "~> 5.0" spec.add_development_dependency "standard", "~> 1.38" + spec.add_development_dependency "test-cmd.rb", "~> 0.12" end diff --git a/share/ruby-capsicum/examples/1_capability_mode_example.rb b/share/ruby-capsicum/examples/1_capability_mode_example.rb new file mode 100644 index 0000000..b1e601b --- /dev/null +++ b/share/ruby-capsicum/examples/1_capability_mode_example.rb @@ -0,0 +1,18 @@ +require "bundler/setup" +require "capsicum" + +print "In capability mode: ", Capsicum.in_capability_mode? ? "yes" : "no", "\n" +print "Enter capability mode: ", Capsicum.enter! ? "ok" : "error", "\n" +print "In capability mode: ", Capsicum.in_capability_mode? ? "yes" : "no", "\n" + +begin + File.new(File::NULL) +rescue Errno::ECAPMODE => ex + print "Error: #{ex.message} (#{ex.class})", "\n" +end + +## +# In capability mode: no +# Enter capability mode: ok +# In capability mode: yes +# Error: Not permitted in capability mode @ rb_sysopen - /dev/null (Errno::ECAPMODE) diff --git a/test/readme_test.rb b/test/readme_test.rb new file mode 100644 index 0000000..e3a3522 --- /dev/null +++ b/test/readme_test.rb @@ -0,0 +1,22 @@ +require "test_helper" +require "test-cmd" + +class ReadMeTest < Minitest::Test + def test_1_capability_mode_example + r = ruby(readme_example("1_capability_mode_example.rb")) + assert_match %r|In capability mode: no\n|, r.stdout + assert_match %r|Enter capability mode: ok\n|, r.stdout + assert_match %r|In capability mode: yes\n|, r.stdout + assert_match %r|Error:.+\(Errno::ECAPMODE\)\n|, r.stdout + end + + private + + def ruby(*argv) + cmd("ruby", *argv) + end + + def readme_example(example_name) + File.join(__dir__, "..", "share", "ruby-capsicum", "examples", example_name) + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index ea4bfc2..8684446 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,4 @@ require "bundler/setup" +require "test-cmd" require "capsicum" require "minitest/autorun"