Added public attributes 'address' and 'port' to Dns::Server.

This removes the need to search config.yaml for the address:port.
Also included unit tests.
This commit is contained in:
soh_cah_toa
2013-07-19 22:33:40 -04:00
parent 8d961c1938
commit 6a62cf9eaa
2 changed files with 14 additions and 14 deletions

View File

@@ -17,6 +17,8 @@ module Dns
include Singleton
attr_reader :address, :port
# @!method self.instance
# Returns the singleton instance. Use this in place of {#initialize}.
@@ -32,6 +34,9 @@ module Dns
# @param address [String] interface address server should run on
# @param port [Integer] desired server port number
def run_server(address = '0.0.0.0', port = 5300)
@address = address
@port = port
@lock.synchronize do
Thread.new do
# @note Calling #sleep is a quick fix that prevents race conditions
@@ -40,9 +45,9 @@ module Dns
sleep(1)
if EventMachine.reactor_running?
EventMachine.next_tick { run_server_block(address, port) }
EventMachine.next_tick { run_server_block(@address, @port) }
else
run_server_block(address, port)
run_server_block(@address, @port)
end
end
end

View File

@@ -55,13 +55,16 @@ class TC_Dns < Test::Unit::TestCase
assert_respond_to(@@dns, :remove_ruleset)
end
# Starts DNS server (does not test anything)
# Tests that DNS server runs correctly on desired address and port
def test_04_run_server
address = @@dns_config['address']
port = @@dns_config['port']
@@dns.run_server(address, port)
sleep(3)
assert_equal(address, @@dns.address)
assert_equal(port, @@dns.port)
end
# Tests procedure for properly adding new DNS rules
@@ -251,22 +254,14 @@ class TC_Dns < Test::Unit::TestCase
assert_equal(expected[:response], rule[:response][0])
end
# Compares output of dig command against regex
def check_dns_response(regex, type, pattern)
address = @@dns_config['address']
port = @@dns_config['port']
dig_output = `dig @#{address} -p #{port} -t #{type} #{pattern}`
assert_match(regex, dig_output)
end
# Confirms that a query for the rule given in 'id' returns a 'type' failure status
def check_failure_status(id, type)
rule = @@dns.get_rule(id)
status = type.to_s.force_encoding('UTF-8').upcase
assert_equal(status, rule[:response][0])
check_dns_response(/status: #{status}/, rule[:type], rule[:pattern])
dig_output = `dig @#{@@dns.address} -p #{@@dns.port} -t #{rule[:type]} #{rule[:pattern]}`
assert_match(/status: #{status}/, dig_output)
end
end