Skip to content

Commit 4d5d65b

Browse files
committed
test: conditionally deny network access in sandbox
1 parent ad67d31 commit 4d5d65b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Library/Homebrew/dev-cmd/test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def run
8080

8181
exec_args << "--HEAD" if f.head?
8282

83-
Utils.safe_fork do
83+
Utils.safe_fork do |error_pipe|
8484
if Sandbox.available?
8585
sandbox = Sandbox.new
8686
f.logs.mkpath
@@ -92,6 +92,10 @@ def run
9292
sandbox.allow_write_path(HOMEBREW_PREFIX/"var/homebrew/locks")
9393
sandbox.allow_write_path(HOMEBREW_PREFIX/"var/log")
9494
sandbox.allow_write_path(HOMEBREW_PREFIX/"var/run")
95+
if f.class.offline_phase?(:test)
96+
sandbox.deny_all_network
97+
sandbox.allow_network(error_pipe, type: :literal)
98+
end
9599
sandbox.exec(*exec_args)
96100
else
97101
exec(*exec_args)

Library/Homebrew/test/dev-cmd/test_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "cmd/shared_examples/args_parse"
44
require "dev-cmd/test"
5+
require "sandbox"
56

67
RSpec.describe Homebrew::DevCmd::Test do
78
it_behaves_like "parseable arguments"
@@ -18,4 +19,19 @@
1819
.and not_to_output.to_stderr
1920
.and be_a_success
2021
end
22+
23+
it "blocks network access when test phase is offline", :integration_test do
24+
if Sandbox.available?
25+
install_test_formula "testball_offline_test", <<~RUBY
26+
enable_offline_phase [:test]
27+
test do
28+
system "curl", "example.org"
29+
end
30+
RUBY
31+
32+
expect { brew "test", "--verbose", "testball_offline_test" }
33+
.to output(/curl: \(6\) Could not resolve host: example\.org/).to_stdout
34+
.and be_a_failure
35+
end
36+
end
2137
end

0 commit comments

Comments
 (0)