Add test/readme_test.rb

This commit is contained in:
0x1eef 2024-06-24 23:36:09 -03:00
parent 614d9d5f07
commit d59fdef28e
7 changed files with 66 additions and 13 deletions

View file

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

View file

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

6
bin/test-runner Executable file
View file

@ -0,0 +1,6 @@
#!/bin/sh
set -e
for t in test/*_test.rb; do
ruby -Itest "${t}"
done

View file

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

View file

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

22
test/readme_test.rb Normal file
View file

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

View file

@ -1,3 +1,4 @@
require "bundler/setup"
require "test-cmd"
require "capsicum"
require "minitest/autorun"