From d4b9df79d1b449f13cfca0b635cea7b96cfc8d1a Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Thu, 18 Jun 2020 09:34:53 -0700 Subject: [PATCH] validate rdata replace() --- dns/rdata.py | 9 +++++++-- tests/test_rdata.py | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/dns/rdata.py b/dns/rdata.py index 90a44249..cc44d002 100644 --- a/dns/rdata.py +++ b/dns/rdata.py @@ -310,8 +310,13 @@ class Rdata: # kwargs if present, and the current value otherwise. args = (kwargs.get(key, getattr(self, key)) for key in parameters) - # Create and return the new object. - return self.__class__(*args) + # Create, validate, and return the new object. + # + # Note that if we make constructors do validation in the future, + # this validation can go away. + rd = self.__class__(*args) + dns.rdata.from_text(rd.rdclass, rd.rdtype, rd.to_text()) + return rd class GenericRdata(Rdata): diff --git a/tests/test_rdata.py b/tests/test_rdata.py index 1a514616..6ec48ccf 100644 --- a/tests/test_rdata.py +++ b/tests/test_rdata.py @@ -19,6 +19,7 @@ import io import unittest +import dns.exception import dns.name import dns.rdata import dns.rdataclass @@ -79,6 +80,12 @@ class RdataTestCase(unittest.TestCase): with self.assertRaises(AttributeError): mx.replace(invalid_parameter=1) + def test_invalid_replace(self): + a1 = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, "1.2.3.4") + def bad(): + a1.replace(address="bogus") + self.assertRaises(dns.exception.SyntaxError, bad) + def test_to_generic(self): a = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, "1.2.3.4") self.assertEqual(str(a.to_generic()), r'\# 4 01020304') -- 2.47.3