bsdcapsicum.rb/test/readme_test.rb

47 lines
1.4 KiB
Ruby
Raw Normal View History

2024-06-27 06:01:24 +02:00
# frozen_string_literal: true
2024-06-25 04:36:09 +02:00
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"))
2024-06-27 06:07:15 +02:00
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
2024-06-25 04:36:09 +02:00
end
2024-06-25 05:23:16 +02:00
def test_2_fork_example
r = ruby(readme_example("2_fork_example.rb"))
["[parent] In capability mode: no\n",
"[subprocess] Enter capability mode: ok\n",
"[subprocess] In capability mode: yes\n",
"[subprocess] Exit\n",
"[parent] In capability mode: no\n"
].each { assert_match(/#{Regexp.escape(_1)}/, r.stdout) }
end
def test_3_set_rights_example
r = ruby(readme_example("3_set_rights_example.rb"))
2024-06-27 10:09:13 +02:00
["[parent] Obtain file descriptor (with all capabilities)\n",
"[subprocess] Reduce capabilities to read\n",
"[subprocess] Read OK\n",
2024-06-27 06:07:15 +02:00
%r|\[subprocess\] Error:.+\(Errno::ENOTCAPABLE\)\n|,
2024-06-27 10:09:13 +02:00
"[parent] Write OK\n"
2024-06-27 06:01:24 +02:00
].each { assert_match((Regexp === _1) ? _1 : /#{Regexp.escape(_1)}/, r.stdout) }
ensure
FileUtils.rm File.join(Dir.home, "bsdcapsicum.txt")
end
2024-06-25 04:36:09 +02:00
private
def ruby(*argv)
cmd("ruby", *argv)
end
def readme_example(example_name)
2024-07-19 05:52:20 +02:00
File.join(__dir__, "..", "share", "examples", "bsdcapsicum.rb", example_name)
2024-06-25 04:36:09 +02:00
end
end