module FWToolkit module Test module NetworkHelpers #Given /^the network simulates a 3G connection #ipfw add pipe 1 all from any to any out #ipfw add pipe 2 all from any to any in #ipfw pipe 1 config bw 64Kbit/s #ipfw pipe 2 config bw 128Kbit/s def ipfw_reset_rule_number @@ipfw_rule_number = 1 end def ipfw_exit_unless_clean ipfw_list = `sudo ipfw list` if ipfw_list =~ /^(?!65535 allow ip from any to any$).*/ puts 'The firewall table contains rules other than the default. Run `sudo ipfw flush` before rerunning cucumber if you don\'t need these rules.' exit -1 end end def ipfw_deny(host='localhost') `sudo ipfw add #{@@ipfw_rule_number} deny tcp from any to #{host}` @@ipfw_rule_number += 1 end def ipfw_flush `sudo ipfw flush` end end end end World(FWToolkit::Test::NetworkHelpers) # Check IPFW because we flush firewall rules at the end of each scenario # If we have anything but the default rule, warn the user and exit Before do ipfw_exit_unless_clean ipfw_reset_rule_number end After do ipfw_flush end # # Steps # Given /^the network has no connection$/ do ipfw_deny end Then /^the network is returned to its original state$/ do ipfw_flush end