Error if a forwarded port is below 1024 [closes GH-97]

This commit is contained in:
Mitchell Hashimoto 2010-07-14 21:27:00 -07:00
parent 7de7982214
commit a5643d3239
3 changed files with 34 additions and 1 deletions

View File

@ -10,7 +10,8 @@ module Vagrant
@app = app
@env = env
external_collision_check
threshold_check
external_collision_check if !env.error?
end
#--------------------------------------------------------------
@ -18,6 +19,14 @@ module Vagrant
# executing the action
#--------------------------------------------------------------
# This method checks for any forwarded ports on the host below
# 1024, which causes the forwarded ports to fail.
def threshold_check
@env.env.config.vm.forwarded_ports.each do |name, options|
return @env.error!(:vm_port_below_threshold) if options[:hostport] <= 1024
end
end
# This method checks for any port collisions with any VMs
# which are already created (by Vagrant or otherwise).
# report the collisions detected or will attempt to fix them

View File

@ -272,6 +272,11 @@
VM: <%= vm_name %>
Forwarded port: <%= name %> (<%= options[:guestport] %> => <%= options[:hostport] %>)
:vm_port_below_threshold: |-
The host port of all forwarded ports must be above 1024. VirtualBox
does not allow host ports to be below 1024. (Guest ports below 1024
are fine. For example: SSH on port 22 on the guest can be forwarded
to port 2222, but not 222).
:vm_port_collision: |-
Vagrant cannot forward the specified ports on this VM, since they
would collide with another VirtualBox virtual machine's forwarded

View File

@ -12,11 +12,29 @@ class ForwardPortsVMActionTest < Test::Unit::TestCase
context "initializing" do
should "call proper methods" do
@klass.any_instance.expects(:threshold_check)
@klass.any_instance.expects(:external_collision_check)
@klass.new(@app, @env)
end
end
context "checking for threshold" do
should "error if has a port below threshold" do
@env.env.config.vm.forwarded_ports.clear
@env.env.config.vm.forward_port("foo", 22, 222)
@klass.new(@app, @env)
assert @env.error?
assert_equal :vm_port_below_threshold, @env.error.first
end
should "not error if ports are fine" do
@env.env.config.vm.forwarded_ports.clear
@env.env.config.vm.forward_port("foo", 22, 2222)
@klass.new(@app, @env)
assert !@env.error?
end
end
context "checking for colliding external ports" do
setup do
@env.env.config.vm.forwarded_ports.clear
@ -43,6 +61,7 @@ class ForwardPortsVMActionTest < Test::Unit::TestCase
context "with instance" do
setup do
@klass.any_instance.stubs(:threshold_check)
@klass.any_instance.stubs(:external_collision_check)
@instance = @klass.new(@app, @env)
end