def test_domain_handler(self): dh = pdyndns.DomainHandler(self.config['domain'], self.config['soa'], self.config['nameservers'], self.config['ttl']) instr = 'Q\t%s\tIN\tSOA\t-1\t%s\n' % (self.config['domain'], self.addrs) outstr = 'DATA\t0\t1\t%s\tIN\tSOA\t%d\t-1\t%s\n' % ( self.config['domain'], self.config['ttl'], self.config['soa']) outstr += 'END\n' fdout = StringIO() pdyndns.process_query(instr, [dh], fdout) self.assertEqual(fdout.getvalue(), outstr) instr = 'Q\t%s\tIN\tANY\t-1\t%s\n' % (self.config['domain'], self.addrs) outstr = 'DATA\t0\t1\t%s\tIN\tSOA\t%d\t-1\t%s\n' % ( self.config['domain'], self.config['ttl'], self.config['soa']) for ns in self.config['nameservers']: outstr += 'DATA\t0\t1\t%s\tIN\tNS\t%d\t-1\t%s\n' % ( self.config['domain'], self.config['ttl'], ns) outstr += 'END\n' fdout = StringIO() pdyndns.process_query(instr, [dh], fdout) self.assertEqual(fdout.getvalue(), outstr)
def test_domain_handler_broken_query(self): dh = pdyndns.DomainHandler(self.config['domain'], self.config['soa'], self.config['nameservers'], self.config['ttl']) instr = '%s\tIN\tSOA\t-1\t%s\n' % (self.config['domain'], self.addrs) outstr = 'END\n' fdout = StringIO() with self.assertRaises(ValueError): pdyndns.process_query(instr, [dh], fdout) self.assertEqual(fdout.getvalue(), outstr)
def test_domain_handler_empty_response(self): dh = pdyndns.DomainHandler(self.config['domain'], self.config['soa'], self.config['nameservers'], self.config['ttl']) instr = 'Q\t%s\tIN\tA\t-1\t%s\n' % (self.config['domain'], self.addrs) outstr = 'END\n' fdout = StringIO() pdyndns.process_query(instr, [dh], fdout) self.assertEqual(fdout.getvalue(), outstr) instr = 'Q\t%s\tIN\tA\t-1\t%s\n' % ('t1.%s' % self.config['domain'], self.addrs) outstr = 'END\n' fdout = StringIO() pdyndns.process_query(instr, [dh], fdout) self.assertEqual(fdout.getvalue(), outstr)
def test_rr_rewind(self): handlers = [ pdyndns.RoundRobinFileHandler(h['qname'], h['qtype'], h['file']) for h in self.config['handlers'] ] names = [h['qname'] for h in self.config['handlers']] names += ['unknown.dyndns.example.net'] for _ in range(64): for name in names[0:2]: instr = 'Q\t%s\tIN\tA\t-1\t%s\n' % (name, self.addrs) outstr = 'DATA\t0\t1\t%s\tIN\tA\t0\t-1\t' % name fdout = StringIO() pdyndns.process_query(instr, handlers, fdout) self.assertTrue(fdout.getvalue().startswith(outstr)) self.assertTrue(fdout.getvalue().endswith('END\n')) instr = 'Q\t%s\tIN\tAAAA\t-1\t%s\n' % (name, self.addrs) fdout = StringIO() pdyndns.process_query(instr, handlers, fdout) self.assertEqual(fdout.getvalue(), 'END\n') for name in names[2:3]: instr = 'Q\t%s\tIN\tAAAA\t-1\t%s\n' % (name, self.addrs) outstr = 'DATA\t0\t1\t%s\tIN\tAAAA\t0\t-1\t' % name fdout = StringIO() pdyndns.process_query(instr, handlers, fdout) self.assertTrue(fdout.getvalue().startswith(outstr)) self.assertTrue(fdout.getvalue().endswith('END\n')) instr = 'Q\t%s\tIN\tA\t-1\t%s\n' % (name, self.addrs) fdout = StringIO() pdyndns.process_query(instr, handlers, fdout) self.assertEqual(fdout.getvalue(), 'END\n') for name in names[3:4]: instr = 'Q\t%s\tIN\tA\t-1\t%s\n' % (name, self.addrs) fdout = StringIO() pdyndns.process_query(instr, handlers, fdout) self.assertEqual(fdout.getvalue(), 'END\n') for handler in handlers: handler.close()
def test_rr_reload(self): h = self.config['handlers'][0] rr = pdyndns.RoundRobinFileHandler(h['qname'], h['qtype'], h['file']) instr = 'Q\t%s\tIN\tA\t-1\t%s\n' % (h['qname'], self.addrs) outstr = 'DATA\t0\t1\t%s\tIN\tA\t0\t-1\t10.0.0.1\n' % h['qname'] outstr += 'END\n' fdout = StringIO() pdyndns.process_query(instr, [rr], fdout) self.assertEqual(fdout.getvalue(), outstr) with tempfile.NamedTemporaryFile() as fd: # Overwrite t1.txt with t2.txt shutil.copy(h['file'], fd.name) shutil.copy(self.config['handlers'][1]['file'], h['file']) instr = 'Q\t%s\tIN\tA\t-1\t%s\n' % (h['qname'], self.addrs) outstr = 'DATA\t0\t1\t%s\tIN\tA\t0\t-1\t10.1.0.1\n' % h['qname'] outstr += 'END\n' fdout = StringIO() pdyndns.process_query(instr, [rr], fdout) self.assertEqual(fdout.getvalue(), outstr) # Restore shutil.copy(fd.name, h['file']) instr = 'Q\t%s\tIN\tA\t-1\t%s\n' % (h['qname'], self.addrs) outstr = 'DATA\t0\t1\t%s\tIN\tA\t0\t-1\t10.0.0.1\n' % h['qname'] outstr += 'END\n' fdout = StringIO() pdyndns.process_query(instr, [rr], fdout) self.assertEqual(fdout.getvalue(), outstr) rr.close()