]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
validate rdata replace()
authorBob Halley <halley@dnspython.org>
Thu, 18 Jun 2020 16:34:53 +0000 (09:34 -0700)
committerBob Halley <halley@dnspython.org>
Thu, 18 Jun 2020 16:34:53 +0000 (09:34 -0700)
dns/rdata.py
tests/test_rdata.py

index 90a44249b85b52e18ab67ad46e41dec050b5c5ae..cc44d00243e63ed6ae9c59abce71d63ddc7c1c8e 100644 (file)
@@ -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):
index 1a51461638758737d58761165b009a05776d6a8e..6ec48ccfc35eab546c3ae47c316cc3665345bb18 100644 (file)
@@ -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')