import libnacl.utils
# Python2/3 compatibility hacks
-if sys.version_info[0] == 2:
+try:
+ from queue import Queue
+except ImportError:
from Queue import Queue
+
+try:
range = xrange
-else:
- from queue import Queue
- range = range # allow re-export of the builtin name
+except NameError:
+ pass
class DNSDistTest(unittest.TestCase):
_dnsdistStartupDelay = 2.0
_dnsdist = None
_responsesCounter = {}
- _shutUp = True
_config_template = """
"""
_config_params = ['_testServerPort']
cls._TCPResponder.start()
@classmethod
- def startDNSDist(cls, shutUp=True):
+ def startDNSDist(cls):
print("Launching dnsdist..")
- conffile = 'dnsdist_test.conf'
+ confFile = os.path.join('configs', 'dnsdist_%s.conf' % (cls.__name__))
params = tuple([getattr(cls, param) for param in cls._config_params])
print(params)
- with open(conffile, 'w') as conf:
+ with open(confFile, 'w') as conf:
conf.write("-- Autogenerated by dnsdisttests.py\n")
conf.write(cls._config_template % params)
- dnsdistcmd = [os.environ['DNSDISTBIN'], '-C', conffile,
+ dnsdistcmd = [os.environ['DNSDISTBIN'], '-C', confFile,
'-l', '%s:%d' % (cls._dnsDistListeningAddr, cls._dnsDistPort) ]
for acl in cls._acl:
dnsdistcmd.extend(['--acl', acl])
# validate config with --check-config, which sets client=true, possibly exposing bugs.
testcmd = dnsdistcmd + ['--check-config']
- output = subprocess.check_output(testcmd, close_fds=True)
- if output != b'Configuration \'dnsdist_test.conf\' OK!\n':
+ try:
+ output = subprocess.check_output(testcmd, stderr=subprocess.STDOUT, close_fds=True)
+ except subprocess.CalledProcessError as exc:
+ raise AssertionError('dnsdist --check-config failed (%d): %s' % (exc.returncode, exc.output))
+ expectedOutput = ('Configuration \'%s\' OK!\n' % (confFile)).encode()
+ if output != expectedOutput:
raise AssertionError('dnsdist --check-config failed: %s' % output)
- if shutUp:
- with open(os.devnull, 'w') as fdDevNull:
- cls._dnsdist = subprocess.Popen(dnsdistcmd, close_fds=True, stdout=fdDevNull)
- else:
- cls._dnsdist = subprocess.Popen(dnsdistcmd, close_fds=True)
+ logFile = os.path.join('configs', 'dnsdist_%s.log' % (cls.__name__))
+ with open(logFile, 'w') as fdLog:
+ cls._dnsdist = subprocess.Popen(dnsdistcmd, close_fds=True, stdout=fdLog, stderr=fdLog)
if 'DNSDIST_FAST_TESTS' in os.environ:
delay = 0.5
def setUpClass(cls):
cls.startResponders()
- cls.startDNSDist(cls._shutUp)
+ cls.startDNSDist()
cls.setUpSockets()
print("Launching tests..")
@classmethod
def UDPResponder(cls, port, fromQueue, toQueue, trailingDataResponse=False):
+ # trailingDataResponse=True means "ignore trailing data".
+ # Other values are either False (meaning "raise an exception")
+ # or are interpreted as a response RCODE for queries with trailing data.
ignoreTrailing = trailingDataResponse is True
+
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.bind(("127.0.0.1", port))
try:
request = dns.message.from_wire(data, ignore_trailing=ignoreTrailing)
except dns.message.TrailingJunk as e:
- if trailingDataResponse is False:
+ if trailingDataResponse is False or forceRcode is True:
raise
print("UDP query with trailing data, synthesizing response")
request = dns.message.from_wire(data, ignore_trailing=True)
@classmethod
def TCPResponder(cls, port, fromQueue, toQueue, trailingDataResponse=False, multipleResponses=False):
+ # trailingDataResponse=True means "ignore trailing data".
+ # Other values are either False (meaning "raise an exception")
+ # or are interpreted as a response RCODE for queries with trailing data.
ignoreTrailing = trailingDataResponse is True
+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
try:
try:
request = dns.message.from_wire(data, ignore_trailing=ignoreTrailing)
except dns.message.TrailingJunk as e:
- if trailingDataResponse is False:
+ if trailingDataResponse is False or forceRcode is True:
raise
print("TCP query with trailing data, synthesizing response")
request = dns.message.from_wire(data, ignore_trailing=True)