import threading
import time
import unittest
+import random
+
class DNSDistTest(unittest.TestCase):
"""
"""
_dnsDistPort = 5340
_testServerPort = 5350
- _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort) + " 127.0.0.1:" + str(_testServerPort)).split()
_toResponderQueue = Queue.Queue()
_fromResponderQueue = Queue.Queue()
_dnsdist = None
_responsesCounter = {}
+ _config_template = """
+ newServer{address="127.0.0.1:%s"}
+ truncateTC(true)
+ addAnyTCRule()
+ addAction(RegexRule("evil[0-9]{4,}\\\\.regex\\\\.tests\\\\.powerdns\\\\.com$"), RCodeAction(5))
+ mySMN = newSuffixMatchNode()
+ mySMN:add(newDNSName("nameAndQtype.tests.powerdns.com."))
+ addAction(AndRule{SuffixMatchNodeRule(mySMN), QTypeRule("TXT")}, RCodeAction(4))
+ block=newDNSName("powerdns.org.")
+ function blockFilter(remote, qname, qtype, dh)
+ if(qname:isPartOf(block))
+ then
+ print("Blocking *.powerdns.org")
+ return true
+ end
+ return false
+ end
+ """
+ _config_params = ['_testServerPort']
+ _acl = ['127.0.0.1/32']
@classmethod
def startResponders(cls):
@classmethod
def startDNSDist(cls, shutUp=True):
print("Launching dnsdist..")
- print(' '.join(cls._dnsdistcmd))
+ conffile = 'dnsdist_test.conf'
+ params = tuple([getattr(cls, param) for param in cls._config_params])
+ print(params)
+ 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,
+ '-l', '127.0.0.1:%d' % cls._dnsDistPort]
+ for acl in cls._acl:
+ dnsdistcmd.extend(['--acl', acl])
+ print(' '.join(dnsdistcmd))
+
if shutUp:
with open(os.devnull, 'w') as fdDevNull:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull)
+ cls._dnsdist = subprocess.Popen(dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull)
else:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True)
+ cls._dnsdist = subprocess.Popen(dnsdistcmd, close_fds=True)
time.sleep(1)
class TestAdvancedFixupCase(DNSDistTest):
- _dnsDistPort = 5340
_config_template = """
truncateTC(true)
fixupCase(true)
newServer{address="127.0.0.1:%s"}
"""
- _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_fixupcase.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split()
-
- @classmethod
- def startDNSDist(cls, shutUp=True):
- print("Launching dnsdist..")
- with open('dnsdist_fixupcase.conf', 'w') as conf:
- conf.write(cls._config_template % str(cls._testServerPort))
-
- print(' '.join(cls._dnsdistcmd))
- if shutUp:
- with open(os.devnull, 'w') as fdDevNull:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull)
- else:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True)
-
- time.sleep(1)
-
- if cls._dnsdist.poll() is not None:
- cls._dnsdist.terminate()
- cls._dnsdist.wait()
- sys.exit(cls._dnsdist.returncode)
-
def testAdvancedFixupCase(self):
"""
Send a query with lower and upper chars,
class TestAdvancedRemoveRD(DNSDistTest):
- _dnsDistPort = 5340
_config_template = """
addNoRecurseRule("norecurse.advanced.tests.powerdns.com.")
newServer{address="127.0.0.1:%s"}
"""
- _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_noRD.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split()
-
- @classmethod
- def startDNSDist(cls, shutUp=True):
- print("Launching dnsdist..")
- with open('dnsdist_noRD.conf', 'w') as conf:
- conf.write(cls._config_template % str(cls._testServerPort))
-
- print(' '.join(cls._dnsdistcmd))
- if shutUp:
- with open(os.devnull, 'w') as fdDevNull:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull)
- else:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True)
-
- time.sleep(1)
-
- if cls._dnsdist.poll() is not None:
- cls._dnsdist.terminate()
- cls._dnsdist.wait()
- sys.exit(cls._dnsdist.returncode)
-
def testAdvancedNoRD(self):
"""
Send a query with RD,
class TestAdvancedAddCD(DNSDistTest):
- _dnsDistPort = 5340
_config_template = """
addDisableValidationRule("setcd.advanced.tests.powerdns.com.")
newServer{address="127.0.0.1:%s"}
"""
- _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_setCD.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split()
-
- @classmethod
- def startDNSDist(cls, shutUp=True):
- print("Launching dnsdist..")
- with open('dnsdist_setCD.conf', 'w') as conf:
- conf.write(cls._config_template % str(cls._testServerPort))
-
- print(' '.join(cls._dnsdistcmd))
- if shutUp:
- with open(os.devnull, 'w') as fdDevNull:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull)
- else:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True)
-
- time.sleep(1)
-
- if cls._dnsdist.poll() is not None:
- cls._dnsdist.terminate()
- cls._dnsdist.wait()
- sys.exit(cls._dnsdist.returncode)
-
def testAdvancedSetCD(self):
"""
Send a query with CD cleared,
class TestAdvancedSpoof(DNSDistTest):
- _dnsDistPort = 5340
_config_template = """
addDomainSpoof("spoof.tests.powerdns.com.", "192.0.2.1", "2001:DB8::1")
newServer{address="127.0.0.1:%s"}
"""
- _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_spoof.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split()
-
- @classmethod
- def startDNSDist(cls, shutUp=True):
- print("Launching dnsdist..")
- with open('dnsdist_spoof.conf', 'w') as conf:
- conf.write(cls._config_template % str(cls._testServerPort))
-
- print(' '.join(cls._dnsdistcmd))
- if shutUp:
- with open(os.devnull, 'w') as fdDevNull:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull)
- else:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True)
-
- time.sleep(1)
-
- if cls._dnsdist.poll() is not None:
- cls._dnsdist.terminate()
- cls._dnsdist.wait()
- sys.exit(cls._dnsdist.returncode)
-
def testSpoofA(self):
"""
Send an A query to "spoof.tests.powerdns.com.",
class TestAdvancedPoolRouting(DNSDistTest):
- _dnsDistPort = 5340
_config_template = """
newServer{address="127.0.0.1:%s", pool="real"}
addPoolRule("pool.tests.powerdns.com", "real")
"""
- _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_pool.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split()
-
- @classmethod
- def startDNSDist(cls, shutUp=True):
- print("Launching dnsdist..")
- with open('dnsdist_pool.conf', 'w') as conf:
- conf.write(cls._config_template % str(cls._testServerPort))
-
- print(' '.join(cls._dnsdistcmd))
- if shutUp:
- with open(os.devnull, 'w') as fdDevNull:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull)
- else:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True)
-
- time.sleep(1)
-
- if cls._dnsdist.poll() is not None:
- cls._dnsdist.terminate()
- cls._dnsdist.wait()
- sys.exit(cls._dnsdist.returncode)
-
def testPolicyPool(self):
"""
Send an A query to "pool.tests.powerdns.com.",
class TestAdvancedRoundRobinLB(DNSDistTest):
- _dnsDistPort = 5340
_testServer2Port = 5351
+ _config_params = ['_testServerPort', '_testServer2Port']
_config_template = """
setServerPolicy(roundrobin)
s1 = newServer{address="127.0.0.1:%s"}
s2:setUp()
"""
- _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_rr_lb.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split()
-
- @classmethod
- def startDNSDist(cls, shutUp=True):
- print("Launching dnsdist..")
- with open('dnsdist_rr_lb.conf', 'w') as conf:
- conf.write(cls._config_template % (str(cls._testServerPort), str(cls._testServer2Port)))
-
- print(' '.join(cls._dnsdistcmd))
- if shutUp:
- with open(os.devnull, 'w') as fdDevNull:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull)
- else:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True)
-
- time.sleep(1)
-
- if cls._dnsdist.poll() is not None:
- cls._dnsdist.terminate()
- cls._dnsdist.wait()
- sys.exit(cls._dnsdist.returncode)
-
@classmethod
def startResponders(cls):
print("Launching responders..")
class TestAdvancedRoundRobinLBOneDown(DNSDistTest):
- _dnsDistPort = 5340
_testServer2Port = 5351
+ _config_params = ['_testServerPort', '_testServer2Port']
_config_template = """
setServerPolicy(roundrobin)
s1 = newServer{address="127.0.0.1:%s"}
s2:setDown()
"""
- _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_rr_lb_down.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split()
-
- @classmethod
- def startDNSDist(cls, shutUp=True):
- print("Launching dnsdist..")
- with open('dnsdist_rr_lb_down.conf', 'w') as conf:
- conf.write(cls._config_template % (str(cls._testServerPort), str(cls._testServer2Port)))
-
- print(' '.join(cls._dnsdistcmd))
- if shutUp:
- with open(os.devnull, 'w') as fdDevNull:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull)
- else:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True)
-
- time.sleep(1)
-
- if cls._dnsdist.poll() is not None:
- cls._dnsdist.terminate()
- cls._dnsdist.wait()
- sys.exit(cls._dnsdist.returncode)
-
def testRRWithOneDown(self):
"""
Send 100 A queries to "rr.tests.powerdns.com.",
class TestAdvancedACL(DNSDistTest):
- _dnsDistPort = 5340
_config_template = """
newServer{address="127.0.0.1:%s"}
"""
-
- _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_acl.conf --acl 192.0.2.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split()
-
- @classmethod
- def startDNSDist(cls, shutUp=True):
- print("Launching dnsdist..")
- with open('dnsdist_acl.conf', 'w') as conf:
- conf.write(cls._config_template % str(cls._testServerPort))
-
- print(' '.join(cls._dnsdistcmd))
- if shutUp:
- with open(os.devnull, 'w') as fdDevNull:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull)
- else:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True)
-
- time.sleep(1)
-
- if cls._dnsdist.poll() is not None:
- cls._dnsdist.terminate()
- cls._dnsdist.wait()
- sys.exit(cls._dnsdist.returncode)
+ _acl = ['192.0.2.1/32']
def testACLBlocked(self):
"""
class TestAdvancedDelay(DNSDistTest):
- _dnsDistPort = 5340
_config_template = """
addAction(AllRule(), DelayAction(1000))
newServer{address="127.0.0.1:%s"}
"""
- _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_delay.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split()
-
- @classmethod
- def startDNSDist(cls, shutUp=True):
- print("Launching dnsdist..")
- with open('dnsdist_delay.conf', 'w') as conf:
- conf.write(cls._config_template % str(cls._testServerPort))
-
- print(' '.join(cls._dnsdistcmd))
- if shutUp:
- with open(os.devnull, 'w') as fdDevNull:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull)
- else:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True)
-
- time.sleep(1)
-
- if cls._dnsdist.poll() is not None:
- cls._dnsdist.terminate()
- cls._dnsdist.wait()
- sys.exit(cls._dnsdist.returncode)
-
def testDelayed(self):
"""
Send an A query to "tests.powerdns.com.",
original query.
"""
- _dnsDistPort = 5340
_config_template = """
truncateTC(true)
block=newDNSName("powerdns.org.")
newServer{address="127.0.0.1:%s", useClientSubnet=true}
"""
- _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_ecs_no_override.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split()
-
- @classmethod
- def startDNSDist(cls, shutUp=True):
- print("Launching dnsdist..")
- with open('dnsdist_ecs_no_override.conf', 'w') as conf:
- conf.write(cls._config_template % str(cls._testServerPort))
-
- print(' '.join(cls._dnsdistcmd))
- if shutUp:
- with open(os.devnull, 'w') as fdDevNull:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull)
- else:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True)
-
- time.sleep(1)
-
- if cls._dnsdist.poll() is not None:
- cls._dnsdist.terminate()
- cls._dnsdist.wait()
- sys.exit(cls._dnsdist.returncode)
-
def testWithoutEDNS(self):
"""
Send a query without EDNS, check that the query
option, overwriting any existing value.
"""
- _dnsDistPort = 5340
_config_template = """
truncateTC(true)
block=newDNSName("powerdns.org.")
newServer{address="127.0.0.1:%s", useClientSubnet=true}
"""
- _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_ecs_override.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split()
-
- @classmethod
- def startDNSDist(cls, shutUp=True):
- print("Launching dnsdist..")
- with open('dnsdist_ecs_override.conf', 'w') as conf:
- conf.write(cls._config_template % str(cls._testServerPort))
-
- print(' '.join(cls._dnsdistcmd))
- if shutUp:
- with open(os.devnull, 'w') as fdDevNull:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull)
- else:
- cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True)
-
- time.sleep(1)
-
- if cls._dnsdist.poll() is not None:
- cls._dnsdist.terminate()
- cls._dnsdist.wait()
- sys.exit(cls._dnsdist.returncode)
-
def testWithoutEDNS(self):
"""
Send a query without EDNS, check that the query