def test_rrsig_ttl(self): '''Similar to the previous test, but for RRSIGs of different covered types. They shouldn't be compacted. ''' diff = Diff(self, Name('example.org.')) rrsig1 = RRset(Name('example.org'), self.__rrclass, RRType.RRSIG, RRTTL(3600)) rrsig1.add_rdata( Rdata( RRType.RRSIG, self.__rrclass, 'A 5 3 3600 20000101000000 20000201000000 ' + '0 example.org. FAKEFAKEFAKE')) diff.add_data(rrsig1) rrsig2 = RRset(Name('example.org'), self.__rrclass, RRType.RRSIG, RRTTL(1800)) rrsig2.add_rdata( Rdata( RRType.RRSIG, self.__rrclass, 'AAAA 5 3 3600 20000101000000 20000201000000 ' + '1 example.org. FAKEFAKEFAKE')) diff.add_data(rrsig2) diff.compact() self.assertEqual(2, len(diff.get_buffer()))
def test_ttl(self): """ Test the TTL handling. A warn function should have been called if they differ, but that's all, it should not crash or raise. """ self.orig_logger = bundy.xfrin.diff.logger try: bundy.xfrin.diff.logger = self diff = Diff(self, Name('example.org.')) diff.add_data(self.__rrset1) rrset2 = RRset(Name('a.example.org.'), self.__rrclass, self.__type, RRTTL(120)) rrset2.add_rdata(Rdata(self.__type, self.__rrclass, '192.10.2.2')) diff.add_data(rrset2) rrset2 = RRset(Name('a.example.org.'), self.__rrclass, self.__type, RRTTL(6000)) rrset2.add_rdata(Rdata(self.__type, self.__rrclass, '192.10.2.3')) diff.add_data(rrset2) # They should get compacted together and complain. diff.compact() self.assertEqual(1, len(diff.get_buffer())) # The TTL stays on the first value, no matter if smaller or bigger # ones come later. self.assertEqual(self.__ttl, diff.get_buffer()[0][1].get_ttl()) self.assertTrue(self.__warn_called) finally: bundy.xfrin.diff.logger = self.orig_logger
def test_wrong_class(self): """ Test a wrong class of rrset is rejected. """ diff = Diff(self, Name('example.org.')) rrset = RRset(Name('a.example.org.'), RRClass.CH, RRType.NS, self.__ttl) rrset.add_rdata(Rdata(RRType.NS, RRClass.CH, 'ns.example.org.')) self.assertRaises(ValueError, diff.add_data, rrset) self.assertRaises(ValueError, diff.delete_data, rrset)
def test_find_all_existing_data(self): diff = Diff(self, Name('example.org'), single_update_mode=True) diff.add_data(self.__rrset_soa) diff.delete_data(self.__rrset_soa) # override the actual find method self.__create_find_all(ZoneFinder.SUCCESS, [self.__rrset3], 0) # Sanity check result, rrsets, _ = diff.find_all_updated(self.__rrset3.get_name()) self.assertEqual(ZoneFinder.SUCCESS, result) self.assertEqual([self.__rrset3], rrsets) self.__check_find_all_call(diff.find_all_updated, self.__rrset3, ZoneFinder.SUCCESS, [self.__rrset3]) self.__check_find_all_call(diff.find_all, self.__rrset3, ZoneFinder.SUCCESS, [self.__rrset3]) # Add a second rr with different type at same name add_rrset = RRset(self.__rrset3.get_name(), self.__rrclass, RRType.A, self.__ttl) add_rrset.add_rdata(Rdata(RRType.A, self.__rrclass, "192.0.2.2")) diff.add_data(add_rrset) self.__check_find_all_call(diff.find_all_updated, self.__rrset3, ZoneFinder.SUCCESS, [self.__rrset3, add_rrset]) self.__check_find_all_call(diff.find_all, self.__rrset3, ZoneFinder.SUCCESS, [self.__rrset3]) # Remove original one diff.delete_data(self.__rrset3) self.__check_find_all_call(diff.find_all_updated, self.__rrset3, ZoneFinder.SUCCESS, [add_rrset]) self.__check_find_all_call(diff.find_all, self.__rrset3, ZoneFinder.SUCCESS, [self.__rrset3]) # And remove new one, result should then become NXDOMAIN diff.delete_data(add_rrset) result, rrsets, _ = diff.find_all_updated(self.__rrset3.get_name()) self.assertEqual(ZoneFinder.NXDOMAIN, result) self.assertEqual([], rrsets) self.__check_find_all_call(diff.find_all_updated, self.__rrset3, ZoneFinder.NXDOMAIN) self.__check_find_all_call(diff.find_all, self.__rrset3, ZoneFinder.SUCCESS, [self.__rrset3])
def test_single_update_mode(self): ''' Test single-update mode. In this mode, updates and deletes can be done in any order, but there may only be one changeset. For both updates and deletes, exactly one SOA rr must be given, and it must be the first change. ''' # full rrset for A (to check compact()) txt = RRset(Name('c.example.org.'), self.__rrclass, RRType.TXT, RRTTL(3600)) txt.add_rdata(Rdata(txt.get_type(), txt.get_class(), "one")) txt.add_rdata(Rdata(txt.get_type(), txt.get_class(), "two")) txt.add_rdata(Rdata(txt.get_type(), txt.get_class(), "three")) a = RRset(Name('d.example.org.'), self.__rrclass, RRType.A, RRTTL(3600)) a.add_rdata(Rdata(a.get_type(), a.get_class(), "192.0.2.1")) a.add_rdata(Rdata(a.get_type(), a.get_class(), "192.0.2.2")) diff = Diff(self, Name('example.org.'), single_update_mode=True) # adding a first should fail self.assertRaises(ValueError, diff.add_data, a) # But soa should work diff.add_data(self.__rrset_soa) # And then A should as well diff.add_data(self.__rrset3) diff.add_data(self.__rrset4) diff.add_data(self.__rrset5) # But another SOA should fail again self.assertRaises(ValueError, diff.add_data, self.__rrset_soa) # Same for delete self.assertRaises(ValueError, diff.delete_data, self.__rrset6) diff.delete_data(self.__rrset_soa) diff.delete_data(self.__rrset6) diff.delete_data(self.__rrset7) self.assertRaises(ValueError, diff.delete_data, self.__rrset_soa) # Not compacted yet, so the buffers should be as we # filled them (delbuf, addbuf) = diff.get_single_update_buffers() self.assertEqual([('delete', self.__rrset_soa), ('delete', self.__rrset6), ('delete', self.__rrset7)], delbuf) self.assertEqual([('add', self.__rrset_soa), ('add', self.__rrset3), ('add', self.__rrset4), ('add', self.__rrset5)], addbuf) # Compact should compact the A records in both buffers diff.compact() (delbuf, addbuf) = diff.get_single_update_buffers() # need rrset equality again :/ self.assertEqual(2, len(delbuf)) self.assertEqual(2, len(delbuf[0])) self.assertEqual('delete', delbuf[0][0]) self.assertEqual(self.__rrset_soa.to_text(), delbuf[0][1].to_text()) self.assertEqual(2, len(delbuf[1])) self.assertEqual('delete', delbuf[1][0]) self.assertEqual(a.to_text(), delbuf[1][1].to_text()) self.assertEqual(2, len(addbuf)) self.assertEqual(2, len(addbuf[0])) self.assertEqual('add', addbuf[0][0]) self.assertEqual(self.__rrset_soa.to_text(), addbuf[0][1].to_text()) self.assertEqual(2, len(addbuf[1])) self.assertEqual('add', addbuf[1][0]) self.assertEqual(txt.to_text(), addbuf[1][1].to_text()) # Apply should reset the buffers diff.apply() (delbuf, addbuf) = diff.get_single_update_buffers() self.assertEqual([], delbuf) self.assertEqual([], addbuf) # Now the change has been applied, and the buffers are cleared, # Adding non-SOA records should fail again. self.assertRaises(ValueError, diff.add_data, a) self.assertRaises(ValueError, diff.delete_data, a)
def setUp(self): """ This sets internal variables so we can see nothing was called yet. It also creates some variables used in multiple tests. """ # Track what was called already self.__updater_requested = False self.__compact_called = False self.__data_operations = [] self.__apply_called = False self.__commit_called = False self.__broken_called = False self.__warn_called = False self.__should_replace = False self.__find_called = False self.__find_name = None self.__find_type = None self.__find_options = None self.__find_all_called = False self.__find_all_name = None self.__find_all_options = None # Some common values self.__rrclass = RRClass.IN self.__type = RRType.A self.__ttl = RRTTL(3600) # And RRsets # Create two valid rrsets self.__rrset1 = RRset(Name('a.example.org.'), self.__rrclass, self.__type, self.__ttl) self.__rdata = Rdata(self.__type, self.__rrclass, '192.0.2.1') self.__rrset1.add_rdata(self.__rdata) self.__rrset2 = RRset(Name('b.example.org.'), self.__rrclass, self.__type, self.__ttl) self.__rrset2.add_rdata(self.__rdata) # And two invalid self.__rrset_empty = RRset(Name('empty.example.org.'), self.__rrclass, self.__type, self.__ttl) self.__rrset_multi = RRset(Name('multi.example.org.'), self.__rrclass, self.__type, self.__ttl) self.__rrset_multi.add_rdata(self.__rdata) self.__rrset_multi.add_rdata( Rdata(self.__type, self.__rrclass, '192.0.2.2')) # Also create a few other (valid) rrsets # A SOA record self.__rrset_soa = RRset(Name('example.org.'), self.__rrclass, RRType.SOA, RRTTL(3600)) self.__rrset_soa.add_rdata( Rdata( RRType.SOA, self.__rrclass, "ns1.example.org. " + "admin.example.org. " + "1233 3600 1800 2419200 7200")) # A few single-rr rrsets that together would for a multi-rr rrset self.__rrset3 = RRset(Name('c.example.org.'), self.__rrclass, RRType.TXT, self.__ttl) self.__rrset3.add_rdata(Rdata(RRType.TXT, self.__rrclass, "one")) self.__rrset4 = RRset(Name('c.example.org.'), self.__rrclass, RRType.TXT, self.__ttl) self.__rrset4.add_rdata(Rdata(RRType.TXT, self.__rrclass, "two")) self.__rrset5 = RRset(Name('c.example.org.'), self.__rrclass, RRType.TXT, self.__ttl) self.__rrset5.add_rdata(Rdata(RRType.TXT, self.__rrclass, "three")) self.__rrset6 = RRset(Name('d.example.org.'), self.__rrclass, RRType.A, self.__ttl) self.__rrset6.add_rdata(Rdata(RRType.A, self.__rrclass, "192.0.2.1")) self.__rrset7 = RRset(Name('d.example.org.'), self.__rrclass, RRType.A, self.__ttl) self.__rrset7.add_rdata(Rdata(RRType.A, self.__rrclass, "192.0.2.2"))
def test_compact(self): """ Test the compaction works as expected, eg. it compacts only consecutive changes of the same operation and on the same domain/type. The test case checks that it does merge them, but also puts some different operations "in the middle", changes the type and name and places the same kind of change further away of each other to see they are not merged in that case. """ diff = Diff(self, Name('example.org.')) # Check we can do a compact on empty data, it shouldn't break diff.compact() self.assertEqual([], diff.get_buffer()) # This data is the way it should look like after the compact # ('operation', 'domain.prefix', 'type', ['rdata', 'rdata']) # The notes say why the each of consecutive can't be merged data = [ ('add', 'a', 'A', ['192.0.2.1', '192.0.2.2']), # Different type. ('add', 'a', 'AAAA', ['2001:db8::1', '2001:db8::2']), # Different operation ('delete', 'a', 'AAAA', ['2001:db8::3']), # Different domain ('delete', 'b', 'AAAA', ['2001:db8::4']), # This does not get merged with the first, even if logically # possible. We just don't do this. ('add', 'a', 'A', ['192.0.2.3']) ] # Now, fill the data into the diff, in a "flat" way, one by one for (op, nprefix, rrtype, rdata) in data: name = Name(nprefix + '.example.org.') rrtype_obj = RRType(rrtype) for rdatum in rdata: rrset = RRset(name, self.__rrclass, rrtype_obj, self.__ttl) rrset.add_rdata(Rdata(rrtype_obj, self.__rrclass, rdatum)) if op == 'add': diff.add_data(rrset) else: diff.delete_data(rrset) # Compact it diff.compact() # Now check they got compacted. They should be in the same order as # pushed inside. So it should be the same as data modulo being in # the rrsets and bundy.dns objects. def check(): buf = diff.get_buffer() self.assertEqual(len(data), len(buf)) for (expected, received) in zip(data, buf): (eop, ename, etype, edata) = expected (rop, rrrset) = received self.assertEqual(eop, rop) ename_obj = Name(ename + '.example.org.') self.assertEqual(ename_obj, rrrset.get_name()) # We check on names to make sure they are printed nicely self.assertEqual(etype, str(rrrset.get_type())) rdata = rrrset.get_rdata() self.assertEqual(len(edata), len(rdata)) # It should also preserve the order for (edatum, rdatum) in zip(edata, rdata): self.assertEqual(edatum, str(rdatum)) check() # Try another compact does nothing, but survives diff.compact() check()